/ scripts / oraclize-args / testarg.py
testarg.py
  1  import os, argparse
  2  import json, urllib2, datetime
  3  from collections import defaultdict
  4  start = ''
  5  
  6  parser = argparse.ArgumentParser()
  7  parser.add_argument('-S','--script')
  8  
  9  parser.add_argument('-U','--username')
 10  parser.add_argument('-u','--userid')
 11  
 12  parser.add_argument('-R','--repoid')
 13  parser.add_argument('-r','--reponame')
 14  
 15  parser.add_argument('-C','--commitid')
 16  
 17  parser.add_argument('-B','--branch')
 18  parser.add_argument('-T','--tail')
 19  parser.add_argument('-H','--head')
 20  
 21  parser.add_argument('-I','--issueid')
 22  parser.add_argument('-P','--pullid')
 23  
 24  parser.add_argument('-c','--client')
 25  parser.add_argument('-s','--secret')
 26  
 27  
 28  args = parser.parse_args()
 29                              
 30  if(args.client is not None and args.secret is not None):
 31      client_id = args.client
 32      client_secret = args.secret
 33      auth = "?client_id="+client_id+"&client_secret="+client_secret                            
 34  else:
 35      auth = ""
 36  count = 0
 37  repo_link = ""
 38  if(args.reponame is not None):
 39      repo_link = "https://api.github.com/repos/" + args.reponame
 40  else:
 41      repo_link = "https://api.github.com/repositories/" + args.repoid
 42  
 43  
 44  points = defaultdict(int)      
 45  claimed = defaultdict(bool)   
 46  
 47  def relatedIssues(issue):
 48      link_issue = repo_link + "/issues/" + issue + "/timeline" + auth
 49      print link_issue
 50      req = urllib2.Request(link_issue)
 51      req.add_header("Accept", "application/vnd.github.mockingbird-preview")
 52      issue = json.load(urllib2.urlopen(req))
 53      for elem in issue:
 54          if(elem["event"] == "cross-referenced"):
 55              if(elem["source"]["type"] == "issue"):
 56                  #print elem["source"]["issue"]["number"];
 57                  pullCommitPoints(json.dumps(elem["source"]["issue"]["number"]))
 58     
 59      
 60  
 61  def issueStatus(issue):
 62      link_issue = repo_link + "/issues/" + issue + auth
 63      issue = json.load(urllib2.urlopen(link_issue))
 64      if(issue['state'] == "closed"):
 65          print datetime.datetime.strptime( json.dumps(issue['closed_at'])[1:-1], "%Y-%m-%dT%H:%M:%SZ" ).strftime('%s') +","+ json.dumps(issue['closed_by']['login'])
 66      else:
 67          print "open"
 68      
 69      
 70  def pullCommitPoints(pr):
 71      link_pull = repo_link + "/pulls/" + pr + auth
 72      pull = json.load(urllib2.urlopen(link_pull))
 73      if(pull['merged_at']):
 74          link_pulls_commits = repo_link + "/pulls/" + pr + "/commits" +auth
 75          points = defaultdict(int)
 76          commits = json.load(urllib2.urlopen(link_pulls_commits))
 77          for commit in commits:
 78              if(commit['url']):
 79                  link_commit = json.dumps(commit['url'])[1:-1] +auth
 80                  _commit = json.load(urllib2.urlopen(link_commit))
 81                  #print _commit['sha'], 
 82                  author = json.dumps(_commit['author']['login'])[1:-1]
 83                  points[author] += int(json.dumps(_commit['stats']['total']))
 84          print points.items(), 
 85  
 86  
 87  def issueCommits(issueid):
 88      link_issue = repo_link + "/issues/" + issueid + "/timeline" + auth
 89      req = urllib2.Request(link_issue)
 90      req.add_header("Accept", "application/vnd.github.mockingbird-preview")
 91      issue = json.load(urllib2.urlopen(req))
 92      for elem in issue:
 93          if(elem["event"] == "cross-referenced"):
 94              if(elem["source"]["type"] == "issue"):
 95                  #print elem["source"]["issue"]["number"];
 96                  pullCommitPoints(json.dumps(elem["source"]["issue"]["number"]))
 97  
 98  def commitData(commit):
 99      link_commit = repo_link +"/commits/"+ commit;
100      req = urllib2.Request(link_commit)
101      req.add_header("Accept", "application/vnd.github.mockingbird-preview")
102      commit = json.load(urllib2.urlopen(req))
103      print json.dumps(commit)
104  
105  
106  
107  def updateIssue(issueid):
108      link_issue = repo_link + "/issues/" + issueid + auth
109      issue = json.load(urllib2.urlopen(link_issue))
110      print issue['state'] + ", " + datetime.datetime.strptime( json.dumps(issue['closed_at'])[1:-1], "%Y-%m-%dT%H:%M:%SZ" ).strftime('%s') + ", ", 
111      
112      link_issue = repo_link + "/issues/" + issueid + "/timeline" + auth
113      req = urllib2.Request(link_issue)
114      req.add_header("Accept", "application/vnd.github.mockingbird-preview")
115      issue = json.load(urllib2.urlopen(req))
116      
117      for elem in issue:
118          if(elem["event"] == "cross-referenced"):
119              if(elem["source"]["type"] == "issue"):
120                  #print elem["source"]["issue"]["number"];
121                  pr = json.dumps(elem["source"]["issue"]["number"])
122                  link_pull = repo_link + "/pulls/" + pr + auth
123                  pull = json.load(urllib2.urlopen(link_pull))
124                  if(pull['merged_at']):
125                      link_pulls_commits = repo_link + "/pulls/" + pr + "/commits" +auth
126                      commits = json.load(urllib2.urlopen(link_pulls_commits))
127                      for commit in commits:
128                          if(commit['url']):
129                              link_commit = json.dumps(commit['url'])[1:-1] + auth
130                              _commit = json.load(urllib2.urlopen(link_commit))
131                              author = _commit['author']['login']
132                              points[author] += int(json.dumps(_commit['stats']['total']))
133      print points.items() 
134  
135  #branchname required
136  
137  
138  
139  
140  
141  def updateCommits(branchname, head, tail):
142      if(branchname is None):
143          req = urllib2.Request(repo_link+auth)
144          repo = json.load(urllib2.urlopen(req))
145          branchname = json.dumps(repo['default_branch'])[1:-1]
146      if(head is None and tail is not None): #tail only = error
147          print "bad call"
148          return
149      elif(head is None and tail is None): #no head and no tail (new) = from latest head to reachable tails
150          branches_link = repo_link + "/branches/" + branchname + auth
151          req = urllib2.Request(branches_link)
152          branch = json.load(urllib2.urlopen(req))
153          _head = json.dumps(branch['commit']['sha'])[1:-1]
154          head = _head
155      elif(head is not None and tail is None): #head only (sync) = from latestHead to head  
156          setClaimedChunk(head, "")
157          branches_link = repo_link + "/branches/" + branchname + auth
158          req = urllib2.Request(branches_link)
159          branch = json.load(urllib2.urlopen(req))
160          _head = json.dumps(branch['commit']['sha'])[1:-1]
161          head = _head
162      elif(tail is not None and head is not None): #head and tail (continue) = continue from tail
163          _head = tail
164  
165      commit_link = repo_link + "/commits/" + _head + auth
166      #print commit_link
167      req = urllib2.Request(commit_link)
168      _head = json.load(urllib2.urlopen(req))
169      ntail = loadPoints(_head)
170  
171      print json.dumps(head) + "," + json.dumps(ntail['sha']) + ", ", 
172      print str(len(points)) + ", ",
173      print json.dumps(points.items()), 
174   
175  
176  #https://api.github.com/repos/status-im/github-oracle/compare/master...6e2528a9eb3fec21ca0679ec0c2a0935c2aa6656  COMPARE IF COMMIT IS IN `master`
177  #https://api.github.com/repos/status-im/github-oracle/commits?per_page=100&sha=e0a340e72784b1322929d6803773b31a2b6b5707
178  #https://api.github.com/repos/status-im/github-oracle/git/refs/heads BRANCHES
179  #https://developer.github.com/v3/#conditional-requests
180  
181  def setCommittedTree(commit):
182      if(claimed[commit['sha']] == False):
183          claimed[commit['sha']] = True;
184          for parent in commit['parents']:    
185              link_commit = json.dumps(parent['url'])[1:-1] + auth
186              _commit = json.load(urllib2.urlopen(link_commit))
187              setCommittedTree(_commit);
188              
189              
190  def setClaimedChunk(head, tail):
191      global count
192      while (tail != head):
193          commit_link = repo_link + "/commits" + auth + "&per_page=100&sha=" + head
194          req = urllib2.Request(commit_link)
195          commits = json.load(urllib2.urlopen(req));
196          for commit in commits:
197              if(claimed[commit['sha']] != True):
198                  claimed[commit['sha']] = True
199                  #print "claimed "+str(count)+": "+commit['sha']
200                  count += 1
201                  head = commit['sha']
202                  if(len(commit['parents']) == 2):
203                      setClaimedChunk("", json.dumps(commit['parents'])[1][1:-1])
204                  if (tail == head): 
205                      break
206          if(len(commits) < 100):
207              break
208      return head
209  
210  def loadPoints(head):
211      global count
212  
213      while (claimed[head['sha']] == False and count < 1000):
214          claimed[head['sha']] = True
215          count += 1
216          if (head['author'] is not None):
217              author = json.dumps(head['author']['login'])[1:-1]
218              if (len(points) > 5 and points[author] == 0): break;
219              if (len(head['parents']) < 2):
220                  points[author] += int(json.dumps(head['stats']['total']))
221                  #print str(count) + ": " + head['sha'] +" +"+ str(head['stats']['total'])
222              #else:
223                  #print str(count) + ": " + head['sha'] +" -"+ str(head['stats']['total'])
224          #else:
225              #print str(count) + ": " + head['sha'] +" x"+ str(head['stats']['total'])
226          for parent in head['parents']:    
227              link_commit = json.dumps(parent['url'])[1:-1] + auth
228              _commit = json.load(urllib2.urlopen(link_commit))
229              head = loadPoints(_commit)
230      return head
231      
232          
233      
234  if args.script == 'issue-status':
235      issueStatus(args.issueid)
236  elif args.script == 'issue-update':  
237      updateIssue(args.issueid)
238  elif args.script == 'issue-commits':  
239      issueCommits(args.issueid)
240  elif args.script == 'related-issues':
241      relatedIssues(args.issueid)
242  elif args.script == 'commit-points':
243      pullCommitPoints(args.pullid)
244  elif args.script == 'commit-data':
245      commitData(args.commitid)
246  elif args.script== 'update-commits':
247      updateCommits(args.branch, args.head, args.tail)
248