Move resolveToken to pr-checks/api-client.ts

This commit is contained in:
Mario Campos
2026-10-29 09:49:40 -05:00
parent c069d49e81
commit afaf324646
3 changed files with 13 additions and 14 deletions

View File

@@ -8,3 +8,14 @@ export function getApiClient(token: string): ApiClient {
const opts = githubUtils.getOctokitOptions(token);
return new githubUtils.GitHub(opts);
}
/** Resolves the GitHub API token to use, from `GH_TOKEN` or `GITHUB_TOKEN`. */
export function resolveToken(env: NodeJS.ProcessEnv): string {
const token = env.GH_TOKEN?.trim() || env.GITHUB_TOKEN?.trim();
if (!token) {
throw new Error(
"Missing authentication token. Set GH_TOKEN or GITHUB_TOKEN.",
);
}
return token;
}

View File

@@ -7,12 +7,11 @@ Tests for post-comment.ts.
import * as assert from "node:assert/strict";
import { describe, it } from "node:test";
import { type ApiClient } from "./api-client";
import { type ApiClient, resolveToken } from "./api-client";
import {
findExistingComment,
type Options,
parseOptions,
resolveToken,
setComment,
} from "./post-comment";

View File

@@ -17,7 +17,7 @@ The `.github/actions/post-comment` action requires callers to supply this value
import assert from "node:assert/strict";
import { parseArgs } from "node:util";
import { type ApiClient, getApiClient } from "./api-client";
import { type ApiClient, getApiClient, resolveToken } from "./api-client";
/** Represents the command-line options. */
export interface Options {
@@ -55,17 +55,6 @@ export function parseOptions(argv?: string[]): Options {
};
}
/** Resolves the GitHub API token to use, from `GH_TOKEN` or `GITHUB_TOKEN`. */
export function resolveToken(env: NodeJS.ProcessEnv): string {
const token = env.GH_TOKEN?.trim() || env.GITHUB_TOKEN?.trim();
if (!token) {
throw new Error(
"Missing authentication token. Set GH_TOKEN or GITHUB_TOKEN.",
);
}
return token;
}
/**
* Finds the first comment on the given issue/pull request whose body starts with `marker`, if any.
*/