-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (76 loc) · 2.74 KB
/
Copy pathpr-version-comment.yml
File metadata and controls
89 lines (76 loc) · 2.74 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
name: PR Version Comment
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
comment-version-bump:
runs-on: ubuntu-latest
steps:
- name: Comment on version bump
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
if (!pr) {
core.info("No pull_request in context; skipping.");
return;
}
const owner = context.repo.owner;
const repo = context.repo.repo;
const files = await github.paginate(
github.rest.pulls.listFiles,
{ owner, repo, pull_number: pr.number }
);
const cargoChanged = files.some((f) => f.filename === "Cargo.toml");
if (!cargoChanged) {
core.info("Cargo.toml not changed; skipping.");
return;
}
async function getVersion(ref) {
const res = await github.rest.repos.getContent({
owner,
repo,
path: "Cargo.toml",
ref,
});
const content = Buffer.from(res.data.content, "base64").toString("utf8");
const match = content.match(/^version\s*=\s*"([0-9]+\.[0-9]+\.[0-9]+)"/m);
return match ? match[1] : null;
}
const baseVersion = await getVersion(pr.base.sha);
const headVersion = await getVersion(pr.head.sha);
if (!baseVersion || !headVersion) {
core.info("Unable to determine versions; skipping.");
return;
}
if (baseVersion === headVersion) {
core.info("Version unchanged; skipping.");
return;
}
const marker = "<!-- version-bump -->";
const body = `${marker}\nVersion bump detected: \`${baseVersion}\` → \`${headVersion}\``;
const comments = await github.paginate(
github.rest.issues.listComments,
{ owner, repo, issue_number: pr.number }
);
const existing = comments.find((c) => c.body && c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
core.info("Updated existing version bump comment.");
return;
}
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body,
});
core.info("Posted version bump comment.");