From 54529e33a30fddad8778b0dfce11f6e007034cd9 Mon Sep 17 00:00:00 2001 From: Haydn Evans Date: Mon, 12 Jun 2023 19:44:46 +0200 Subject: [PATCH] added new variables to replace the values constant. --- package.json | 2 +- src/actions/run/createAzurePipeline.ts | 4 +- src/actions/run/permitAzurePipeline.ts | 2 +- src/actions/run/runAzurePipeline.ts | 70 ++++++++++++++++++-------- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index cf01098..5a2411f 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "dependencies": { "@backstage/backend-common": "^0.18.3", "@backstage/errors": "^1.1.5", - "@backstage/integration": "^1.4.3", + "@backstage/integration": "^1.4.5", "@backstage/plugin-scaffolder-backend": "^1.13.1", "@backstage/plugin-scaffolder-node": "^0.1.1", "@backstage/types": "^1.0.2", diff --git a/src/actions/run/createAzurePipeline.ts b/src/actions/run/createAzurePipeline.ts index 0f9b841..866f390 100644 --- a/src/actions/run/createAzurePipeline.ts +++ b/src/actions/run/createAzurePipeline.ts @@ -90,7 +90,7 @@ export const createAzurePipelineAction = (options: { description: "The location of the Azure DevOps Pipeline definition file. Defaults to /azure-pipelines.yaml", }, token: { - title: "Authenticatino Token", + title: "Authentication Token", type: "string", description: "The token to use for authorization.", }, @@ -131,7 +131,7 @@ export const createAzurePipelineAction = (options: { // See the Azure DevOps documentation for more information about the REST API: // https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view=azure-devops-rest-6.1 await fetch( - `https://dev.azure.com/${organization}/${project}/_apis/pipelines?api-version=6.1-preview.1`, + `https://${host}/${organization}/${project}/_apis/pipelines?api-version=6.1-preview.1`, { method: "POST", headers: { diff --git a/src/actions/run/permitAzurePipeline.ts b/src/actions/run/permitAzurePipeline.ts index 44c6063..88d47d9 100644 --- a/src/actions/run/permitAzurePipeline.ts +++ b/src/actions/run/permitAzurePipeline.ts @@ -130,7 +130,7 @@ export const permitAzurePipelineAction = (options: { // See the Azure DevOps documentation for more information about the REST API: // https://docs.microsoft.com/en-us/rest/api/azure/devops/approvalsandchecks/pipeline-permissions/update-pipeline-permisions-for-resource?view=azure-devops-rest-7.1 await fetch( - `https://dev.azure.com/${organization}/${project}/_apis/pipelines/pipelinepermissions/${resourceType}/${resourceId}?api-version=7.1-preview.1`, + `https://${host}/${organization}/${project}/_apis/pipelines/pipelinepermissions/${resourceType}/${resourceId}?api-version=7.1-preview.1`, { method: "PATCH", headers: { diff --git a/src/actions/run/runAzurePipeline.ts b/src/actions/run/runAzurePipeline.ts index 773afad..ca6d4ca 100644 --- a/src/actions/run/runAzurePipeline.ts +++ b/src/actions/run/runAzurePipeline.ts @@ -20,14 +20,34 @@ import { createTemplateAction } from "@backstage/plugin-scaffolder-node"; import fetch from "node-fetch"; +interface RunPipelineRequest { + previewRun?: boolean; + resources?: { + repositories: { + self: { + refName: string; + repositoryId?: string; + repositoryType?: string; + }; + }; + }; + templateParameters?: { + [key: string]: string; + }; + variables?: { + [key: string]: string; + }; + yamlOverrides?: string; +} + export const runAzurePipelineAction = (options: { integrations: ScmIntegrationRegistry; }) => { const { integrations } = options; - async function checkPipelineStatus(organization: string, project: string, runId: number, token: string): Promise { + async function checkPipelineStatus(host: string, organization: string, project: string, runId: number, token: string): Promise { const response = await fetch( - `https://dev.azure.com/${organization}/${project}/_apis/build/builds/${runId}?api-version=6.1-preview.6`, + `https://${host}/${organization}/${project}/_apis/build/builds/${runId}?api-version=6.1-preview.6`, { headers: { Authorization: `Basic ${Buffer.from(`PAT:${token}`).toString("base64")}`, @@ -58,7 +78,8 @@ export const runAzurePipelineAction = (options: { project: string; branch?: string; token?: string; - values?: object; + pipelineParameters?: object; + pipelineVariables?: object; }>({ id: "azure:pipeline:run", schema: { @@ -100,10 +121,15 @@ export const runAzurePipelineAction = (options: { type: "string", description: "The token to use for authorization.", }, - values: { - title: "Values", + pipelineParameters: { + title: "Pipeline Parameters", + type: "object", + description: "The values you need as parameters on the request to start a build.", + }, + pipelineVariables: { + title: "Pipeline Variables", type: "object", - description: "The values you need as parameters on the request to azure.", + description: "The values you need as variables on the request to start a build.", }, }, }, @@ -115,7 +141,8 @@ export const runAzurePipelineAction = (options: { pipelineId, project, branch, - values + pipelineParameters, + pipelineVariables, } = ctx.input; const host = server ?? "dev.azure.com"; @@ -135,24 +162,25 @@ export const runAzurePipelineAction = (options: { ctx.logger.info(`Running Azure pipeline with the ID ${pipelineId}.`); - let body: string; - if (values) { - body = JSON.stringify(values); - } else { - body = JSON.stringify({ - resources: { - repositories: { - self: { - refName: `refs/heads/${branch ?? "main"}`, - }, + const request: RunPipelineRequest = { + resources: { + repositories: { + self: { + refName: `refs/heads/${branch ?? "main"}`, }, }, - }); - } + }, + templateParameters: pipelineParameters as Record, + variables: pipelineVariables as Record, + yamlOverrides: "", + }; + + const body = JSON.stringify(request); + // See the Azure DevOps documentation for more information about the REST API: // https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-6.1 await fetch( - `https://dev.azure.com/${organization}/${project}/_apis/pipelines/${pipelineId}/runs?api-version=6.1-preview.1`, + `https://${host}/${organization}/${project}/_apis/pipelines/${pipelineId}/runs?api-version=6.1-preview.1`, { method: "POST", headers: { @@ -178,7 +206,7 @@ export const runAzurePipelineAction = (options: { const pipelineRunId = json.id; // Poll the pipeline status until it completes. - return checkPipelineStatus(organization, project, pipelineRunId, token); + return checkPipelineStatus(host, organization, project, pipelineRunId, token); }) .then((success) => { if (success) {