Skip to content

Commit 3534bfc

Browse files
authored
Merge pull request github#15251 from github/z80coder/dry-run
Support dry-run of publishing script
2 parents a732199 + 75545db commit 3534bfc

File tree

1 file changed

+105
-37
lines changed

1 file changed

+105
-37
lines changed

java/ql/automodel/publish.sh

Lines changed: 105 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
11
#!/bin/bash
22
set -e
33

4-
# Add help message
4+
help="Usage: ./publish [--override-release] [--dry-run]
5+
Publish the automodel query pack.
6+
7+
If no arguments are provided, publish the version of the codeql repo specified by the latest official release of the codeml-automodel repo.
8+
If the --override-release argument is provided, your current local HEAD is used (for unofficial releases or patching).
9+
If the --dry-run argument is provided, the release is not published (for testing purposes)."
10+
11+
# Echo the help message
512
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
6-
echo "Usage: ./publish [override-release]"
7-
echo "By default we publish the version of the codeql repo specified by the latest official release defined by the codeml-automodel repo."
8-
echo "Otherwise, the optional argument override-release forces your current HEAD to be published."
13+
echo "$help"
914
exit 0
1015
fi
1116

12-
# Check that either there are 0 or 1 arguments, and if 1 argument then check that it is "override-release"
13-
if [ $# -gt 1 ] || [ $# -eq 1 ] && [ "$1" != "override-release" ]; then
14-
echo "Error: Invalid arguments. Please run './publish --help' for usage information."
17+
# Check the number of arguments are valid
18+
if [ $# -gt 2 ]; then
19+
echo "Error: Invalid arguments provided"
20+
echo "$help"
1521
exit 1
1622
fi
1723

24+
OVERRIDE_RELEASE=0
25+
DRY_RUN=0
26+
for arg in "$@"
27+
do
28+
case $arg in
29+
--override-release)
30+
OVERRIDE_RELEASE=1
31+
shift # Remove --override-release from processing
32+
;;
33+
--dry-run)
34+
DRY_RUN=1
35+
shift # Remove --dry-run from processing
36+
;;
37+
*)
38+
echo "Error: Invalid argument provided: $arg"
39+
echo "$help"
40+
exit 1
41+
;;
42+
esac
43+
done
44+
45+
# Describe what we're about to do based on the command-line arguments
46+
if [ $OVERRIDE_RELEASE = 1 ]; then
47+
echo "Publishing the current HEAD of the automodel repo"
48+
else
49+
echo "Publishing the version of the automodel repo specified by the latest official release of the codeml-automodel repo"
50+
fi
51+
if [ $DRY_RUN = 1 ]; then
52+
echo "Dry run: we will step through the process but we won't publish the query pack"
53+
else
54+
echo "Not a dry run! Publishing the query pack"
55+
fi
56+
1857
# If we're publishing the codeml-automodel release then we will checkout the sha specified in the release.
1958
# So we need to check that there are no uncommitted changes in the local branch.
2059
# And, if we're publishing the current HEAD, it's cleaner to ensure that there are no uncommitted changes.
@@ -28,10 +67,6 @@ if [ -z "${GITHUB_TOKEN}" ]; then
2867
echo "Error: GITHUB_TOKEN environment variable not set. Please set this to a token with package:write permissions to codeql."
2968
exit 1
3069
fi
31-
if [ -z "${CODEQL_DIST}" ]; then
32-
echo "Error: CODEQL_DIST environment variable not set. Please set this to the path of a codeql distribution."
33-
exit 1
34-
fi
3570
if [ -z "${GH_TOKEN}" ]; then
3671
echo "Error: GH_TOKEN environment variable not set. Please set this to a token with repo permissions to github/codeml-automodel."
3772
exit 1
@@ -49,8 +84,14 @@ fi
4984
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
5085
CURRENT_SHA=$(git rev-parse HEAD)
5186

52-
if [ -z "${1:-}" ]; then
53-
# If the first argument is empty, use the latest release of codeml-automodel
87+
if [ $OVERRIDE_RELEASE = 1 ]; then
88+
# Check that the current HEAD is downstream from PREVIOUS_RELEASE_SHA
89+
if ! git merge-base --is-ancestor "$PREVIOUS_RELEASE_SHA" "$CURRENT_SHA"; then
90+
echo "Error: The current HEAD is not downstream from the previous release"
91+
exit 1
92+
fi
93+
else
94+
# Get the latest release of codeml-automodel
5495
TAG_NAME=$(gh api -H 'Accept: application/vnd.github+json' -H 'X-GitHub-Api-Version: 2022-11-28' /repos/github/codeml-automodel/releases/latest | jq -r .tag_name)
5596
# Check TAG_NAME is not empty
5697
if [ -z "$TAG_NAME" ]; then
@@ -73,12 +114,6 @@ if [ -z "${1:-}" ]; then
73114
fi
74115
# Get the version of the codeql code specified by the codeml-automodel release
75116
git checkout "$REVISION"
76-
else
77-
# Check that the current HEAD is downstream from PREVIOUS_RELEASE_SHA
78-
if ! git merge-base --is-ancestor "$PREVIOUS_RELEASE_SHA" "$CURRENT_SHA"; then
79-
echo "Error: The current HEAD is not downstream from the previous release"
80-
exit 1
81-
fi
82117
fi
83118

84119
# Get the absolute path of the automodel repo
@@ -88,21 +123,28 @@ WORKSPACE_ROOT="$AUTOMODEL_ROOT/../../.."
88123
# Specify the groups of queries to test and publish
89124
GRPS="automodel,-test"
90125

126+
# Install the codeql gh extension
127+
gh extensions install github/gh-codeql
128+
91129
pushd "$AUTOMODEL_ROOT"
92130
echo Testing automodel queries
93-
"${CODEQL_DIST}/codeql" test run test
131+
gh codeql test run test
94132
popd
95133

96134
pushd "$WORKSPACE_ROOT"
97135
echo "Preparing the release"
98-
"${CODEQL_DIST}/codeql" pack release --groups $GRPS -v
136+
gh codeql pack release --groups $GRPS -v
99137

100-
echo "Publishing the release"
101-
# Add --dry-run to test publishing
102-
"${CODEQL_DIST}/codeql" pack publish --groups $GRPS -v
138+
if [ $DRY_RUN = 1 ]; then
139+
echo "Dry run: not publishing the query pack"
140+
gh codeql pack publish --groups $GRPS --dry-run -v
141+
else
142+
echo "Not a dry run! Publishing the query pack"
143+
gh codeql pack publish --groups $GRPS -v
144+
fi
103145

104146
echo "Bumping versions"
105-
"${CODEQL_DIST}/codeql" pack post-release --groups $GRPS -v
147+
gh codeql pack post-release --groups $GRPS -v
106148
popd
107149

108150
# The above commands update
@@ -112,18 +154,44 @@ popd
112154
# and add a new file
113155
# ./src/change-notes/released/<version>.md
114156

115-
if [ -z "${1:-}" ]; then
116-
# If we used the latest release of codeml-automodel, then we need to return to the current branch
117-
git checkout "$CURRENT_BRANCH"
157+
# Get the filename of the most recently created file in ./src/change-notes/released/*.md
158+
# This will be the file for the new release
159+
NEW_CHANGE_NOTES_FILE=$(ls -t ./src/change-notes/released/*.md | head -n 1)
160+
161+
# Make a copy of the modified files
162+
mv ./src/CHANGELOG.md ./src/CHANGELOG.md.dry-run
163+
mv ./src/codeql-pack.release.yml ./src/codeql-pack.release.yml.dry-run
164+
mv ./src/qlpack.yml ./src/qlpack.yml.dry-run
165+
mv "$NEW_CHANGE_NOTES_FILE" ./src/change-notes/released.md.dry-run
166+
167+
if [ $OVERRIDE_RELEASE = 1 ]; then
168+
# Restore the original files
169+
git checkout ./src/CHANGELOG.md
170+
git checkout ./src/codeql-pack.release.yml
171+
git checkout ./src/qlpack.yml
172+
else
173+
# Restore the original files
174+
git checkout "$CURRENT_BRANCH" --force
118175
fi
119176

120-
# Add the updated files to the current branch
121-
git add ./src/CHANGELOG.md
122-
git add ./src/codeql-pack.release.yml
123-
git add ./src/qlpack.yml
124-
git add ./src/change-notes/released/*
125-
echo "Added the following updated version files to the current branch:"
126-
git status -s
127-
128-
echo "Automodel packs successfully published. Local files have been modified. Please commit and push the version changes and then merge into main."
177+
if [ $DRY_RUN = 1 ]; then
178+
echo "Inspect the updated dry-run version files:"
179+
ls -l ./src/*.dry-run
180+
ls -l ./src/change-notes/*.dry-run
181+
else
182+
# Add the updated files to the current branch
183+
echo "Adding the version changes"
184+
mv -f ./src/CHANGELOG.md.dry-run ./src/CHANGELOG.md
185+
mv -f ./src/codeql-pack.release.yml.dry-run ./src/codeql-pack.release.yml
186+
mv -f ./src/qlpack.yml.dry-run ./src/qlpack.yml
187+
mv -f ./src/change-notes/released.md.dry-run "$NEW_CHANGE_NOTES_FILE"
188+
git add ./src/CHANGELOG.md
189+
git add ./src/codeql-pack.release.yml
190+
git add ./src/qlpack.yml
191+
git add "$NEW_CHANGE_NOTES_FILE"
192+
echo "Added the following updated version files to the current branch:"
193+
git status -s
194+
echo "To complete the release, please commit these files and merge to the main branch"
195+
fi
129196

197+
echo "Done"

0 commit comments

Comments
 (0)