From 12a93287cebbeb791f5cd797e44babac69587c71 Mon Sep 17 00:00:00 2001 From: Jeffrey Hung <17494876+Jeffreyhung@users.noreply.github.com> Date: Thu, 20 Feb 2025 13:45:16 -0800 Subject: [PATCH] fix(): GitHub username not found when pushing to registry (#589) * restore `getAuthUsername` and pass placeholderusername if not found * return placeholder if no user find * remove getAuthUsername and username var * remove username and getauthuser from codes --- src/targets/awsLambdaLayer.ts | 4 +--- src/targets/ghPages.ts | 4 ---- src/targets/registry.ts | 4 +--- src/targets/upm.ts | 3 --- src/utils/githubApi.ts | 24 ++++-------------------- 5 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/targets/awsLambdaLayer.ts b/src/targets/awsLambdaLayer.ts index 33c93a90..e4245aef 100644 --- a/src/targets/awsLambdaLayer.ts +++ b/src/targets/awsLambdaLayer.ts @@ -4,7 +4,6 @@ import * as path from 'path'; import { Octokit } from '@octokit/rest'; import simpleGit from 'simple-git'; import { - getAuthUsername, getGitHubApiToken, getGitHubClient, GitHubRemote, @@ -139,8 +138,7 @@ export class AwsLambdaLayerTarget extends BaseTarget { this.logger.trace('AWS regions: ', awsRegions); const remote = this.awsLambdaConfig.registryRemote; - const username = await getAuthUsername(this.github); - remote.setAuth(username, getGitHubApiToken()); + remote.setAuth(getGitHubApiToken()); await withTempDir( async directory => { diff --git a/src/targets/ghPages.ts b/src/targets/ghPages.ts index 0ffe314a..c83dd7f6 100644 --- a/src/targets/ghPages.ts +++ b/src/targets/ghPages.ts @@ -8,7 +8,6 @@ import { GitHubGlobalConfig, TargetConfig } from '../schemas/project_config'; import { ConfigurationError, reportError } from '../utils/errors'; import { withTempDir } from '../utils/files'; import { - getAuthUsername, getGitHubApiToken, getGitHubClient, GitHubRemote, @@ -219,12 +218,9 @@ export class GhPagesTarget extends BaseTarget { packageFiles[0] ); - const username = await getAuthUsername(this.github); - const remote = new GitHubRemote( githubOwner, githubRepo, - username, getGitHubApiToken() ); diff --git a/src/targets/registry.ts b/src/targets/registry.ts index 2e27e514..d85fc933 100644 --- a/src/targets/registry.ts +++ b/src/targets/registry.ts @@ -6,7 +6,6 @@ import { GitHubGlobalConfig, TargetConfig } from '../schemas/project_config'; import { ConfigurationError, reportError } from '../utils/errors'; import { withTempDir } from '../utils/files'; import { - getAuthUsername, getGitHubApiToken, getGitHubClient, GitHubRemote, @@ -427,8 +426,7 @@ export class RegistryTarget extends BaseTarget { private async cloneRegistry(directory: string): Promise { const remote = this.remote; - const username = await getAuthUsername(this.github); - remote.setAuth(username, getGitHubApiToken()); + remote.setAuth(getGitHubApiToken()); const git = simpleGit(directory); this.logger.info( diff --git a/src/targets/upm.ts b/src/targets/upm.ts index 23be07c5..be9d8761 100644 --- a/src/targets/upm.ts +++ b/src/targets/upm.ts @@ -1,7 +1,6 @@ import { Octokit } from '@octokit/rest'; import simpleGit from 'simple-git'; import { - getAuthUsername, getGitHubApiToken, getGitHubClient, GitHubRemote, @@ -107,11 +106,9 @@ export class UpmTarget extends BaseTarget { packageFile ); - const username = await getAuthUsername(this.github); const remote = new GitHubRemote( this.config.releaseRepoOwner, this.config.releaseRepoName, - username, getGitHubApiToken() ); const remoteAddr = remote.getRemoteString(); diff --git a/src/utils/githubApi.ts b/src/utils/githubApi.ts index 40e40fbe..fa3a41d9 100644 --- a/src/utils/githubApi.ts +++ b/src/utils/githubApi.ts @@ -26,13 +26,12 @@ export class GitHubRemote { public constructor( owner: string, repo: string, - username?: string, apiToken?: string ) { this.owner = owner; this.repo = repo; - if (username && apiToken) { - this.setAuth(username, apiToken); + if (apiToken) { + this.setAuth(apiToken); } this.url = `/${this.owner}/${this.repo}/`; } @@ -43,8 +42,8 @@ export class GitHubRemote { * @param username GitHub username * @param apiToken GitHub API token */ - public setAuth(username: string, apiToken: string): void { - this.username = username; + public setAuth(apiToken: string): void { + this.username = 'placeholderusername'; this.apiToken = apiToken; } @@ -123,21 +122,6 @@ export function getGitHubClient(token = ''): Octokit { return _GitHubClientCache[githubApiToken]; } -/** - * Gets the currently authenticated GitHub user from the client - * - * @param github GitHub client - * @returns GitHub username - */ -export async function getAuthUsername(github: Octokit): Promise { - const userData = await github.users.getAuthenticated({}); - const username = (userData.data || {}).login; - if (!username) { - throw new Error('Cannot reliably detect GitHub username, aborting'); - } - return username; -} - /** * Loads a file from the context's repository *