Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 89aa1c2

Browse files
Merge pull request #217 from Microsoft/users/yacao/addattachment
Implement add attachment command
2 parents ce58c8e + 9c85701 commit 89aa1c2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Diff for: src/agent/commands/task.addattachment.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/// <reference path="../definitions/vso-node-api.d.ts" />
2+
3+
import ctxm = require('../context');
4+
import cm = require('../common');
5+
import Q = require('q');
6+
import cmdm = require('./command');
7+
import webapim = require('vso-node-api/WebApi');
8+
import buildifm = require('vso-node-api/interfaces/BuildInterfaces');
9+
import path = require('path');
10+
import fs = require('fs');
11+
12+
export function createAsyncCommand(executionContext: cm.IExecutionContext, command: cm.ITaskCommand) {
13+
return new AddAttachmentCommand(executionContext, command);
14+
}
15+
16+
export class AddAttachmentCommand implements cm.IAsyncCommand {
17+
constructor(executionContext: cm.IExecutionContext, command: cm.ITaskCommand) {
18+
this.command = command;
19+
this.executionContext = executionContext;
20+
this.description = "Upload and attach attachment to current timeline record.";
21+
}
22+
23+
public command: cm.ITaskCommand;
24+
public description: string;
25+
public executionContext: cm.IExecutionContext;
26+
27+
public runCommandAsync(): Q.Promise<any> {
28+
var filename = this.command.message;
29+
if (!filename) {
30+
return Q(null);
31+
}
32+
33+
var type = this.command.properties['type'];
34+
if (!type) {
35+
return Q(null);
36+
}
37+
38+
var name = this.command.properties['name'];
39+
if (!name) {
40+
return Q(null);
41+
}
42+
43+
var deferred = Q.defer();
44+
fs.exists(filename, (exists: boolean) => {
45+
if (!exists) {
46+
deferred.resolve(null);
47+
}
48+
49+
var projectId: string = this.executionContext.variables[ctxm.WellKnownVariables.projectId];
50+
51+
var webapi = this.executionContext.getWebApi();
52+
var taskClient = webapi.getQTaskApi();
53+
54+
fs.stat(filename, (err: NodeJS.ErrnoException, stats: fs.Stats) => {
55+
if (err) {
56+
deferred.reject(err);
57+
}
58+
else {
59+
var headers = {};
60+
headers["Content-Length"] = stats.size;
61+
var stream = fs.createReadStream(filename);
62+
taskClient.createAttachment(
63+
headers,
64+
stream,
65+
projectId,
66+
this.executionContext.jobInfo.description,
67+
this.executionContext.jobInfo.planId,
68+
this.executionContext.jobInfo.timelineId,
69+
this.executionContext.recordId,
70+
type,
71+
name).then(() => deferred.resolve(null), (err: any) => deferred.reject(err));
72+
}
73+
})
74+
});
75+
76+
return deferred.promise;
77+
}
78+
}

0 commit comments

Comments
 (0)