Skip to content

Commit

Permalink
Merge pull request #77 from gentlementlegen/fix/close-prs
Browse files Browse the repository at this point in the history
fix: ensure assignee is checked in userUnassigned handler
  • Loading branch information
gentlementlegen authored Nov 9, 2024
2 parents 844cac7 + f36e484 commit 0480ab3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ export async function userSelfAssign(context: Context<"issues.assigned">): Promi
const { issue } = payload;
const deadline = getDeadline(issue.labels);

if (!deadline) {
context.logger.debug("Skipping deadline posting message because no deadline has been set.");
// We avoid posting a message if the bot is the actor to avoid double posting
if (!deadline || payload.sender.type === "Bot") {
context.logger.debug("Skipping deadline posting message.", {
senderType: payload.sender.type,
deadline: deadline,
});
return { status: HttpStatusCode.NOT_MODIFIED };
}

Expand Down Expand Up @@ -91,13 +95,18 @@ export async function userPullRequest(context: Context<"pull_request.opened" | "
return { status: HttpStatusCode.NOT_MODIFIED };
}

export async function userUnassigned(context: Context): Promise<Result> {
export async function userUnassigned(context: Context<"issues.unassigned">): Promise<Result> {
if (!("issue" in context.payload)) {
context.logger.debug("Payload does not contain an issue, skipping issues.unassigned event.");
return { status: HttpStatusCode.NOT_MODIFIED };
}
const { payload } = context;
const { issue, sender, repository } = payload;
await closePullRequestForAnIssue(context, issue.number, repository, sender.login);
const { issue, repository, assignee } = payload;
// 'assignee' is the user that actually got un-assigned during this event. Since it can theoretically be null,
// we display an error if none is found in the payload.
if (!assignee) {
throw context.logger.fatal("No assignee found in payload, failed to close pull-requests.");
}
await closePullRequestForAnIssue(context, issue.number, repository, assignee?.login);
return { status: HttpStatusCode.OK, content: "Linked pull-requests closed." };
}
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function startStopTask(inputs: PluginInputs, env: Env) {
case "pull_request.edited":
return await userPullRequest(context as Context<"pull_request.edited">);
case "issues.unassigned":
return await userUnassigned(context);
return await userUnassigned(context as Context<"issues.unassigned">);
default:
context.logger.error(`Unsupported event: ${context.eventName}`);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ export function createContext(
action: "created",
installation: { id: 1 } as unknown as Context["payload"]["installation"],
organization: { login: "ubiquity" } as unknown as Context["payload"]["organization"],
assignee: {
...sender,
},
} as Context["payload"],
logger: new Logs("debug"),
config: {
Expand Down
4 changes: 2 additions & 2 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ubiquity-os-command-start-stop"
main = "src/worker.ts"
compatibility_date = "2024-05-23"
node_compat = true
compatibility_date = "2024-09-23"
compatibility_flags = [ "nodejs_compat" ]
[env.dev]
[env.prod]

Expand Down

0 comments on commit 0480ab3

Please sign in to comment.