|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | +shopt -s nullglob |
| 5 | + |
| 6 | +root=$1 |
| 7 | +currentPrNumber=${2:-} |
| 8 | + |
| 9 | +[[ "$(toml get --raw "$root"/Cargo.toml package.version)" =~ ([0-9]+)\.([0-9]+)\.([0-9]+) ]] |
| 10 | +splitVersion=( "${BASH_REMATCH[@]:1}" ) |
| 11 | + |
| 12 | +majorChanges=( "$root"/changes/unreleased/major/*.md ) |
| 13 | +mediumChanges=( "$root"/changes/unreleased/medium/*.md ) |
| 14 | +minorChanges=( "$root"/changes/unreleased/minor/*.md ) |
| 15 | + |
| 16 | +if (( ${#majorChanges[@]} > 0 )); then |
| 17 | + # If we didn't have `|| true` this would exit the program due to `set -e`, |
| 18 | + # because (( ... )) returns the incremental value, which is treated as the exit code.. |
| 19 | + (( splitVersion[0]++ )) || true |
| 20 | + splitVersion[1]=0 |
| 21 | + splitVersion[2]=0 |
| 22 | +elif (( ${#mediumChanges[@]} > 0 )); then |
| 23 | + (( splitVersion[1]++ )) || true |
| 24 | + splitVersion[2]=0 |
| 25 | +elif (( ${#minorChanges[@]} > 0 )); then |
| 26 | + (( splitVersion[2]++ )) || true |
| 27 | +else |
| 28 | + echo >&2 "No changes" |
| 29 | + exit 0 |
| 30 | +fi |
| 31 | + |
| 32 | +next=${splitVersion[0]}.${splitVersion[1]}.${splitVersion[2]} |
| 33 | +releaseFile=$root/changes/released/${next}.md |
| 34 | +mkdir -p "$(dirname "$releaseFile")" |
| 35 | + |
| 36 | +echo "# Version $next ($(date --iso-8601 --utc))" > "$releaseFile" |
| 37 | +echo "" >> "$releaseFile" |
| 38 | + |
| 39 | +# shellcheck disable=SC2016 |
| 40 | +for file in "${majorChanges[@]}" "${mediumChanges[@]}" "${minorChanges[@]}"; do |
| 41 | + commit=$(git -C "$root" log -1 --format=%H -- "$file") |
| 42 | + if ! gh api graphql \ |
| 43 | + -f sha="$commit" \ |
| 44 | + -f query=' |
| 45 | + query ($sha: String) { |
| 46 | + repository(owner: "NixOS", name: "nixpkgs-check-by-name") { |
| 47 | + commit: object(expression: $sha) { |
| 48 | + ... on Commit { |
| 49 | + associatedPullRequests(first: 100) { |
| 50 | + nodes { |
| 51 | + merged |
| 52 | + baseRefName |
| 53 | + baseRepository { nameWithOwner } |
| 54 | + number |
| 55 | + author { login } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }' | \ |
| 62 | + jq --exit-status -r ${currentPrNumber:+--argjson currentPrNumber "$currentPrNumber"} --arg file "$file" ' |
| 63 | + .data.repository.commit.associatedPullRequests?.nodes?[]? |
| 64 | + | select( |
| 65 | + # We need to make sure to get the right PR, there can be many |
| 66 | + (.merged or .number == $ARGS.named.currentPrNumber) and |
| 67 | + .baseRepository.nameWithOwner == "NixOS/nixpkgs-check-by-name" and |
| 68 | + .baseRefName == "main") |
| 69 | + | "\(.number) \(.author.login) \($ARGS.named.file)"'; then |
| 70 | + echo >&2 "Couldn't get PR for file $file" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +done | \ |
| 74 | +sort -n | \ |
| 75 | +while read -r number author file; do |
| 76 | + # Replace the first line `# <title>` by `- <title> by @author in #number` |
| 77 | + # All other non-empty lines are indented with 2 spaces to make the markdown formatting work |
| 78 | + sed "$file" >> "$releaseFile" \ |
| 79 | + -e "1s|#[[:space:]]\(.*\)|- \1 by [@$author](https://github.com/$author) in [#$number](https://github.com/NixOS/nixpkgs-check-by-name/pull/$number)|" \ |
| 80 | + -e '2,$s/^\(.\)/ \1/' |
| 81 | + |
| 82 | + rm "$file" |
| 83 | +done |
| 84 | + |
| 85 | +cargo set-version --manifest-path "$root"/Cargo.toml "$next" |
| 86 | +echo "$next" |
0 commit comments