git-point icon indicating copy to clipboard operation
git-point copied to clipboard

Issues and Pull Requests count is wrong

Open housseindjirdeh opened this issue 8 years ago • 11 comments

image

The react repository definitely has more than 20 open and closed issues :). If the total number of issues and pull requests aren't easily retrievable from the API - we should probably just remove it.

housseindjirdeh avatar Jul 24 '17 04:07 housseindjirdeh

Looks like this is because we're not taking into account the pagination. To deal with this, let's use the open_issues property from the repo API (https://api.github.com/repos/gitpoint/git-point). Note this will mean getting rid of closed issues because GitHub doesn't give us that count.

andrewda avatar Jul 24 '17 05:07 andrewda

That api endpoint has open_issues_count and open_issues. In our case, the numbers are the same. Is there any case in which they would be different?

SammyIsra avatar Jul 24 '17 07:07 SammyIsra

Looking at this we could fetch https://api.github.com/repos/gitpoint/git-point/issues?state=closed&per_page=21 and count them. If the count is 21, we can just show 20+. Otherwise, we can just show the count. (or any other number would do.

I don't think per_page has an upper limit, so technically we can use any number, but maybe we don't want to overfetch info.

SammyIsra avatar Jul 24 '17 07:07 SammyIsra

@SammyIsra Actually here's a fun trick:

Make a request to https://api.github.com/repos/gitpoint/git-point/issues?state=closed&per_page=1&page=1. This shows us the most recently closed issue (or opened if state=open) from the repository, but that's not what we're interested in. We'll receive a Link header that looks something like this:

Link:<https://api.github.com/repositories/86202845/issues?state=closed&per_page=1&page=2>; rel="next", <https://api.github.com/repositories/86202845/issues?state=closed&per_page=1&page=102>; rel="last"

Note this part:

<https://api.github.com/repositories/86202845/issues?state=closed&per_page=1&page=102>; rel="last"

That's the link to the last page using the same per_page as we defined earlier. As we said only 1 item per page, we can parse the URL to determine that there are 102 closed issues in this repository. Sadly this endpoint combines issues and pull requests, though, so we're going to have to do the same thing with pull requests and subtract that number to get the total closed issues.

I think that's a little confusing (and I just woke up so I feel like I'm typing nonsense) so here's a quick TL;DR/summary:

  1. Make a request to https://api.github.com/repos/gitpoint/git-point/issues?state=closed&per_page=1&page=1.
  2. Determine total number of closed issues/pull requests from the last page returned in the Link header.
  3. Make another request to https://api.github.com/repos/gitpoint/git-point/pulls?state=closed&per_page=1&page=1.
  4. Determine total number of closed pull requests from the last page returned in the Link header.
  5. closed_issues = closed_issues_and_prs - closed_prs

Repeat for open issues and PR's. This method takes 4 requests and should work well regardless of the total number of issues. Just tested on tensorflow/tensorflow and it works flawlessly, even with 6,500 closed issues and 4,180 closed PR's.

andrewda avatar Jul 24 '17 16:07 andrewda

You really are a wizard @andrewda, 4 requests is a bit more than I would love but I think that's definitely worth it to get that information. This is great thank you so much.

housseindjirdeh avatar Jul 24 '17 18:07 housseindjirdeh

For future GraphQL reference, here's how we'd get the first 3 issues on the repository screen, along with the total number of open/closed issues:

{
  repository(owner: "gitpoint", name: "git-point") {
    open: issues(first: 3, states: OPEN) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
    closed: issues(first: 3, states: CLOSED) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
  }
}

And for pull requests:

{
  repository(owner: "gitpoint", name: "git-point") {
    open: pullRequests(first: 3, states: OPEN) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
    closed: pullRequests(first: 3, states: [CLOSED, MERGED]) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
  }
}

Edit: and both of them combined looks like this:

{
  repository(owner: "gitpoint", name: "git-point") {
    openIssues: issues(first: 3, states: OPEN) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
    closedIssues: issues(first: 3, states: CLOSED) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
    openPullRequests: pullRequests(first: 3, states: OPEN) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
    closedPullRequests: pullRequests(first: 3, states: [CLOSED, MERGED]) {
      totalCount
      edges {
        node {
          title
        }
      }
    }
  }
}

andrewda avatar Jul 24 '17 18:07 andrewda

I love GraphQL now :joy: This simplifies things a ton.

andrewda avatar Jul 24 '17 18:07 andrewda

Amazing, queries look so nice 😍

I think this (and #46) is a great place to start introducing GraphQL to the app

housseindjirdeh avatar Jul 25 '17 14:07 housseindjirdeh

Awesome @housseindjirdeh! I have a graphql branch made where we can start working on this and merge it into master when we're satisfied :)

andrewda avatar Jul 25 '17 16:07 andrewda

@andrewda yes that would be amazing <3

housseindjirdeh avatar Jul 25 '17 22:07 housseindjirdeh

There are no issues/pull requests count on UI now. Shall we add them back?

chinesedfan avatar Jan 05 '19 14:01 chinesedfan