66 getGithubReadToken ,
77 getGithubReleaseToken ,
88 getGithubPullRequestToken ,
9- revokeGithubToken ,
9+ type OctoStsToken ,
1010} from './secrets.ts'
1111import { fetchHandlingError } from './executionUtils.ts'
1212
@@ -30,7 +30,8 @@ interface GitHubReleaseParams {
3030}
3131
3232export 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 */
4748export 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
6568export 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
7478export 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
8585export function getLastCommonCommit ( baseBranch : string ) : string {
@@ -109,18 +109,14 @@ export function initGitConfig(repository: string): void {
109109}
110110export 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}
0 commit comments