@@ -23,86 +23,84 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
return result ;
24
24
} ;
25
25
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
26
- exports . main = exports . getAllTeamsWithAtLeastWriteAccess = exports . getAllCodeowners = exports . getFileContents = void 0 ;
26
+ exports . main = void 0 ;
27
27
/* eslint-disable no-process-env */
28
28
const util_1 = require ( "util" ) ;
29
- const fs = __importStar ( require ( "fs" ) ) ;
30
29
const core = __importStar ( require ( "@actions/core" ) ) ;
31
30
const github = __importStar ( require ( "@actions/github" ) ) ;
32
- function getFileContents ( defaultFileDetectionLocations = [ "CODEOWNERS" , "docs/CODEOWNERS" , ".github/CODEOWNERS" ] ) {
33
- const filePath = core . getInput ( "file-path" , { required : true } ) ;
34
- let locationsToCheck = defaultFileDetectionLocations ;
35
- if ( filePath . length > 0 ) {
36
- const thisPlatformPath = core . toPlatformPath ( filePath ) ;
37
- core . debug ( `Using specified path: ${ thisPlatformPath } ` ) ;
38
- locationsToCheck = [ thisPlatformPath ] ;
39
- }
40
- else {
41
- core . info ( "Did not find specified input path, using default detection method." ) ;
42
- }
43
- const existingPaths = locationsToCheck . filter ( ( path ) => {
44
- return fs . existsSync ( path ) ;
45
- } ) ;
46
- return existingPaths . map ( ( path ) => {
47
- core . notice ( `Found CODEOWNERS file at '${ path } ' to operate on.` ) ;
48
- return {
49
- path,
50
- contents : fs . readFileSync ( path , "utf8" ) ,
51
- } ;
52
- } ) ;
53
- }
54
- exports . getFileContents = getFileContents ;
55
- function getAllCodeowners ( fileContents ) {
56
- const allCodeowners = fileContents
57
- . map ( ( { contents } ) => {
58
- return contents
59
- . split ( "\n" )
60
- . filter ( ( line ) => {
61
- return ! line . startsWith ( "#" ) && line . length > 0 ;
62
- } )
63
- . map ( ( line ) => {
64
- const [ _path , ...owners ] = line . split ( " " ) ;
65
- return owners ;
66
- } )
67
- . flat ( ) ;
68
- } )
69
- . flat ( ) ;
70
- return [ ...new Set ( allCodeowners ) ] ;
71
- }
72
- exports . getAllCodeowners = getAllCodeowners ;
73
- async function getAllTeamsWithAtLeastWriteAccess ( octokit , owner , repo ) {
74
- let { data } = await octokit . rest . repos . listTeams ( { owner, repo } ) ;
75
- console . log ( data ) ;
76
- const teams = data
77
- . filter ( ( { permission } ) => {
78
- return [ "admin" , "push" ] . includes ( permission ) ;
79
- } )
80
- . map ( ( { slug } ) => {
81
- return slug ;
82
- } ) ;
83
- return [ ...new Set ( teams ) ] ;
84
- }
85
- exports . getAllTeamsWithAtLeastWriteAccess = getAllTeamsWithAtLeastWriteAccess ;
31
+ const helpers_1 = require ( "./helpers" ) ;
32
+ const teams_1 = require ( "./teams" ) ;
33
+ const collaborators_1 = require ( "./collaborators" ) ;
86
34
async function main ( ) {
87
35
try {
88
36
const isDryRun = ( core . getInput ( "dry-run" , { required : false } ) || "false" ) === "true" ;
89
37
if ( isDryRun ) {
90
38
core . notice ( '"dry-run" enabled; will not make changes.' ) ;
91
39
}
92
- const currentCodeowners = getFileContents ( ) ;
40
+ const currentCodeowners = ( 0 , helpers_1 . getFileContents ) ( ) ;
93
41
if ( currentCodeowners . length === 0 ) {
94
42
const errorMsg = "No CODEOWNERS file(s) found." ;
95
43
core . error ( errorMsg ) ;
96
44
throw new Error ( errorMsg ) ;
97
45
}
98
- const allCodeowners = getAllCodeowners ( currentCodeowners ) ;
46
+ const allCodeowners = ( 0 , helpers_1 . getAllCodeowners ) ( currentCodeowners ) ;
99
47
core . notice ( `Found ${ allCodeowners . length } unique codeowners.` ) ;
100
48
core . debug ( `All codeowners: ${ ( 0 , util_1 . inspect ) ( allCodeowners ) } ` ) ;
49
+ if ( allCodeowners . length === 0 ) {
50
+ core . notice ( "No CODEOWNERS found; nothing to do." ) ;
51
+ return ;
52
+ }
53
+ const teamCodeowners = allCodeowners . filter ( ( codeowner ) => {
54
+ return codeowner . includes ( "/" ) ;
55
+ } ) ;
56
+ core . notice ( `Found ${ teamCodeowners . length } unique team codeowners.` ) ;
57
+ core . debug ( `Team codeowners: ${ ( 0 , util_1 . inspect ) ( teamCodeowners ) } ` ) ;
58
+ const userCodeowners = allCodeowners . filter ( ( codeowner ) => {
59
+ return ! codeowner . includes ( "/" ) ;
60
+ } ) ;
61
+ core . notice ( `Found ${ userCodeowners . length } unique user codeowners.` ) ;
62
+ core . debug ( `User codeowners: ${ ( 0 , util_1 . inspect ) ( userCodeowners ) } ` ) ;
101
63
const githubToken = core . getInput ( "github-token" , { required : true } ) ;
102
64
const octokit = github . getOctokit ( githubToken ) ;
103
- const allTeamsWithAtLeastWriteAccess = await getAllTeamsWithAtLeastWriteAccess ( octokit , github . context . repo . owner , github . context . repo . repo ) ;
104
- core . notice ( `Found ${ allTeamsWithAtLeastWriteAccess . length } unique teams with write access.` ) ;
105
- core . debug ( `All teams with write access: ${ allTeamsWithAtLeastWriteAccess } ` ) ;
65
+ // TEAM-Y OPERATIONS
66
+ const allTeamsWithAtLeastWriteAccess = await ( 0 , teams_1 . getAllTeamsWithAtLeastWriteAccess ) ( octokit , github . context . repo . owner , github . context . repo . repo ) ;
67
+ core . info ( `Found ${ allTeamsWithAtLeastWriteAccess . length } teams with at least write access.` ) ;
68
+ core . debug ( `All teams with at least write access: ${ ( 0 , util_1 . inspect ) ( allTeamsWithAtLeastWriteAccess ) } ` ) ;
69
+ const failedTeams = [ ] ;
70
+ teamCodeowners . forEach ( async ( team ) => {
71
+ const [ orgName , teamSlug ] = team . replaceAll ( "@" , "" ) . split ( "/" ) ;
72
+ core . debug ( `Found org: '${ orgName } ' and team: '${ teamSlug } ' from '${ team } '.` ) ;
73
+ if ( allTeamsWithAtLeastWriteAccess . includes ( teamSlug ) ) {
74
+ core . notice ( `Team '${ teamSlug } ' already has at least write access; skipping.` ) ;
75
+ }
76
+ else {
77
+ await ( 0 , teams_1 . addTeamToWriteAccess ) ( octokit , orgName , github . context . repo . repo , teamSlug , isDryRun ) . catch ( ( error ) => {
78
+ failedTeams . push ( teamSlug ) ;
79
+ core . warning ( `Failed to give write access to team '${ teamSlug } ': ${ error } ` ) ;
80
+ } ) ;
81
+ }
82
+ } ) ;
83
+ // USER-Y OPERATIONS HERE
84
+ const allUsersWithAtLeastWriteAccess = await ( 0 , collaborators_1 . getAllDirectCollaboratorsWithAtLeastWriteAccess ) ( octokit , github . context . repo . owner , github . context . repo . repo ) ;
85
+ core . info ( `Found ${ allUsersWithAtLeastWriteAccess . length } users with at least write access.` ) ;
86
+ core . debug ( `All users with at least write access: ${ ( 0 , util_1 . inspect ) ( allUsersWithAtLeastWriteAccess ) } ` ) ;
87
+ const failedUsers = [ ] ;
88
+ userCodeowners . forEach ( async ( user ) => {
89
+ if ( allUsersWithAtLeastWriteAccess . includes ( user ) ) {
90
+ core . notice ( `User '${ user } ' already has at least write access; skipping.` ) ;
91
+ }
92
+ else {
93
+ await ( 0 , collaborators_1 . addUserToWriteAccess ) ( octokit , github . context . repo . owner , github . context . repo . repo , user , isDryRun ) . catch ( ( error ) => {
94
+ failedUsers . push ( user ) ;
95
+ core . warning ( `Failed to give write access to user '${ user } ': ${ error } ` ) ;
96
+ } ) ;
97
+ }
98
+ } ) ;
99
+ if ( failedTeams . length > 0 || failedUsers . length > 0 ) {
100
+ const errorMsg = `Failed to give write access to teams: ${ failedTeams . join ( ", " ) } . ` +
101
+ `Failed to give write access to users: ${ failedUsers . join ( ", " ) } .` ;
102
+ throw new Error ( errorMsg ) ;
103
+ }
106
104
}
107
105
catch ( error ) {
108
106
core . debug ( ( 0 , util_1 . inspect ) ( error , false , 2 , true ) ) ;
0 commit comments