Skip to content

Commit 3899559

Browse files
CopilotMalcolmnixon
andcommitted
Add GITHUB_TOKEN fallback in GitHub connector authentication
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
1 parent 0f5f40b commit 3899559

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

THEORY-OF-OPERATIONS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ BuildMark also runs `git rev-parse --abbrev-ref HEAD` to determine the current b
8282
##### 2. Authenticate with GitHub
8383

8484
BuildMark reads an authentication token from environment variables. It checks `GH_TOKEN` first,
85-
then falls back to `GITHUB_TOKEN`. If no token is found, unauthenticated requests are made, which
86-
are subject to lower API rate limits (60 requests per hour versus 5,000 with a token).
85+
then falls back to `GITHUB_TOKEN`. If neither environment variable is set, BuildMark attempts to
86+
obtain a token by running `gh auth token` (the GitHub CLI). If no token can be obtained from any
87+
of these sources, BuildMark fails with an error.
8788

8889
##### 3. Fetch repository data
8990

src/DemaConsulting.BuildMark/RepoConnectors/GitHubRepoConnector.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,21 +875,28 @@ private static string GetTypeFromLabels(IReadOnlyList<PullRequestLabelInfo> labe
875875
/// <returns>GitHub token.</returns>
876876
private async Task<string> GetGitHubTokenAsync()
877877
{
878-
// Try to get token from environment variable first
878+
// Try GH_TOKEN environment variable first
879879
var token = Environment.GetEnvironmentVariable("GH_TOKEN");
880880
if (!string.IsNullOrEmpty(token))
881881
{
882882
return token;
883883
}
884884

885+
// Try GITHUB_TOKEN environment variable next
886+
token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
887+
if (!string.IsNullOrEmpty(token))
888+
{
889+
return token;
890+
}
891+
885892
// Fall back to gh auth token
886893
try
887894
{
888895
return await RunCommandAsync("gh", "auth token");
889896
}
890897
catch (InvalidOperationException)
891898
{
892-
throw new InvalidOperationException("Unable to get GitHub token. Set GH_TOKEN environment variable or authenticate with 'gh auth login'.");
899+
throw new InvalidOperationException("Unable to get GitHub token. Set GH_TOKEN or GITHUB_TOKEN environment variable or authenticate with 'gh auth login'.");
893900
}
894901
}
895902

0 commit comments

Comments
 (0)