Debian Codegraph @

scripts/bump-changelogs

raw
1 #!/usr/bin/env bash
2
3 # updates changelog version indicated by release-plz
4
5 set -eEuo pipefail
6
7 ### START HELPERS
8
9 function bump_changelog_version() {
10 local package_dir="$1"
11 local version="$2"
12
13 local cargo_manifest="${package_dir}/Cargo.toml"
14 local current_version="$(sed -nE 's/^version ?= ?"([^"]+)"$/\1/ p' "$cargo_manifest" | head -n 1)"
15 local changelog_file="${package_dir}/CHANGELOG.md"
16 local change_chunk_file="$(mktemp)"
17 local readme_file="${package_dir}/README.md"
18
19 echo "${package_dir}/CHANGELOG.md | $current_version -> $version"
20
21 # get changelog chunk and save to temp file
22 cat "$changelog_file" |
23 # skip up to unreleased heading
24 sed '1,/Unreleased/ d' |
25 # take up to previous version heading
26 sed "/$current_version/ q" |
27 # drop last line
28 sed '$d' \
29 >"$change_chunk_file"
30
31 # if word count of changelog chunk is 0 then insert filler changelog chunk
32 if [[ "$(wc -w "$change_chunk_file" | awk '{ print $1 }')" == "0" ]]; then
33 echo >"$change_chunk_file"
34 echo "- No significant changes since \`$current_version\`." >>"$change_chunk_file"
35 echo >>"$change_chunk_file"
36 fi
37
38 (
39 sed '/Unreleased/ q' "$changelog_file" # up to unreleased heading
40 echo # blank line
41 echo "## $version" # new version heading
42 cat "$change_chunk_file" # previously unreleased changes
43 sed "/$current_version/ q" "$changelog_file" | tail -n 1 # the previous version heading
44 sed "1,/$current_version/ d" "$changelog_file" # everything after previous version heading
45 ) >"$changelog_file.bak"
46
47 mv "$changelog_file.bak" "$changelog_file"
48
49 # update readme links
50 if [ -f "$readme_file" ]; then
51 sed -i.bak -E "s#([=/])$current_version([/)])#\1$version\2#g" "$readme_file"
52 rm -f "${readme_file}.bak"
53 fi
54 }
55
56 ### END HELPERS
57
58 RELEASE_PLZ_PR_JSON="$1"
59
60 specs="$(echo "$RELEASE_PLZ_PR_JSON" | jq -r '.releases[] | "\(.package_name):\(.version)"')"
61
62 for spec in $specs; do
63 name="$(echo "$spec" | cut -d: -f1)"
64 version="$(echo "$spec" | cut -d: -f2)"
65
66 bump_changelog_version "." "$version"
67 done
68
69 PR_NUMBER="$(echo "$RELEASE_PLZ_PR_JSON" | jq -r '.number')"
70
71 # update release PR if one was created
72 if [[ -n "$PR_NUMBER" ]]; then
73 gh pr checkout "$PR_NUMBER"
74 git add --all
75 git commit -m "docs: update changelog versions"
76 git push
77 fi