Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (81 loc) · 2.87 KB
/
Copy pathindex.js
File metadata and controls
94 lines (81 loc) · 2.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
import { getInput, notice, setFailed } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { readFileSync } from "fs";
export const run = async () => {
const content = getInput("content", {
required: true,
trimWhitespace: false,
});
const contentIsFilePath = getInput("contentIsFilePath");
const contentRegex = getInput("contentRegex") || "";
const contentRegexFlags = getInput("contentRegexFlags") || "";
const regex = getInput("regex") || "---.*";
const regexFlags = getInput("regexFlags") || "";
const appendContentOnMatchOnly = getInput("appendContentOnMatchOnly");
const token = getInput("token", { required: true });
const { owner, repo } = context.repo;
const octokit = getOctokit(token);
let prNumber = context.payload.pull_request?.number;
if (!prNumber) {
// not a pull_request event, try and find the PR number from the commit sha
const { data: pullRequests } =
await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner,
repo,
commit_sha: context.sha,
});
const candidatePullRequests = pullRequests.filter(
(pr) =>
context.payload.ref === `refs/heads/${pr.head.ref}` &&
pr.state === "open",
);
prNumber = candidatePullRequests?.[0]?.number;
}
if (!prNumber) {
setFailed(
`No open pull request found for ${context.eventName}, ${context.sha}`,
);
return;
}
const { data } = await octokit.rest.pulls.get({
owner,
repo,
pull_number: prNumber,
});
let body = data.body;
let output = content;
if (contentIsFilePath && contentIsFilePath === "true") {
output = readFileSync(content).toString("utf-8");
}
if (contentRegex) {
const contentRe = RegExp(contentRegex, contentRegexFlags);
const matchedContent = output.match(contentRe);
if (matchedContent) {
notice("Selecting matched content");
output = matchedContent.join("");
}
}
const re = RegExp(regex, regexFlags);
if (body && body.match(re)) {
notice("Replacing matched PR body with content");
body = body.replace(re, output);
} else if (body && appendContentOnMatchOnly !== "true") {
notice("Append content to PR body");
body += output;
} else if (appendContentOnMatchOnly !== "true") {
notice("Setting PR body to content");
body = output;
} else {
notice(
`No match found and appendContentOnMatchOnly is set, not updating PR body`,
);
return;
}
await octokit.rest.pulls.update({
owner,
repo,
body: body,
pull_number: prNumber,
});
};
run().catch((error) => setFailed(error.message));