2828jobs :
2929 prepare-release :
3030 outputs :
31- pull-request-head-sha : ${{ steps.create-release-pull-request .outputs.pull-request-head-sha }}
31+ pull-request-head-sha : ${{ steps.determine-pr-head-sha .outputs.pull-request-head-sha }}
3232 name : " Prepare release"
3333 if : github.event_name == 'workflow_dispatch'
34- runs-on : ubuntu-latest
34+ runs-on : ubuntu-latest
3535 steps :
36- - name : Checkout
36+ - name : Checkout
3737 uses : actions/checkout@v3
3838 with :
3939 ref : ${{ inputs.ref }}
4040
41+ - name : Validate release precondition
42+ env :
43+ RELEASE_VERSION : ${{ inputs.version }}
44+ GITHUB_TOKEN : ${{ github.token }}
45+ run : |
46+ read -r release type < <(gh release list | awk -v release="v$RELEASE_VERSION" '$1 ~ release { print $1,$2; ++n } END { if (n == 0) print "undefined", "undefined" }')
47+ if [[ "$release" == "undefined" ]]; then
48+ echo "Release v$RELEASE_VERSION does not exist. Proceeding"
49+ echo "create_draft_release=true" >> "$GITHUB_ENV"
50+ else
51+ if [[ "$type" != "Draft" ]]; then
52+ echo "Release '$release' already exists and is not a draft, but has release state '$type'. Cannot proceed"
53+ exit 1
54+ else
55+ echo "Release '$release' already exists and is a draft. Proceeding"
56+ echo "create_draft_release=false" >> "$GITHUB_ENV"
57+ fi
58+ fi
59+
60+ if [[ -z $(git ls-remote --heads origin rc/$RELEASE_VERSION) ]]; then
61+ echo "Release branch rc/$RELEASE_VERSION does not exist."
62+ echo "create_release_branch=true" >> "$GITHUB_ENV"
63+ echo "create_release_pr=true" >> "$GITHUB_ENV"
64+ else
65+ echo "Release branch rc/$RELEASE_VERSION already exists."
66+ echo "create_release_branch=false" >> "$GITHUB_ENV"
67+
68+ pr_state=$(gh pr view rc/$RELEASE_VERSION --json title,state)
69+ pr_title=$(echo "$pr_state" | jq -r '.title')
70+ pr_state=$(echo "$pr_state" | jq -r '.state')
71+
72+ echo "Found PR '$pr_title' with state '$pr_state'"
73+
74+ if [[ "$pr_title" == "Release v$RELEASE_VERSION" ]] && [[ "$pr_state" == "OPEN" ]]; then
75+ echo "Release PR for rc/$RELEASE_VERSION already exists and is open."
76+ echo "create_release_pr=false" >> "$GITHUB_ENV"
77+ else
78+ echo "Release PR for rc/$RELEASE_VERSION does not exist or is closed."
79+ echo "create_release_pr=true" >> "$GITHUB_ENV"
80+ fi
81+
82+ fi
83+
4184 - name : Install Python
4285 uses : actions/setup-python@v4
4386 with :
@@ -49,17 +92,38 @@ jobs:
4992 - name : Validate version
5093 run : |
5194 python scripts/release/validate-version.py "$RELEASE_VERSION"
52-
95+
5396 - name : Create release branch
97+ if : env.create_release_branch == 'true'
5498 run : |
5599 git switch -c rc/$RELEASE_VERSION
56100 git push --set-upstream origin rc/$RELEASE_VERSION
57101
102+ - name : Create draft release
103+ if : env.create_draft_release == 'true'
104+ env :
105+ RELEASE_VERSION : ${{ inputs.version }}
106+ GITHUB_TOKEN : ${{ github.token }}
107+ run : |
108+ gh release create \
109+ -R $GITHUB_REPOSITORY \
110+ --title "v$RELEASE_VERSION" \
111+ --draft \
112+ --target rc/$RELEASE_VERSION \
113+ $RELEASE_VERSION
114+
58115 - name : Create feature branch for PR
116+ if : env.create_release_pr == 'true'
59117 run : |
60118 git switch -c feature/update-user-manual-for-$RELEASE_VERSION
61119 git push --set-upstream origin feature/update-user-manual-for-$RELEASE_VERSION
62120
121+ - name : Get feature branch for PR
122+ if : env.create_release_pr == 'false'
123+ run : |
124+ git switch feature/update-user-manual-for-$RELEASE_VERSION
125+ git pull --rebase
126+
63127 - name : Update user manual version
64128 run : |
65129 find docs -name 'user_manual.md' | xargs sed -i "s/code-scanning-cpp-query-pack-.*\.zip\`/code-scanning-cpp-query-pack-$RELEASE_VERSION.zip\`/"
@@ -68,16 +132,41 @@ jobs:
68132 find docs -name 'user_manual.md' | xargs sed -i "s/user_manual_.*\.md\`/user_manual_$RELEASE_VERSION.md\`/"
69133 find docs -name 'user_manual.md' | xargs sed -i "s/This user manual documents release \`.*\` of/This user manual documents release \`$RELEASE_VERSION\` of/"
70134
135+ if git diff --quiet; then
136+ echo "update-release-pr=true" >> "$GITHUB_ENV"
137+ else
138+ echo "update-release-pr=false" >> "$GITHUB_ENV"
139+ fi
140+
141+ - name : Update feature branch for PR
142+ if : env.update-release-pr == 'true'
143+ run : |
144+ find docs -name 'user_manual.md' -exec git add {} \;
145+ git commit -m "Update user manual for release $RELEASE_VERSION."
146+ git push
147+
71148 - name : Create release PR
72- id : create-release-pull-request
149+ if : env.create_release_pr == 'true'
73150 uses : peter-evans/create-pull-request@v5
74151 with :
75- title : " Release ${{ inputs.version }}."
152+ title : " Release v ${{ inputs.version }}."
76153 body : " This PR releases codeql-coding-standards version ${{ inputs.version }}."
77154 commit-message : " Update user manual for release ${{ inputs.version }}."
78155 delete-branch : true
79156 branch : " rc/${{ inputs.version }}"
80-
157+
158+ - name : Determine pull request head SHA
159+ id : determine-pr-head-sha
160+ env :
161+ GITHUB_TOKEN : ${{ github.token }}
162+ run : |
163+ read -r pull_request_head_sha pr_state < <(gh pr view rc/$RELEASE_VERSION --json headRefOid,state --jq '.headRefOid + " " + .state')
164+ if [[ "$pr_state" != "OPEN" ]]; then
165+ echo "Release PR for rc/$RELEASE_VERSION is not open, but in state '$pr_state'. Cannot proceed!"
166+ exit 1
167+ fi
168+ echo "pull-request-head-sha=$pull_request_head_sha" >> "$GITHUB_OUTPUT"
169+
81170 # Invoke release validation because our PRs created with a GitHub token do not trigger a `pull_request` event.
82171 validate-release :
83172 name : " Validate coding standards release"
0 commit comments