Add DevOps helpers module with API token retrieval and client functions
This commit is contained in:
1
src/devops.d.ts
vendored
Normal file
1
src/devops.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//
|
||||
49
src/devops.js
Normal file
49
src/devops.js
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* A DevOps helpers module.
|
||||
*/
|
||||
|
||||
import { loginInteractive } from "./azure.js";
|
||||
import * as azdev from "azure-devops-node-api";
|
||||
|
||||
const AZURE_DEVOPS_SCOPES = ["https://app.vssps.visualstudio.com/.default"];
|
||||
|
||||
/**
|
||||
* Get Azure DevOps API token.
|
||||
*/
|
||||
|
||||
export async function getDevOpsApiToken(tenantId, clientId) {
|
||||
const result = await loginInteractive({
|
||||
tenantId,
|
||||
clientId,
|
||||
scopes: AZURE_DEVOPS_SCOPES,
|
||||
});
|
||||
|
||||
const accessToken = result?.accessToken;
|
||||
|
||||
if(!accessToken) {
|
||||
throw new Error("Failed to obtain Azure DevOps API token");
|
||||
}
|
||||
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Azure DevOps clients - Core and Git.
|
||||
*
|
||||
* @param { string } orgUrl - The Azure DevOps organization URL
|
||||
* @param { string } tenantId - The Azure AD tenant ID
|
||||
* @param { string } clientId - The Azure AD client ID
|
||||
* @returns
|
||||
*/
|
||||
|
||||
export async function getDevOpsClients(orgUrl, tenantId, clientId) {
|
||||
const accessToken = await getDevOpsApiToken(tenantId, clientId);
|
||||
|
||||
const authHandler = azdev.getBearerHandler(accessToken);
|
||||
const connection = new azdev.WebApi(orgUrl, authHandler);
|
||||
|
||||
const coreClient = await connection.getCoreApi();
|
||||
const gitClient = await connection.getGitApi();
|
||||
|
||||
return { coreClient, gitClient };
|
||||
}
|
||||
Reference in New Issue
Block a user