git-url-parse 0.4.3

A parser for git repo urls based on url crate
Documentation
name: Update Repo stuff

on:
  workflow_call:

env:
  CHANGELOG_FILENAME: CHANGELOG.md
  DEFAULT_BRANCH: main
  ADD_PR_LABELS: bot-updated
  RESTART_PR_DELAY: 30s
  GIT_BOT_USERNAME: github-actions[bot]
  GIT_BOT_EMAIL: github-actions[bot]@users.noreply.github.com

jobs:
  get_pr_number_pr_event:
    name: Get the PR number from PR event
    runs-on: ubuntu-latest
    outputs:
      pr_number: ${{ steps.pr.outputs.number }}
    steps:
      - name: Dump context
        uses: crazy-max/ghaction-dump-context@v1.2.1
      - name: Get PR number
        id: pr
        if: github.event_name == 'pull_request'
        run: |
          NUMBER=${{ github.event.pull_request.number }}
          echo "PR number is: $NUMBER"
          echo "PR_NUMBER=$NUMBER" >> $GITHUB_ENV
          echo "::set-output name=number::$NUMBER"

  get_pr_number_push_event:
    name: Get the PR number from Push event
    runs-on: ubuntu-latest
    outputs:
      pr_number: ${{ steps.pr.outputs.number }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Dump context
        uses: crazy-max/ghaction-dump-context@v1.2.1

      - name: Get PR number
        id: pr
        if: github.event_name == 'push' && github.ref == 'refs/heads/staging'
        run: |
          git log --format=%B -n 1 $GITHUB_SHA | head -1 > /tmp/commit_msg

          NUMBER=$( cat /tmp/commit_msg | head -1 | sed 's/.*(\#\(.*\))/\1/' )
          echo "PR number is: $NUMBER"
          echo "::set-output name=number::$NUMBER"

  get_pr_number:
    name: Get PR number from event
    runs-on: ubuntu-latest
    needs:
      - get_pr_number_pr_event
      - get_pr_number_push_event
    outputs:
      pr_number: ${{ steps.pr.outputs.number }}
    steps:
      - name: Dump context
        uses: crazy-max/ghaction-dump-context@v1.2.1

      - name: Print PR number
        id: pr
        run: |
          cat $GITHUB_ENV

          if [[ ${{ github.event_name == 'pull_request' }}  = 'true' ]]; then
            NUMBER=${{ needs.get_pr_number_pr_event.outputs.pr_number }}
          fi

          if [[ ${{ github.event_name == 'push' }}  = 'true' ]]; then
            NUMBER=${{ needs.get_pr_number_push_event.outputs.pr_number }}
          fi

          echo "PR number is: $NUMBER"
          echo "::set-output name=number::$NUMBER"

  print_inputs:
    name: Print the inputs to the workflow
    runs-on: ubuntu-latest
    needs:
      - get_pr_number
    steps:
      - name: Print
        run: |
          echo event name: ${{ github.event_name }}
          echo pr number: ${{ needs.get_pr_number.outputs.pr_number }}

  get_pr_info:
    name: Get the PR branch name and issue number
    needs:
      - get_pr_number
      - print_inputs
    outputs:
      pr_branch: ${{ steps.pr_info.outputs.name }}
      pr_number: ${{ steps.pr_info.outputs.number }}
      pr_title: ${{ steps.pr_info.outputs.title }}
    permissions: read-all
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Get PR info from pull request event
        if: github.event_name == 'pull_request'
        id: info
        uses: Brymastr/pr-info-action@v1

      - name: Dump context
        uses: crazy-max/ghaction-dump-context@v1.2.1

      - name: Set outputs from pull request event
        if: github.event_name == 'pull_request'
        run: |
          echo "PR_BRANCH=${{ steps.info.outputs.head_branch }}" >> $GITHUB_ENV
          echo "PR_NUMBER=${{ needs.get_pr_number.outputs.pr_number }}" >> $GITHUB_ENV
          echo "PR_TITLE=${{ steps.info.outputs.title }}" >> $GITHUB_ENV

      - name: Get PR info from push event
        if: github.event_name == 'push'
        run: |
          echo "PR_BRANCH='$(git rev-parse --abbrev-ref HEAD)'" >> $GITHUB_ENV
          echo "PR_BRANCH=$(git rev-parse --abbrev-ref HEAD)"
          echo "PR_NUMBER='${{ needs.get_pr_number.outputs.pr_number }}'" >> $GITHUB_ENV
          echo "PR_NUMBER='${{ needs.get_pr_number.outputs.pr_number }}'"

          # Might need to b64 encode titles, so they can avoid exec from print later on
          # The commit message can be multiple lines, but we only want the first one

          git log --format=%B -n 1 $GITHUB_SHA | head -1 | base64 > /tmp/pr-title
          echo PR_TITLE="$(cat /tmp/pr-title)" >> $GITHUB_ENV
          echo PR_TITLE="$(cat /tmp/pr-title)"

          gh auth login --with-token < <(echo ${{ secrets.GITHUB_TOKEN }})
          gh pr checkout ${{ needs.get_pr_number.outputs.pr_number }}

      - name: Set outputs from push event
        id: pr_info
        run: |
          echo "::set-output name=name::$PR_BRANCH"
          echo "pr branch is: $PR_BRANCH"

          echo "::set-output name=number::$PR_NUMBER"
          echo "pr number is: $PR_NUMBER"

          echo "::set-output name=title::$PR_TITLE"
          echo "b64 encoded pr title is: $PR_TITLE"

  # This isn't working as expected
  #bot-label-check:
  #  name: Check if we've already done the hard work
  #  runs-on: ubuntu-latest
  #  needs: get_pr_info
  #  outputs:
  #    label-exists: ${{ steps.label-check.outputs.result }}
  #  steps:
  #    - name: Check if already labeled before running
  #      id: label-check
  #      uses: paul1k/check-pr-labels@v1.0.0
  #      with:
  #        github-token: ${{ secrets.GITHUB_TOKEN }}
  #        pull-number: ${{ fromJSON(needs.get_pr_info.outputs.pr_number) }}
  #        labels: '["bot-updated"]'

  generate_changelog:
    name: Generate changelog
    runs-on: ubuntu-latest
    needs:
      #- bot-label-check
      - get_pr_info
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Configure git user
        run: |
          git status
          git config user.name "${{ env.GIT_BOT_USERNAME }}"
          git config user.email "${{ env.GIT_BOT_EMAIL }}"

      - name: Dump context
        uses: crazy-max/ghaction-dump-context@v1.2.1

      - name: Simulate PR merge for changelog updates
        run: |
          git checkout ${{ env.DEFAULT_BRANCH }}

          # Write commit into file, so we don't have to
          echo ${{ needs.get_pr_info.outputs.pr_title }} | base64 -d > /tmp/commit_title

          git commit --all --allow-empty -F /tmp/commit_title
          git log -5

      - name: Generate a changelog
        uses: orhun/git-cliff-action@v1
        id: git-cliff
        with:
          config: cliff.toml
          args: --verbose
        env:
          OUTPUT: ${{ env.CHANGELOG_FILENAME }}
      - name: Print the changelog
        run: cat "${{ steps.git-cliff.outputs.changelog }}"
      - name: Upload generated CHANGELOG
        uses: actions/upload-artifact@v2
        with:
          name: ${{ env.CHANGELOG_FILENAME }}
          path: ${{ env.CHANGELOG_FILENAME }}
          retention-days: 1

  # In the future, we should find a way to skip this step for the final build
  # after msrv and changelog have been updated
  # possibly using labels on PR
  generate_msrv:
    name: Generate Minimum Supported Rust Version badge
    #if: github.event_name == 'push' && github.ref == 'refs/heads/staging'
    #needs: bot-label-check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: rust-toolchain
        uses: actions-rs/toolchain@v1.0.6
        with:
          profile: minimal
          toolchain: stable
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-msrv
        run: cargo install cargo-msrv

      - name: Get Minimum Supported Rust Version
        id: get-msrv
        timeout-minutes: 10
        run: |
          MSRV_RESULT=$(cargo msrv --output-format json | tail -1)
          MSRV=$(echo $MSRV_RESULT | jq -r '.msrv')
          echo "::set-output name=msrv::$MSRV"

      - name: Create Badge
        run: curl https://img.shields.io/badge/rustc-${{ steps.get-msrv.outputs.msrv }}%2B-blue?logo=rust -o msrv-badge.svg
      - uses: actions/upload-artifact@v3
        with:
          name: msrv-badge.svg
          path: msrv-badge.svg

  commit_updates:
    name: Commit updates
    #if: github.event_name == 'push' && github.ref == 'refs/heads/staging'
    runs-on: ubuntu-latest
    needs:
      - get_pr_info
      - generate_changelog
      - generate_msrv
      #- bot-label-check
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Get GH App token
        id: app
        uses: getsentry/action-github-app-token@v1
        with:
          app_id: ${{ secrets.APP_ID }}
          private_key: ${{ secrets.APP_PRIVATE_KEY }}

      - name: Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ steps.app.outputs.token }}
          fetch-depth: 0

      - name: Dump context
        uses: crazy-max/ghaction-dump-context@v1.2.1

      - name: Checkout PR branch
        env:
          GH_TOKEN: ${{ steps.app.outputs.token }}
        run: |
          gh pr checkout ${{ needs.get_pr_info.outputs.pr_number }}
          ls
          git status

      - name: Download ${{ env.CHANGELOG_FILENAME }}
        uses: actions/download-artifact@v2
        with:
          name: ${{ env.CHANGELOG_FILENAME }}
          path: .

      - name: Download msrv-badge.svg
        uses: actions/download-artifact@v2
        with:
          name: msrv-badge.svg
          path: .github/assets

      #- name: Check repo
      #  run: |
      #    ls
      #    git status

      - name: Commit any updates to ${{ env.CHANGELOG_FILENAME }}
        id: auto-commit-action
        uses: stefanzweifel/git-auto-commit-action@v4.14.1
        with:
          commit_message: "Updating ${{ env.CHANGELOG_FILENAME }} and/or MSRV badge [actions skip]"
          commit_user_name: ${{ env.GIT_BOT_USERNAME }}
          commit_user_email: ${{ env.GIT_BOT_EMAIL }}
          commit_author: ${{ env.GIT_BOT_USERNAME }} <${{ env.GIT_BOT_EMAIL }}>

      # Make sure labels are created first
      # If you're using bors, remember to add your user as a reviewer
      # Note: You can only add reviewer if `Synchronize` is None.
      - name: Wait, then restart Bors and label PR
        if: ${{ steps.auto-commit-action.outputs.changes_detected == 'true' }}
        env:
          GH_TOKEN: ${{ steps.app.outputs.token }}
        run: |
          printf "Changelog and/or MSRV badge was updated.\n\nBors will cancel job, but no action is required. Job will be restarted." | gh pr comment ${{ needs.get_pr_info.outputs.pr_number }} --body-file -
          sleep ${{ env.RESTART_PR_DELAY }}
          gh pr edit ${{ needs.get_pr_info.outputs.pr_number }} --add-label ${{ env.ADD_PR_LABELS }}
          printf "Updated changelog before staging build.\nbors r+" | gh pr comment ${{ needs.get_pr_info.outputs.pr_number }} --body-file -