diff --git a/.github/workflows/automation-changenote.yml b/.github/workflows/automation-changenote.yml new file mode 100644 index 00000000..975168ac --- /dev/null +++ b/.github/workflows/automation-changenote.yml @@ -0,0 +1,79 @@ +name: Automation Change Note +description: Adds a changenote to PRs from automation such as Dependabot. + +on: + workflow_call: + +permissions: + pull-requests: write + +env: + COMMIT_MESSAGE: "Add changenote." + +jobs: + changenote: + name: Automated Change Note + if: github.repository != 'beeware/.github' + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 1 + token: ${{ secrets.BRUTUS_PAT_TOKEN }} + + - name: Configure git + run: | + git config user.email "brutus@beeware.org" + git config user.name "Brutus (robot)" + + - name: Change Note Details + id: changenote + run: | + # If the last commit was a changenote commit, then a changenote commit is not needed on this run + if [[ '${{ github.event.head_commit.message }}' == "${COMMIT_MESSAGE}"* ]]; then + echo "needed=false" >> ${GITHUB_OUTPUT} + else + echo "needed=true" >> ${GITHUB_OUTPUT} + + # Generate a filename for the changenote based on the PR for this commit + API_URL="${{ github.api_url }}/repos/${{ github.repository }}/commits/${{ github.event.head_commit.id }}/pulls" + PR_NUM=$(curl -s -H "Accept: application/vnd.github+json" "${API_URL}" | jq -r '.[].number') + echo "filename=./changes/${PR_NUM}.misc.rst" >> ${GITHUB_OUTPUT} + fi + + - name: Create Dependabot Change Note + id: dependabot + if: startsWith(github.ref, 'refs/heads/dependabot/') && (steps.changenote.outputs.needed == 'true') + run: | + # Create change note from first line of dependabot commit + NEWS=$(printf "%s" "${{ github.event.head_commit.message }}" | head -n1 | sed -e 's/Bump/Updated/') + printf "%s.\n" "${NEWS}" > "${{ steps.changenote.outputs.filename }}" + + git add "${{ steps.changenote.outputs.filename }}" + echo "skip=true" >> ${GITHUB_OUTPUT} + + - name: Create pre-commit Autoupdate Change Note + id: pre-commit + if: github.ref == 'refs/heads/autoupdates/pre-commit' && (steps.changenote.outputs.needed == 'true') + run: | + printf 'Updated hooks for ``pre-commit`` to the latest version.\n' > "${{ steps.changenote.outputs.filename }}" + + git add "${{ steps.changenote.outputs.filename }}" + + - name: Commit and Push changenote + if: steps.changenote.outputs.needed == 'true' + run: | + if [[ "${{ steps.dependabot.outputs.skip }}" == "true" ]]; then + # "dependabot skip" tells Dependabot to continue rebasing this branch despite foreign commits + COMMIT_MESSAGE="${COMMIT_MESSAGE} [dependabot skip]" + fi + + # Only attempt to push if files changed; otherwise, the empty push fails the entire job. + if [[ $(git status --porcelain) ]]; then + echo "Pushing commit..." + git commit -m "${COMMIT_MESSAGE}" + git push origin + fi diff --git a/.github/workflows/dependabot-changenote.yml b/.github/workflows/dependabot-changenote.yml deleted file mode 100644 index 3e39a8b0..00000000 --- a/.github/workflows/dependabot-changenote.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Dependabot Change Note - -on: - workflow_call: - -permissions: - pull-requests: write - -jobs: - changenote: - name: Dependabot Change Note - if: github.actor == 'dependabot[bot]' && github.repository != 'beeware/.github' - runs-on: ubuntu-latest - timeout-minutes: 5 - steps: - - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 1 - token: ${{ secrets.BRUTUS_PAT_TOKEN }} - - - name: Configure git - run: | - git config --local user.email "$(git log --pretty='%ae' -1)" - git config --local user.name "Dependabot[bot]" - - - name: Commit Change Note - run: | - # Fetch PR number for commit from Github API - API_URL="${{ github.api_url }}/repos/${{ github.repository }}/commits/${{ github.event.head_commit.id }}/pulls" - PR_NUM=$(curl -s -H "Accept: application/vnd.github+json" "${API_URL}" | jq -r '.[].number') - - # Create change note from first line of dependabot commit - NEWS=$(printf "%s" "${{ github.event.head_commit.message }}" | head -n1 | sed -e 's/Bump/Updated/') - printf "%s.\n" "${NEWS}" > "./changes/${PR_NUM}.misc.rst" - - # Commit the change note - git add "./changes/${PR_NUM}.misc.rst" - # "dependabot skip" tells Dependabot to continue rebasing this branch despite foreign commits - git commit -m "Add changenote. [dependabot skip]" - - - name: Push - run: git push origin diff --git a/.github/workflows/pre-commit-update.yml b/.github/workflows/pre-commit-update.yml new file mode 100644 index 00000000..66714d06 --- /dev/null +++ b/.github/workflows/pre-commit-update.yml @@ -0,0 +1,61 @@ +name: Update pre-commit +description: Updates pre-commit's hooks, runs pre-commit, and commits all changes into a PR. + +on: + workflow_call: + inputs: + pre-commit-source: + description: "The arguments for `pip install` to install pre-commit; use ./path/to/package[dev] for the repo's pinned version." + default: '.[dev]' + type: string + +permissions: + pull-requests: write + +jobs: + pre-commit-update: + name: Update pre-commit + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 1 + token: ${{ secrets.BRUTUS_PAT_TOKEN }} + + - name: Configure git + run: | + git config user.email "brutus@beeware.org" + git config user.name "Brutus (robot)" + + - name: Set up Python + uses: actions/setup-python@v4.5.0 + with: + python-version: 3.X + + - name: Install pre-commit + run: python -m pip install ${{ inputs.pre-commit-source }} + + - name: Update pre-commit hooks + run: | + pre-commit autoupdate + pre-commit install-hooks + + - name: Run pre-commit + # pre-commit returns non-zero when files are changed and fails the job + continue-on-error: true + run: pre-commit run --all-files + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v4.2.3 + with: + token: ${{ secrets.BRUTUS_PAT_TOKEN }} + title: Auto-update `pre-commit` hooks + branch: autoupdates/pre-commit + commit-message: Auto-updated `pre-commit` hooks. + committer: Brutus (robot) + author: Brutus (robot) + body: Updated `pre-commit` hooks to their latest versions and ran updated hooks against the repo. + labels: dependencies