Skip to content

Commit 3375121

Browse files
committed
wip
wip wip more checks fix bug if release folder already exists fix bug if release folder already exists ensure branch has correct release; dry-run simplify branches step by step fix paths pushd/popd pushd/popd use bash simplify simplify simplify simplify add dry run
1 parent 6572be6 commit 3375121

File tree

1 file changed

+99
-43
lines changed

1 file changed

+99
-43
lines changed

java/ql/automodel/publish.sh

Lines changed: 99 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,118 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
set -e
33

4-
# Before running this, make sure
5-
# 1. there is an SSO-enabled token with package:write permissions to codeql supplied via the GITHUB_TOKEN environment variable
6-
# 2. the CODEQL_DIST environment variable is set to the path of a codeql distribution
7-
# 3. the gh command line tool is installed and authenticated with a token that has repo permissions to github/codeml-automodel
8-
# supplied via the GH_TOKEN environment variable
4+
# Add help message
5+
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."
9+
exit 0
10+
fi
11+
12+
# If we're publishing the codeml-automodel release then we will checkout the sha specified in the release.
13+
# So we need to check that there are no uncommitted changes in the local branch.
14+
# And, if we're publishing the current HEAD, it's cleaner to ensure that there are no uncommitted changes.
15+
if ! git diff --quiet; then
16+
echo "Error: Uncommitted changes exist. Please commit or stash your changes before publishing."
17+
exit 1
18+
fi
19+
20+
# Check the above environment variables are set
21+
if [ -z "${GITHUB_TOKEN:-}" ]; then
22+
echo "Error: GITHUB_TOKEN environment variable not set. Please set this to a token with package:write permissions to codeql."
23+
exit 1
24+
fi
25+
if [ -z "${CODEQL_DIST:-}" ]; then
26+
echo "Error: CODEQL_DIST environment variable not set. Please set this to the path of a codeql distribution."
27+
exit 1
28+
fi
29+
if [ -z "${GH_TOKEN:-}" ]; then
30+
echo "Error: GH_TOKEN environment variable not set. Please set this to a token with repo permissions to github/codeml-automodel."
31+
exit 1
32+
fi
33+
34+
# Get the sha of the previous release
35+
PREVIOUS_RELEASE_SHA=$(git rev-list -n 1 main -- ./src/qlpack.yml)
36+
if [ -z "$PREVIOUS_RELEASE_SHA" ]; then
37+
echo "Error: Could not get the sha of the previous release of codeml-automodel query pack"
38+
exit 1
39+
else
40+
echo "Previous query-pack release sha: $PREVIOUS_RELEASE_SHA"
41+
fi
942

10-
# Script to publish a new version of the automodel package to the package registry.
11-
# Usage: ./publish [override-release]
12-
# By default the sha of the codeql repo specified in the latest release of codeml-automodel will be published.
13-
# Otherwise, the optional argument override-release forces the current HEAD to be published.
43+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
44+
CURRENT_SHA=$(git rev-parse HEAD)
1445

15-
# If the first argument is empty, use the latest release of codeml-automodel
1646
if [ -z "${1:-}" ]; then
17-
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)
18-
# Check TAG_NAME is not empty
19-
if [ -z "$TAG_NAME" ]; then
20-
echo "Error: Could not get latest release of codeml-automodel"
21-
exit 1
22-
fi
23-
echo "Updating to latest automodel release: $TAG_NAME"
24-
rm release.zip || true
25-
gh release download $TAG_NAME -A zip -O release.zip --repo 'https://github.com/github/codeml-automodel'
26-
unzip -o release.zip -d release
27-
REVISION=$(jq -r '.["codeql-sha"]' release/codeml-automodel*/codeml-automodel-release.json)
28-
echo "The latest automodel release specifies a codeql revision of $REVISION"
29-
if git diff --quiet; then
30-
echo "Checking out CodeQL revision $REVISION"
31-
git reset --hard "$REVISION"
32-
else
33-
echo "Error: Uncommitted changes exist. Please commit or stash your changes before resetting."
47+
# If the first argument is empty, use the latest release of codeml-automodel
48+
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)
49+
# Check TAG_NAME is not empty
50+
if [ -z "$TAG_NAME" ]; then
51+
echo "Error: Could not get latest release of codeml-automodel"
3452
exit 1
35-
fi
53+
fi
54+
echo "Updating to latest automodel release: $TAG_NAME"
55+
rm release.zip || true
56+
gh release download $TAG_NAME -A zip -O release.zip --repo 'https://github.com/github/codeml-automodel'
57+
rm -rf release || true
58+
unzip -o release.zip -d release
59+
REVISION=$(jq -r '.["codeql-sha"]' release/codeml-automodel*/codeml-automodel-release.json)
60+
echo "The latest codeml-automodel release specifies the codeql sha $REVISION"
61+
# Check that REVISION is downstream from PREVIOUS_RELEASE_SHA
62+
if ! git merge-base --is-ancestor "$PREVIOUS_RELEASE_SHA" "$REVISION"; then
63+
echo "Error: The codeql version $REVISION is not downstream of the query-pack version $PREVIOUS_RELEASE_SHA"
64+
exit 1
65+
fi
66+
# Get the version of the codeql code specified by the codeml-automodel release
67+
git checkout "$REVISION"
68+
else
69+
# Check that the current HEAD is downstream from PREVIOUS_RELEASE_SHA
70+
if ! git merge-base --is-ancestor "$PREVIOUS_RELEASE_SHA" "$CURRENT_SHA"; then
71+
echo "Error: The current HEAD is not downstream from the previous release"
72+
exit 1
73+
fi
3674
fi
3775

3876
AUTOMODEL_ROOT="$(readlink -f "$(dirname $0)")"
3977
WORKSPACE_ROOT="$AUTOMODEL_ROOT/../../.."
4078
GRPS="automodel,-test"
4179

42-
if [ -z "$CODEQL_DIST" ]; then
43-
echo "CODEQL_DIST not set"
44-
exit -1
45-
fi
46-
47-
cd "$AUTOMODEL_ROOT"
80+
pushd "$AUTOMODEL_ROOT"
4881
echo Testing automodel queries
4982
"${CODEQL_DIST}/codeql" test run test
83+
popd
84+
85+
pushd "$WORKSPACE_ROOT"
86+
echo "Preparing the release"
87+
"${CODEQL_DIST}/codeql" pack release --groups $GRPS -v
5088

51-
cd "$WORKSPACE_ROOT"
89+
echo "Publishing the release"
90+
# Add --dry-run to test publishing
91+
"${CODEQL_DIST}/codeql" pack publish --groups $GRPS -v
5292

53-
echo Preparing release
54-
"${CODEQL_DIST}/codeql" pack release --groups $GRPS
93+
echo "Bumping versions"
94+
"${CODEQL_DIST}/codeql" pack post-release --groups $GRPS -v
95+
popd
96+
97+
# The above commands update
98+
# ./src/CHANGELOG.md
99+
# ./src/codeql-pack.release.yml
100+
# ./src/qlpack.yml
101+
# and add a new file
102+
# ./src/change-notes/released/<version>.md
103+
104+
if [ -z "${1:-}" ]; then
105+
# If we used the latest release of codeml-automodel, then we need to return to the current branch
106+
git checkout "$CURRENT_BRANCH"
107+
fi
55108

56-
echo Publishing automodel
57-
"${CODEQL_DIST}/codeql" pack publish --groups $GRPS
109+
# Add the updated files to the current branch
110+
git add ./src/CHANGELOG.md
111+
git add ./src/codeql-pack.release.yml
112+
git add ./src/qlpack.yml
113+
git add ./src/change-notes/released/*
114+
echo "Added the following updated version files to the current branch:"
115+
git status -s
58116

59-
echo Bumping versions
60-
"${CODEQL_DIST}/codeql" pack post-release --groups $GRPS
117+
echo "Automodel packs successfully published. Local files have been modified. Please commit and push the version changes and then merge into main."
61118

62-
echo Automodel packs successfully published. Please commit and push the version changes.

0 commit comments

Comments
 (0)