Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround until will be merged: https://github.com/actions/runner/pull/1684 #159

Open
2 tasks
github-actions bot opened this issue Oct 29, 2023 · 0 comments
Open
2 tasks
Assignees
Labels
bug Something isn't working todo

Comments

@github-actions
Copy link
Contributor

github-actions bot commented Oct 29, 2023

Tasks

Preview Give feedback

https://api.github.com/hoverkraft-tech/ci-github-common/blob/31ca94a6a841f74bb795817a3ae58210e00fd1c2/actions/create-and-merge-pull-request/action.yml#L30

name: "Create and merge Pull Request"
description: "Action to create and merge Pull Request. Opinionated, set GitHub Actions bot as author, then rebase and merge."
author: Hoverkraft
branding:
  icon: git-pull-request
  color: gray-dark

inputs:
  github-token:
    description: "GitHub token for creating and merging pull request (permissions contents: write and pull-requests: write). See [https://github.com/peter-evans/create-pull-request#action-inputs](https://github.com/peter-evans/create-pull-request#action-inputs)"
    default: ${{ github.token }}
    required: false
  branch:
    description: "The pull request branch name"
    required: true
  title:
    description: "The pull request title"
    required: true
  body:
    description: "The pull request body"
    required: true
  commit-message:
    description: "The commit message for the pull request"
    required: true

runs:
  using: "composite"
  steps:
    - shell: bash
      # FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
      run: mkdir -p ./self-actions/ && cp -r $GITHUB_ACTION_PATH/../* ./self-actions/

    - id: github-actions-bot-user
      uses: ./self-actions/get-github-actions-bot-user

    - shell: bash
      # FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
      run: |
        rm -fr ./self-actions

    - uses: peter-evans/[email protected]
      id: create-pull-request
      with:
        token: ${{ inputs.github-token }}
        signoff: true
        base: ${{ github.event.repository.default_branch }}
        delete-branch: true
        branch: ${{ inputs.branch }}
        title: ${{ inputs.title }}
        body: ${{ inputs.body }}
        commit-message: ${{ inputs.commit-message }}
        author: ${{ steps.github-actions-bot-user.outputs.name }} <${{ steps.github-actions-bot-user.outputs.email }}>
        committer: ${{ steps.github-actions-bot-user.outputs.name }} <${{ steps.github-actions-bot-user.outputs.email }}>

    - id: wait-for-pull-request-mergeable-by-admin
      if: steps.create-pull-request.outputs.pull-request-number && steps.create-pull-request.outputs.pull-request-operation != 'closed'
      uses: actions/github-script@v6
      with:
        script: |
          let attemps = 0;
          const maxAttemps = 10;
          while (attemps < maxAttemps) {
            const { data: { mergeable, mergeable_state } } = await github.rest.pulls.get({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: ${{ steps.create-pull-request.outputs.pull-request-number }},
            });

            if (mergeable === true) {
              core.setOutput('is-mergeable', true);
              return;
            }

            core.debug(`Pull request is not mergeable, mergeable_state: ${mergeable_state}`);

            await new Promise(resolve => setTimeout(resolve, 5000));
            attemps++;
          }

          core.error('Pull request is not mergeable');

    - name: Merge pull request
      if: steps.wait-for-pull-request-mergeable-by-admin.outputs.is-mergeable
      shell: bash
      run: |
        set +e

        ATTEMPS=0
        MAX_ATTEMPS=10
        REQUIRED_WORKFLOWS_ERROR="Required workflow"

        while [ $ATTEMPS -lt $MAX_ATTEMPS ]; do
          echo "::debug::Merging pull request #${{ steps.create-pull-request.outputs.pull-request-number }} for repository ${{ github.repository }}..."
          MERGE_OUTPUTS=$(gh pr merge -R "${{ github.repository }}" --rebase --admin "${{ steps.create-pull-request.outputs.pull-request-number }}" 2>&1)
          MERGE_EXIT_CODE=$?
          echo "::debug::Merge outputs: $MERGE_OUTPUTS"
          echo "::debug::Merge exit code: $MERGE_EXIT_CODE"

          if [ "$MERGE_EXIT_CODE" = "0" ]; then
            exit 0
          fi

          if [[ "$MERGE_OUTPUTS" != *"$REQUIRED_WORKFLOWS_ERROR"* ]]; then
            echo "::error::Failed to merge pull request: $MERGE_OUTPUTS"
            exit 1
          fi

          echo "::debug::Pull request is not mergeable yet because some of required workflow check issues, retrying in 5 seconds..."
          sleep 5
          ATTEMPS=$((ATTEMPS+1))
        done

        echo "::error::Failed to merge pull request after $MAX_ATTEMPS attemps: $MERGE_OUTPUTS"
        exit 1
      env:
        GH_TOKEN: ${{ inputs.github-token }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working todo
Projects
None yet
Development

No branches or pull requests

1 participant