Skip to content

Commit d76b226

Browse files
authored
Merge pull request #37 from chartgerink/fix/36
Add rate limit checking
2 parents 0d98aa8 + 373d29d commit d76b226

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

NEWS.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
# Development
2+
<<<<<<< fix/36 -- Incoming Change
3+
4+
## Minor changes
5+
6+
- Add a rate limit checker for the GitHub API calls. Will warn if rate limit is exceeded.
7+
8+
---
9+
10+
# v 0.1.1
11+
=======
12+
>>>>>>> main -- Current Change
213
314
## Minor changes
415

R/github.R

+23
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ get_gh_issue_people <- function (org, repo,
293293
repo = repo,
294294
end_cursor = end_cursor
295295
)
296+
297+
check_rate_limit()
296298
dat <- gh::gh_gql (q)
297299

298300
has_next_page <- dat$data$repository$issues$pageInfo$hasNextPage
@@ -443,6 +445,7 @@ get_gh_issue_titles <- function (org, repo) {
443445
repo = repo,
444446
end_cursor = end_cursor
445447
)
448+
check_rate_limit()
446449
dat <- gh::gh_gql (q)
447450

448451
has_next_page <- dat$data$repository$issues$pageInfo$hasNextPage
@@ -534,3 +537,23 @@ get_gh_contrib_issue <- function (org, repo) {
534537

535538
unlist (pings)
536539
}
540+
541+
#' Check the GitHub rate limit and warn if exceeded.
542+
#'
543+
#' @noRd
544+
check_rate_limit <- function () {
545+
gh_state <- gh::gh_rate_limit()
546+
limit_warn <- 0.1
547+
warn_txt <- NULL
548+
if (gh_state$remaining / gh_state$limit < limit_warn) {
549+
warn_txt <- "You have used > 90% of your GitHub calls..."
550+
} else if (gh_state$remaining == 0) {
551+
warn_txt <- "The GitHub rate limit has been reached..."
552+
}
553+
if (!is.null (warn_txt)) {
554+
cli::cli_alert_warning (sprintf(
555+
"%s It resets in ~%s minutes.",
556+
warn_txt,
557+
ceiling(difftime(gh_state$reset, Sys.time(), units = "mins"))))
558+
}
559+
}

tests/testthat/getcontribs/api.github.com/graphql-8cf72c-POST.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"issues": {
55
"pageInfo": {
66
"hasNextPage": false,
7-
"endCursor": "Y3Vyc29yOnYyOpHObs1_rQ=="
7+
"endCursor": "Y3Vyc29yOnYyOpK5MjAyMy0wOC0yMVQxMToyMDozMiswMjowMM5uzX-t"
88
},
99
"edges": [
1010
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"resources": {
3+
"core": {
4+
"limit": 5000,
5+
"used": 22,
6+
"remaining": 4978,
7+
"reset": 1710753401
8+
},
9+
"search": {
10+
"limit": 30,
11+
"used": 0,
12+
"remaining": 30,
13+
"reset": 1710752737
14+
},
15+
"graphql": {
16+
"limit": 5000,
17+
"used": 9,
18+
"remaining": 4991,
19+
"reset": 1710754258
20+
},
21+
"integration_manifest": {
22+
"limit": 5000,
23+
"used": 0,
24+
"remaining": 5000,
25+
"reset": 1710756277
26+
},
27+
"source_import": {
28+
"limit": 100,
29+
"used": 0,
30+
"remaining": 100,
31+
"reset": 1710752737
32+
},
33+
"code_scanning_upload": {
34+
"limit": 1000,
35+
"used": 0,
36+
"remaining": 1000,
37+
"reset": 1710756277
38+
},
39+
"actions_runner_registration": {
40+
"limit": 10000,
41+
"used": 0,
42+
"remaining": 10000,
43+
"reset": 1710756277
44+
},
45+
"scim": {
46+
"limit": 15000,
47+
"used": 0,
48+
"remaining": 15000,
49+
"reset": 1710756277
50+
},
51+
"dependency_snapshots": {
52+
"limit": 100,
53+
"used": 0,
54+
"remaining": 100,
55+
"reset": 1710752737
56+
},
57+
"audit_log": {
58+
"limit": 1750,
59+
"used": 0,
60+
"remaining": 1750,
61+
"reset": 1710756277
62+
},
63+
"code_search": {
64+
"limit": 10,
65+
"used": 0,
66+
"remaining": 10,
67+
"reset": 1710752737
68+
}
69+
},
70+
"rate": {
71+
"limit": 5000,
72+
"used": 22,
73+
"remaining": 4978,
74+
"reset": 1710753401
75+
}
76+
}

tests/testthat/getcontribs/api/contributors-b0fb04.json

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"received_events_url": "https://api.github.com/users/mpadge/received_events",
1919
"type": "User",
2020
"site_admin": false,
21-
"contributions": 213
21+
"contributions": 214
2222
},
2323
{
2424
"login": "daniellemccool",
@@ -40,5 +40,26 @@
4040
"type": "User",
4141
"site_admin": false,
4242
"contributions": 1
43+
},
44+
{
45+
"login": "olivroy",
46+
"id": 52606734,
47+
"node_id": "MDQ6VXNlcjUyNjA2NzM0",
48+
"avatar_url": "https://avatars.githubusercontent.com/u/52606734?v=4",
49+
"gravatar_id": "",
50+
"url": "https://api.github.com/users/olivroy",
51+
"html_url": "https://github.com/olivroy",
52+
"followers_url": "https://api.github.com/users/olivroy/followers",
53+
"following_url": "https://api.github.com/users/olivroy/following{/other_user}",
54+
"gists_url": "https://api.github.com/users/olivroy/gists{/gist_id}",
55+
"starred_url": "https://api.github.com/users/olivroy/starred{/owner}{/repo}",
56+
"subscriptions_url": "https://api.github.com/users/olivroy/subscriptions",
57+
"organizations_url": "https://api.github.com/users/olivroy/orgs",
58+
"repos_url": "https://api.github.com/users/olivroy/repos",
59+
"events_url": "https://api.github.com/users/olivroy/events{/privacy}",
60+
"received_events_url": "https://api.github.com/users/olivroy/received_events",
61+
"type": "User",
62+
"site_admin": false,
63+
"contributions": 1
4364
}
4465
]

0 commit comments

Comments
 (0)