This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from Microsoft/users/yacao/addattachment
Implement add attachment command
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/// <reference path="../definitions/vso-node-api.d.ts" /> | ||
|
||
import ctxm = require('../context'); | ||
import cm = require('../common'); | ||
import Q = require('q'); | ||
import cmdm = require('./command'); | ||
import webapim = require('vso-node-api/WebApi'); | ||
import buildifm = require('vso-node-api/interfaces/BuildInterfaces'); | ||
import path = require('path'); | ||
import fs = require('fs'); | ||
|
||
export function createAsyncCommand(executionContext: cm.IExecutionContext, command: cm.ITaskCommand) { | ||
return new AddAttachmentCommand(executionContext, command); | ||
} | ||
|
||
export class AddAttachmentCommand implements cm.IAsyncCommand { | ||
constructor(executionContext: cm.IExecutionContext, command: cm.ITaskCommand) { | ||
this.command = command; | ||
this.executionContext = executionContext; | ||
this.description = "Upload and attach attachment to current timeline record."; | ||
} | ||
|
||
public command: cm.ITaskCommand; | ||
public description: string; | ||
public executionContext: cm.IExecutionContext; | ||
|
||
public runCommandAsync(): Q.Promise<any> { | ||
var filename = this.command.message; | ||
if (!filename) { | ||
return Q(null); | ||
} | ||
|
||
var type = this.command.properties['type']; | ||
if (!type) { | ||
return Q(null); | ||
} | ||
|
||
var name = this.command.properties['name']; | ||
if (!name) { | ||
return Q(null); | ||
} | ||
|
||
var deferred = Q.defer(); | ||
fs.exists(filename, (exists: boolean) => { | ||
if (!exists) { | ||
deferred.resolve(null); | ||
} | ||
|
||
var projectId: string = this.executionContext.variables[ctxm.WellKnownVariables.projectId]; | ||
|
||
var webapi = this.executionContext.getWebApi(); | ||
var taskClient = webapi.getQTaskApi(); | ||
|
||
fs.stat(filename, (err: NodeJS.ErrnoException, stats: fs.Stats) => { | ||
if (err) { | ||
deferred.reject(err); | ||
} | ||
else { | ||
var headers = {}; | ||
headers["Content-Length"] = stats.size; | ||
var stream = fs.createReadStream(filename); | ||
taskClient.createAttachment( | ||
headers, | ||
stream, | ||
projectId, | ||
this.executionContext.jobInfo.description, | ||
this.executionContext.jobInfo.planId, | ||
this.executionContext.jobInfo.timelineId, | ||
this.executionContext.recordId, | ||
type, | ||
name).then(() => deferred.resolve(null), (err: any) => deferred.reject(err)); | ||
} | ||
}) | ||
}); | ||
|
||
return deferred.promise; | ||
} | ||
} |