Search code examples
bashcurlsyntaxjqcommand-substitution

How to count amount of pull request made by user?


I have bash sript pull.sh with one argument is github nickname of a person. And I want to count amount of all pull request which was made with this nickname. I select pr this way in command line:

curl -s https://api.github.com/repos/owner/repo/pulls?"state=all&per_page=100" | jq '.[] | select(.user.login=="nickname") | .user.login'

And this work well. But how can I count how much values was selected?

In bash I am trying this:

amount_of_pr=$(curl -s https://api.github.com/repos/owner/repo/pulls?"state=all&per_page=100" | jq '.[] | select(.user.login=="$1") | .user.login')
echo "$amount_of_pr"

But it doesnt print anything.

Another problem is that amount of pull requests in repo is much bigger than 100. And I need to search through all of them. How can I do it?

Example of curl output

[
  {
    "url": "https://api.github.com/repos/datamove/linux-git2/pulls/315",
    "id": 771895341,
    "node_id": "PR_kwDOEh6nts4uAjAt",
    "html_url": "https://github.com/datamove/linux-git2/pull/315",
    "diff_url": "https://github.com/datamove/linux-git2/pull/315.diff",
    "patch_url": "https://github.com/datamove/linux-git2/pull/315.patch",
    "issue_url": "https://api.github.com/repos/datamove/linux-git2/issues/315",
    "number": 315,
    "state": "open",
    "locked": false,
    "title": "hw git2 nicknazarov",
    "user": {
      "login": "nicknazarov",
      "id": 16031089,
      "node_id": "MDQ6VXNlcjE2MDMxMDg5",
      "avatar_url": "https://avatars.githubusercontent.com/u/16031089?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/nicknazarov",
      "html_url": "https://github.com/nicknazarov",
      "followers_url": "https://api.github.com/users/nicknazarov/followers",
      "following_url": "https://api.github.com/users/nicknazarov/following{/other_user}",
      "gists_url": "https://api.github.com/users/nicknazarov/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/nicknazarov/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/nicknazarov/subscriptions",
      "organizations_url": "https://api.github.com/users/nicknazarov/orgs",
      "repos_url": "https://api.github.com/users/nicknazarov/repos",
      "events_url": "https://api.github.com/users/nicknazarov/events{/privacy}",
      "received_events_url": "https://api.github.com/users/nicknazarov/received_events",
      "type": "User",
      "site_admin": false
    }
]

Solution

  • You could use jq --arg ... option to pass values and build a valid jq request like this:

    curl -s \
      -H "Accept: application/vnd.github.v3+json" \
      --url "https://api.github.com/repos/datamove/linux-git2/pulls?state=all&per_page=100" \
    | jq \
      --arg user_login "$1" \
      '.[] | select(.user.login==$user_login) | .user.login' \
    | wc -l
    

    UPDATE

    Full jq solution:

    curl \
      -s \
      -H "Accept: application/vnd.github.v3+json" \
      --url "https://api.github.com/repos/datamove/linux-git2/pulls?state=all&per_page=100" \
    | jq \
      --arg user_login "$1" \
      '[select(.[].user.login == $user_login)] | length'