Search code examples
githubgithub-actions

How to convert a pull-request to draft with github action


I am trying to convert a pull-request back to a draft, using github actions and gh.

But my action always fails when converting an open PR back to draft, with the log:

API call failed: GraphQL: github-actions[bot] does not have permission to convert the pull request PR_somenumbers to draft. (convertPullRequestToDraft)

Info: At the time writing this question, GitHub faced an outage. I tested again now one day later and the error still exists. So it is not related to the GitHub outage.

My actions looks (trimmed) like so:

  1. On a new opened pull request do:
  2. checkout the repo
  3. Get the base branch of this pull request
  4. Search for open pull-requests, that have this base branch from 3. as head
  5. Iterate through the open pull-requests from 4. and if the base of this PR is my main branch, then:
  6. Set the PR status to 'draft' <- This is not working
    name: Bot
    on:
      pull_request:
        types:
          - opened
    jobs:
      comment:
        runs-on: ubuntu-latest
        env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            MAIN_BRANCH_NAME: 'main'
            BASE_PR_REF: ${{ github.base_ref }}
        steps:
          - name: Check out repository code
            uses: actions/checkout@v3
            with:
              ref: ${{ github.event.pull_request.head.ref }}
          - run: |
              for base_pr_id in $(gh pr list --search "head:${{ env.BASE_PR_REF }}" --json number --jq '.[].number'); do 
                  if [ ${{ env.MAIN_BRANCH_NAME }} == $( gh pr view $base_pr_id --json baseRefName  --jq '.baseRefName') ]; then
                      gh pr ready $base_pr_id --undo <-- This won't work
                  fi
                  
              done

I also tried to add specific permissions, but none of them worked:

    permissions: 
        pull-requests: write
        contents: write
        statuses: write

I also gave the actions read and write permissions in GitHub Settings. No success. Any idea is highly welcome.


Solution

  • Not every pull request on Github can be changed to a draft. For example free accounts on private repositories don't have the option to put a pull request into draft status.

    So if that is the case (private repository which does not allow drafts on pull requests), consider to upgrade the account or first of all create a public test repository for sandboxing as public repositories should allow draft status on pull requests.

    This is easy to forget once the draft option is considered to be available.

    Quoting from the OPs' comment, user JacksOnF1re, who confirms the educated guess:

    [...] I wasn't aware that private repos (not company) don't support drafts as a feature. And guess what, I changed it to be public and now it works. [...]