Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for collaborator roles #669

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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