|
1 | 1 | #! /usr/bin/env python
|
2 |
| - |
| 2 | +import getopt |
3 | 3 | import sys
|
4 | 4 |
|
5 | 5 | if sys.version_info >= (3, 0):
|
6 | 6 | print("\nThe script doesn't support python 3. Please use python 2.7+\n")
|
7 | 7 | sys.exit(1)
|
8 | 8 |
|
| 9 | +import os |
9 | 10 | import requests
|
10 | 11 | import csv
|
11 | 12 | import youtrackutils.csvClient
|
|
42 | 43 |
|
43 | 44 | CSV_FILE = "github2youtrack-{repo}-{data}.csv"
|
44 | 45 |
|
| 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 | + |
45 | 86 |
|
46 | 87 | 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 | + |
48 | 115 | if github_repo.find('/') > -1:
|
49 | 116 | github_repo_owner, github_repo = github_repo.split('/')
|
50 | 117 | github_repo = github_repo.replace('/', '_').replace('-', '_')
|
51 | 118 | else:
|
52 | 119 | 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) |
63 | 141 |
|
64 | 142 |
|
65 | 143 | def get_last_part_of_url(url_string):
|
|
0 commit comments