milestone_check.py 6.4 KB

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