milestone_check.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Copyright (c) 2018 Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. # Generate a GitHub token at https://github.com/settings/tokens
  4. # Invoke this script using something like:
  5. # python scripts/milestone_check.py
  6. import subprocess
  7. import requests
  8. import os
  9. import sys
  10. ranges = {
  11. '0.35': 'origin/0.35.0 --not origin/0.34.x',
  12. '0.35.x': 'origin/0.35.x --not v0.35.0',
  13. '1.0': 'origin/1.0.x --not origin/0.35.x',
  14. '1.1': 'v1.1.0 --not origin/1.0.x',
  15. '1.1.1': 'v1.1.1 --not v1.1.0',
  16. '1.1.2': 'v1.1.2 --not v1.1.1',
  17. '1.1.3': 'v1.1.3 --not v1.1.2',
  18. '1.2': 'origin/1.x --not origin/1.1.x',
  19. '2.0': 'v2.0.0 --not origin/1.x',
  20. '2.0.1': 'v2.0.1 --not v2.0.0',
  21. '2.0.2': 'origin/2.0.x --not v2.0.1',
  22. '2.1': 'origin/2.1.x --not origin/2.0.x',
  23. '2.2': 'origin/2.2.x --not origin/2.1.x',
  24. '3.0': 'origin/master --not origin/2.2.x'
  25. }
  26. try:
  27. api_token = os.environ['GITHUB_TOKEN']
  28. except KeyError:
  29. print('Error: set the environment variable GITHUB_TOKEN to a GitHub authentication token (see https://github.com/settings/tokens)')
  30. exit(1)
  31. if len(sys.argv) != 2:
  32. print('Error: exactly one argument expected, the milestone.')
  33. exit(1)
  34. MILESTONE=sys.argv[1]
  35. if MILESTONE not in ranges:
  36. print('Error: I do not know about milestone %r. Possible milestones are %r'%(MILESTONE, list(ranges.keys())))
  37. exit(1)
  38. out = subprocess.run("git log {} --format='%H,%cE,%s'".format(ranges[MILESTONE]), shell=True, encoding='utf8', stdout=subprocess.PIPE)
  39. commits = {i[0]: (i[1], i[2]) for i in (x.split(',',2) for x in out.stdout.splitlines())}
  40. url = 'https://api.github.com/graphql'
  41. json = { 'query' : """
  42. query test($cursor: String) {
  43. search(first: 50, after: $cursor, type: ISSUE, query: "repo:jupyterlab/jupyterlab milestone:%s is:pr is:merged ") {
  44. issueCount
  45. pageInfo {
  46. endCursor
  47. hasNextPage
  48. }
  49. nodes {
  50. ... on PullRequest {
  51. title
  52. number
  53. mergeCommit {
  54. oid
  55. }
  56. commits(first: 100) {
  57. totalCount
  58. nodes {
  59. commit {
  60. oid
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. """%MILESTONE,
  69. 'variables': {
  70. 'cursor': None
  71. }
  72. }
  73. headers = {'Authorization': 'token %s' % api_token}
  74. # construct a commit to PR dictionary
  75. prs = {}
  76. large_prs = []
  77. cursor = None
  78. while True:
  79. json['variables']['cursor'] = cursor
  80. r = requests.post(url=url, json=json, headers=headers)
  81. results = r.json()['data']['search']
  82. total_prs = results['issueCount']
  83. pr_list = results['nodes']
  84. for pr in pr_list:
  85. if pr['commits']['totalCount'] > 100:
  86. large_prs.append(pr['number'])
  87. continue
  88. # TODO fetch commits
  89. prs[pr['number']] = {'mergeCommit': pr['mergeCommit']['oid'],
  90. 'commits': set(i['commit']['oid'] for i in pr['commits']['nodes'])}
  91. has_next_page = results['pageInfo']['hasNextPage']
  92. cursor = results['pageInfo']['endCursor']
  93. if not has_next_page:
  94. break
  95. prjson = {'query': """
  96. query test($pr:Int!, $cursor: String) {
  97. repository(owner: "jupyterlab", name: "jupyterlab") {
  98. pullRequest(number: $pr) {
  99. title
  100. number
  101. mergeCommit {
  102. oid
  103. }
  104. commits(first: 100, after: $cursor) {
  105. totalCount
  106. pageInfo {
  107. endCursor
  108. hasNextPage
  109. }
  110. nodes {
  111. commit {
  112. oid
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. """, 'variables': {
  120. 'pr': None,
  121. 'cursor': None
  122. }}
  123. for prnumber in large_prs:
  124. prjson['variables']['pr']=prnumber
  125. pr_commits = set()
  126. while True:
  127. r = requests.post(url=url, json=prjson, headers=headers)
  128. pr = r.json()['data']['repository']['pullRequest']
  129. assert pr['number']==prnumber
  130. total_commits = pr['commits']['totalCount']
  131. pr_commits.update(i['commit']['oid'] for i in pr['commits']['nodes'])
  132. has_next_page = results['pageInfo']['hasNextPage']
  133. cursor = results['pageInfo']['endCursor']
  134. if not pr['commits']['pageInfo']['hasNextPage']:
  135. break
  136. prjson['variables']['cursor'] = pr['commits']['pageInfo']['endCursor']
  137. prs[prnumber] = {'mergeCommit': pr['mergeCommit']['oid'],
  138. 'commits': pr_commits}
  139. if total_commits > len(pr_commits):
  140. print("WARNING: PR %d (merge %s) has %d commits, but GitHub is only giving us %d of them"%(prnumber, pr['mergeCommit']['oid'], total_commits, len(pr_commits)))
  141. # Check we got all PRs
  142. assert len(prs) == total_prs
  143. # Reverse dictionary
  144. commits_to_prs={}
  145. for key,value in prs.items():
  146. commits_to_prs[value['mergeCommit']]=key
  147. for c in value['commits']:
  148. commits_to_prs[c]=key
  149. # Check to see if commits in the repo are represented in PRs
  150. good = set()
  151. notfound = set()
  152. for c in commits:
  153. if c in commits_to_prs:
  154. good.add(commits_to_prs[c])
  155. else:
  156. notfound.add(c)
  157. prs_not_represented = set(prs.keys()) - good
  158. print("Milestone: %s, %d merged PRs, %d commits in history"%(MILESTONE, total_prs, len(commits)))
  159. print()
  160. print('-'*40)
  161. print()
  162. if len(prs_not_represented) > 0:
  163. print("""
  164. PRs that are in the milestone, but have no commits in the version range.
  165. These PRs probably belong in a different milestone.
  166. """)
  167. print('\n'.join('https://github.com/jupyterlab/jupyterlab/pull/%d'%i for i in prs_not_represented))
  168. else:
  169. print('Congratulations! All PRs in this milestone have commits in the commit history for this version range, so they all probably belong in this milestone.')
  170. print()
  171. print('-'*40)
  172. print()
  173. if len(notfound):
  174. print("""The following commits are not included in any PR on this milestone.
  175. This probably means the commit's PR needs to be assigned to this milestone,
  176. or the commit was pushed to master directly.
  177. """)
  178. print('\n'.join('%s %s %s'%(c, commits[c][0], commits[c][1]) for c in notfound))
  179. prs_to_check = [c for c in notfound if 'Merge pull request #' in commits[c][1] and commits[c][0] == 'noreply@github.com']
  180. if len(prs_to_check)>0:
  181. print()
  182. print("Try checking these PRs. They probably should be in the milestone, but probably aren't:")
  183. print()
  184. print('\n'.join('%s %s'%(c, commits[c][1]) for c in prs_to_check))
  185. else:
  186. print('Congratulations! All commits in the commit history are included in some PR in this milestone.')