Skip to content

Commit

Permalink
Merge pull request #2 from AnuragThePathak/rm-output
Browse files Browse the repository at this point in the history
add check for membership and collaborator
  • Loading branch information
AnuragThePathak committed Dec 24, 2022
2 parents 0eea91a + c689584 commit 5fdc923
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
28 changes: 27 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ function run() {
labelsToRemove.push(label);
}
}
if (labels.length > 0) {
const hasWriteAccess = yield checkWriteAccess(client, github.context.repo.owner, github.context.repo.repo, github.context.actor);
if (labels.length > 0 && hasWriteAccess) {
yield addLabels(client, prNumber, labels);
}
if (syncLabels && labelsToRemove.length) {
Expand Down Expand Up @@ -220,6 +221,31 @@ function checkMatch(changedFiles, matchConfig) {
}
return true;
}
function checkWriteAccess(client, owner, repo, username) {
return __awaiter(this, void 0, void 0, function* () {
const level = (yield client.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username
})).data.permission;
if (level === "admin" || level === "write") {
return true;
}
try {
const res = (yield client.rest.orgs.checkMembershipForUser({
org: owner,
username
}));
if (res.status == 204) {
return true;
}
}
catch (_a) {
return false;
}
return false;
});
}
function addLabels(client, prNumber, labels) {
return __awaiter(this, void 0, void 0, function* () {
yield client.rest.issues.addLabels({
Expand Down
22 changes: 19 additions & 3 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function run() {
}
}

const hasWriteAccess = await checkWritePermission(
const hasWriteAccess = await checkWriteAccess(
client,
github.context.repo.owner,
github.context.repo.repo,
Expand Down Expand Up @@ -235,7 +235,7 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
return true
}

async function checkWritePermission(
async function checkWriteAccess(
client: ClientType,
owner: string,
repo: string,
Expand All @@ -246,8 +246,24 @@ async function checkWritePermission(
repo,
username
})).data.permission

if (level === "admin" || level === "write") {
return true;
}

return level === "write" || level === "admin"
try {
const res = (await client.rest.orgs.checkMembershipForUser({
org: owner,
username
}))

if (res.status as number == 204) {
return true
}
} catch {
return false
}
return false
}

async function addLabels(
Expand Down

0 comments on commit 5fdc923

Please sign in to comment.