Skip to content

Commit

Permalink
Merge pull request ubiquity-os-marketplace#37 from ubq-testing/fix/ap…
Browse files Browse the repository at this point in the history
…proved-roles

fix: add rolesWithReviewAuth to config
  • Loading branch information
ubiquity-os[bot] authored Sep 23, 2024
2 parents b49ef68 + 44454b7 commit 05bacd5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ To configure your Ubiquibot to run this plugin, add the following to the `.ubiqu
contributor: 3
startRequiresWallet: true # default is true
emptyWalletText: "Please set your wallet address with the /wallet command first and try again."
rolesWithReviewAuthority: ["MEMBER", "OWNER"]
```
# Testing
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"description": "Unassign yourself from the issue."
}
}
}
}
23 changes: 19 additions & 4 deletions src/types/plugin-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ export interface PluginInputs<T extends SupportedEventsU = SupportedEventsU, TU
ref: string;
}

const rolesWithReviewAuthority = T.Array(T.String(), { default: ["COLLABORATOR", "OWNER", "MEMBER", "ADMIN"] });

function maxConcurrentTasks() {
return T.Transform(T.Record(T.String(), T.Integer(), { default: { member: 10, contributor: 2 } }))
.Decode((value) => {
.Decode((obj) => {
// normalize the role keys to lowercase
obj = Object.keys(obj).reduce(
(acc, key) => {
acc[key.toLowerCase()] = obj[key];
return acc;
},
{} as Record<string, number>
);

// If admin is omitted, defaults to infinity
if (!Object.keys(value).includes("admin")) {
value["admin"] = Infinity;
if (!obj["admin"]) {
obj["admin"] = Infinity;
}
return value;

return obj;
})
.Encode((value) => value);
}
Expand All @@ -30,6 +42,9 @@ export const startStopSchema = T.Object(
startRequiresWallet: T.Boolean({ default: true }),
maxConcurrentTasks: maxConcurrentTasks(),
emptyWalletText: T.String({ default: "Please set your wallet address with the /wallet command first and try again." }),
rolesWithReviewAuthority: T.Transform(rolesWithReviewAuthority)
.Decode((value) => value.map((role) => role.toUpperCase()))
.Encode((value) => value.map((role) => role.toUpperCase())),
},
{
default: {},
Expand Down
17 changes: 11 additions & 6 deletions src/utils/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ export async function getAllPullRequests(context: Context, state: "open" | "clos
}

export async function getAllPullRequestReviews(context: Context, pullNumber: number, owner: string, repo: string) {
const {
config: { rolesWithReviewAuthority },
} = context;
try {
return (await context.octokit.paginate(context.octokit.pulls.listReviews, {
owner,
repo,
pull_number: pullNumber,
per_page: 100,
})) as Review[];
return (
await context.octokit.paginate(context.octokit.pulls.listReviews, {
owner,
repo,
pull_number: pullNumber,
per_page: 100,
})
).filter((review) => rolesWithReviewAuthority.includes(review.author_association)) as Review[];
} catch (err: unknown) {
throw new Error(context.logger.error("Fetching all pull request reviews failed!", { error: err as Error }).logMessage.raw);
}
Expand Down
9 changes: 7 additions & 2 deletions tests/__mocks__/valid-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"reviewDelayTolerance": "1 Day",
"taskStaleTimeoutDuration": "30 Days",
"startRequiresWallet": true,
"maxConcurrentTasks": { "admin": 20, "member": 10, "contributor": 2 },
"emptyWalletText": "Please set your wallet address with the /wallet command first and try again."
"maxConcurrentTasks": {
"admin": 20,
"member": 10,
"contributor": 2
},
"emptyWalletText": "Please set your wallet address with the /wallet command first and try again.",
"rolesWithReviewAuthority": ["OWNER", "ADMIN", "MEMBER"]
}
17 changes: 16 additions & 1 deletion tests/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@ import cfg from "./__mocks__/valid-configuration.json";

describe("Configuration tests", () => {
it("Should decode the configuration", () => {
const settings = Value.Default(startStopSchema, { maxConcurrentTasks: { admin: 20, member: 10, contributor: 2 } }) as StartStopSettings;
const settings = Value.Default(startStopSchema, {
reviewDelayTolerance: "1 Day",
taskStaleTimeoutDuration: "30 Days",
startRequiresWallet: true,
emptyWalletText: "Please set your wallet address with the /wallet command first and try again.",
maxConcurrentTasks: { admin: 20, member: 10, contributor: 2 },
rolesWithReviewAuthority: ["OWNER", "ADMIN", "MEMBER"],
}) as StartStopSettings;
expect(settings).toEqual(cfg);
});
it("Should default the admin to infinity if missing from config when decoded", () => {
const settings = Value.Default(startStopSchema, {}) as StartStopSettings;
const decodedSettings = Value.Decode(startStopSchema, settings);
expect(decodedSettings.maxConcurrentTasks["admin"]).toEqual(Infinity);
});

it("Should normalize maxConcurrentTasks role keys to lowercase when decoded", () => {
const settings = Value.Default(startStopSchema, {
maxConcurrentTasks: { ADMIN: 20, memBER: 10, CONTRIBUTOR: 2 },
}) as StartStopSettings;
const decodedSettings = Value.Decode(startStopSchema, settings);
expect(decodedSettings.maxConcurrentTasks).toEqual({ admin: 20, member: 10, contributor: 2 });
});
});
1 change: 1 addition & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ function createContext(
maxConcurrentTasks: maxConcurrentDefaults,
startRequiresWallet,
emptyWalletText: "Please set your wallet address with the /wallet command first and try again.",
rolesWithReviewAuthority: ["ADMIN", "OWNER", "MEMBER"],
},
octokit: new octokit.Octokit(),
eventName: "issue_comment.created" as SupportedEventsU,
Expand Down

0 comments on commit 05bacd5

Please sign in to comment.