Skip to content

Commit

Permalink
fix: ensure assignee is checked in userUnassigned handler
Browse files Browse the repository at this point in the history
Check `assignee` before closing pull requests to avoid errors.
  • Loading branch information
gentlementlegen committed Nov 4, 2024
1 parent f496b20 commit 5659fc3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,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 @@ -35,7 +35,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 @@ -622,6 +622,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

0 comments on commit 5659fc3

Please sign in to comment.