|
1 |
| -#!/bin/sh |
| 1 | +#!/bin/bash |
2 | 2 | set -e
|
3 | 3 |
|
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) |
6 | 51 |
|
| 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 |
7 | 85 | AUTOMODEL_ROOT="$(readlink -f "$(dirname $0)")"
|
| 86 | +# Get the absolute path of the workspace root |
8 | 87 | WORKSPACE_ROOT="$AUTOMODEL_ROOT/../../.."
|
| 88 | +# Specify the groups of queries to test and publish |
9 | 89 | GRPS="automodel,-test"
|
10 | 90 |
|
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" |
17 | 92 | echo Testing automodel queries
|
18 | 93 | "${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 |
19 | 103 |
|
20 |
| -cd "$WORKSPACE_ROOT" |
| 104 | +echo "Bumping versions" |
| 105 | +"${CODEQL_DIST}/codeql" pack post-release --groups $GRPS -v |
| 106 | +popd |
21 | 107 |
|
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 |
24 | 119 |
|
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 |
27 | 127 |
|
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." |
30 | 129 |
|
31 |
| -echo Automodel packs successfully published. Please commit and push the version changes. |
|
0 commit comments