From 7dab3a78bd98c2800d983dbb3ed78dd58b61b2bb Mon Sep 17 00:00:00 2001 From: Katelyn Gigante Date: Wed, 8 Mar 2017 14:26:39 +1100 Subject: [PATCH] tiny refactor, with the intention to add support for other issue providers (eg bitbucket, gitlab, phabricator) --- autoissue.py | 38 +++++++++++++++++++++++++++++++++++--- github.py | 19 +------------------ settings.py | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 settings.py diff --git a/autoissue.py b/autoissue.py index 8e68942..ce0f755 100644 --- a/autoissue.py +++ b/autoissue.py @@ -166,8 +166,33 @@ def injectNumber(issue, number): with open(issue.fileName, 'w') as file: file.writelines(data) +def identifyOrigin(): + from settings import getValue, addProperty + val = getValue("provider") + if val is not None: + return val + + origin = False + with open('.git/config') as f: + for line in f: + if "[remote \"origin\"]" in line: + origin = True + if "url = " in line and origin: + if "github.com" in line: + r = "github" + # elif "bitbucket" in line: + # r = "bitbucket" + origin = False + + # Add to our settings file + if r: + addProperty("provider", r) + return r + else: + print("Unknown origin host") + + def main(): - from github import createIssues parser = argparse.ArgumentParser(description="Auto-Issue-Creator argument parser") parser.add_argument("-s", "--start", help="the token that begins the TODO: (ie. 'TODO')", default="TODO") parser.add_argument("-d", "--debug", action='store_true', help="enable debug mode (no POSTing to github)") @@ -190,18 +215,25 @@ def main(): basePath = os.path.abspath(os.path.expanduser(args['path'])) print "Base path of project:", basePath + provider = identifyOrigin() + issueList = getIssues() print "Found {} {}:".format(len(issueList), "issue" if len(issueList) is 1 else "issues") for issue in list(issueList): print issue if args["interactive"]: - post = raw_input('Create this issue in github? (y/n) ') + post = raw_input('Create this issue in {provider}? (y/n) '.format(provider=provider)) if post.lower() == 'n' or post.lower() == 'no': issueList.remove(issue) print "Creating {} issues".format(len(issueList)) - createIssues(issueList, debug) + + if (provider == "github"): + import github + github.createIssues(issueList, debug) + #elif provider == "bitbucket": + # print("Please implement me") if __name__ == "__main__": main() diff --git a/github.py b/github.py index f0d98d5..f9c5248 100644 --- a/github.py +++ b/github.py @@ -5,10 +5,9 @@ import util from autoissue import injectNumber from urlparse import urljoin - +from settings import addProperty, getValue, SETTINGS API_URL = 'https://api.github.com' -SETTINGS = 'settings.williames' #settings file HEADERS = {'Content-type':'application/json'} TOKEN_KEY = 'auth_token' @@ -35,22 +34,6 @@ def getToken(): print r.text return None -def getValue(key): - if os.path.exists(SETTINGS): - with open(SETTINGS) as f: - for line in f: - if key in line: - return line.split(" ", 1)[1].strip(" \n") - return None - -def addProperty(key, value): - with open(SETTINGS, "a+") as sett: # Will create the file if it does not exist - sett.write(key + " " + value + "\n") - return True - - return False - - def getRepo(): val = getValue("repo") diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..9fd459c --- /dev/null +++ b/settings.py @@ -0,0 +1,18 @@ +import os + +SETTINGS = 'settings.williames' #settings file + +def getValue(key): + if os.path.exists(SETTINGS): + with open(SETTINGS) as f: + for line in f: + if key in line: + return line.split(" ", 1)[1].strip(" \n") + return None + +def addProperty(key, value): + with open(SETTINGS, "a+") as sett: # Will create the file if it does not exist + sett.write(key + " " + value + "\n") + return True + + return False