Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 61 additions & 38 deletions .github/workflows/commit/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,76 @@ outputs:
runs:
using: "composite"
steps:
- name: Check for changes
id: check
run: |
set -x
if [ -n "$(git status --porcelain)" ]; then
echo "has_changes=true" | tee -a $GITHUB_OUTPUT
else
echo "has_changes=false" | tee -a $GITHUB_OUTPUT
fi
shell: bash

- name: Commit if changed, create a PR if protected
id: commit
if: steps.check.outputs.has_changes == 'true'
env:
GITHUB_TOKEN: ${{ inputs.token }}
run: |
set -x
if [ -n "$(git status --porcelain)" ]; then
echo "Changed"
protected=${{ github.ref_protected }}
foreign=${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
if [ "${foreign}" = "true" ]; then
# https://github.com/krlmlr/actions-sync/issues/44
echo "Can't push to foreign branch"
elif [ "${protected}" = "true" ]; then
current_branch=$(git branch --show-current)
new_branch=gha-commit-$(git rev-parse --short HEAD)
git checkout -b ${new_branch}
git add .
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
# Force-push, used in only one place
# Alternative: separate branch names for each usage
git push -u origin HEAD -f

existing_pr=$(gh pr list --state open --base main --head ${new_branch} --json number --jq '.[] | .number')
if [ -n "${existing_pr}" ]; then
echo "Existing PR: ${existing_pr}"
else
gh pr create --base main --head ${new_branch} --title "chore: Auto-update from GitHub Actions" --body "Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi
protected=${{ github.ref_protected }}
foreign=${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
is_pr=${{ github.event_name == 'pull_request' }}
if [ "${is_pr}" = "true" ]; then
# Running on a PR - will use reviewdog in next step
echo "Code changes detected on PR, will suggest changes via reviewdog"
echo "use_reviewdog=true" | tee -a $GITHUB_OUTPUT
git reset HEAD
git status
elif [ "${foreign}" = "true" ]; then
# https://github.com/krlmlr/actions-sync/issues/44
echo "Can't push to foreign branch"
elif [ "${protected}" = "true" ]; then
current_branch=$(git branch --show-current)
new_branch=gha-commit-$(git rev-parse --short HEAD)
git checkout -b ${new_branch}
git add .
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
# Force-push, used in only one place
# Alternative: separate branch names for each usage
git push -u origin HEAD -f

gh workflow run rcc -f ref=$(git rev-parse HEAD)
gh pr merge --merge --auto
existing_pr=$(gh pr list --state open --base main --head ${new_branch} --json number --jq '.[] | .number')
if [ -n "${existing_pr}" ]; then
echo "Existing PR: ${existing_pr}"
else
git fetch
if [ -n "${GITHUB_HEAD_REF}" ]; then
git add .
git stash save
git switch ${GITHUB_HEAD_REF}
git merge origin/${GITHUB_BASE_REF} --no-edit
git stash pop
fi
git add .
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
git push -u origin HEAD
gh pr create --base main --head ${new_branch} --title "chore: Auto-update from GitHub Actions" --body "Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi

# Only set output if changed
echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
gh workflow run rcc -f ref=$(git rev-parse HEAD)
gh pr merge --merge --auto
else
git fetch
if [ -n "${GITHUB_HEAD_REF}" ]; then
git add .
git stash save
git switch ${GITHUB_HEAD_REF}
git merge origin/${GITHUB_BASE_REF} --no-edit
git stash pop
fi
git add .
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
git push -u origin HEAD

# Only set output if changed
echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
fi
shell: bash

- name: Suggest changes via reviewdog
if: steps.commit.outputs.use_reviewdog == 'true'
uses: krlmlr/action-suggester@main
with:
github_token: ${{ inputs.token }}
tool_name: "rcc"
11 changes: 8 additions & 3 deletions .github/workflows/update-snapshots/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,24 @@ runs:
if: ${{ steps.run-tests.outputs.changed }}
id: check-changed
run: |
echo "changed=$(git status --porcelain -- tests/testthat/_snaps | head -n 1)" >> $GITHUB_OUTPUT
set -x
if [ "${{ github.event_name}}" != "pull_request" ] ; then
echo "changed=$(git status --porcelain -- tests/testthat/_snaps | head -n 1)" | tee -a $GITHUB_OUTPUT
fi
shell: bash

- name: Derive branch name
if: ${{ steps.check-changed.outputs.changed }}
id: matrix-desc
run: |
set -x
config=$(echo '${{ toJSON(matrix) }}' | jq -c .)
echo "text=$(echo ${config})" >> $GITHUB_OUTPUT
echo "branch=$(echo ${config} | sed -r 's/[^0-9a-zA-Z]+/-/g;s/^-//;s/-$//')" >> $GITHUB_OUTPUT
echo "text=$(echo ${config})" | tee -a $GITHUB_OUTPUT
echo "branch=$(echo ${config} | sed -r 's/[^0-9a-zA-Z]+/-/g;s/^-//;s/-$//')" | tee -a $GITHUB_OUTPUT
shell: bash

- name: Create pull request
# Fall through if PR, will use reviewdog/action-suggester in the commit action
if: ${{ steps.check-changed.outputs.changed }}
id: cpr
uses: peter-evans/create-pull-request@v6
Expand Down
Loading