1
1
const github = require ( "@actions/github" ) ;
2
- const token = process . argv . slice ( 2 ) . join ( '' ) . substring ( 6 ) ;
3
- const owner = github . context . repo . owner ;
4
- const repo = github . context . repo . repo ;
5
- const octo = github . getOctokit ( token ) ;
6
-
7
- const pullRequests = ( ) => {
8
- var response = octo . rest . pulls . list ( {
9
- owner : owner ,
10
- repo : repo ,
11
- } ) [ "catch" ] ( ( e ) => {
12
- console . log ( e . message )
13
- } ) ;
14
- return response ;
15
- }
16
-
17
- const parsePullRequestId = githubRef => {
18
- const result = / r e f s \/ p u l l \/ ( \d + ) \/ m e r g e / g. exec ( githubRef ) ;
19
- if ( ! result ) throw new Error ( "Reference not found." ) ;
20
- const [ , pullRequestId ] = result ;
21
- return pullRequestId ;
22
- } ;
23
2
24
3
async function main ( ) {
25
4
26
- // current pull request id
27
- const pullRequestId = parsePullRequestId ( process . env . GITHUB_REF ) ;
28
-
29
- await pullRequests ( ) . then ( prs => {
5
+ const token = process . env . TOKEN ;
6
+ const owner = github . context . repo . owner ;
7
+ const repo = github . context . repo . repo ;
8
+ const octo = github . getOctokit ( token ) ;
9
+ const currentPR = github . context . issue . number ;
10
+
11
+ //Fetch PRs
12
+ octo . rest . pulls . list ( {
13
+ owner : owner ,
14
+ repo : repo ,
15
+ } ) . then ( prs => {
16
+ //Filter PRs
30
17
let filteredPrs = prs . data . filter ( pr => {
31
18
let regex = new RegExp ( '^Lokalise:[ _a-zA-Z0-9]+' ) ;
32
- return regex . test ( pr . title ) && pr . number != pullRequestId ;
19
+ return regex . test ( pr . title ) && pr . number != currentPR ;
33
20
} ) ;
34
-
35
- //if no extra PRs under Lokalise, exit
36
- if ( ! filteredPrs . length > 0 ) return true ;
37
-
38
- //otherwise, go ahead and set to close and delete the branch.
39
- filteredPrs . forEach ( pr => {
40
- //Close Branch
41
- octo . rest . pulls . update ( {
21
+ if ( filteredPrs && filteredPrs . length ) {
22
+ filteredPrs . forEach ( pr => {
23
+ //Close Branch
24
+ octo . rest . pulls . update ( {
25
+ owner : owner ,
26
+ repo : repo ,
27
+ pull_number : pr . number ,
28
+ state : 'closed'
29
+ } ) ;
30
+ //Delete Branch
31
+ octo . rest . pulls . get ( {
42
32
owner : owner ,
43
33
repo : repo ,
44
- pull_number : pr . number ,
45
- state : 'closed'
34
+ pull_number : pr . number
35
+ } ) . then ( ( { data } ) => {
36
+ const ref = 'heads/' + data [ 'head' ] [ 'ref' ] ;
37
+ try {
38
+ octo . rest . git . deleteRef ( {
39
+ owner : owner ,
40
+ repo : repo ,
41
+ ref
42
+ } ) ;
43
+ } catch ( { message } ) { console . log ( message ) }
44
+ } ) ;
46
45
} ) ;
47
- octo . rest . pulls . get ( {
48
- owner : owner ,
49
- repo : repo ,
50
- pull_number : pr . number
51
- } ) . then ( ( { data } ) => {
52
- const ref = 'heads/' + data [ 'head' ] [ 'ref' ] ;
53
- try {
54
- //Delete Branch
55
- octo . rest . git . deleteRef ( {
56
- owner : owner ,
57
- repo : repo ,
58
- ref
59
- } ) ;
60
- }
61
- catch ( e ) {
62
- console . log ( e . message )
63
- }
64
- } ) ;
65
- } ) ;
46
+ }
47
+ } ) . catch ( e => {
48
+ console . error ( e ) ;
66
49
} ) ;
67
50
} ;
68
51
69
- main ( ) . catch ( err => {
70
- console . error ( err ) ;
71
- } ) ; ;
52
+ main ( ) ;
0 commit comments