Skip to content

Commit bc401aa

Browse files
♻️ use exclicit resource management for octo tokens (#3790)
1 parent de7db7d commit bc401aa

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

scripts/lib/gitUtils.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getGithubReadToken,
77
getGithubReleaseToken,
88
getGithubPullRequestToken,
9-
revokeGithubToken,
9+
type OctoStsToken,
1010
} from './secrets.ts'
1111
import { fetchHandlingError } from './executionUtils.ts'
1212

@@ -30,7 +30,8 @@ interface GitHubReleaseParams {
3030
}
3131

3232
export async function fetchPR(localBranch: string): Promise<GitHubPR | null> {
33-
const pr = await callGitHubApi<GitHubPR[]>('GET', `pulls?head=DataDog:${localBranch}`, getGithubReadToken())
33+
using readToken = getGithubReadToken()
34+
const pr = await callGitHubApi<GitHubPR[]>('GET', `pulls?head=DataDog:${localBranch}`, readToken)
3435
if (pr && pr.length > 1) {
3536
throw new Error('Multiple pull requests found for the branch')
3637
}
@@ -45,8 +46,9 @@ export async function fetchPR(localBranch: string): Promise<GitHubPR | null> {
4546
* @param params.body - The body of the release.
4647
*/
4748
export async function createGitHubRelease({ version, body }: GitHubReleaseParams): Promise<GitHubRelease> {
49+
using readToken = getGithubReadToken()
4850
try {
49-
await callGitHubApi('GET', `releases/tags/${version}`, getGithubReadToken())
51+
await callGitHubApi('GET', `releases/tags/${version}`, readToken)
5052
throw new Error(`Release ${version} already exists`)
5153
} catch (error) {
5254
if ((error as any).status !== 404) {
@@ -55,31 +57,29 @@ export async function createGitHubRelease({ version, body }: GitHubReleaseParams
5557
}
5658

5759
// content write
58-
return callGitHubApi<GitHubRelease>('POST', 'releases', getGithubReleaseToken(), {
60+
using releaseToken = getGithubReleaseToken()
61+
return callGitHubApi<GitHubRelease>('POST', 'releases', releaseToken, {
5962
tag_name: version,
6063
name: version,
6164
body,
6265
})
6366
}
6467

6568
export async function getPrComments(prNumber: number): Promise<Array<{ id: number; body: string }>> {
69+
using readToken = getGithubReadToken()
6670
const response = await callGitHubApi<Array<{ id: number; body: string }>>(
6771
'GET',
6872
`issues/${prNumber}/comments`,
69-
getGithubReadToken()
73+
readToken
7074
)
7175
return response
7276
}
7377

7478
export function createPullRequest(mainBranch: string) {
75-
const token = getGithubPullRequestToken()
76-
try {
77-
command`gh auth login --with-token`.withInput(token).run()
78-
const pullRequestUrl = command`gh pr create --fill --base ${mainBranch}`.run()
79-
return pullRequestUrl.trim()
80-
} finally {
81-
revokeGithubToken(token)
82-
}
79+
using token = getGithubPullRequestToken()
80+
command`gh auth login --with-token`.withInput(token.value).run()
81+
const pullRequestUrl = command`gh pr create --fill --base ${mainBranch}`.run()
82+
return pullRequestUrl.trim()
8383
}
8484

8585
export function getLastCommonCommit(baseBranch: string): string {
@@ -109,18 +109,14 @@ export function initGitConfig(repository: string): void {
109109
}
110110
export const LOCAL_BRANCH = process.env.CI_COMMIT_REF_NAME
111111

112-
async function callGitHubApi<T>(method: string, path: string, token: string, body?: any): Promise<T> {
113-
try {
114-
const response = await fetchHandlingError(`https://api.github.com/repos/DataDog/browser-sdk/${path}`, {
115-
method,
116-
headers: {
117-
Authorization: `token ${token}`,
118-
'X-GitHub-Api-Version': '2022-11-28',
119-
},
120-
body: body ? JSON.stringify(body) : undefined,
121-
})
122-
return (await response.json()) as Promise<T>
123-
} finally {
124-
revokeGithubToken(token)
125-
}
112+
async function callGitHubApi<T>(method: string, path: string, token: OctoStsToken, body?: any): Promise<T> {
113+
const response = await fetchHandlingError(`https://api.github.com/repos/DataDog/browser-sdk/${path}`, {
114+
method,
115+
headers: {
116+
Authorization: `token ${token.value}`,
117+
'X-GitHub-Api-Version': '2022-11-28',
118+
},
119+
body: body ? JSON.stringify(body) : undefined,
120+
})
121+
return (await response.json()) as Promise<T>
126122
}

scripts/lib/secrets.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,34 @@ export function getGithubDeployKey(): string {
44
return getSecretKey('ci.browser-sdk.github_deploy_key')
55
}
66

7+
export class OctoStsToken {
8+
readonly value: string
9+
10+
constructor(name: string) {
11+
this.value = command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.${name}`.run().trim()
12+
}
13+
14+
[Symbol.dispose]() {
15+
command`dd-octo-sts revoke --token ${this.value}`.run()
16+
}
17+
}
18+
719
/**
820
* This token is scoped to main branch only.
921
*/
10-
export function getGithubPullRequestToken(): string {
11-
return command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.pull_request`.run().trim()
22+
export function getGithubPullRequestToken() {
23+
return new OctoStsToken('pull_request')
1224
}
1325

1426
/**
1527
* This token is scoped to tags only.
1628
*/
17-
export function getGithubReleaseToken(): string {
18-
return command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.release`.run().trim()
19-
}
20-
21-
export function getGithubReadToken(): string {
22-
return command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.read`.run().trim()
29+
export function getGithubReleaseToken() {
30+
return new OctoStsToken('release')
2331
}
2432

25-
export function revokeGithubToken(token: string): string {
26-
return command`dd-octo-sts revoke --token ${token}`.run().trim()
33+
export function getGithubReadToken() {
34+
return new OctoStsToken('read')
2735
}
2836

2937
export function getOrg2ApiKey(): string {

0 commit comments

Comments
 (0)