11import * as core from '@actions/core' ;
22import { context } from '@actions/github' ;
3- import { Octokit } from '@octokit/rest' ;
3+ import { Octokit , RestEndpointMethodTypes } from '@octokit/rest' ;
44import minimatch from 'minimatch' ;
55
6+ const statusContext = 'google-internal-tests' ;
7+
8+ type GithubStatus =
9+ RestEndpointMethodTypes [ 'repos' ] [ 'getCombinedStatusForRef' ] [ 'response' ] [ 'data' ] [ 'statuses' ] [ 0 ] ;
10+
611async function main ( ) {
712 if ( context . repo . owner !== 'angular' ) {
813 core . info ( 'Skipping Google Internal Tests action for non-Angular repos.' ) ;
@@ -26,6 +31,13 @@ async function main() {
2631 const github = new Octokit ( { auth : githubToken } ) ;
2732 const prNum = context . payload . pull_request ! . number ;
2833 const prHeadSHA = context . payload . pull_request ! . head ! . sha ;
34+ const existingGoogleStatus = await findExistingTestStatus ( github , prHeadSHA ) ;
35+
36+ // If there is an existing status already pointing to an internal CL, we do not override
37+ // the status. This can happen when e.g. a presubmit-tested PR is closed and reopened.
38+ if ( existingGoogleStatus && existingGoogleStatus . target_url ?. startsWith ( 'http://cl/' ) ) {
39+ return ;
40+ }
2941
3042 const files = await github . paginate ( github . pulls . listFiles , {
3143 ...context . repo ,
@@ -61,7 +73,7 @@ async function main() {
6173 await github . repos . createCommitStatus ( {
6274 ...context . repo ,
6375 ...( affectsGoogle ? waitingForG3Status : irrelevantToG3Status ) ,
64- context : 'google-internal-tests' ,
76+ context : statusContext ,
6577 sha : prHeadSHA ,
6678 } ) ;
6779}
@@ -80,6 +92,22 @@ function constructPatterns(rawPatterns: string): minimatch.IMinimatch[] {
8092 return patterns ;
8193}
8294
95+ async function findExistingTestStatus (
96+ github : Octokit ,
97+ prHeadSHA : string ,
98+ ) : Promise < GithubStatus | null > {
99+ const existingStatuses = await github . paginate (
100+ github . repos . getCombinedStatusForRef ,
101+ {
102+ ...context . repo ,
103+ ref : prHeadSHA ,
104+ } ,
105+ ( r ) => r . data . statuses as GithubStatus [ ] ,
106+ ) ;
107+
108+ return existingStatuses . find ( ( s ) => s . context === statusContext ) ?? null ;
109+ }
110+
83111main ( ) . catch ( ( e : Error ) => {
84112 core . error ( e ) ;
85113 core . setFailed ( e . message ) ;
0 commit comments