Skip to content

Commit 7e45d61

Browse files
authored
Revert "fix:(GitHub Auth): Deprecate getAuthUsername (#585)" (#590)
This reverts commit d66030d.
1 parent b04b579 commit 7e45d61

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

src/targets/awsLambdaLayer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as path from 'path';
44
import { Octokit } from '@octokit/rest';
55
import simpleGit from 'simple-git';
66
import {
7+
getAuthUsername,
8+
getGitHubApiToken,
79
getGitHubClient,
810
GitHubRemote,
911
} from '../utils/githubApi';
@@ -137,6 +139,8 @@ export class AwsLambdaLayerTarget extends BaseTarget {
137139
this.logger.trace('AWS regions: ', awsRegions);
138140

139141
const remote = this.awsLambdaConfig.registryRemote;
142+
const username = await getAuthUsername(this.github);
143+
remote.setAuth(username, getGitHubApiToken());
140144

141145
await withTempDir(
142146
async directory => {

src/targets/ghPages.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { GitHubGlobalConfig, TargetConfig } from '../schemas/project_config';
88
import { ConfigurationError, reportError } from '../utils/errors';
99
import { withTempDir } from '../utils/files';
1010
import {
11+
getAuthUsername,
12+
getGitHubApiToken,
1113
getGitHubClient,
1214
GitHubRemote,
1315
} from '../utils/githubApi';
@@ -217,9 +219,13 @@ export class GhPagesTarget extends BaseTarget {
217219
packageFiles[0]
218220
);
219221

222+
const username = await getAuthUsername(this.github);
223+
220224
const remote = new GitHubRemote(
221225
githubOwner,
222226
githubRepo,
227+
username,
228+
getGitHubApiToken()
223229
);
224230

225231
await withTempDir(

src/targets/registry.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { GitHubGlobalConfig, TargetConfig } from '../schemas/project_config';
66
import { ConfigurationError, reportError } from '../utils/errors';
77
import { withTempDir } from '../utils/files';
88
import {
9+
getAuthUsername,
10+
getGitHubApiToken,
911
getGitHubClient,
1012
GitHubRemote,
1113
} from '../utils/githubApi';
@@ -425,6 +427,8 @@ export class RegistryTarget extends BaseTarget {
425427

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

429433
const git = simpleGit(directory);
430434
this.logger.info(

src/targets/upm.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Octokit } from '@octokit/rest';
22
import simpleGit from 'simple-git';
33
import {
4+
getAuthUsername,
45
getGitHubApiToken,
56
getGitHubClient,
67
GitHubRemote,
@@ -106,9 +107,11 @@ export class UpmTarget extends BaseTarget {
106107
packageFile
107108
);
108109

110+
const username = await getAuthUsername(this.github);
109111
const remote = new GitHubRemote(
110112
this.config.releaseRepoOwner,
111113
this.config.releaseRepoName,
114+
username,
112115
getGitHubApiToken()
113116
);
114117
const remoteAddr = remote.getRemoteString();

src/utils/githubApi.ts

+33-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class GitHubRemote {
1212
public readonly owner: string;
1313
/** GitHub repository name */
1414
public readonly repo: string;
15+
/** GitHub username */
16+
protected username?: string;
1517
/** GitHub personal authentication token */
1618
protected apiToken?: string;
1719
/** GitHub hostname */
@@ -24,14 +26,27 @@ export class GitHubRemote {
2426
public constructor(
2527
owner: string,
2628
repo: string,
29+
username?: string,
2730
apiToken?: string
2831
) {
2932
this.owner = owner;
3033
this.repo = repo;
31-
this.apiToken = apiToken;
34+
if (username && apiToken) {
35+
this.setAuth(username, apiToken);
36+
}
3237
this.url = `/${this.owner}/${this.repo}/`;
3338
}
3439

40+
/**
41+
* Sets authentication arguments: username and personal API token
42+
*
43+
* @param username GitHub username
44+
* @param apiToken GitHub API token
45+
*/
46+
public setAuth(username: string, apiToken: string): void {
47+
this.username = username;
48+
this.apiToken = apiToken;
49+
}
3550

3651
/**
3752
* Returns an HTTP-based git remote
@@ -45,14 +60,12 @@ export class GitHubRemote {
4560
/**
4661
* Returns an HTTP-based git remote with embedded HTTP basic auth
4762
*
48-
* Using placeholder username as it does not matter for cloning
49-
*
5063
* It MAY contain sensitive information (e.g. API tokens)
5164
*/
5265
public getRemoteStringWithAuth(): string {
5366
const authData =
54-
this.apiToken
55-
? `placeholderusername:${this.apiToken}@`
67+
this.username && this.apiToken
68+
? `${this.username}:${this.apiToken}@`
5669
: '';
5770
return this.PROTOCOL_PREFIX + authData + this.GITHUB_HOSTNAME + this.url;
5871
}
@@ -110,6 +123,21 @@ export function getGitHubClient(token = ''): Octokit {
110123
return _GitHubClientCache[githubApiToken];
111124
}
112125

126+
/**
127+
* Gets the currently authenticated GitHub user from the client
128+
*
129+
* @param github GitHub client
130+
* @returns GitHub username
131+
*/
132+
export async function getAuthUsername(github: Octokit): Promise<string> {
133+
const userData = await github.users.getAuthenticated({});
134+
const username = (userData.data || {}).login;
135+
if (!username) {
136+
throw new Error('Cannot reliably detect GitHub username, aborting');
137+
}
138+
return username;
139+
}
140+
113141
/**
114142
* Loads a file from the context's repository
115143
*

0 commit comments

Comments
 (0)