From a5295aca1b1a257af9dfe36e83816eee0b819c7e Mon Sep 17 00:00:00 2001 From: Alexander Johansson Date: Mon, 28 Sep 2020 01:11:16 +0200 Subject: [PATCH] Added the Pipelines service. --- src/Pipelines/Pipelines.ts | 333 +++++++++++++++++++++++++++++++ src/Pipelines/PipelinesClient.ts | 276 +++++++++++++++++++++++++ src/Pipelines/index.ts | 2 + 3 files changed, 611 insertions(+) create mode 100644 src/Pipelines/Pipelines.ts create mode 100644 src/Pipelines/PipelinesClient.ts create mode 100644 src/Pipelines/index.ts diff --git a/src/Pipelines/Pipelines.ts b/src/Pipelines/Pipelines.ts new file mode 100644 index 00000000..63ed6ce6 --- /dev/null +++ b/src/Pipelines/Pipelines.ts @@ -0,0 +1,333 @@ +/* + * --------------------------------------------------------- + * Copyright(C) Microsoft Corporation. All rights reserved. + * --------------------------------------------------------- + */ + +export interface Run { + /** + * The class to represent a collection of REST reference links. + */ + _links: any; + createdDate: Date; + finalYaml: string; + finishedDate: Date; + id: number; + name: string; + /** + * A reference to a Pipeline. + */ + pipeline: PipelineReference; + resources: RunResources; + result: RunResult; + state: RunState; + url: string; + variables: { [key: string]: Variable } +} + +/** + * A reference to a Pipeline. + */ +export interface PipelineReference { + /** + * Pipeline folder + */ + folder: string; + /** + * Pipeline ID + */ + id: number; + /** + * Pipeline name + */ + name: string; + /** + * Revision number + */ + revision: number; + url: string; +} + +export interface RunResources { + repositories: { [key: string]: RepositoryResource }; +} + +export interface RepositoryResource { + refName: string; + repository: Repository; + version: string; +} + +export interface Repository { + type: RepositoryType; +} + +export enum RepositoryType { + azureReposGit = 0, + azureReposGitHyphenated = 1, + gitHub = 2, + unknown = 3 +} + +export interface RunResult { + canceled: string; + failed: string; + succedded: string; + unknown: string; +} + +export interface RunState { + canceling: string; + completed: string; + inProgress: string; + unknown: string; +} + +export interface Variable { + isSecret: boolean; + value: string; +} + +export interface RunResourcesParameters { + builds?: { [key: string]: BuildResourceParameters }; + containers?: { [key: string]: ContainerResourceParameters }; + packages?: { [key: string]: PackageResourceParameters }; + pipelines?: { [key: string]: PipelineResourceParameters }; + repositories?: { [key: string]: RepositoryResourceParameters }; +} + +export interface BuildResourceParameters { + version: string; +} + +export interface ContainerResourceParameters { + version: string; +} + +export interface PackageResourceParameters { + version: string; +} + +export interface PipelineResourceParameters { + version: string; +} + +export interface RepositoryResourceParameters { + refName: string; + /** + * This is the security token to use when connecting to the repository. + */ + token: string; + /** + * Optional. This is the type of the token given. If not provided, a type of "Bearer" is assumed. Note: Use "Basic" for a PAT token. + */ + tokenType: string; + version: string; +} + +/** + * Settings which influence pipeline runs. + */ +export interface RunPipelineParameters { + /** + * If true, don't actually create a new run. Instead, return the final YAML document after parsing templates. + */ + previewRun?: boolean; + /** + * The resources the run requires. + */ + resources?: RunResourcesParameters + stagesToSkip?: string[]; + templateParameters?: object; + variables?: { [key: string]: Variable }; + /** + * If you use the preview run option, you may optionally supply different YAML. This allows you to preview the final YAML document without committing a changed file. + */ + yamlOverride?: string; +} + +/** + * Type of configuration. + */ +export enum ConfigurationType { + /** + * Designer-JSON. + */ + designerHyphenJson = "designerHyphenJson", + /** + * Designer JSON. + */ + designerJson = "designerJson ", + /** + * Just-in-time. + */ + justInTime = "justInTime", + /** + * Unknown type. + */ + unknown = "unknown", + /** + * YAML. + */ + yaml = "yaml" +} + +export interface PipelineConfiguration { + type: ConfigurationType +} + +export interface Pipeline { + /** + * The class to represent a collection of REST reference links. + */ + _links: any, + configuration: PipelineConfiguration, + /** + * Pipeline folder + */ + folder: string, + /** + * Pipeline ID + */ + id: number, + /** + * Pipeline name + */ + name: string, + /** + * Revision number + */ + revision: number, + /** + * URL of the pipeline + */ + url: string +} + +/** + * Configuration parameters of the pipeline. + */ +export interface CreatePipelineConfigurationParameters { + /** + * Type of configuration. + */ + type: ConfigurationType +} + +/** + * Parameters to create a pipeline. + */ +export interface CreatePipelineParameters { + /** + * Configuration parameters of the pipeline. + */ + configuration: CreatePipelineConfigurationParameters, + /** + * Folder of the pipeline. + */ + folder: string, + /** + * Name of the pipeline. + */ + name: string +} + +/** + * Expand options. Default is None. + */ +export interface GetLogExpandOptions { + none: string, + signedContent: string +} + +/** + * Log for a pipeline. + */ +export interface Log { + /** + * The date and time the log was created. + */ + createdOn: Date, + /** + * + The ID of the log. + */ + id: number, + /** + * The date and time the log was last changed. + */ + lastChangedOn: Date, + /** + * The number of lines in the log. + */ + lineCount: number + /** + * A signed url allowing limited-time anonymous access to private resources. + */ + signedContent: SignedUrl, + url: string +} + +/** + * A collection of logs. + */ +export interface LogCollection { + /** + * The list of logs. + */ + logs: Log[], + /** + * A signed url allowing limited-time anonymous access to private resources. + */ + signedContent: SignedUrl + /** + * URL of the log. + */ + url: string +} + +/** + * A signed url allowing limited-time anonymous access to private resources. + */ +export interface SignedUrl { + /** + * Timestamp when access expires. + */ + signatureExpires: Date, + /** + * The URL to allow access to. + */ + url: string +} + +/** + * Artifacts are collections of files produced by a pipeline. Use artifacts to share files between stages in a pipeline or between different pipelines. + */ +export interface Artifact { + /** + * The name of the artifact. + */ + name: string, + /** + * Signed url for downloading this artifact + */ + signedContent: SignedUrl, + /** + * Self-referential url + */ + url: string +} + +/** + * Expand options. Default is None. + */ +export interface GetArtifactExpandOptions { + /** + * No expansion. + */ + none: string, + /** + * Include signed content. + */ + signedContent: string +} diff --git a/src/Pipelines/PipelinesClient.ts b/src/Pipelines/PipelinesClient.ts new file mode 100644 index 00000000..9afd2af7 --- /dev/null +++ b/src/Pipelines/PipelinesClient.ts @@ -0,0 +1,276 @@ +/* + * --------------------------------------------------------- + * Copyright(C) Microsoft Corporation. All rights reserved. + * --------------------------------------------------------- + */ + +import { IVssRestClientOptions } from "../Common/Context"; +import { RestClientBase } from "../Common/RestClientBase"; + +import * as Pipelines from "../Pipelines/Pipelines" + +export class PipelineRestClient extends RestClientBase { + constructor(options: IVssRestClientOptions) { + super(options); + } + + /** + * Get a specific artifact from a pipeline run + * @param pipelineId - ID of the pipeline. + * @param project - Project ID or project name + * @param runId - ID of the run of that pipeline. + * @param artifactName - Name of the artifact. + * @param expand - Expand options. Default is None. + */ + public async getArtifact( + pipelineId: number, + project: string, + runId: number, + artifactName: string, + expand: Pipelines.GetArtifactExpandOptions + ): Promise { + + const queryValues: any = { + artifactName: artifactName, + '$expand': expand + }; + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}/runs/{runId}/artifacts?artifactName={artifactName}", + routeValues: { + project: project, + pipelineId: pipelineId, + runId: runId, + artifactName: artifactName + }, + queryParams: queryValues + }); + } + + /** + * Get a specific log from a pipeline run + * @param logId - ID of the log. + * @param pipelineId - ID of the pipeline. + * @param project - Project ID or project name + * @param runId - ID of the run of that pipeline. + * @param expand - Expand options. Default is None. + */ + public async getLog( + logId: number, + pipelineId: number, + project: string, + runId: number, + expand?: Pipelines.GetLogExpandOptions + ): Promise { + + const queryValues: any = { + '$expand': expand + }; + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}/runs/{runId}/logs/{logId}", + routeValues: { + project: project, + pipelineId: pipelineId, + runId: runId, + logId: logId + }, + queryParams: queryValues + }); + } + + /** + * Get a list of logs from a pipeline run. + * @param pipelineId - ID of the pipeline. + * @param project - Project ID or project name + * @param runId - ID of the run of that pipeline. + * @param expand - Expand options. Default is None. + */ + public async listLogs( + pipelineId: number, + project: string, + runId: number, + expand?: Pipelines.GetLogExpandOptions + ): Promise { + + const queryValues: any = { + '$expand': expand + }; + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}/runs/{runId}/logs", + routeValues: { + project: project, + pipelineId: pipelineId, + runId: runId + }, + queryParams: queryValues + }); + } + + /** + * Create a pipeline. + * @param createPipelineParameters - Parameters to create a pipeline. + * @param project - Project ID or project name + */ + public async createPipeline( + createPipelineParameters: Pipelines.CreatePipelineParameters, + project: string + ): Promise { + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "POST", + routeTemplate: "{project}/_apis/pipelines", + routeValues: { + project: project + }, + body: createPipelineParameters + }); + } + + /** + * Gets a pipeline, optionally at the specified version + * + * @param pipelineId - The pipeline ID + * @param project - Project ID or project name + * @param pipelineVersion - The pipeline version + */ + public async getPipeline( + pipelineId: number, + project: string, + pipelineVersion?: number + ): Promise { + + const queryValues: any = { + pipelineVersion: pipelineVersion + }; + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}", + routeValues: { + project: project, + pipelineId: pipelineId + }, + queryParams: queryValues + }); + } + + /** + * Get a list of pipelines. + * + * @param project - Project ID or project name + * @param top - The maximum number of pipelines to return + * @param continuationToken - A continuation token from a previous request, to retrieve the next page of results + * @param orderBy - A sort expression. Defaults to "name asc" + */ + public async listPipelines( + project: string, + top?: number, + continuationToken?: string, + orderBy?: string + ): Promise { + + const queryValues: any = { + '$top': top, + continuationToken: continuationToken, + orderBy: orderBy + }; + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines", + routeValues: { + project: project + }, + queryParams: queryValues + }); + } + + /** + * Gets a run for a particular pipeline. + * + * @param pipelineId - The pipeline id + * @param project - Project ID or project name + * @param runId - The run id + */ + public async getPipelineRun( + pipelineId: number, + project: string, + runId: number + ): Promise { + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}/runs/{runId}", + routeValues: { + project: project, + pipelineId: pipelineId, + runId: runId + } + }); + } + + /** + * Gets top 10000 runs for a particular pipeline. + * + * @param pipelineId - The pipeline id + * @param project - Project ID or project name + */ + public async listPipelineRuns( + pipelineId: number, + project: string + ): Promise { + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "GET", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}/runs", + routeValues: { + project: project, + pipelineId: pipelineId + } + }); + } + + /** + * Runs a pipeline. + * + * @param runPipelineParameters - Settings which influence pipeline runs. + * @param pipelineId - The pipeline ID. + * @param project - Project ID or project name. + * @param pipelineVersion - The pipeline version. + */ + public async runPipeline( + runPipelineParameters: Pipelines.RunPipelineParameters, + pipelineId: number, + project: string, + pipelineVersion?: number + ): Promise { + + const queryValues: any = { + pipelineVersion + }; + + return this.beginRequest({ + apiVersion: "6.0-preview.1", + method: "POST", + routeTemplate: "{project}/_apis/pipelines/{pipelineId}/runs", + routeValues: { + project: project, + pipelineId: pipelineId + }, + queryParams: queryValues, + body: runPipelineParameters + }); + } +} diff --git a/src/Pipelines/index.ts b/src/Pipelines/index.ts new file mode 100644 index 00000000..ddf8510b --- /dev/null +++ b/src/Pipelines/index.ts @@ -0,0 +1,2 @@ +export * from "./Pipelines"; +export * from "./PipelinesClient"; \ No newline at end of file