milestone_check.py 6.2 KB

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