Migrated to TypeScript.
This commit is contained in:
42
src/devops/index.ts
Normal file
42
src/devops/index.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/**
|
||||
* A DevOps helpers module.
|
||||
*/
|
||||
|
||||
import { loginInteractive } from "../azure/index.ts";
|
||||
import * as azdev from "azure-devops-node-api";
|
||||
|
||||
const AZURE_DEVOPS_SCOPES = ["https://app.vssps.visualstudio.com/.default"];
|
||||
|
||||
type LoginInteractiveResult = {
|
||||
accessToken?: string;
|
||||
};
|
||||
|
||||
export async function getDevOpsApiToken(tenantId: string, clientId: string): Promise<string> {
|
||||
const result = await loginInteractive({
|
||||
tenantId,
|
||||
clientId,
|
||||
scopes: AZURE_DEVOPS_SCOPES,
|
||||
}) as LoginInteractiveResult;
|
||||
|
||||
const accessToken = result?.accessToken;
|
||||
|
||||
if (!accessToken) {
|
||||
throw new Error("Failed to obtain Azure DevOps API token");
|
||||
}
|
||||
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
export async function getDevOpsClients(orgUrl: string, tenantId: string, clientId: string): Promise<{ coreClient: unknown; gitClient: unknown }> {
|
||||
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