remove ia64 keywords
[gentoo-zh.git] / .github / workflows / pkgcheck-checks-on-pr.yml
blobe45407029ec70c56b09fd09be1a4e18c8f2ed8b3
1 name: pkgcheck
2 on:
3   workflow_dispatch:
4   pull_request_target:
5     branches:
6       - master
7     paths-ignore:
8       - '.github/**'
9       - 'metadata/**'
10       - 'README.md'
11       - '.gitignore'
12     types: [opened, reopened, synchronize]
14 permissions:
15   contents: read
16   pull-requests: write
18 jobs:
19   check-changed-ebuilds:
20     name: check changed ebuilds
21     runs-on: ubuntu-latest
22     permissions:
23       contents: read
24       pull-requests: write
25     container:
26       image: ghcr.io/liangyongxiang/gentoo-testing:master
28     steps:
29       - name: Sync main tree
30         run: emerge --sync gentoo
31       - name: Checkout the head ref of the pull request
32         uses: actions/checkout@v4
33         with:
34           fetch-depth: 0
35           repository: ${{ github.event.pull_request.head.repo.full_name }}
36           ref: ${{ github.event.pull_request.head.sha }}
37           path: gentoo-zh
38       - name: Get the base commit
39         id: basecommit
40         shell: bash
41         env:
42           COMMITS_URL: ${{ github.event.pull_request.commits_url }}
43           COMMITS_JSON: /tmp/commits.json
44         run: |
45           set -xe
46           curl -H "Accept: application/vnd.github.v3+json" \
47                -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
48                -o ${COMMITS_JSON} ${COMMITS_URL}
49           [[ $(file --mime-type ${COMMITS_JSON}) =~ application/json$ ]]
50           <${COMMITS_JSON} jq '.message' && false || true
51           commits=( $(<${COMMITS_JSON} jq '.[].sha' | cut -d'"' -f2) )
52           pcommits=( $(<${COMMITS_JSON} jq '.[].parents[].sha' | cut -d'"' -f2) )
53           sha=${pcommits[0]}
54           for pcommit in ${pcommits[@]}; do
55             pcommit_not_matched=0
56             for commit in ${commits[@]}; do
57               if [[ ${pcommit} == ${commit} ]]; then
58                 pcommit_not_matched=1
59               fi
60             done
61             if [[ ${pcommit_not_matched} == 0 ]]; then
62               sha=${pcommit}
63               break
64             fi
65           done
66           echo "sha=$sha" >> $GITHUB_OUTPUT
67       - name: Check
68         id: check
69         shell: bash
70         env:
71           THEBASEREF: ${{ steps.basecommit.outputs.sha }}
72         run: |
73           set -e
74           mv ${GITHUB_WORKSPACE}/gentoo-zh /var/db/repos/
75           mkdir -p /etc/portage/repos.conf
76           echo "[gentoo]
77           location = /var/db/repos/gentoo
78           " >/etc/portage/repos.conf/gentoo.conf
79           echo "[gentoo-zh]
80           location = /var/db/repos/gentoo-zh
81           " >/etc/portage/repos.conf/gentoo-zh.conf
82           cd /var/db/repos/gentoo-zh
83           echo "git diff --raw ${THEBASEREF}"
84           git diff --raw ${THEBASEREF}
85           diff_files=$(git diff --raw --name-only ${THEBASEREF})
86           cates=$(cat /var/db/repos/gentoo{,-zh}/profiles/categories | sort -du)
87           declare -a check_pkgs
88           for file in ${diff_files}; do
89             c=${file%%/*}
90             for cate in ${cates}; do
91               if [[ ${c} == ${cate} ]]; then
92                 n=${file#*/}
93                 n=${n%%/*}
94                 check_pkgs+=( ${c}/${n} )
95               fi
96             done
97           done
98           check_pkgs=( $(echo "${check_pkgs[@]}" | tr ' ' '\n' | sort -du | tr '\n' ' ') )
99           ret=0
100           echo "pkgs: ${check_pkgs[@]}"
101           set -- pkgcheck scan --exit error "${check_pkgs[@]}"
102           echo ">>> " "${@}"
103           "${@}" >/var/tmp/report.txt || ret=$?
104           cat /var/tmp/report.txt || true
105           set --
106           echo "ret=$ret" >> $GITHUB_OUTPUT
107           [[ ${ret} == 0 ]]
108       - name: Format report
109         if: ${{ always() }}
110         id: report
111         shell: bash
112         env:
113           ret: ${{ steps.check.outputs.ret }}
114         run: |
115           set -e
117           v_pkgcheck=$(pkgcheck --version) || true
118           commit_gentoo=$(cd /var/db/repos/gentoo && git rev-list -n1 HEAD) || true
120           r_len=$(wc -c /var/tmp/report.txt | awk -F'[[:space:]]' '{printf $1}')
121           r_header='`pkgcheck` checks'
122           if [[ ${r_len} == 0 ]]; then
123             r_title=":heavy_check_mark: ${r_header} passed"
124           else
125             r_explain="[:mag: QA Keywords Explanation](https://pkgcore.github.io/pkgcheck/man/pkgcheck.html#keywords)"
126             if [[ ${ret} == 0 ]]; then
127               r_title=":white_circle: ${r_header} passed but with something can be improved!"
128             else
129               r_title=":x: ${r_header} failed"
130             fi
131           fi
133           cat <<EOF >/var/tmp/report.md
134           ## ${r_title}
136           * ${v_pkgcheck}
137           * the corresponding _**::gentoo**_ commit: https://github.com/gentoo-mirror/gentoo/commit/${commit_gentoo}
139           EOF
141           if [[ -n ${r_explain} ]]; then
142             echo "<details open><summary><b>QA Results:</b></summary>
143             " >>/var/tmp/report.md
144             if [[ ${r_len} -le 65000 ]]; then
145               echo '```' >>/var/tmp/report.md
146               cat /var/tmp/report.txt >>/var/tmp/report.md
147               echo '```' >>/var/tmp/report.md
148             else
149               cd /var/db/repos/gentoo-zh || true
150               commit_id=$(git rev-list -n1 HEAD | cut -b1-7)
151               cat <<EOF >>/var/tmp/report.md
152           * _the report is too long, can be downloaded from **Artifacts** list of the current run:
153             https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
154             filename: **pkgcheck-report-${commit_id}**_
155           EOF
156               echo "commit=$commit_id" >> $GITHUB_OUTPUT
157             fi
158             cat <<EOF >>/var/tmp/report.md
159           </details>
161           ${r_explain}
162           EOF
163           fi
164           echo "has=true" >> $GITHUB_OUTPUT
165       - name: Hide previous results
166         if: ${{ always() }}
167         shell: bash
168         env:
169           COMMENTS_URL: ${{ github.event.pull_request.comments_url }}
170           COMMENTS_JSON: /tmp/comments.json
171         run: |
172           curl -H "Accept: application/vnd.github.v3+json" \
173                -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
174                -o ${COMMENTS_JSON} ${COMMENTS_URL}
175           node_ids=( $(<${COMMENTS_JSON} jq '.[] | select(.body|test("^##\\s*[a-zA-Z_:]+\\s*`pkgcheck`\\s*checks")) | select(.user.id == 41898282) | .node_id' | cut -d'"' -f2) )
176           echo "${node_ids[@]}"
177           json="{ \"query\": \"mutation {
178               minimizeComment(input: {
179                 subjectId: \\\"NODEID\\\",
180                 classifier: OUTDATED
181               }) {
182                 clientMutationId,
183                 minimizedComment {
184                   isMinimized,
185                   minimizedReason,
186                   viewerCanMinimize
187                 }
188               }
189             }\"
190           }"
191           json=$(tr -d '\n' <<<${json})
192           for node_id in ${node_ids[@]}; do
193             curl -X POST \
194               -H "Content-Type: application/json" \
195               -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
196               -d "${json/NODEID/${node_id}}" \
197               https://api.github.com/graphql
198           done
199       - name: Post result
200         if: ${{ always() && steps.report.outputs.has }}
201         uses: actions/github-script@v7
202         with:
203           script: |
204             const fs = require("fs").promises;
205             var cbody = await fs.readFile("/var/tmp/report.md", "utf8");
206             github.rest.issues.createComment({
207               issue_number: context.issue.number,
208               owner: context.repo.owner,
209               repo: context.repo.repo,
210               body: cbody
211             })
212       - name: Upload big result
213         if: ${{ always() && steps.report.outputs.commit }}
214         uses: actions/upload-artifact@v4
215         with:
216           name: pkgcheck-report-${{ steps.report.outputs.commit }}
217           path: /var/tmp/report.txt