如何计算用户提出的拉取请求数量?
How to count amount of pull request made by user?
我有 bash 脚本 pull.sh 其中一个参数是 github 一个人的昵称。我想计算用这个昵称发出的所有拉取请求的数量。我在命令行中 select pr 是这样的:
curl -s https://api.github.com/repos/owner/repo/pulls?"state=all&per_page=100" | jq '.[] | select(.user.login=="nickname") | .user.login'
这很好用。
但是我如何计算 selected 了多少值?
在 bash 我正在尝试这个:
amount_of_pr=$(curl -s https://api.github.com/repos/owner/repo/pulls?"state=all&per_page=100" | jq '.[] | select(.user.login=="") | .user.login')
echo "$amount_of_pr"
但它不打印任何东西。
另一个问题是回购中的拉取请求数量远远大于 100。我需要搜索所有这些。我该怎么做?
curl 输出示例
[
{
"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
}
]
您可以使用 jq --arg ...
选项传递值并构建有效的 jq
请求,如下所示:
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 "" \
'.[] | select(.user.login==$user_login) | .user.login' \
| wc -l
更新
完整 jq
解决方案:
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 "" \
'[select(.[].user.login == $user_login)] | length'
我有 bash 脚本 pull.sh 其中一个参数是 github 一个人的昵称。我想计算用这个昵称发出的所有拉取请求的数量。我在命令行中 select pr 是这样的:
curl -s https://api.github.com/repos/owner/repo/pulls?"state=all&per_page=100" | jq '.[] | select(.user.login=="nickname") | .user.login'
这很好用。 但是我如何计算 selected 了多少值?
在 bash 我正在尝试这个:
amount_of_pr=$(curl -s https://api.github.com/repos/owner/repo/pulls?"state=all&per_page=100" | jq '.[] | select(.user.login=="") | .user.login')
echo "$amount_of_pr"
但它不打印任何东西。
另一个问题是回购中的拉取请求数量远远大于 100。我需要搜索所有这些。我该怎么做?
curl 输出示例
[
{
"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
}
]
您可以使用 jq --arg ...
选项传递值并构建有效的 jq
请求,如下所示:
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 "" \
'.[] | select(.user.login==$user_login) | .user.login' \
| wc -l
更新
完整 jq
解决方案:
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 "" \
'[select(.[].user.login == $user_login)] | length'