Skip to content

Commit 7c6d30b

Browse files
authored
Merge pull request #15165 from github/z80coder/automodel-release
ensure `publish.sh` uses the latest `automodel` release
2 parents 5778720 + 4684546 commit 7c6d30b

File tree

2 files changed

+116
-18
lines changed

2 files changed

+116
-18
lines changed

java/ql/automodel/publish.sh

Lines changed: 115 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,129 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
set -e
33

4-
# Before running this, make sure there is an SSO-enabled token with package:write
5-
# permissions to codeql supplied via the GITHUB_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+
# 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."
15+
exit 1
16+
fi
17+
18+
# If we're publishing the codeml-automodel release then we will checkout the sha specified in the release.
19+
# So we need to check that there are no uncommitted changes in the local branch.
20+
# And, if we're publishing the current HEAD, it's cleaner to ensure that there are no uncommitted changes.
21+
if ! git diff --quiet; then
22+
echo "Error: Uncommitted changes exist. Please commit or stash your changes before publishing."
23+
exit 1
24+
fi
25+
26+
# Check the above environment variables are set
27+
if [ -z "${GITHUB_TOKEN}" ]; then
28+
echo "Error: GITHUB_TOKEN environment variable not set. Please set this to a token with package:write permissions to codeql."
29+
exit 1
30+
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
35+
if [ -z "${GH_TOKEN}" ]; then
36+
echo "Error: GH_TOKEN environment variable not set. Please set this to a token with repo permissions to github/codeml-automodel."
37+
exit 1
38+
fi
39+
40+
# Get the sha of the previous release, i.e. the last commit to the main branch that updated the query pack version
41+
PREVIOUS_RELEASE_SHA=$(git rev-list -n 1 main -- ./src/qlpack.yml)
42+
if [ -z "$PREVIOUS_RELEASE_SHA" ]; then
43+
echo "Error: Could not get the sha of the previous release of codeml-automodel query pack"
44+
exit 1
45+
else
46+
echo "Previous query-pack release sha: $PREVIOUS_RELEASE_SHA"
47+
fi
48+
49+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
50+
CURRENT_SHA=$(git rev-parse HEAD)
651

52+
if [ -z "${1:-}" ]; then
53+
# If the first argument is empty, use the latest release of codeml-automodel
54+
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)
55+
# Check TAG_NAME is not empty
56+
if [ -z "$TAG_NAME" ]; then
57+
echo "Error: Could not get latest release of codeml-automodel"
58+
exit 1
59+
fi
60+
echo "Updating to latest automodel release: $TAG_NAME"
61+
# Before downloading, delete any existing release.zip, and ignore failure if not present
62+
rm release.zip || true
63+
gh release download $TAG_NAME -A zip -O release.zip --repo 'https://github.com/github/codeml-automodel'
64+
# Before unzipping, delete any existing release directory, and ignore failure if not present
65+
rm -rf release || true
66+
unzip -o release.zip -d release
67+
REVISION=$(jq -r '.["codeql-sha"]' release/codeml-automodel*/codeml-automodel-release.json)
68+
echo "The latest codeml-automodel release specifies the codeql sha $REVISION"
69+
# Check that REVISION is downstream from PREVIOUS_RELEASE_SHA
70+
if ! git merge-base --is-ancestor "$PREVIOUS_RELEASE_SHA" "$REVISION"; then
71+
echo "Error: The codeql version $REVISION is not downstream of the query-pack version $PREVIOUS_RELEASE_SHA"
72+
exit 1
73+
fi
74+
# Get the version of the codeql code specified by the codeml-automodel release
75+
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
82+
fi
83+
84+
# Get the absolute path of the automodel repo
785
AUTOMODEL_ROOT="$(readlink -f "$(dirname $0)")"
86+
# Get the absolute path of the workspace root
887
WORKSPACE_ROOT="$AUTOMODEL_ROOT/../../.."
88+
# Specify the groups of queries to test and publish
989
GRPS="automodel,-test"
1090

11-
if [ -z "$CODEQL_DIST" ]; then
12-
echo "CODEQL_DIST not set"
13-
exit -1
14-
fi
15-
16-
cd "$AUTOMODEL_ROOT"
91+
pushd "$AUTOMODEL_ROOT"
1792
echo Testing automodel queries
1893
"${CODEQL_DIST}/codeql" test run test
94+
popd
95+
96+
pushd "$WORKSPACE_ROOT"
97+
echo "Preparing the release"
98+
"${CODEQL_DIST}/codeql" pack release --groups $GRPS -v
99+
100+
echo "Publishing the release"
101+
# Add --dry-run to test publishing
102+
"${CODEQL_DIST}/codeql" pack publish --groups $GRPS -v
19103

20-
cd "$WORKSPACE_ROOT"
104+
echo "Bumping versions"
105+
"${CODEQL_DIST}/codeql" pack post-release --groups $GRPS -v
106+
popd
21107

22-
echo Preparing release
23-
"${CODEQL_DIST}/codeql" pack release --groups $GRPS
108+
# The above commands update
109+
# ./src/CHANGELOG.md
110+
# ./src/codeql-pack.release.yml
111+
# ./src/qlpack.yml
112+
# and add a new file
113+
# ./src/change-notes/released/<version>.md
114+
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"
118+
fi
24119

25-
echo Publishing automodel
26-
"${CODEQL_DIST}/codeql" pack publish --groups $GRPS
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
27127

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

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

java/ql/automodel/src/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ A significant part of the behavior of extraction queries is implemented in share
6565

6666
## Packaging
6767

68-
Automodel extraction queries come as a dedicated package. See [qlpack.yml](https://github.com/github/codeql/blob/main/java/ql/automodel/src/qlpack.yml). The [publish.sh](https://github.com/github/codeql/blob/main/java/ql/automodel/publish.sh) script is responsible for publishing a new version to the [package registry](https://github.com/orgs/codeql/packages/container/package/java-automodel-queries).
68+
Automodel extraction queries come as a dedicated package. See [qlpack.yml](https://github.com/github/codeql/blob/main/java/ql/automodel/src/qlpack.yml). The [publish.sh](https://github.com/github/codeql/blob/main/java/ql/automodel/publish.sh) script is responsible for publishing a new version to the [package registry](https://github.com/orgs/codeql/packages/container/package/java-automodel-queries). **The extraction queries are functionally coupled with other automodel components. Only publish the query pack as part of the automodel release process.**
6969

7070
### Backwards Compatibility
7171

0 commit comments

Comments
 (0)