forked from markwylde/claude-code-gitea-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger.ts
More file actions
187 lines (163 loc) · 5.67 KB
/
Copy pathtrigger.ts
File metadata and controls
187 lines (163 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bun
import * as core from "@actions/core";
import {
isIssuesEvent,
isIssueCommentEvent,
isPullRequestEvent,
isPullRequestReviewEvent,
isPullRequestReviewCommentEvent,
} from "../context";
import type { IssuesLabeledEvent } from "@octokit/webhooks-types";
import type { ParsedGitHubContext } from "../context";
export function checkContainsTrigger(context: ParsedGitHubContext): boolean {
const {
inputs: { assigneeTrigger, triggerPhrase, directPrompt },
} = context;
console.log(
`Checking trigger: event=${context.eventName}, action=${context.eventAction}, phrase='${triggerPhrase}', assignee='${assigneeTrigger}', direct='${directPrompt}'`,
);
// If direct prompt is provided, always trigger
if (directPrompt) {
console.log(`Direct prompt provided, triggering action`);
return true;
}
// Check for assignee trigger
if (isIssuesEvent(context) && context.eventAction === "assigned") {
// Remove @ symbol from assignee_trigger if present
let triggerUser = assigneeTrigger?.replace(/^@/, "") || "";
const assigneeUsername = context.payload.issue.assignee?.login || "";
console.log(
`Checking assignee trigger: user='${triggerUser}', assignee='${assigneeUsername}'`,
);
if (triggerUser && assigneeUsername === triggerUser) {
console.log(`Issue assigned to trigger user '${triggerUser}'`);
return true;
}
}
// Check for issue label trigger
if (isIssuesEvent(context) && context.eventAction === "labeled") {
const triggerLabel = context.inputs.labelTrigger?.trim();
const appliedLabel = (
context.payload as IssuesLabeledEvent
).label?.name?.trim();
console.log(
`Checking label trigger: expected='${triggerLabel}', applied='${appliedLabel}'`,
);
if (
triggerLabel &&
appliedLabel &&
triggerLabel.localeCompare(appliedLabel, undefined, {
sensitivity: "accent",
}) === 0
) {
console.log(`Issue labeled with trigger label '${triggerLabel}'`);
return true;
}
}
// Check for issue body and title trigger on issue creation
if (isIssuesEvent(context) && context.eventAction === "opened") {
const issueBody = context.payload.issue.body || "";
const issueTitle = context.payload.issue.title || "";
// Check for exact match with word boundaries or punctuation
const regex = new RegExp(
`(^|\\s)${escapeRegExp(triggerPhrase)}([\\s.,!?;:]|$)`,
);
// Check in body
if (regex.test(issueBody)) {
console.log(
`Issue body contains exact trigger phrase '${triggerPhrase}'`,
);
return true;
}
// Check in title
if (regex.test(issueTitle)) {
console.log(
`Issue title contains exact trigger phrase '${triggerPhrase}'`,
);
return true;
}
}
// Check for pull request body and title trigger
if (isPullRequestEvent(context)) {
const prBody = context.payload.pull_request.body || "";
const prTitle = context.payload.pull_request.title || "";
// Check for exact match with word boundaries or punctuation
const regex = new RegExp(
`(^|\\s)${escapeRegExp(triggerPhrase)}([\\s.,!?;:]|$)`,
);
// Check in body
if (regex.test(prBody)) {
console.log(
`Pull request body contains exact trigger phrase '${triggerPhrase}'`,
);
return true;
}
// Check in title
if (regex.test(prTitle)) {
console.log(
`Pull request title contains exact trigger phrase '${triggerPhrase}'`,
);
return true;
}
// Check if trigger user is in requested reviewers (treat same as mention in text)
const triggerUser = triggerPhrase.replace(/^@/, "");
const requestedReviewers =
context.payload.pull_request.requested_reviewers || [];
const isReviewerRequested = requestedReviewers.some(
(reviewer) => "login" in reviewer && reviewer.login === triggerUser,
);
if (isReviewerRequested) {
console.log(
`Pull request has '${triggerUser}' as requested reviewer (treating as trigger)`,
);
return true;
}
}
// Check for pull request review body trigger
if (
isPullRequestReviewEvent(context) &&
(context.eventAction === "submitted" ||
context.eventAction === "edited" ||
context.eventAction === "reviewed")
) {
const reviewBody =
context.payload.review?.body ?? context.payload.review?.content ?? "";
// Check for exact match with word boundaries or punctuation
const regex = new RegExp(
`(^|\\s)${escapeRegExp(triggerPhrase)}([\\s.,!?;:]|$)`,
);
if (regex.test(reviewBody)) {
console.log(
`Pull request review contains exact trigger phrase '${triggerPhrase}'`,
);
return true;
}
}
// Check for comment trigger
if (
isIssueCommentEvent(context) ||
isPullRequestReviewCommentEvent(context)
) {
const commentBody = isIssueCommentEvent(context)
? context.payload.comment.body
: (context.payload.comment?.body ?? context.payload.review?.content);
// Check for exact match with word boundaries or punctuation
const regex = new RegExp(
`(^|\\s)${escapeRegExp(triggerPhrase)}([\\s.,!?;:]|$)`,
);
if (regex.test(commentBody)) {
console.log(`Comment contains exact trigger phrase '${triggerPhrase}'`);
return true;
}
}
console.log(`No trigger was met for ${triggerPhrase}`);
return false;
}
export function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export async function checkTriggerAction(context: ParsedGitHubContext) {
const containsTrigger = checkContainsTrigger(context);
core.setOutput("contains_trigger", containsTrigger.toString());
return containsTrigger;
}