11// NOTE: This is the source file!
22// ~> Run `npm run build` to produce `index.js`
33
4- import * as core from ' @actions/core' ;
5- import * as github from ' @actions/github' ;
4+ import * as core from " @actions/core" ;
5+ import * as github from " @actions/github" ;
66import * as codeOwnersUtils from "codeowners-utils" ;
77
88// This pulls assignment logic from our codeowners file
99
1010( async function ( ) {
11- try {
12- const token = core . getInput ( 'GITHUB_TOKEN' , { required : true } ) ;
13-
14- const payload = github . context . payload ;
15-
16- const { action, repository, issue } = payload ;
17- if ( ! issue ) throw new Error ( 'Missing "issue" object!' ) ;
18- if ( ! repository ) throw new Error ( 'Missing "repository" object!' ) ;
19- if ( action !== 'opened' ) throw new Error ( 'Must be "issues.opened" event!' ) ;
20-
21- // stop here if "engineering" issue
22- const labels : string [ ] = ( issue . labels || [ ] ) . map ( x => x . name ) ;
23- if ( labels . includes ( 'engineering' ) ) return console . log ( 'ignore "engineering" issues' ) ;
24-
25- // continue for other assignments
26- let cwd = process . cwd ( ) ;
27- let codeowners = await codeOwnersUtils . loadOwners ( cwd ) ;
28- const assignees = new Set < string > ( ) ;
29- const content = issue . body ;
30- if ( ! content ) throw new Error ( 'Missing "issue.body" content!' ) ;
31- if ( ! issue . number ) throw new Error ( 'Missing "issue.number" value!' ) ;
32-
33- const regex = / h t t p s ? : \/ \/ d e v e l o p e r s \. c l o u d f l a r e \. c o m ( [ ^ \s | ) ] * ) / gm;
34- let links = [ ]
35- let m ;
36-
37- while ( ( m = regex . exec ( content ) ) !== null ) {
38- // This is necessary to avoid infinite loops with zero-width matches
39- if ( m . index === regex . lastIndex ) {
40- regex . lastIndex ++ ;
41- }
42-
43- // The result can be accessed through the `m`-variable.
44- m . forEach ( ( match , groupIndex ) => {
45- if ( groupIndex === 1 ) {
46- links . push ( match ) ;
47- }
48- } ) ;
49- }
50-
51- console . log ( "Links are:" )
52- console . log ( links ) ;
53-
54- for ( const item of links ) {
55- const updatedLink = "content" . concat ( item ) ;
56- console . log ( "Updated link is:" )
57- console . log ( updatedLink ) ;
58- const match = codeOwnersUtils . matchFile ( updatedLink , codeowners ) ;
59- for ( const owner of match . owners ) {
60- if ( ! owner . includes ( "/" ) ) {
61- assignees . add ( owner . replace ( / ^ @ / , "" ) ) ;
62- }
63- }
64- }
65- console . log ( "Assignees are:" )
66- console . log ( assignees ) ;
67-
68- if ( assignees . size === 0 ) {
69- assignees . add ( "kodster28" ) ;
70- }
71-
72- const client = github . getOctokit ( token ) ;
73-
74- await client . rest . issues . addAssignees ( {
75- owner : repository . owner . login ,
76- issue_number : issue . number ,
77- repo : repository . name ,
78- assignees : [ ...assignees ] ,
79- } ) ;
80-
81- console . log ( 'Assignees added (if present)' )
82-
83- // Add labels for future reporting
84-
85- const labelPrefix = 'product:' ;
86- const newLabels = new Set < string > ( ) ;
87-
88- for ( const link of links ) {
89- const parts = link . split ( '/' ) ;
90- if ( parts [ 1 ] !== undefined ) {
91- newLabels . add ( labelPrefix . concat ( parts [ 1 ] ) ) ;
92- }
93- }
94-
95- console . log ( newLabels )
96-
97- if ( newLabels . size > 0 ) {
98- await client . rest . issues . addLabels ( {
99- owner : repository . owner . login ,
100- issue_number : issue . number ,
101- repo : repository . name ,
102- labels : [ ...newLabels ] ,
103- } ) ;
104- }
105-
106- console . log ( 'Labels added' )
107-
108- console . log ( 'DONE~!' ) ;
109- } catch ( error ) {
110- core . setFailed ( error . message ) ;
111- }
112- } ) ( ) ;
11+ try {
12+ const token = core . getInput ( "GITHUB_TOKEN" , { required : true } ) ;
13+
14+ const payload = github . context . payload ;
15+
16+ const { action, repository, issue } = payload ;
17+ if ( ! issue ) throw new Error ( 'Missing "issue" object!' ) ;
18+ if ( ! repository ) throw new Error ( 'Missing "repository" object!' ) ;
19+ if ( action !== "opened" ) throw new Error ( 'Must be "issues.opened" event!' ) ;
20+
21+ // stop here if "engineering" issue
22+ const labels : string [ ] = ( issue . labels || [ ] ) . map ( ( x ) => x . name ) ;
23+ if ( labels . includes ( "engineering" ) )
24+ return console . log ( 'ignore "engineering" issues' ) ;
25+
26+ // continue for other assignments
27+ let cwd = process . cwd ( ) ;
28+ let codeowners = await codeOwnersUtils . loadOwners ( cwd ) ;
29+ const assignees = new Set < string > ( ) ;
30+ const content = issue . body ;
31+ if ( ! content ) throw new Error ( 'Missing "issue.body" content!' ) ;
32+ if ( ! issue . number ) throw new Error ( 'Missing "issue.number" value!' ) ;
33+
34+ const regex = / h t t p s ? : \/ \/ d e v e l o p e r s \. c l o u d f l a r e \. c o m ( [ ^ \s | ) ] * ) / gm;
35+ let links = [ ] ;
36+ let m ;
37+
38+ while ( ( m = regex . exec ( content ) ) !== null ) {
39+ // This is necessary to avoid infinite loops with zero-width matches
40+ if ( m . index === regex . lastIndex ) {
41+ regex . lastIndex ++ ;
42+ }
43+
44+ // The result can be accessed through the `m`-variable.
45+ m . forEach ( ( match , groupIndex ) => {
46+ if ( groupIndex === 1 ) {
47+ links . push ( match ) ;
48+ }
49+ } ) ;
50+ }
51+
52+ console . log ( "Links are:" ) ;
53+ console . log ( links ) ;
54+
55+ for ( const item of links ) {
56+ const updatedLink = "src/content/docs" . concat ( item ) ;
57+ console . log ( "Updated link is:" ) ;
58+ console . log ( updatedLink ) ;
59+ const match = codeOwnersUtils . matchFile ( updatedLink , codeowners ) ;
60+ for ( const owner of match . owners ) {
61+ if ( ! owner . includes ( "/" ) ) {
62+ assignees . add ( owner . replace ( / ^ @ / , "" ) ) ;
63+ }
64+ }
65+ }
66+ console . log ( "Assignees are:" ) ;
67+ console . log ( assignees ) ;
68+
69+ if ( assignees . size === 0 ) {
70+ // assign folks which will manually reassign
71+ [ "haleycode" , "pedrosousa" , "dcpena" , "patriciasantaana" ] . forEach (
72+ ( username ) => assignees . add ( username ) ,
73+ ) ;
74+ }
75+
76+ const client = github . getOctokit ( token ) ;
77+
78+ await client . rest . issues . addAssignees ( {
79+ owner : repository . owner . login ,
80+ issue_number : issue . number ,
81+ repo : repository . name ,
82+ assignees : [ ...assignees ] ,
83+ } ) ;
84+
85+ console . log ( "Assignees added (if present)" ) ;
86+
87+ // Add labels for future reporting
88+
89+ const labelPrefix = "product:" ;
90+ const newLabels = new Set < string > ( ) ;
91+
92+ for ( const link of links ) {
93+ const parts = link . split ( "/" ) ;
94+ if ( parts [ 1 ] !== undefined ) {
95+ newLabels . add ( labelPrefix . concat ( parts [ 1 ] ) ) ;
96+ }
97+ }
98+
99+ console . log ( newLabels ) ;
100+
101+ if ( newLabels . size > 0 ) {
102+ await client . rest . issues . addLabels ( {
103+ owner : repository . owner . login ,
104+ issue_number : issue . number ,
105+ repo : repository . name ,
106+ labels : [ ...newLabels ] ,
107+ } ) ;
108+ }
109+
110+ console . log ( "Labels added" ) ;
111+
112+ console . log ( "DONE~!" ) ;
113+ } catch ( error ) {
114+ core . setFailed ( error . message ) ;
115+ }
116+ } ) ( ) ;
0 commit comments