Skip to content

Commit d794479

Browse files
Dmitry Bakhtiyarovcertainty3452
authored andcommitted
Adjusted to use azure devops credentials provider
1 parent e7f53db commit d794479

File tree

6 files changed

+6960
-7181
lines changed

6 files changed

+6960
-7181
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ permission for `Service Connections`. Simply add the PAT to your
8585
integrations:
8686
azure:
8787
- host: dev.azure.com
88-
token: ${AZURE_TOKEN}
88+
credentials:
89+
- personalAccessToken: ${AZURE_TOKEN}
8990
```
9091
9192
Read more on integrations in Backstage in the [Integrations

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@parfuemerie-douglas/scaffolder-backend-module-azure-pipelines",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A collection of Backstage scaffolder backend modules for Azure pipelines.",
55
"main": "dist/index.cjs.js",
66
"types": "dist/index.d.ts",
@@ -39,17 +39,17 @@
3939
},
4040
"homepage": "https://github.com/Parfuemerie-Douglas/scaffolder-backend-module-azure-pipelines#readme",
4141
"dependencies": {
42-
"@backstage/backend-common": "^0.18.3",
43-
"@backstage/errors": "^1.1.5",
44-
"@backstage/integration": "^1.4.5",
45-
"@backstage/plugin-scaffolder-backend": "^1.13.1",
46-
"@backstage/plugin-scaffolder-node": "^0.1.1",
47-
"@backstage/types": "^1.0.2",
42+
"@backstage/backend-common": "^0.20.0",
43+
"@backstage/errors": "^1.2.3",
44+
"@backstage/integration": "^1.8.0",
45+
"@backstage/plugin-scaffolder-backend": "^1.19.2",
46+
"@backstage/plugin-scaffolder-node": "^0.2.9",
47+
"@backstage/types": "^1.1.1",
4848
"node-fetch": "^2.6.2"
4949
},
5050
"devDependencies": {
51-
"@backstage/cli": "^0.22.4",
52-
"@types/node-fetch": "^2.6.2"
51+
"@backstage/cli": "^0.25.1",
52+
"@types/node-fetch": "^2.6.2"
5353
},
5454
"dependenciesMeta": {
5555
"buildcheck@0.0.4": {

src/actions/run/createAzurePipeline.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { InputError } from "@backstage/errors";
18-
import { ScmIntegrationRegistry } from "@backstage/integration";
17+
import { DefaultAzureDevOpsCredentialsProvider, ScmIntegrationRegistry } from "@backstage/integration";
1918
import { createTemplateAction } from "@backstage/plugin-scaffolder-node";
2019

2120
import fetch from "node-fetch";
@@ -55,11 +54,6 @@ export const createAzurePipelineAction = (options: {
5554
title: "Create API version",
5655
description: "The Azure Create Pipeline API version to use. Defaults to 6.1-preview.1",
5756
},
58-
server: {
59-
type: "string",
60-
title: "Server hostname",
61-
description: "The hostname of the Azure DevOps service. Defaults to dev.azure.com",
62-
},
6357
organization: {
6458
type: "string",
6559
title: "Organization",
@@ -95,11 +89,6 @@ export const createAzurePipelineAction = (options: {
9589
title: "Azure DevOps Pipelines Definition",
9690
description: "The location of the Azure DevOps Pipeline definition file. Defaults to /azure-pipelines.yaml",
9791
},
98-
token: {
99-
title: "Authentication Token",
100-
type: "string",
101-
description: "The token to use for authorization.",
102-
},
10392
},
10493
},
10594
},
@@ -118,19 +107,10 @@ export const createAzurePipelineAction = (options: {
118107

119108
const host = server ?? "dev.azure.com";
120109
const apiVersion = createApiVersion ?? "6.1-preview.1";
121-
const integrationConfig = integrations.azure.byHost(host);
122-
123-
if (!integrationConfig) {
124-
throw new InputError(
125-
`No matching integration configuration for host ${host}, please check your integrations config`
126-
);
127-
}
128-
129-
if (!integrationConfig.config.token && !ctx.input.token) {
130-
throw new InputError(`No token provided for Azure Integration ${host}`);
131-
}
132-
133-
const token = ctx.input.token ?? integrationConfig.config.token!;
110+
const provider = DefaultAzureDevOpsCredentialsProvider.fromIntegrations(integrations);
111+
const url = `https://${host}/${ctx.input.organization}`;
112+
const credentials = await provider.getCredentials({ url: url });
113+
const token = ctx.input.token ?? credentials?.token;
134114

135115
ctx.logger.info(
136116
`Creating an Azure pipeline for the repository ${repositoryName} with the ID ${repositoryId}.`

src/actions/run/permitAzurePipeline.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { InputError } from "@backstage/errors";
18-
import { ScmIntegrationRegistry } from "@backstage/integration";
17+
import { DefaultAzureDevOpsCredentialsProvider, ScmIntegrationRegistry } from "@backstage/integration";
1918
import { createTemplateAction } from "@backstage/plugin-scaffolder-node";
2019

2120
import fetch from "node-fetch";
@@ -54,11 +53,6 @@ export const permitAzurePipelineAction = (options: {
5453
title: "Permits API version",
5554
description: "The Azure Permits Pipeline API version to use. Defaults to 7.1-preview.1",
5655
},
57-
server: {
58-
type: "string",
59-
title: "Server hostname",
60-
description: "The hostname of the Azure DevOps service. Defaults to dev.azure.com",
61-
},
6256
organization: {
6357
type: "string",
6458
title: "Organization",
@@ -89,11 +83,6 @@ export const permitAzurePipelineAction = (options: {
8983
title: "Pipeline ID",
9084
description: "The pipeline ID.",
9185
},
92-
token: {
93-
title: "Authenticatino Token",
94-
type: "string",
95-
description: "The token to use for authorization.",
96-
},
9786
},
9887
},
9988
},
@@ -111,19 +100,10 @@ export const permitAzurePipelineAction = (options: {
111100

112101
const host = server ?? "dev.azure.com";
113102
const apiVersion = permitsApiVersion ?? "7.1-preview.1";
114-
const integrationConfig = integrations.azure.byHost(host);
115-
116-
if (!integrationConfig) {
117-
throw new InputError(
118-
`No matching integration configuration for host ${host}, please check your integrations config`
119-
);
120-
}
121-
122-
if (!integrationConfig.config.token && !ctx.input.token) {
123-
throw new InputError(`No token provided for Azure Integration ${host}`);
124-
}
125-
126-
const token = ctx.input.token ?? integrationConfig.config.token!;
103+
const provider = DefaultAzureDevOpsCredentialsProvider.fromIntegrations(integrations);
104+
const url = `https://${host}/${ctx.input.organization}`;
105+
const credentials = await provider.getCredentials({ url: url });
106+
const token = ctx.input.token ?? credentials?.token;
127107

128108
if (ctx.input.authorized === true) {
129109
ctx.logger.info(

src/actions/run/runAzurePipeline.ts

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { InputError } from "@backstage/errors";
18-
import { ScmIntegrationRegistry } from "@backstage/integration";
17+
import { DefaultAzureDevOpsCredentialsProvider, ScmIntegrationRegistry } from "@backstage/integration";
1918
import { createTemplateAction } from "@backstage/plugin-scaffolder-node";
2019

2120
import fetch from "node-fetch";
@@ -48,7 +47,7 @@ export const runAzurePipelineAction = (options: {
4847
}) => {
4948
const { integrations } = options;
5049

51-
async function checkPipelineStatus(host: string, organization: string, project: string, runId: number, token: string, buildApiVersion: string): Promise<boolean> {
50+
async function checkPipelineStatus(host: string, organization: string, project: string, runId: number, token: string | null | undefined, buildApiVersion: string): Promise<boolean> {
5251
const response = await fetch(
5352
`https://${host}/${organization}/${project}/_apis/build/builds/${runId}?api-version=${buildApiVersion}`,
5453
{
@@ -69,9 +68,8 @@ export const runAzurePipelineAction = (options: {
6968
// If the pipeline is still running, wait 10 seconds and check again.
7069
await new Promise((resolve) => setTimeout(resolve, 10000));
7170
return checkPipelineStatus(host, organization, project, runId, token,buildApiVersion);
72-
} else {
73-
throw new Error(`Azure pipeline failed with status: ${status}.`);
7471
}
72+
throw new Error(`Azure pipeline failed with status: ${status}.`);
7573
}
7674

7775
return createTemplateAction<{
@@ -106,11 +104,6 @@ export const runAzurePipelineAction = (options: {
106104
title: "Build API version",
107105
description: "The Builds API version to use. Defaults to 6.1-preview.6",
108106
},
109-
server: {
110-
type: "string",
111-
title: "Server hostname",
112-
description: "The hostname of the Azure DevOps service. Defaults to dev.azure.com",
113-
},
114107
organization: {
115108
type: "string",
116109
title: "Organization",
@@ -131,11 +124,6 @@ export const runAzurePipelineAction = (options: {
131124
type: "string",
132125
description: "The branch of the pipeline's repository.",
133126
},
134-
token: {
135-
title: "Authentication Token",
136-
type: "string",
137-
description: "The token to use for authorization.",
138-
},
139127
pipelineParameters: {
140128
title: "Pipeline Parameters",
141129
type: "object",
@@ -161,19 +149,10 @@ export const runAzurePipelineAction = (options: {
161149
runApiVersion: runApiVersion ?? "7.0",
162150
buildApiVersion: buildApiVersion ?? "6.1-preview.6"
163151
}
164-
const integrationConfig = integrations.azure.byHost(host);
165-
166-
if (!integrationConfig) {
167-
throw new InputError(
168-
`No matching integration configuration for host ${host}, please check your integrations config`
169-
);
170-
}
171-
172-
if (!integrationConfig.config.token && !ctx.input.token) {
173-
throw new InputError(`No token provided for Azure Integration ${host}`);
174-
}
175-
176-
const token = ctx.input.token ?? integrationConfig.config.token!;
152+
const provider = DefaultAzureDevOpsCredentialsProvider.fromIntegrations(integrations);
153+
const url = `https://${host}/${ctx.input.organization}`;
154+
const credentials = await provider.getCredentials({ url: url });
155+
const token = ctx.input.token ?? credentials?.token;
177156

178157
ctx.logger.info(`Running Azure pipeline with the ID ${pipelineId}.`);
179158

@@ -210,9 +189,8 @@ export const runAzurePipelineAction = (options: {
210189
).then((response) => {
211190
if (response.ok) {
212191
return response.json();
213-
} else {
214-
throw new Error(`Failed to run Azure pipeline. Status code ${response.status}.`);
215192
}
193+
throw new Error(`Failed to run Azure pipeline. Status code ${response.status}.`);
216194
}).then((json) => {
217195
const pipelineUrl = json._links.web.href;
218196
ctx.logger.info(`Successfully started Azure pipeline run: ${pipelineUrl}`);

0 commit comments

Comments
 (0)