Skip to content

Commit 4dbc9e5

Browse files
Only do team/user operations if exist
1 parent 458096f commit 4dbc9e5

File tree

2 files changed

+105
-82
lines changed

2 files changed

+105
-82
lines changed

compiled/index.js

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,40 +62,50 @@ async function main() {
6262
core.debug(`User codeowners: ${(0, util_1.inspect)(userCodeowners)}`);
6363
const githubToken = core.getInput("github-token", { required: true });
6464
const octokit = github.getOctokit(githubToken);
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)}`);
6965
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)}`);
8766
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-
});
67+
// TEAM-Y OPERATIONS
68+
if (teamCodeowners.length > 0) {
69+
const allTeamsWithAtLeastWriteAccess = await (0, teams_1.getAllTeamsWithAtLeastWriteAccess)(octokit, github.context.repo.owner, github.context.repo.repo);
70+
core.info(`Found ${allTeamsWithAtLeastWriteAccess.length} teams with at least write access.`);
71+
core.debug(`All teams with at least write access: ${(0, util_1.inspect)(allTeamsWithAtLeastWriteAccess)}`);
72+
teamCodeowners.forEach(async (team) => {
73+
const [orgName, teamSlug] = team.replaceAll("@", "").split("/");
74+
core.debug(`Found org: '${orgName}' and team: '${teamSlug}' from '${team}'.`);
75+
if (allTeamsWithAtLeastWriteAccess.includes(teamSlug)) {
76+
core.notice(`Team '${teamSlug}' already has at least write access; skipping.`);
77+
}
78+
else {
79+
await (0, teams_1.addTeamToWriteAccess)(octokit, orgName, github.context.repo.repo, teamSlug, isDryRun).catch((error) => {
80+
failedTeams.push(teamSlug);
81+
core.warning(`Failed to give write access to team '${teamSlug}': ${error}`);
82+
});
83+
}
84+
});
85+
}
86+
else {
87+
core.notice("No team codeowners found; skipping team operations.");
88+
}
89+
// USER-Y OPERATIONS HERE
90+
if (userCodeowners.length > 0) {
91+
const allUsersWithAtLeastWriteAccess = await (0, collaborators_1.getAllDirectCollaboratorsWithAtLeastWriteAccess)(octokit, github.context.repo.owner, github.context.repo.repo);
92+
core.info(`Found ${allUsersWithAtLeastWriteAccess.length} users with at least write access.`);
93+
core.debug(`All users with at least write access: ${(0, util_1.inspect)(allUsersWithAtLeastWriteAccess)}`);
94+
userCodeowners.forEach(async (user) => {
95+
if (allUsersWithAtLeastWriteAccess.includes(user)) {
96+
core.notice(`User '${user}' already has at least write access; skipping.`);
97+
}
98+
else {
99+
await (0, collaborators_1.addUserToWriteAccess)(octokit, github.context.repo.owner, github.context.repo.repo, user, isDryRun).catch((error) => {
100+
failedUsers.push(user);
101+
core.warning(`Failed to give write access to user '${user}': ${error}`);
102+
});
103+
}
104+
});
105+
}
106+
else {
107+
core.notice("No user codeowners found; skipping user operations.");
108+
}
99109
if (failedTeams.length > 0 || failedUsers.length > 0) {
100110
const errorMsg = `Failed to give write access to teams: ${failedTeams.join(", ")}. ` +
101111
`Failed to give write access to users: ${failedUsers.join(", ")}.`;

src/index.ts

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -49,60 +49,73 @@ export async function main() {
4949
const githubToken = core.getInput("github-token", { required: true });
5050
const octokit = github.getOctokit(githubToken);
5151

52+
const failedTeams: string[] = [];
53+
const failedUsers: string[] = [];
54+
5255
// TEAM-Y OPERATIONS
53-
const allTeamsWithAtLeastWriteAccess = await getAllTeamsWithAtLeastWriteAccess(
54-
octokit,
55-
github.context.repo.owner,
56-
github.context.repo.repo
57-
);
58-
core.info(`Found ${allTeamsWithAtLeastWriteAccess.length} teams with at least write access.`);
59-
core.debug(`All teams with at least write access: ${inspect(allTeamsWithAtLeastWriteAccess)}`);
56+
if (teamCodeowners.length > 0) {
57+
const allTeamsWithAtLeastWriteAccess = await getAllTeamsWithAtLeastWriteAccess(
58+
octokit,
59+
github.context.repo.owner,
60+
github.context.repo.repo
61+
);
62+
core.info(`Found ${allTeamsWithAtLeastWriteAccess.length} teams with at least write access.`);
63+
core.debug(
64+
`All teams with at least write access: ${inspect(allTeamsWithAtLeastWriteAccess)}`
65+
);
6066

61-
const failedTeams: string[] = [];
62-
teamCodeowners.forEach(async (team) => {
63-
const [orgName, teamSlug] = team.replaceAll("@", "").split("/");
64-
core.debug(`Found org: '${orgName}' and team: '${teamSlug}' from '${team}'.`);
65-
if (allTeamsWithAtLeastWriteAccess.includes(teamSlug)) {
66-
core.notice(`Team '${teamSlug}' already has at least write access; skipping.`);
67-
} else {
68-
await addTeamToWriteAccess(
69-
octokit,
70-
orgName,
71-
github.context.repo.repo,
72-
teamSlug,
73-
isDryRun
74-
).catch((error) => {
75-
failedTeams.push(teamSlug);
76-
core.warning(`Failed to give write access to team '${teamSlug}': ${error}`);
77-
});
78-
}
79-
});
67+
teamCodeowners.forEach(async (team) => {
68+
const [orgName, teamSlug] = team.replaceAll("@", "").split("/");
69+
core.debug(`Found org: '${orgName}' and team: '${teamSlug}' from '${team}'.`);
70+
if (allTeamsWithAtLeastWriteAccess.includes(teamSlug)) {
71+
core.notice(`Team '${teamSlug}' already has at least write access; skipping.`);
72+
} else {
73+
await addTeamToWriteAccess(
74+
octokit,
75+
orgName,
76+
github.context.repo.repo,
77+
teamSlug,
78+
isDryRun
79+
).catch((error) => {
80+
failedTeams.push(teamSlug);
81+
core.warning(`Failed to give write access to team '${teamSlug}': ${error}`);
82+
});
83+
}
84+
});
85+
} else {
86+
core.notice("No team codeowners found; skipping team operations.");
87+
}
8088

8189
// USER-Y OPERATIONS HERE
82-
const allUsersWithAtLeastWriteAccess = await getAllDirectCollaboratorsWithAtLeastWriteAccess(
83-
octokit,
84-
github.context.repo.owner,
85-
github.context.repo.repo
86-
);
87-
core.info(`Found ${allUsersWithAtLeastWriteAccess.length} users with at least write access.`);
88-
core.debug(`All users with at least write access: ${inspect(allUsersWithAtLeastWriteAccess)}`);
89-
const failedUsers: string[] = [];
90-
userCodeowners.forEach(async (user) => {
91-
if (allUsersWithAtLeastWriteAccess.includes(user)) {
92-
core.notice(`User '${user}' already has at least write access; skipping.`);
93-
} else {
94-
await addUserToWriteAccess(
95-
octokit,
96-
github.context.repo.owner,
97-
github.context.repo.repo,
98-
user,
99-
isDryRun
100-
).catch((error) => {
101-
failedUsers.push(user);
102-
core.warning(`Failed to give write access to user '${user}': ${error}`);
103-
});
104-
}
105-
});
90+
if (userCodeowners.length > 0) {
91+
const allUsersWithAtLeastWriteAccess = await getAllDirectCollaboratorsWithAtLeastWriteAccess(
92+
octokit,
93+
github.context.repo.owner,
94+
github.context.repo.repo
95+
);
96+
core.info(`Found ${allUsersWithAtLeastWriteAccess.length} users with at least write access.`);
97+
core.debug(
98+
`All users with at least write access: ${inspect(allUsersWithAtLeastWriteAccess)}`
99+
);
100+
userCodeowners.forEach(async (user) => {
101+
if (allUsersWithAtLeastWriteAccess.includes(user)) {
102+
core.notice(`User '${user}' already has at least write access; skipping.`);
103+
} else {
104+
await addUserToWriteAccess(
105+
octokit,
106+
github.context.repo.owner,
107+
github.context.repo.repo,
108+
user,
109+
isDryRun
110+
).catch((error) => {
111+
failedUsers.push(user);
112+
core.warning(`Failed to give write access to user '${user}': ${error}`);
113+
});
114+
}
115+
});
116+
} else {
117+
core.notice("No user codeowners found; skipping user operations.");
118+
}
106119

107120
if (failedTeams.length > 0 || failedUsers.length > 0) {
108121
const errorMsg =

0 commit comments

Comments
 (0)