-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·167 lines (142 loc) · 4.87 KB
/
Copy pathindex.js
File metadata and controls
executable file
·167 lines (142 loc) · 4.87 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
#!/usr/bin/env node
const fs = require("fs");
const core = require("@actions/core");
const { context, getOctokit} = require("@actions/github");
const axios = require("axios")
async function validateSubscription() {
let repoPrivate;
const eventPath = process.env.GITHUB_EVENT_PATH;
if (eventPath && fs.existsSync(eventPath)) {
const payload = JSON.parse(fs.readFileSync(eventPath, "utf8"));
repoPrivate = payload?.repository?.private;
}
const upstream = "Khan/pull-request-comment-trigger";
const action = process.env.GITHUB_ACTION_REPOSITORY;
const docsUrl =
"https://docs.stepsecurity.io/actions/stepsecurity-maintained-actions";
core.info("");
core.info("\u001b[1;36mStepSecurity Maintained Action\u001b[0m");
core.info(`Secure drop-in replacement for ${upstream}`);
if (repoPrivate === false)
core.info("\u001b[32m\u2713 Free for public repositories\u001b[0m");
core.info(`\u001b[36mLearn more:\u001b[0m ${docsUrl}`);
core.info("");
if (repoPrivate === false) return;
const serverUrl = process.env.GITHUB_SERVER_URL || "https://github.com";
const body = { action: action || "" };
if (serverUrl !== "https://github.com") body.ghes_server = serverUrl;
try {
await axios.post(
`https://agent.api.stepsecurity.io/v1/github/${process.env.GITHUB_REPOSITORY}/actions/maintained-actions-subscription`,
body,
{ timeout: 3000 },
);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 403) {
core.error(
`\u001b[1;31mThis action requires a StepSecurity subscription for private repositories.\u001b[0m`,
);
core.error(
`\u001b[31mLearn how to enable a subscription: ${docsUrl}\u001b[0m`,
);
process.exit(1);
}
core.info("Timeout or API not reachable. Continuing to next step.");
}
}
/**
* Main execution function for the pull request comment trigger action
*/
async function execute() {
try {
await validateSubscription();
const triggerWord = core.getInput("trigger", { required: true });
const reactionType = core.getInput("reaction");
const strictPrefixMode = core.getInput("prefix_only") === 'true';
const githubToken = process.env.GITHUB_TOKEN;
if (reactionType && !githubToken) {
core.setFailed('GitHub token is required when reaction parameter is provided');
return;
}
const commentText = extractCommentBody();
core.setOutput('comment_body', commentText);
if (shouldSkipProcessing()) {
setTriggeredOutput(false);
return;
}
const { owner, repo } = context.repo;
const isTriggered = checkTriggerMatch(commentText, triggerWord, strictPrefixMode);
setTriggeredOutput(isTriggered);
if (isTriggered && reactionType) {
await addReactionToComment(githubToken, owner, repo, reactionType);
}
} catch (error) {
handleError(error);
}
}
/**
* Extracts comment body from the GitHub event context
*/
function extractCommentBody() {
const eventType = context.eventName;
let bodyContent = '';
if (eventType === "issue_comment") {
bodyContent = context.payload.comment?.body || '';
} else {
bodyContent = context.payload.pull_request?.body || '';
}
return bodyContent;
}
/**
* Determines if processing should be skipped based on event context
*/
function shouldSkipProcessing() {
const isIssueComment = context.eventName === "issue_comment";
const isPullRequestComment = context.payload.issue?.pull_request;
return isIssueComment && !isPullRequestComment;
}
/**
* Checks if the trigger word matches in the comment body
*/
function checkTriggerMatch(text, trigger, prefixOnly) {
if (prefixOnly) {
return text.startsWith(trigger);
}
return text.includes(trigger);
}
/**
* Sets the triggered output value
*/
function setTriggeredOutput(isTriggered) {
core.setOutput("triggered", isTriggered ? "true" : "false");
}
/**
* Adds a reaction to the comment or pull request
*/
async function addReactionToComment(token, owner, repo, reaction) {
const octokit = getOctokit(token);
const eventType = context.eventName;
if (eventType === "issue_comment") {
await octokit.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: context.payload.comment.id,
content: reaction
});
} else {
await octokit.rest.reactions.createForIssue({
owner,
repo,
issue_number: context.payload.pull_request.number,
content: reaction
});
}
}
/**
* Handles and logs errors appropriately
*/
function handleError(error) {
console.error('Action execution failed:', error);
core.setFailed("An unexpected error occurred during execution");
}
execute();