|
23 | 23 | user_collabo_url = "https://api.github.com/repos/%s/%s/collaborators/%s" |
24 | 24 | issue_url = "https://api.github.com/repos/%s/%s/issues/%s" |
25 | 25 | issue_labels_url = "https://api.github.com/repos/%s/%s/issues/%s/labels" |
| 26 | +commit_search_url = "https://api.github.com/search/commits?q=repo:%s/%s+author:%s" |
26 | 27 |
|
27 | 28 | welcome_with_reviewer = '@%s (or someone else)' |
28 | 29 | welcome_without_reviewer = "@nrc (NB. this repo may be misconfigured)" |
@@ -165,24 +166,24 @@ def parse_header_links(value): |
165 | 166 |
|
166 | 167 | return links |
167 | 168 |
|
168 | | -def is_new_contributor(username, owner, repo, user, token, config): |
169 | | - if 'contributors' in config and username in config['contributors']: |
| 169 | +def is_new_contributor(username, owner, repo, user, token, payload): |
| 170 | + # If this is a fork, we do not treat anyone as a new user. This is |
| 171 | + # because the API endpoint called in this function indicates all |
| 172 | + # users in repository forks have zero commits. |
| 173 | + if payload['repository']['fork']: |
170 | 174 | return False |
171 | 175 |
|
172 | | - # iterate through the pages to try and find the contributor |
173 | | - url = contributors_url % (owner, repo) |
174 | | - while True: |
175 | | - stats_raw = api_req("GET", url, None, user, token) |
176 | | - stats = json.loads(stats_raw['body']) |
177 | | - links = parse_header_links(stats_raw['header'].get('Link')) |
178 | | - |
179 | | - for contributor in stats: |
180 | | - if contributor['login'] == username: |
181 | | - return False |
182 | | - |
183 | | - if not links or 'next' not in links: |
184 | | - return True |
185 | | - url = links['next'] |
| 176 | + try: |
| 177 | + result = api_req( |
| 178 | + 'GET', commit_search_url % (owner, repo, username), None, user, token, |
| 179 | + 'application/vnd.github.cloak-preview' |
| 180 | + ) |
| 181 | + return json.loads(result['body'])['total_count'] > 0 |
| 182 | + except urllib2.HTTPError, e: |
| 183 | + if e.code == 422: |
| 184 | + return False |
| 185 | + else: |
| 186 | + raise e |
186 | 187 |
|
187 | 188 | # If the user specified a reviewer, return the username, otherwise returns None. |
188 | 189 | def find_reviewer(msg): |
@@ -374,7 +375,7 @@ def new_pr(payload, user, token): |
374 | 375 |
|
375 | 376 | set_assignee(reviewer, owner, repo, issue, user, token, author, to_mention) |
376 | 377 |
|
377 | | - if is_new_contributor(author, owner, repo, user, token, config): |
| 378 | + if is_new_contributor(author, owner, repo, user, token, payload): |
378 | 379 | post_comment(welcome_msg(reviewer, config), owner, repo, issue, user, token) |
379 | 380 | elif post_msg: |
380 | 381 | post_comment(review_msg(reviewer, author), owner, repo, issue, user, token) |
|
0 commit comments