Skip to content

Commit 440f762

Browse files
committed
🔨 refactor js handler to delete old comments, fix io redirection
1 parent 072176e commit 440f762

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

check_no_api_breakages.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function build_and_do() {
3636

3737
(
3838
cd "$repodir"
39-
git checkout -q "$tag"
39+
git -c advice.detachedHead=false checkout -q "$tag"
4040
swift build
4141
while read -r module; do
4242
swift api-digester -sdk "$sdk" -dump-sdk -module "$module" \
@@ -71,13 +71,11 @@ fi
7171

7272
hash jq 2> /dev/null || { echo >&2 "ERROR: jq must be installed"; exit 1; }
7373
tmpdir=$(mktemp -d /tmp/.check-api_XXXXXX)
74-
repo_url=$1
74+
repodir=$1
7575
new_tag=$2
7676
shift 2
7777

78-
repodir="$tmpdir/repo"
79-
git clone -q "$repo_url" "$repodir"
80-
git -C "$repodir" fetch -q origin '+refs/pull/*:refs/remotes/origin/pr/*'
78+
git -C "$repodir" fetch -q --prune
8179
errors=0
8280

8381
for old_tag in "$@"; do
@@ -103,9 +101,12 @@ for old_tag in "$@"; do
103101
> "$report" 2>&1
104102

105103
if ! shasum "$report" | grep -q cefc4ee5bb7bcdb7cb5a7747efa178dab3c794d5; then
104+
echo ERROR
106105
echo >&2 "🔀 Public API change in $f"
107106
cat >&2 "$report"
108107
errors=$(( errors + 1 ))
108+
else
109+
echo OK
109110
fi
110111
done
111112
rm -rf "$tmpdir/api-new" "$tmpdir/api-old"

entrypoint.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/bin/bash
22

33
# Run the breakage checker script and hand the API Diff report to the user
4-
REPORT_PATH=/report.txt
5-
OLD_REF=$GITHUB_BASE_REF
6-
echo "Comparing $GITHUB_SHA with $OLD_REF and writing to $REPORT_PATH"
7-
/check_no_api_breakages.sh $GITHUB_WORKSPACE $GITHUB_SHA $OLD_REF 2>$REPORT_PATH
4+
REPORT_PATH=/breakage-report.txt
5+
echo "Comparing $GITHUB_HEAD_REF with $GITHUB_BASE_REF and writing to $REPORT_PATH"
6+
/check_no_api_breakages.sh $GITHUB_WORKSPACE $GITHUB_HEAD_REF $GITHUB_BASE_REF 2>$REPORT_PATH
87
node /src/entrypoint.js $REPORT_PATH

src/entrypoint.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const { readFile } = require("fs").promises
22
const { Toolkit } = require("actions-toolkit")
3+
const getComments = require("./get-comments")
4+
35
const tools = new Toolkit()
46
const argv = process.argv.slice(2)
57
// const arguments = tools.arguments
68

9+
const me = "github-actions[bot]"
10+
711
const [ reportFilePath ] = argv
812
const { event, payload, sha } = tools.context
913

@@ -23,20 +27,32 @@ async function run() {
2327
report = error
2428
}
2529

26-
const octokit = tools.github
30+
const {issues} = tools.github
2731

2832
// add comment on PR
2933
const { owner, repo } = tools.context.repo
34+
const params = { owner, repo }
3035

3136
const body = `## API Breakage Report\n${report}`
3237

38+
const commentParams = {
39+
...params,
40+
issue_number: pr
41+
? pr.number
42+
: tools.context.issue.number
43+
}
44+
3345
try {
34-
await octokit.issues.createComment({
35-
owner,
36-
repo,
37-
issue_number: pr
38-
? pr.number
39-
: tools.context.issue.number,
46+
const myComments = await getComments(issues, commentParams, me)
47+
48+
// Delete old comments by this action
49+
await Promise.all(myComments.map(async ({ id }) => issues.deleteComment({
50+
...commentParams,
51+
comment_id: id
52+
})))
53+
54+
await issues.createComment({
55+
...commentParams,
4056
body
4157
})
4258

src/get-comments.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const PER_PAGE = 100
2+
3+
module.exports = async function getComments(issues, params, ofUser) {
4+
const myComments = []
5+
6+
let page = 0
7+
while (true) {
8+
const nextComments = (await issues.listComments({
9+
...params,
10+
per_page: PER_PAGE,
11+
page
12+
})).data
13+
14+
if (ofUser === undefined) {
15+
myComments.push(...nextComments)
16+
} else {
17+
myComments.push(...nextComments.filter(c => c.user.login === ofUser))
18+
}
19+
20+
page++
21+
if (nextComments.length < PER_PAGE) break
22+
}
23+
24+
return myComments
25+
}

0 commit comments

Comments
 (0)