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

fix: cancel trigger.dev job on unassign #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion lib/github/hooks/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getRepositoryByGithubId } from "@/lib/repository/service";
import { getUser } from "@/lib/user/service";
import { issueReminderTask } from "@/src/trigger/issueReminder";
import { Webhooks } from "@octokit/webhooks";
import { runs } from "@trigger.dev/sdk/v3";

import { isMemberOfRepository } from "../services/user";
import {
Expand Down Expand Up @@ -69,6 +70,12 @@ export const onIssueOpened = async (webhooks: Webhooks) => {
});
};

const issueReminderRuns = new Map<string, string>();

const getIssueKey = (owner: string, repo: string, issueNumber: number) => {
return `${owner}/${repo}/${issueNumber}`;
};

export const onAssignCommented = async (webhooks: Webhooks) => {
webhooks.on(EVENT_TRIGGERS.ISSUE_COMMENTED, async (context) => {
try {
Expand Down Expand Up @@ -232,13 +239,15 @@ Thanks for playing 🕹️ OPEN SOURCE LETS GOOOO! `;
//send trigger event to wait for 36hrs then send a reminder if the user has not created a pull request
try {
if (context.payload.installation?.id) {
await issueReminderTask.trigger({
const {id} = await issueReminderTask.trigger({
issueNumber,
repo,
owner,
commenter,
installationId: context.payload.installation.id ?? "",
});
const issueKey = getIssueKey(owner, repo, issueNumber);
issueReminderRuns.set(issueKey, id);
}
} catch (error) {
console.error("Error sending event:", error.message);
Expand Down Expand Up @@ -331,13 +340,27 @@ export const onUnassignCommented = async (webhooks: Webhooks) => {
}

const assignee = context.payload.issue.assignees[0].login;
const cancelReminderTask = async () => {
try {
const issueKey = getIssueKey(owner, repo, issueNumber);
const runId = issueReminderRuns.get(issueKey);

if (runId) {
await runs.cancel(runId);
issueReminderRuns.delete(issueKey);
}
} catch (error) {
console.error("Error cancelling run:", error);
}
};
if (assignee === commenter) {
await octokit.issues.removeAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [assignee],
});
await cancelReminderTask();
await octokit.issues.createComment({
owner,
repo,
Expand Down
Loading