Skip to content

Commit b80fd19

Browse files
authored
Merge pull request #2 from seriousben/first-version
2 parents 8a4422e + 1a7f4d2 commit b80fd19

File tree

1 file changed

+100
-8
lines changed

1 file changed

+100
-8
lines changed

action.yaml

+100-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
name: "Go Patch Coverage"
2-
description: "Display golang patch test coverage on your pull requests"
2+
description: |
3+
Display golang patch test coverage on your pull requests.
4+
5+
Required permissions:
6+
- `pull-requests: write` for writing comments on pull requests.
7+
- `contents: write` for the git-notes prev_coverage_mode to maintain the coverage within git notes.
38
author: "Benjamin Boudreau"
49
branding:
510
icon: "code"
@@ -9,7 +14,7 @@ inputs:
914
description: |
1015
The version of go-patch-cover to use.
1116
required: true
12-
default: "latest"
17+
default: "0.2.0"
1318
coverage_filename:
1419
description: |
1520
go coverage file for the code after change.
@@ -23,38 +28,125 @@ inputs:
2328
Unified diff file of the patch to compute coverage for.
2429
Example generation:
2530
git diff -U0 --no-color origin/main > patch.diff
26-
required: true
31+
required: false
2732
default: "patch.diff"
2833
previous_coverage_filename:
2934
description: |
3035
go coverage file for the code before changes.
3136
When not provided, previous coverage information will not be displayed.
37+
default: 'prev_coverage.out'
38+
required: false
39+
diff_enabled:
40+
description: |
41+
Controls whether go-patch-cover action should create the diff file or not.
42+
default: 'true'
43+
required: true
44+
prev_coverage_mode:
45+
description: |
46+
The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to create or update the comment. This defaults to {{ github.token }}.
47+
48+
Modes:
49+
- git-notes: Use git notes: `refs/notes/coverage` to persist the coverage file on pushes.
50+
- file: Expect the file pointed to by the previous_coverage_filename input to already exist.
51+
default: 'git-notes'
3252
required: false
3353
github_token:
34-
description: 'The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to create or update the comment. This defaults to {{ github.token }}.'
54+
description: |
55+
The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to create or update the comment. This defaults to {{ github.token }}.
3556
default: '${{ github.token }}'
3657
required: false
3758
runs:
3859
using: "composite"
3960
steps:
40-
- if: github.event_name == 'pull_request'
61+
- if: github.event_name == 'pull_request' && inputs.diff_enabled == 'true'
4162
run: |
4263
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF}
4364
git diff -U0 --no-color origin/${GITHUB_BASE_REF} > patch.diff
4465
shell: bash
66+
- if: github.event_name == 'pull_request' && inputs.prev_coverage_mode == 'git-notes'
67+
run: |
68+
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF} +refs/notes/coverage:refs/notes/coverage || true
69+
git notes --ref coverage show origin/${GITHUB_BASE_REF} > ${previous_coverage_filename} || rm ${previous_coverage_filename}
70+
shell: bash
71+
env:
72+
previous_coverage_filename : ${{ inputs.previous_coverage_filename }}
4573
- if: github.event_name == 'pull_request'
4674
run: |
4775
go install "github.com/seriousben/go-patch-cover/cmd/go-patch-cover@${version}"
48-
if [ -z "${previous_coverage_filename}" ]; then
49-
$(go env GOPATH)/bin/go-patch-cover ${coverage_filename} ${diff_filename}
76+
if [ -f "${previous_coverage_filename}" ]; then
77+
out="$($(go env GOPATH)/bin/go-patch-cover ${coverage_filename} ${diff_filename} ${previous_coverage_filename})"
5078
else
51-
$(go env GOPATH)/bin/go-patch-cover ${coverage_filename} ${diff_filename} ${previous_coverage_filename}
79+
out="$($(go env GOPATH)/bin/go-patch-cover ${coverage_filename} ${diff_filename})"
5280
fi
81+
82+
# Creating here doc to keep newline intact.
83+
echo "GO_PATCH_COVER_OUT<<EOF" >> $GITHUB_ENV
84+
echo "$out" >> $GITHUB_ENV
85+
echo "EOF" >> $GITHUB_ENV
5386
shell: bash
5487
env:
5588
version: ${{ inputs.version }}
5689
coverage_filename : ${{ inputs.coverage_filename }}
5790
diff_filename : ${{ inputs.diff_filename }}
5891
previous_coverage_filename : ${{ inputs.previous_coverage_filename }}
5992
github_token: ${{ inputs.github_token }}
93+
- if: github.event_name == 'pull_request'
94+
run: |
95+
comment_body=$(cat <<EOF
96+
<!-- go-patch-cover/report -->
97+
$GO_PATCH_COVER_OUT
98+
EOF
99+
)
100+
101+
comments="$(gh api graphql -F subjectId=$PULL_REQUEST_NODE_ID -f query='
102+
query($subjectId: ID!) {
103+
node(id: $subjectId) {
104+
... on PullRequest {
105+
comments(first: 100) {
106+
nodes {
107+
id
108+
isMinimized
109+
body
110+
}
111+
}
112+
}
113+
}
114+
}
115+
' --jq '.data.node.comments.nodes | map(select((.body | contains("<!-- go-patch-cover/report -->")) and .isMinimized == false)) | map(.id)[]')"
60116
117+
if [[ -n "$comments" ]]; then
118+
for val in $comments; do
119+
gh api graphql -X POST -F id=$val -F body="$comment_body" -f query='
120+
mutation UpdateComment($id: ID!, $body: String!) {
121+
updateIssueComment(input: {id: $id, body: $body}) {
122+
clientMutationId
123+
}
124+
}
125+
'
126+
done
127+
else
128+
gh api graphql -X POST -F subjectId=$PULL_REQUEST_NODE_ID -F body="$comment_body" -f query='
129+
mutation AddComment($subjectId: String!, $body: String!) {
130+
addComment(input: {subjectId: $subjectId, body: $body}) {
131+
clientMutationId
132+
}
133+
}
134+
'
135+
fi
136+
shell: bash
137+
env:
138+
GITHUB_TOKEN: ${{ inputs.github_token }}
139+
OWNER: ${{ github.repository_owner }}
140+
REPO: ${{ github.event.repository.name }}
141+
PULL_REQUEST_NODE_ID: ${{ github.event.pull_request.node_id }}
142+
- name: Save Coverage
143+
if: github.event_name == 'push' && inputs.prev_coverage_mode == 'git-notes'
144+
run: |
145+
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF} +refs/notes/coverage:refs/notes/coverage || true
146+
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
147+
git config --local user.name "github-actions[bot]"
148+
git notes --ref coverage add -f -F ${coverage_filename} origin/${GITHUB_REF_NAME}
149+
git push origin refs/notes/coverage
150+
shell: bash
151+
env:
152+
coverage_filename : ${{ inputs.coverage_filename }}

0 commit comments

Comments
 (0)