0

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
    }
]
Vivern202
  • 29
  • 3
  • 7
  • Remove space before and after `=`. – Cyrus Nov 14 '21 at 10:07
  • @Cyrus yes, thank you, but still if I do select(.user.login=="$1") It prints nothing, but I if I try this select(.user.login=="someone_nickname") it works. How can use parametr to select? – Vivern202 Nov 14 '21 at 10:22
  • @Vivern202: See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) and [Passing bash variable to jq](https://stackoverflow.com/q/40027395/3776858) – Cyrus Nov 14 '21 at 10:28

1 Answers1

3

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'
Arnaud Valmary
  • 2,039
  • 9
  • 14
  • Thank you! How this script will be modified if there more than 100 pul-repuests? – Vivern202 Nov 14 '21 at 11:20
  • You can't retrieve more than 100 pull-request one time but you can select the page with `&page=n` where `n` is a number. By default `n` is equal to `1` – Arnaud Valmary Nov 14 '21 at 16:07