0

I would like to automatically close the issue once it is created. For this I referred to Actions marketplace and this action. Currently my code in YAML file looks like below. GitHub token is saved in Actions >> Secrets sections, and it is Private repository. Every time I run below code I am receiving this error GraphQL: Could not resolve to an issue or pull request with the number of 1. (repository.issue). I do not understand why workflow is complaining about Could not resolve an issue? Any setting I need to perform in my GitHub repo for issue to be close automatically?

name: CI
on:
  issues:
    types:
      - opened
jobs:
  titlePrefixCheck:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set repo
        run: gh repo set-default user/private-repo

      - name: Close Issue
        run: gh issue close --comment "Auto-closing issue" "1"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

2 Answers 2

2

GraphQL: Could not resolve to an issue or pull request with the number of 1. (repository.issue)

The number "1" is not a valid issue number in that repository.

The issues trigger's webhook payload has issue.number that'll give the issue number of the newly opened issue. So, you can use github.event.issue.number to refer to that.

Example:

name: close-issue-on-open

on:
  issues:
    types: [opened]

jobs:
  close-issue:
    runs-on: ubuntu-latest

    permissions:
      issues: write

    steps:
    - name: Close
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        REPO: ${{ github.repository }}
        ISSUE: ${{ github.event.issue.number }}
      run: gh issue close --repo "$REPO" --comment "Autoclosing issue $ISSUE" "$ISSUE"

For your described use case, the checkout step is not needed at all. You can simply specify the repository with gh issue close command using -R/--repo flag and that should be enough.

Depending on your GITHUB_TOKEN's permissions, you may need issues:write permission to make this work as specified in the above workflow. See Permissions for the GITHUB_TOKEN for more details on this. For example, in the absence of issues:write permission, you'll see an error like this:

GraphQL: Resource not accessible by integration (addComment)

and, adding this permission i.e.:

permissions:
  issues: write

should resolve this.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you but still receiving same error. It is capturing Issue number correctly but not closing Issue, Please note yml file is in main branch: GraphQL: Could not resolve to an issue or pull request with the number of 32. (repository.issue) Error: Process completed with exit code 1.
@biggboss2019: You're welcome! I've updated my answer for granular permissions. Did that error in your comment occur with correct issue number?
that is correct. Correct issue number was captured. Accepted the answer. Below solution will work as well. :)
0

For Future Stackoverflow reader- The way I solved my issue that I mention to Azeem in comment section was by updating Workflow permissions under Settings >> Actions >> General >> Workflow permissions from Read repository contents and packages permissions to Read and write permissions enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.