Skip to content

Commit 09aa40b

Browse files
committed
GitHub auth check
1 parent 53e4e36 commit 09aa40b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

import-issues.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66

77
import py.path
8+
import requests
89

910
LOG_LEVELS = {logging.getLevelName(level): level for level in (
1011
logging.DEBUG,
@@ -42,6 +43,9 @@ def __call__(self, parser, namespace, value, option_string=None):
4243

4344
parser.add_argument("--statedir", action=PyPathLocalAction,
4445
help="Directory to store change state")
46+
parser.add_argument("--githubroot",
47+
default="https://api.github.com",
48+
help="Root for the GitHub API")
4549

4650
parser.add_argument("trello_json", action=PyPathLocalAction,
4751
help="JSON file exported from Trello")
@@ -50,6 +54,11 @@ def __call__(self, parser, namespace, value, option_string=None):
5054
parser.add_argument("github_repo",
5155
help="Repo in GitHub")
5256

57+
parser.add_argument("github_user",
58+
help="Your GitHub username")
59+
parser.add_argument("github_password",
60+
help="Your GitHub password")
61+
5362

5463
class Card(object):
5564
def __init__(self, card_data, statedir=None):
@@ -69,6 +78,22 @@ def state(self):
6978
return None
7079

7180

81+
def gh_request(path, args):
82+
req = requests.get('%s/%s' % (args.githubroot, path),
83+
auth=(args.github_user, args.github_password))
84+
85+
if not req.ok:
86+
data = req.json()
87+
message = data.get(
88+
'message',
89+
"HTTP error %d: %s" % (req.status_code, req.reason)
90+
)
91+
logging.getLogger('github').error(message)
92+
return False
93+
94+
return req
95+
96+
7297
def main():
7398
args = parser.parse_args()
7499

@@ -92,6 +117,15 @@ def main():
92117
cards_log = logging.getLogger("cards import")
93118
cards_log.info("Importing %d cards", len(trello_data['cards']))
94119

120+
req = gh_request('user', args)
121+
if not req:
122+
sys.exit(1)
123+
124+
logging.info(
125+
"Importing as GitHub user %s",
126+
req.json().get('name', "unknown user name")
127+
)
128+
95129
for card_data in trello_data['cards']:
96130
cards_log.debug("Card %s", card_data['name'])
97131
card = Card(card_data, args.statedir)

0 commit comments

Comments
 (0)