1
1
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.
3
8
author : " Benjamin Boudreau"
4
9
branding :
5
10
icon : " code"
9
14
description : |
10
15
The version of go-patch-cover to use.
11
16
required : true
12
- default : " latest "
17
+ default : " 0.2.0 "
13
18
coverage_filename :
14
19
description : |
15
20
go coverage file for the code after change.
@@ -23,38 +28,125 @@ inputs:
23
28
Unified diff file of the patch to compute coverage for.
24
29
Example generation:
25
30
git diff -U0 --no-color origin/main > patch.diff
26
- required : true
31
+ required : false
27
32
default : " patch.diff"
28
33
previous_coverage_filename :
29
34
description : |
30
35
go coverage file for the code before changes.
31
36
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'
32
52
required : false
33
53
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 }}.
35
56
default : ' ${{ github.token }}'
36
57
required : false
37
58
runs :
38
59
using : " composite"
39
60
steps :
40
- - if : github.event_name == 'pull_request'
61
+ - if : github.event_name == 'pull_request' && inputs.diff_enabled == 'true'
41
62
run : |
42
63
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF}
43
64
git diff -U0 --no-color origin/${GITHUB_BASE_REF} > patch.diff
44
65
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 }}
45
73
- if : github.event_name == 'pull_request'
46
74
run : |
47
75
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})"
50
78
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})"
52
80
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
53
86
shell : bash
54
87
env :
55
88
version : ${{ inputs.version }}
56
89
coverage_filename : ${{ inputs.coverage_filename }}
57
90
diff_filename : ${{ inputs.diff_filename }}
58
91
previous_coverage_filename : ${{ inputs.previous_coverage_filename }}
59
92
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)[]')"
60
116
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