Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / .github / workflows / check-by-name.yml
blobc6cd142bfa6197cc8bbacc8030d4a1e602f3bd67
1 # Checks pkgs/by-name (see pkgs/by-name/README.md)
2 # using the nixpkgs-check-by-name tool (see pkgs/test/nixpkgs-check-by-name)
3 name: Check pkgs/by-name
5 # The pre-built tool is fetched from a channel,
6 # making it work predictable on all PRs.
7 on:
8   # Using pull_request_target instead of pull_request avoids having to approve first time contributors
9   pull_request_target
11 # The tool doesn't need any permissions, it only outputs success or not based on the checkout
12 permissions: {}
14 jobs:
15   check:
16     # This is x86_64-linux, for which the tool is always prebuilt on the nixos-* channels,
17     # as specified in nixos/release-combined.nix
18     runs-on: ubuntu-latest
19     steps:
20       - name: Resolving the merge commit
21         env:
22           GH_TOKEN: ${{ github.token }}
23         run: |
24           # This checks for mergeability of a pull request as recommended in
25           # https://docs.github.com/en/rest/guides/using-the-rest-api-to-interact-with-your-git-database?apiVersion=2022-11-28#checking-mergeability-of-pull-requests
26           while true; do
27             echo "Checking whether the pull request can be merged"
28             prInfo=$(gh api \
29               -H "Accept: application/vnd.github+json" \
30               -H "X-GitHub-Api-Version: 2022-11-28" \
31               /repos/"$GITHUB_REPOSITORY"/pulls/${{ github.event.pull_request.number }})
32             mergeable=$(jq -r .mergeable <<< "$prInfo")
33             mergedSha=$(jq -r .merge_commit_sha <<< "$prInfo")
35             if [[ "$mergeable" == "null" ]]; then
36               # null indicates that GitHub is still computing whether it's mergeable
37               # Wait a couple seconds before trying again
38               echo "GitHub is still computing whether this PR can be merged, waiting 5 seconds before trying again"
39               sleep 5
40             else
41               break
42             fi
43           done
45           if [[ "$mergeable" == "true" ]]; then
46             echo "The PR can be merged, checking the merge commit $mergedSha"
47           else
48             echo "The PR cannot be merged, it has a merge conflict"
49             exit 1
50           fi
51           echo "mergedSha=$mergedSha" >> "$GITHUB_ENV"
52       - uses: actions/checkout@v4
53         with:
54           # pull_request_target checks out the base branch by default
55           ref: ${{ env.mergedSha }}
56           # Fetches the merge commit and its parents
57           fetch-depth: 2
58       - name: Determining PR git hashes
59         run: |
60           # For pull_request_target this is the same as $GITHUB_SHA
61           echo "baseSha=$(git rev-parse HEAD^1)" >> "$GITHUB_ENV"
63           echo "headSha=$(git rev-parse HEAD^2)" >> "$GITHUB_ENV"
64       - uses: cachix/install-nix-action@v23
65       - name: Determining channel to use for dependencies
66         run: |
67           echo "Determining which channel to use for PR base branch $GITHUB_BASE_REF"
68           if [[ "$GITHUB_BASE_REF" =~ ^(release|staging|staging-next)-([0-9][0-9]\.[0-9][0-9])$ ]]; then
69               # Use the release channel for all PRs to release-XX.YY, staging-XX.YY and staging-next-XX.YY
70               channel=nixos-${BASH_REMATCH[2]}
71               echo "PR is for a release branch, using release channel $channel"
72           else
73               # Use the nixos-unstable channel for all other PRs
74               channel=nixos-unstable
75               echo "PR is for a non-release branch, using unstable channel $channel"
76           fi
77           echo "channel=$channel" >> "$GITHUB_ENV"
78       - name: Fetching latest version of channel
79         run: |
80           echo "Fetching latest version of channel $channel"
81           # This is probably the easiest way to get Nix to output the path to a downloaded channel!
82           nixpkgs=$(nix-instantiate --find-file nixpkgs -I nixpkgs=channel:"$channel")
83           # This file only exists in channels
84           rev=$(<"$nixpkgs"/.git-revision)
85           echo "Channel $channel is at revision $rev"
86           echo "nixpkgs=$nixpkgs" >> "$GITHUB_ENV"
87           echo "rev=$rev" >> "$GITHUB_ENV"
88       - name: Fetching pre-built nixpkgs-check-by-name from the channel
89         run: |
90           echo "Fetching pre-built nixpkgs-check-by-name from channel $channel at revision $rev"
91           # Passing --max-jobs 0 makes sure that we won't build anything
92           nix-build "$nixpkgs" -A tests.nixpkgs-check-by-name --max-jobs 0
93       - name: Running nixpkgs-check-by-name
94         run: |
95           echo "Checking whether the check succeeds on the base branch $GITHUB_BASE_REF"
96           git checkout -q "$baseSha"
97           if baseOutput=$(result/bin/nixpkgs-check-by-name . 2>&1); then
98             baseSuccess=1
99           else
100             baseSuccess=
101           fi
102           printf "%s\n" "$baseOutput"
104           echo "Checking whether the check would succeed after merging this pull request"
105           git checkout -q "$mergedSha"
106           if mergedOutput=$(result/bin/nixpkgs-check-by-name . 2>&1); then
107             mergedSuccess=1
108             exitCode=0
109           else
110             mergedSuccess=
111             exitCode=1
112           fi
113           printf "%s\n" "$mergedOutput"
115           resultToEmoji() {
116             if [[ -n "$1" ]]; then
117               echo ":heavy_check_mark:"
118             else
119               echo ":x:"
120             fi
121           }
123           # Print a markdown summary in GitHub actions
124           {
125             echo "| Nixpkgs version | Check result |"
126             echo "| --- | --- |"
127             echo "| Latest base commit | $(resultToEmoji "$baseSuccess") |"
128             echo "| After merging this PR | $(resultToEmoji "$mergedSuccess") |"
129             echo ""
131             if [[ -n "$baseSuccess" ]]; then
132               if [[ -n "$mergedSuccess" ]]; then
133                 echo "The check succeeds on both the base branch and after merging this PR"
134               else
135                 echo "The check succeeds on the base branch, but would fail after merging this PR:"
136                 echo "\`\`\`"
137                 echo "$mergedOutput"
138                 echo "\`\`\`"
139                 echo ""
140               fi
141             else
142               if [[ -n "$mergedSuccess" ]]; then
143                 echo "The check fails on the base branch, but this PR fixes it, nicely done!"
144               else
145                 echo "The check fails on both the base branch and after merging this PR, unknown if only this PRs changes would satisfy the check, the base branch needs to be fixed first."
146                 echo ""
147                 echo "Failure on the base branch:"
148                 echo "\`\`\`"
149                 echo "$baseOutput"
150                 echo "\`\`\`"
151                 echo ""
152                 echo "Failure after merging this PR:"
153                 echo "\`\`\`"
154                 echo "$mergedOutput"
155                 echo "\`\`\`"
156                 echo ""
157               fi
158             fi
160             echo "### Details"
161             echo "- nixpkgs-check-by-name tool:"
162             echo "  - Channel: $channel"
163             echo "  - Nixpkgs commit: [$rev](https://github.com/${GITHUB_REPOSITORY}/commit/$rev)"
164             echo "  - Store path: \`$(realpath result)\`"
165             echo "- Tested Nixpkgs:"
166             echo "  - Base branch: $GITHUB_BASE_REF"
167             echo "  - Latest base branch commit: [$baseSha](https://github.com/${GITHUB_REPOSITORY}/commit/$baseSha)"
168             echo "  - Latest PR commit: [$headSha](https://github.com/${GITHUB_REPOSITORY}/commit/$headSha)"
169             echo "  - Merge commit: [$mergedSha](https://github.com/${GITHUB_REPOSITORY}/commit/$mergedSha)"
170           } >> "$GITHUB_STEP_SUMMARY"
172           exit "$exitCode"