Skip to content
This repository was archived by the owner on Sep 5, 2022. It is now read-only.

Commit 8303f8a

Browse files
committed
fix command line parsing for github import
1 parent eb63103 commit 8303f8a

File tree

2 files changed

+91
-13
lines changed

2 files changed

+91
-13
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
setup(
2121
name='youtrack-scripts',
22-
version='0.1.14',
22+
version='0.1.15',
2323
python_requires='>=2.6, <3',
2424
packages=['youtrackutils',
2525
'youtrackutils.bugzilla',

youtrackutils/github2youtrack.py

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#! /usr/bin/env python
2-
2+
import getopt
33
import sys
44

55
if sys.version_info >= (3, 0):
66
print("\nThe script doesn't support python 3. Please use python 2.7+\n")
77
sys.exit(1)
88

9+
import os
910
import requests
1011
import csv
1112
import youtrackutils.csvClient
@@ -42,24 +43,101 @@
4243

4344
CSV_FILE = "github2youtrack-{repo}-{data}.csv"
4445

46+
help_url = "\
47+
https://www.jetbrains.com/help/youtrack/standalone/import-from-github.html"
48+
49+
50+
def usage():
51+
basename = os.path.basename(sys.argv[0])
52+
53+
print("""
54+
Usage:
55+
%s [OPTIONS] yt_url gh_login gh_password gh_repo
56+
57+
yt_url YouTrack base URL
58+
59+
gh_login The username to log in to GitHub
60+
61+
gh_password The password to log in to GitHub
62+
63+
gh_repo The name of the GitHub repository to import issues from
64+
65+
For instructions, see:
66+
%s
67+
68+
Options:
69+
-h, Show this help and exit
70+
-T TOKEN_FILE,
71+
Path to file with permanent token
72+
-t TOKEN,
73+
Value for permanent token as text
74+
-u LOGIN,
75+
YouTrack user login to perform import on behalf of
76+
-p PASSWORD,
77+
YouTrack user password
78+
79+
Examples:
80+
81+
$ %s -T token https://youtrack.company.com gh-user gh-pass test-repo
82+
83+
84+
""" % (basename, help_url, basename))
85+
4586

4687
def main():
47-
github_user, github_password, github_repo, youtrack_url, youtrack_login, youtrack_password = sys.argv[1:8]
88+
try:
89+
params = {}
90+
opts, args = getopt.getopt(sys.argv[1:], 'hu:p:t:T:')
91+
for opt, val in opts:
92+
if opt == '-h':
93+
usage()
94+
sys.exit(0)
95+
elif opt == '-u':
96+
params['login'] = val
97+
elif opt == '-p':
98+
params['password'] = val
99+
elif opt == '-t':
100+
params['token'] = val
101+
elif opt == '-T':
102+
check_file_and_save(val, params, 'token_file')
103+
except getopt.GetoptError as e:
104+
print(e)
105+
usage()
106+
sys.exit(1)
107+
108+
try:
109+
params['target_url'], github_user, github_password, github_repo = args
110+
except (ValueError, KeyError, IndexError):
111+
print("Bad arguments")
112+
usage()
113+
sys.exit(1)
114+
48115
if github_repo.find('/') > -1:
49116
github_repo_owner, github_repo = github_repo.split('/')
50117
github_repo = github_repo.replace('/', '_').replace('-', '_')
51118
else:
52119
github_repo_owner = github_user
53-
issues_csv_file = CSV_FILE.format(repo=github_repo, data='issues')
54-
comments_csv_file = CSV_FILE.format(repo=github_repo, data='comments')
55-
github2csv(issues_csv_file, comments_csv_file,
56-
github_user, github_password, github_repo, github_repo_owner)
57-
csv2youtrack.csv2youtrack(
58-
dict(issues_file=issues_csv_file,
59-
target_url=youtrack_url,
60-
login=youtrack_login,
61-
password=youtrack_password,
62-
comments_file=comments_csv_file))
120+
121+
params['issues_file'] = CSV_FILE.format(repo=github_repo, data='issues')
122+
params['comments_file'] = CSV_FILE.format(repo=github_repo, data='comments')
123+
124+
github2csv(params['issues_file'],
125+
params['comments_file'],
126+
github_user,
127+
github_password,
128+
github_repo,
129+
github_repo_owner)
130+
131+
csv2youtrack.csv2youtrack(params)
132+
133+
134+
def check_file_and_save(filename, params, key):
135+
try:
136+
params[key] = os.path.abspath(filename)
137+
except (OSError, IOError) as e:
138+
print("Data file is not accessible: " + str(e))
139+
print(filename)
140+
sys.exit(1)
63141

64142

65143
def get_last_part_of_url(url_string):

0 commit comments

Comments
 (0)