Skip to content

Instantly share code, notes, and snippets.

@chuwy
Last active March 1, 2016 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuwy/c5b091a917cb2296e8d5 to your computer and use it in GitHub Desktop.
Save chuwy/c5b091a917cb2296e8d5 to your computer and use it in GitHub Desktop.
Script for looking unmentioned commits
#!/usr/bin/env python3
"""
Script for looking unmentioned commits in milesone comparing with specified PR
Usage: issuer.py user password company/repository milestone_pattern pull_id
"""
import sys
import itertools
import re
import github
class PullRequestNotFoundException(Exception): pass
class MilestoneNotFoundException(Exception): pass
def levenshtein(a, b):
"""Calculates the Levenshtein distance between a and b"""
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a, b = b, a
n, m = m, n
current = range(n+1)
for i in range(1, m+1):
previous, current = current, [i]+[0]*n
for j in range(1, n+1):
add, delete = previous[j]+1, current[j-1]+1
change = previous[j-1]
if a[j-1] != b[i-1]:
change = change + 1
current[j] = min(add, delete, change)
return current[n]
def mentioned_ids(message):
return [int(id) for id in re.findall('^.+.+\#(\d+).+$', message)]
def get_mentioned_ids(pull_request):
return itertools.chain(
*[mentioned_ids(c.commit.message) for c in pull_request.get_commits()])
def get_pr(number):
try:
return repo.get_pull(number)
except github.GithubException:
raise PullRequestNotFoundException
def get_milestone(number):
try:
return repo.get_milestone(number)
except github.GithubException:
raise MilestoneNotFoundException
def find_milestone(pattern):
lower_pattern = pattern.lower()
try:
return sorted(
[m for m in repo.get_milestones() if lower_pattern in m.title.lower()],
key=lambda x: levenshtein(x.title.lower(), lower_pattern))[0]
except IndexError:
raise MilestoneNotFoundException
def diff_milestone_pr(milestone_pattern, pr_id):
milestone = find_milestone(milestone_pattern)
pr = get_pr(pr_id)
mentioned_ids = get_mentioned_ids(pr)
open_issues = [int(i.number) for i in repo.get_issues(milestone)]
return set(open_issues).difference(set(mentioned_ids))
def get_info(issue):
issue_data = repo.get_issue(issue)
info = """Title: {}\nURL: {}\n""".format(issue_data.title, issue_data.html_url)
return info
def run(milestone_pattern, pr_number):
infos = [get_info(issue) for issue in diff_milestone_pr(milestone_pattern, pr_number)]
for info in infos:
print(info)
if __name__ == '__main__':
if '--help' in sys.argv:
print('Usage: issuer.py user password company/repository milestone_pattern pull_id')
else:
gh = github.Authorization.github.Github(sys.argv[1], sys.argv[2])
repo = gh.get_repo(sys.argv[3])
run(sys.argv[4], int(sys.argv[5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment