Skip to content

Commit

Permalink
Fix workflow for regenerating tutorials when there's an existing PR
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
  • Loading branch information
jdbaldry committed Jun 27, 2024
1 parent a50dce3 commit 713dae4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/regenerate-tutorials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ jobs:
path: loki
- uses: actions/checkout@v4
with:
ref: staging
path: killercoda
- uses: actions/setup-go@v5
with:
go-version-file: killercoda/tools/transformer/go.mod
- run: go build ./
working-directory: killercoda/tools/transformer
- run: ./scripts/check-out-branch.bash
shell: bash
working-directory: killercoda
- run: >
./transformer
"${GITHUB_WORKSPACE}/loki/docs/sources/get-started/quick-start.md"
"${GITHUB_WORKSPACE}/killercoda/loki/loki-quickstart"
working-directory: killercoda/tools/transformer
- run: ./scripts/manage-pr.bash
- run: ./scripts/open-pr.bash
env:
GH_TOKEN: ${{ github.token }}
shell: bash
Expand Down
43 changes: 43 additions & 0 deletions scripts/check-out-branch.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

set -euf -o pipefail

function usage {
cat <<EOF
Check out the branch for regenerating tutorials.
Usage:
$0
Examples:
$0
EOF
}

if [[ $# -ne 0 ]]; then
usage
exit 1
fi

readonly BRANCH="${BRANCH:-update-generated-tutorials}"

PR_STATUS=$(gh pr view "${BRANCH}" --json state --jq .state || true)
readponly PR_STATUS

if [[ "${PR_STATUS}" == OPEN ]]; then
gh pr checkout "${BRANCH}"

if ! git diff --exit-code; then
commit
git push
fi

exit 0
fi

# Remove the remote branch if it exists because there is no PR associated with it.
if git fetch origin "${BRANCH}"; then
git push origin --delete "${BRANCH}"
fi

git checkout -b "${BRANCH}"
38 changes: 17 additions & 21 deletions scripts/manage-pr.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

set -euf -o pipefail

function usage {
cat <<EOF
Open a PR for updating generated tutorials if there are changes.
Usage:
$0
Examples:
$0
EOF
}

if [[ $# -ne 0 ]]; then
usage
exit 1
fi

readonly BRANCH="${BRANCH:-update-generated-tutorials}"
readonly SUBJECT="${SUBJECT:-Update generated tutorials}"

Expand All @@ -13,27 +30,6 @@ function commit {
git commit --message "${SUBJECT}"
}

PR_STATUS=$(gh pr view "${BRANCH}" --json state --jq .state || true)
readonly PR_STATUS

if [[ "${PR_STATUS}" == OPEN ]]; then
gh pr checkout "${BRANCH}"

if ! git diff --exit-code; then
commit
git push
fi

exit 0
fi

# Remove the remote branch if it exists because there is no PR associated with it.
if git fetch origin "${BRANCH}"; then
git push origin --delete "${BRANCH}"
fi

git checkout -b "${BRANCH}"

if ! git diff --exit-code; then
commit
git push origin "refs/heads/${BRANCH}"
Expand Down

0 comments on commit 713dae4

Please sign in to comment.