|
| 1 | +import { WebApi, getPersonalAccessTokenHandler } from 'azure-devops-node-api'; |
| 2 | +import { IBuildApi } from 'azure-devops-node-api/BuildApi'; |
| 3 | + |
| 4 | +class API { |
| 5 | + public readonly tasks: string[]; |
| 6 | + |
| 7 | + private readonly projectName: string; |
| 8 | + private readonly webApi: WebApi; |
| 9 | + private buildApi: IBuildApi | null = null; |
| 10 | + |
| 11 | + constructor(argv: string[]) { |
| 12 | + const authToken = argv[2]; |
| 13 | + if (!authToken) { |
| 14 | + throw new Error('Auth token is not provided'); |
| 15 | + } |
| 16 | + const adoUrl = argv[3]; |
| 17 | + if (!adoUrl) { |
| 18 | + throw new Error('ADO url is not provided'); |
| 19 | + } |
| 20 | + this.projectName = argv[4]; |
| 21 | + if (!this.projectName) { |
| 22 | + throw new Error('Project name is not provided'); |
| 23 | + } |
| 24 | + const TaskArg = argv[5]; |
| 25 | + if (!TaskArg) { |
| 26 | + throw new Error('Task list is not provided'); |
| 27 | + } |
| 28 | + |
| 29 | + this.tasks = TaskArg.split(','); |
| 30 | + const authHandler = getPersonalAccessTokenHandler(authToken); |
| 31 | + this.webApi = new WebApi(adoUrl, authHandler); |
| 32 | + } |
| 33 | + |
| 34 | + public async getDefinitions () { |
| 35 | + const api = await this.getBuildApi(); |
| 36 | + |
| 37 | + return await api.getDefinitions(this.projectName); |
| 38 | + } |
| 39 | + |
| 40 | + public async getBuild (buildId: number) { |
| 41 | + const api = await this.getBuildApi(); |
| 42 | + |
| 43 | + return await api.getBuild(this.projectName, buildId); |
| 44 | + } |
| 45 | + |
| 46 | + public async queueBuild (definitionId: number, parameters = {}) { |
| 47 | + const api = await this.getBuildApi(); |
| 48 | + |
| 49 | + return await api.queueBuild({ |
| 50 | + definition: { id: definitionId }, |
| 51 | + parameters: JSON.stringify(parameters) |
| 52 | + }, this.projectName); |
| 53 | + } |
| 54 | + |
| 55 | + public async updateBuild (buildId: number) { |
| 56 | + const api = await this.getBuildApi(); |
| 57 | + |
| 58 | + return await api.updateBuild({}, this.projectName, buildId, true); |
| 59 | + } |
| 60 | + |
| 61 | + private async getBuildApi () { |
| 62 | + if (!this.buildApi) this.buildApi = await this.webApi.getBuildApi(); |
| 63 | + |
| 64 | + return this.buildApi; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export const api = new API(process.argv); |
0 commit comments