Skip to content

Commit

Permalink
fix(): GitHub username not found when pushing to registry (#589)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jeffreyhung authored Feb 20, 2025
1 parent 2a6311e commit 12a9328
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 33 deletions.
4 changes: 1 addition & 3 deletions src/targets/awsLambdaLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as path from 'path';
import { Octokit } from '@octokit/rest';
import simpleGit from 'simple-git';
import {
getAuthUsername,
getGitHubApiToken,
getGitHubClient,
GitHubRemote,
Expand Down Expand Up @@ -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 => {
Expand Down
4 changes: 0 additions & 4 deletions src/targets/ghPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
);

Expand Down
4 changes: 1 addition & 3 deletions src/targets/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -427,8 +426,7 @@ export class RegistryTarget extends BaseTarget {

private async cloneRegistry(directory: string): Promise<SimpleGit> {
const remote = this.remote;
const username = await getAuthUsername(this.github);
remote.setAuth(username, getGitHubApiToken());
remote.setAuth(getGitHubApiToken());

const git = simpleGit(directory);
this.logger.info(
Expand Down
3 changes: 0 additions & 3 deletions src/targets/upm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Octokit } from '@octokit/rest';
import simpleGit from 'simple-git';
import {
getAuthUsername,
getGitHubApiToken,
getGitHubClient,
GitHubRemote,
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 4 additions & 20 deletions src/utils/githubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}/`;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<string> {
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
*
Expand Down

0 comments on commit 12a9328

Please sign in to comment.