Skip to content

Commit 8e532e0

Browse files
committed
separation of concern of creating a PR vs code to change the version
1 parent 3f39af2 commit 8e532e0

File tree

3 files changed

+109
-25
lines changed

3 files changed

+109
-25
lines changed

.github/actions/create-pr/action.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Create a PR if one doesn't exists
2+
description: >
3+
Creates a commit with the current changes to the repo, and opens a PR for that commit. If
4+
any PR with the same title exists, then this action is marked as succeeded.
5+
inputs:
6+
commit-message:
7+
description: >
8+
The message for the commit to be created.
9+
required: true
10+
11+
title:
12+
description: >
13+
The title of the PR. If empty, the title and body will be determined from the commit message.
14+
default: ''
15+
required: false
16+
17+
body:
18+
description: >
19+
The body (description) of the PR. The `title` input must be specified in order for this input to be used.
20+
default: ''
21+
required: false
22+
23+
head-branch:
24+
description: >
25+
The name of the branch to hold the new commit. If an existing open PR with the same head
26+
branch exists, the new branch will be force-pushed to that PR instead of creating a new PR.
27+
required: true
28+
29+
base-branch:
30+
description: >
31+
The base branch to target with the new PR.
32+
required: true
33+
34+
token:
35+
description: |
36+
The GitHub token to use. It must have enough privileges to
37+
make API calls to create and close pull requests.
38+
required: true
39+
40+
runs:
41+
using: composite
42+
steps:
43+
- name: Update git config
44+
shell: bash
45+
run: |
46+
git config --global user.email "github-actions@github.com"
47+
git config --global user.name "github-actions[bot]"
48+
- name: Commit, Push and Open PR
49+
shell: bash
50+
env:
51+
COMMIT_MESSAGE: ${{ inputs.commit-message }}
52+
HEAD_BRANCH: ${{ inputs.head-branch }}
53+
BASE_BRANCH: ${{ inputs.base-branch }}
54+
GITHUB_TOKEN: ${{ inputs.token }}
55+
TITLE: ${{ inputs.title }}
56+
BODY: ${{ inputs.body }}
57+
run: |
58+
set -exu
59+
# stage changes in the working tree
60+
if [[ $(git diff --stat) != '' ]]; then
61+
git add .
62+
fi
63+
# only commit if the working tree is not empty
64+
if [[ $(git diff --stat --cached) != '' ]]; then
65+
git commit -m "$COMMIT_MESSAGE"
66+
git checkout -b "$HEAD_BRANCH"
67+
# CAUTION: gits history changes with the following
68+
git push --force origin "$HEAD_BRANCH"
69+
PR_JSON=$(gh pr list --state open --json number --head "$HEAD_BRANCH")
70+
if [[ $? -ne 0 ]]; then
71+
echo "Failed to fetch existing PRs."
72+
exit 1
73+
fi
74+
PR_NUMBERS=$(echo $PR_JSON | jq '. | length')
75+
if [[ PR_NUMBERS -ne 0 ]]; then
76+
echo "Found existing open PR: $PR_NUMBERS"
77+
exit 0
78+
else
79+
gh pr create --head "$HEAD_BRANCH" --base "$BASE_BRANCH" --title "$TITLE" --body "$BODY" --assignee ${{ github.actor }}
80+
if [[ $? -ne 0 ]]; then
81+
echo "Failed to create new PR."
82+
exit 1
83+
fi
84+
fi
85+
fi

.github/workflows/bump-cli.yml

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: Bump CLI version
22
on:
3+
workflow_dispatch:
34
push:
45
branches: [main]
5-
pull_request:
6-
branches: [main]
6+
paths:
7+
- .github/actions/create-pr/action.yml
78
schedule:
89
- cron: 0 0 */14 * * # run every 14 days
910

@@ -16,29 +17,23 @@ jobs:
1617
uses: actions/checkout@v3
1718
with:
1819
fetch-depth: 1
19-
token: ${{ secrets.WORKFLOW_TOKEN }}
20-
- name: Install dependencies
21-
run: |
22-
sudo apt-get update
23-
sudo apt-get install -qq jq
24-
sudo apt-get install gh
25-
shell: bash
2620
- name: Bump CLI
2721
env:
2822
GH_TOKEN: ${{ github.token }}
2923
run: |
3024
scripts/replace-cli-version.sh
31-
- name: Push changes to a branch
32-
env:
33-
GH_TOKEN: ${{ secrets.WORKFLOW_TOKEN }}
25+
- name: Configure actors
26+
shell: bash
3427
run: |
35-
git config user.name github-actions
36-
git config user.email github-actions@github.com
37-
git checkout -b bump-cli
38-
if [[ `git status --porcelain` ]]; then
39-
git add .
40-
git commit -m "automatically bump cli version"
41-
git push --set-upstream origin bump-cli
42-
git push
43-
gh pr create --title "Bump CLI Version for integration tests" --body ""
44-
fi
28+
git config --global user.email "github-actions@github.com"
29+
git config --global user.name "github-actions[bot]"
30+
- name: Commit, Push and Open a PR
31+
uses: ./.github/actions/create-pr
32+
with:
33+
token: ${{ secrets.WORKFLOW_TOKEN }}
34+
base-branch: refs/heads/main
35+
head-branch: bump-cli
36+
commit-message: Bump CLI version from $PREVIOUS_VERSION to $LATEST_VERSION for integration tests
37+
title: Bump CLI Version to $LATEST_VERSION for integration tests
38+
body: >
39+
Bumps rom $PREVIOUS_VERSION to $LATEST_VERSION

scripts/replace-cli-version.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#!/bin/bash
22

3-
VERSIONS=$(gh api -H "Accept: application/vnd.github+json" /repos/github/codeql-cli-binaries/releases | jq '.[].tag_name' | head -2)
3+
VERSIONS=$(gh api -H "Accept: application/vnd.github+json" /repos/github/codeql-cli-binaries/releases | jq -r '.[].tag_name' | head -2)
44

5-
LATEST_VERSION=$(echo $VERSIONS | awk '{ print $1 }' | sed "s/\"//g")
6-
PREVIOUS_VERSION=$(echo $VERSIONS | awk '{ print $2 }' | sed "s/\"//g")
5+
# we are exporting these variables so that we can access these variables in the workflow
6+
LATEST_VERSION=$(echo $VERSIONS | awk '{ print $1 }')
7+
PREVIOUS_VERSION=$(echo $VERSIONS | awk '{ print $2 }')
8+
9+
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
10+
echo "PREVIOUS_VERSION=$PREVIOUS_VERSION" >> $GITHUB_ENV
711

812
sed -i "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" .github/workflows/main.yml
913
sed -i "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" extensions/ql-vscode/src/vscode-tests/ensureCli.ts

0 commit comments

Comments
 (0)