@@ -12,6 +12,8 @@ export class GitHubRemote {
12
12
public readonly owner : string ;
13
13
/** GitHub repository name */
14
14
public readonly repo : string ;
15
+ /** GitHub username */
16
+ protected username ?: string ;
15
17
/** GitHub personal authentication token */
16
18
protected apiToken ?: string ;
17
19
/** GitHub hostname */
@@ -24,14 +26,27 @@ export class GitHubRemote {
24
26
public constructor (
25
27
owner : string ,
26
28
repo : string ,
29
+ username ?: string ,
27
30
apiToken ?: string
28
31
) {
29
32
this . owner = owner ;
30
33
this . repo = repo ;
31
- this . apiToken = apiToken ;
34
+ if ( username && apiToken ) {
35
+ this . setAuth ( username , apiToken ) ;
36
+ }
32
37
this . url = `/${ this . owner } /${ this . repo } /` ;
33
38
}
34
39
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
+ }
35
50
36
51
/**
37
52
* Returns an HTTP-based git remote
@@ -45,14 +60,12 @@ export class GitHubRemote {
45
60
/**
46
61
* Returns an HTTP-based git remote with embedded HTTP basic auth
47
62
*
48
- * Using placeholder username as it does not matter for cloning
49
- *
50
63
* It MAY contain sensitive information (e.g. API tokens)
51
64
*/
52
65
public getRemoteStringWithAuth ( ) : string {
53
66
const authData =
54
- this . apiToken
55
- ? `placeholderusername :${ this . apiToken } @`
67
+ this . username && this . apiToken
68
+ ? `${ this . username } :${ this . apiToken } @`
56
69
: '' ;
57
70
return this . PROTOCOL_PREFIX + authData + this . GITHUB_HOSTNAME + this . url ;
58
71
}
@@ -110,6 +123,21 @@ export function getGitHubClient(token = ''): Octokit {
110
123
return _GitHubClientCache [ githubApiToken ] ;
111
124
}
112
125
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
+
113
141
/**
114
142
* Loads a file from the context's repository
115
143
*
0 commit comments