Skip to content

Commit 5fdc923

Browse files
Merge pull request #2 from AnuragThePathak/rm-output
add check for membership and collaborator
2 parents 0eea91a + c689584 commit 5fdc923

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

dist/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ function run() {
7575
labelsToRemove.push(label);
7676
}
7777
}
78-
if (labels.length > 0) {
78+
const hasWriteAccess = yield checkWriteAccess(client, github.context.repo.owner, github.context.repo.repo, github.context.actor);
79+
if (labels.length > 0 && hasWriteAccess) {
7980
yield addLabels(client, prNumber, labels);
8081
}
8182
if (syncLabels && labelsToRemove.length) {
@@ -220,6 +221,31 @@ function checkMatch(changedFiles, matchConfig) {
220221
}
221222
return true;
222223
}
224+
function checkWriteAccess(client, owner, repo, username) {
225+
return __awaiter(this, void 0, void 0, function* () {
226+
const level = (yield client.rest.repos.getCollaboratorPermissionLevel({
227+
owner,
228+
repo,
229+
username
230+
})).data.permission;
231+
if (level === "admin" || level === "write") {
232+
return true;
233+
}
234+
try {
235+
const res = (yield client.rest.orgs.checkMembershipForUser({
236+
org: owner,
237+
username
238+
}));
239+
if (res.status == 204) {
240+
return true;
241+
}
242+
}
243+
catch (_a) {
244+
return false;
245+
}
246+
return false;
247+
});
248+
}
223249
function addLabels(client, prNumber, labels) {
224250
return __awaiter(this, void 0, void 0, function* () {
225251
yield client.rest.issues.addLabels({

src/labeler.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function run() {
4949
}
5050
}
5151

52-
const hasWriteAccess = await checkWritePermission(
52+
const hasWriteAccess = await checkWriteAccess(
5353
client,
5454
github.context.repo.owner,
5555
github.context.repo.repo,
@@ -235,7 +235,7 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
235235
return true
236236
}
237237

238-
async function checkWritePermission(
238+
async function checkWriteAccess(
239239
client: ClientType,
240240
owner: string,
241241
repo: string,
@@ -246,8 +246,24 @@ async function checkWritePermission(
246246
repo,
247247
username
248248
})).data.permission
249+
250+
if (level === "admin" || level === "write") {
251+
return true;
252+
}
249253

250-
return level === "write" || level === "admin"
254+
try {
255+
const res = (await client.rest.orgs.checkMembershipForUser({
256+
org: owner,
257+
username
258+
}))
259+
260+
if (res.status as number == 204) {
261+
return true
262+
}
263+
} catch {
264+
return false
265+
}
266+
return false
251267
}
252268

253269
async function addLabels(

0 commit comments

Comments
 (0)