11import os from 'os'
22import fs from 'fs'
33import { command } from './command.ts'
4- import { getGithubDeployKey , getGithubAccessToken } from './secrets.ts'
4+ import {
5+ getGithubDeployKey ,
6+ getGithubReadToken ,
7+ getGithubReleaseToken ,
8+ getGithubPullRequestToken ,
9+ revokeGithubToken ,
10+ } from './secrets.ts'
511import { fetchHandlingError } from './executionUtils.ts'
612
713interface GitHubPR {
@@ -24,7 +30,7 @@ interface GitHubReleaseParams {
2430}
2531
2632export async function fetchPR ( localBranch : string ) : Promise < GitHubPR | null > {
27- const pr = await callGitHubApi < GitHubPR [ ] > ( 'GET' , `pulls?head=DataDog:${ localBranch } ` )
33+ const pr = await callGitHubApi < GitHubPR [ ] > ( 'GET' , `pulls?head=DataDog:${ localBranch } ` , getGithubReadToken ( ) )
2834 if ( pr && pr . length > 1 ) {
2935 throw new Error ( 'Multiple pull requests found for the branch' )
3036 }
@@ -40,31 +46,40 @@ export async function fetchPR(localBranch: string): Promise<GitHubPR | null> {
4046 */
4147export async function createGitHubRelease ( { version, body } : GitHubReleaseParams ) : Promise < GitHubRelease > {
4248 try {
43- await callGitHubApi ( 'GET' , `releases/tags/${ version } ` )
49+ await callGitHubApi ( 'GET' , `releases/tags/${ version } ` , getGithubReadToken ( ) )
4450 throw new Error ( `Release ${ version } already exists` )
4551 } catch ( error ) {
4652 if ( ( error as any ) . status !== 404 ) {
4753 throw error
4854 }
4955 }
5056
51- return callGitHubApi ( 'POST' , 'releases' , {
57+ // content write
58+ return callGitHubApi < GitHubRelease > ( 'POST' , 'releases' , getGithubReleaseToken ( ) , {
5259 tag_name : version ,
5360 name : version ,
5461 body,
5562 } )
5663}
5764
58- async function callGitHubApi < T > ( method : string , path : string , body ?: any ) : Promise < T > {
59- const response = await fetchHandlingError ( `https://api.github.com/repos/DataDog/browser-sdk/${ path } ` , {
60- method,
61- headers : {
62- Authorization : `token ${ getGithubAccessToken ( ) } ` ,
63- 'X-GitHub-Api-Version' : '2022-11-28' ,
64- } ,
65- body : body ? JSON . stringify ( body ) : undefined ,
66- } )
67- return response . json ( ) as Promise < T >
65+ export async function getPrComments ( prNumber : number ) : Promise < Array < { id : number ; body : string } > > {
66+ const response = await callGitHubApi < Array < { id : number ; body : string } > > (
67+ 'GET' ,
68+ `issues/${ prNumber } /comments` ,
69+ getGithubReadToken ( )
70+ )
71+ return response
72+ }
73+
74+ 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+ }
6883}
6984
7085export function getLastCommonCommit ( baseBranch : string ) : string {
@@ -92,5 +107,20 @@ export function initGitConfig(repository: string): void {
92107 command `git config user.name ci.browser-sdk` . run ( )
93108 command `git remote set-url origin ${ repository } ` . run ( )
94109}
95-
96110export const LOCAL_BRANCH = process . env . CI_COMMIT_REF_NAME
111+
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+ }
126+ }
0 commit comments