-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
100 lines (98 loc) · 3.41 KB
/
action.yml
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
name: github-comment-pr
author: [email protected]
branding:
color: purple
icon: edit-3
inputs:
title:
description: "Title of the comment (guaranteed uniqueness of the comment on the PR)"
required: true
content:
description: "Content of the comment"
required: true
state:
description: "State of the comment (present or absent)"
required: false
default: present
token:
description: "GitHub token"
required: true
description: Comment on PR with custom text message.
runs:
using: composite
steps:
- name: Write content to a temp file
id: content
shell: bash
run: |
filename=$(mktemp /tmp/comment.XXXXXX)
cat << 'EOF' > $filename
${{ inputs.content }}
EOF
echo "filename=$filename" >> $GITHUB_OUTPUT
- if: inputs.state == 'present'
name: Comment cost on the PR
uses: actions/github-script@v7
with:
github-token: ${{ inputs.token }}
script: |
const fs = require("fs");
const fileContent = fs.readFileSync("${{ steps.content.outputs.filename }}", "utf8");
const startingText = "${{ inputs.title }}";
const updateTime = new Date().toISOString().slice(0, 19);
const pull = await github.rest.pulls.get({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const lastCommitSha = pull.data.head.sha;
const lastCommitInfo = `Last commit: ${lastCommitSha}`;
const lastUpdated = `Last updated: ${updateTime}\n${lastCommitInfo}`;
const contents = `${startingText}\n${lastUpdated}\n${fileContent}`;
const { data: comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const existingComment = comments.find(
(comment) =>
comment.user.login === "github-actions[bot]" &&
comment.body.startsWith(startingText)
);
if (existingComment !== undefined) {
github.rest.issues.deleteComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
});
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: contents,
});
- if: inputs.state == 'absent'
name: Delete the comment
uses: actions/github-script@v7
with:
github-token: ${{ inputs.token }}
script: |
const startingText = "${{ inputs.title }}";
const { data: comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const existingComment = comments.find(
(comment) =>
comment.user.login === "github-actions[bot]" &&
comment.body.startsWith(startingText)
);
if (existingComment !== undefined) {
github.rest.issues.deleteComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
});
}