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

[feature request] move linked issues together with the pull request #50

Closed
FortitudeIT opened this issue Aug 17, 2022 · 16 comments · Fixed by #59
Closed

[feature request] move linked issues together with the pull request #50

FortitudeIT opened this issue Aug 17, 2022 · 16 comments · Fixed by #59

Comments

@FortitudeIT
Copy link

Describe the bug
I am trying to move an issue to "In review" on the project board, but it's not moving. My guess is that it's because the PR isn't the same as the issue id?

To Reproduce
Steps to reproduce the behavior:

  1. Define the workflow steps with values like
name: Project automations
on:
  issues:
    types:
      - opened
      - reopened
      - closed
  pull_request:
    types:
      - opened
      - reopened
      - review_requested
      - closed

# map fields with customized labels
env:
  backlog: Backlog
  in_progress: 🏗 In progress
  testing: Testing
  in_review: In review
  done: Done ✅

jobs:
  pr_opened_or_reopened_or_reviewrequested:
    name: pr_opened_or_reopened_or_reviewrequested
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'review_requested')
    steps:
      - name: Move PR to ${{ env.testing }}
        uses: leonsteinhaeuser/[email protected]
        with:
          gh_token: xxxx
          organization: abc
          project_id: 3
          resource_node_id: ${{ github.event.pull_request.node_id }}
          status_value: ${{ env.testing }} # Target status

Expected behavior
The issue is moved to the "In review" lane.

@FortitudeIT FortitudeIT added the bug Something isn't working label Aug 17, 2022
@leonsteinhaeuser
Copy link
Owner

Your guess is correct. The issue ID is not the same as the pull request ID. You can take a look at the workflow file that manages this repository:

https://github.com/leonsteinhaeuser/project-beta-automations/blob/main/.github/workflows/project_automations.yml

@FortitudeIT
Copy link
Author

So is there any way we could move an issue like this?

@leonsteinhaeuser
Copy link
Owner

I don't know what the pull_request context contains in terms of information, but maybe it is possible to get the linked issue via that context.

@FortitudeIT
Copy link
Author

They're linked in URLs, but I don't think they're in the response itself, so you'd have to do another API call.

https://docs.github.com/en/rest/pulls/pulls#list-pull-requests

@leonsteinhaeuser
Copy link
Owner

Thanks for investigating the API. This basically allows us to scrape the attached issues from the PR. I'm not sure if this should be covered in this automation, but it could be an interesting feature. If you want, I can suggest a solution that does not require changing the automation.

Otherwise, it will take some time because GitHub has changed the project API, forcing me to basically rewrite some things (v2). After this rewrite is done, I could start implementing this functionality.

@FortitudeIT
Copy link
Author

Either is good for me :)

@leonsteinhaeuser
Copy link
Owner

@FortitudeIT I invested some time in the past day's, but couldn't fight a reliable solution so far. Going to investigate it further, unless you found a solution.

@anrico-oltmanns
Copy link

@leonsteinhaeuser no solution found yet? Having the same issue. There has to be a way to move issues and not only pull requests right?

@leonsteinhaeuser
Copy link
Owner

@anrico-oltmanns I have already invested some time to find a solution to this problem. So far I have not had any luck. The problem is that the links I want are not included in the event response I received from the server (ci-workflow). The only solution I have left is to create more API requests and hope that the response will contain the necessary information.

When I have some time, I will take another look and implement this feature.

@anrico-oltmanns
Copy link

Thank you. I Would highly appreciate if you could comment on this discussion again and tag me when found a solution for that problem.

@leonsteinhaeuser leonsteinhaeuser changed the title [BUG] [feature request] move linked issues together with the pull request Nov 23, 2022
@leonsteinhaeuser
Copy link
Owner

Status update:

The following two files contain the response that the GitHub server sends when I request the value for a particular pull request.

The first one contains the link to the issue in the body field (plain text), the second one does not contain the information at all.

pr_linked_with_comment.json.log
pr_linked_without_comment.json.log

I will investigate this further.

@leonsteinhaeuser leonsteinhaeuser added feature request and removed bug Something isn't working labels Nov 30, 2022
@mustafaozhan
Copy link

mustafaozhan commented Dec 19, 2022

@leonsteinhaeuser I am really interested in this automation also, ie. I want my issues to be moved into PR Review column when there is a PR created, and it has in his body the issue_id with an issue link keyword, ie Resolves #ID_OF_ISSUE

I found this action: https://github.com/1natsu172/github-action-auto-move-related-issue
but unfortunately it works only in old github projects, maybe it can help you to look at the implementation. I created an issue there, but it seems the project is discontinued.

@leonsteinhaeuser
Copy link
Owner

leonsteinhaeuser commented Feb 19, 2023

Hello everyone,
I'm sorry it took me so long to respond. In the last few days I had some time to study GitHub's GraphQL API. In doing so, I found out that GitHub supports the "ClosingIssuesReferences" object reference in pull requests.

This functionality would allow me to simply select the linked issues that would be closed by the desired pull request. A list of keywords for closing can be found here.

In the next few days I will create a first version that we can test to see how it works.

Example GraphQL code:

    local REPO=$2
    local OWNER=$3
    gh api graphql -f query='
    query($owner: String!, $repo: String!) {
      repository(owner: $owner, name: $repo) {
        pullRequest(number: 21) {
          id
          closingIssuesReferences(first: 100) {
            nodes {
                id
                number
                title
            }
            totalCount
          }
        }
      }
    }' -f owner=$OWNER -f repo=$REPO

Response:

{
  "data": {
    "repository": {
      "pullRequest": {
        "id": "PR_kwDOGWypss5KPRmJ",
        "closingIssuesReferences": {
          "nodes": [
            {
              "id": "I_kwDOGWypss5exRM8",
              "number": 28,
              "title": "[BUG]: 1"
            }
          ],
          "totalCount": 1
        }
      }
    }
  }
}

@leonsteinhaeuser
Copy link
Owner

Can someone please verify that the new behavior works as you would expect?

You can test this by defining the workflow as follows:

  pr_closed:
    name: pr_closed
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    env:
      done: "Done"
    steps:
      - name: Move PR to ${{ env.done }}
        uses: leonsteinhaeuser/project-beta-automations@feat/move-related-issues
        with:
          gh_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          user: sample-user
          # organization: sample-org
          project_id: 1
          resource_node_id: ${{ github.event.pull_request.node_id }}
          status_value: ${{ env.done }}
          move_related_issues: true

Once you specify one of the GitHub link options, the issue should be moved along with the pull request.

@mustafaozhan
Copy link

mustafaozhan commented Feb 23, 2023

Hello @leonsteinhaeuser

I confirm that it works!

As an example, I put my PR changes here: https://github.com/Oztechan/CCC/pull/2099/files

I was able to move related issue to

  • PR Review when PR is opened || ready_for_review || reopened
  • In Progress when PR is converted_to_draft
  • Done when PR is closed and merged

Last one is important, we should not invoke it only when is closed, but we need to check if it is merged or not. I check the documentation and found best way to do it is:

if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true

@leonsteinhaeuser
Copy link
Owner

@mustafaozhan thanks for the confirmation. I have merged the pull request and created a new release https://github.com/leonsteinhaeuser/project-beta-automations/releases/tag/v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants