diff --git a/src/devops.d.ts b/src/devops.d.ts new file mode 100644 index 0000000..ab0c014 --- /dev/null +++ b/src/devops.d.ts @@ -0,0 +1 @@ +// \ No newline at end of file diff --git a/src/devops.js b/src/devops.js new file mode 100644 index 0000000..18caeb4 --- /dev/null +++ b/src/devops.js @@ -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 }; +}