Skip to content

Commit

Permalink
Add support for collaborator roles
Browse files Browse the repository at this point in the history
Signed-off-by: Niccolò Fei <niccolo.fei@enterprisedb.com>
  • Loading branch information
NiccoloFei committed Jun 21, 2023
1 parent dfd52a8 commit dd6d811
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Name | Allowed values | Description
`reaction` | `true` (default), `false` | Indicates if a reaction is left on the comment indicating it was seen.
`reaction-type` | `+1` (default), `-1`, `laugh`, `confused`, `heart`, `hooray`, `rocket`, `eyes` | The [reaction type](https://developer.github.com/v3/reactions/#reaction-types) to leave on the comment.
`allow-edits` | `true`, `false` (default) | Indicates if the action should run on comment edits, or the initial comment only.
`permission-level` | `admin`, `write` (default), `read`, `none` | The user's [permission level](https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level) needed to act on the command.
`permission-level` | `admin`, `maintain`, `write` (default), `triage`, `read`, `none` | The user's [permission level](https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level) needed to act on the command.

## License

Expand Down
23 changes: 21 additions & 2 deletions __tests__/commandHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ describe("commandHandler", () => {
});

it("should return false when correct slash command but incorrect repo access", async () => {
const mockedSetOutput = core.setOutput as jest.Mock<typeof core.setOutput>;

context.payload = require(join(__dirname, "payloads", "created.json"));

const commandHandler = new CommandHandler(
Expand All @@ -94,6 +92,27 @@ describe("commandHandler", () => {
expect(scoped.isDone()).toBe(true);
});

it("should return true when repo access being used is greather than the required one", async () => {
context.payload = require(join(__dirname, "payloads", "created.json"));

const commandHandler = new CommandHandler(
/* repoToken */ "-token-",
/* commandName */ "test",
/* addReaction */ false,
/* reactionType */ "+1",
/* allowEdits */ false,
/* requiredPermissionLevel */ "write",
);

const scoped = nock("https://api.github.com")
.get("/repos/xt0rted/slash-command-action/collaborators/test-user/permission")
.reply(200, { permission: "maintain" });

await expect(commandHandler.process()).resolves.toBe(true);

expect(scoped.isDone()).toBe(true);
});

it("should return true and not create reaction when correct slash command and repo access", async () => {
context.payload = require(join(__dirname, "payloads", "created.json"));

Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type PermissionLevel =
| "admin"
| "maintain"
| "write"
| "triage"
| "read"
| "none"
;
Expand Down
6 changes: 4 additions & 2 deletions src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import type { PermissionLevel } from "./interfaces";
export enum PermissionLevels {
none = 0,
read = 1,
write = 2,
admin = 3,
triage = 2,
write = 3,
maintain = 4,
admin = 5,
}

export function checkPermission(required: PermissionLevel, actual: PermissionLevel): boolean {
Expand Down

0 comments on commit dd6d811

Please sign in to comment.