Skip to content

Commit

Permalink
added new variables to replace the values constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
hevans-dglcom committed Jun 12, 2023
1 parent 45fd919 commit 54529e3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/actions/run/createAzurePipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/run/permitAzurePipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
70 changes: 49 additions & 21 deletions src/actions/run/runAzurePipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
async function checkPipelineStatus(host: string, organization: string, project: string, runId: number, token: string): Promise<boolean> {
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")}`,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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.",
},
},
},
Expand All @@ -115,7 +141,8 @@ export const runAzurePipelineAction = (options: {
pipelineId,
project,
branch,
values
pipelineParameters,
pipelineVariables,
} = ctx.input;

const host = server ?? "dev.azure.com";
Expand All @@ -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<string, string>,
variables: pipelineVariables as Record<string, string>,
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: {
Expand All @@ -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) {
Expand Down

0 comments on commit 54529e3

Please sign in to comment.