-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_parser.py
More file actions
102 lines (91 loc) · 5.31 KB
/
Copy pathinput_parser.py
File metadata and controls
102 lines (91 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import re
import argparse
import logging
logger = logging.getLogger(__name__)
def hide(string: str, after: int = 4) -> str:
if string is not None and len(string) > after:
return string[:after] + '*' * (len(string) - after)
return string
def hide_url(url: str) -> str:
if url is None:
return ''
# Simple regex to hide credentials in URL
return re.sub(r'//(.*?):*(.*?)@', lambda m: '//***@', url) # NOSONAR : It is just to hide an URL sensitive data
def parse():
parser = argparse.ArgumentParser(
description='Git Platforms Synchronization')
parser.add_argument('--from-url', required=True,
help='Git "from" platform API URL (Required)')
parser.add_argument('--from-login',
help='Git "from" login or token.')
parser.add_argument('--from-password',
help='Git "from" password.')
parser.add_argument('--from-org',
help='Git "from" organization/user/project.', required=True)
parser.add_argument('--from-type',
help='Git "from" type (Bitbucket, Gerrit, Gitea, GitLab, GitHub, ... ; To use when cannot be detected from URL).', default='')
parser.add_argument('--from-proxy',
help='Git "from" proxy (with credentials if needed).')
parser.add_argument('--from-disable-ssl-verify',
help='Git "from" disable SSL verification.', action='store_true')
parser.add_argument('--to-url', required=True,
help='Git "to" platform API URL (Required)')
parser.add_argument('--to-login',
help='Git "to" login or token (Required).', required=True)
parser.add_argument('--to-password',
help='Git "to" password.')
parser.add_argument('--to-org',
help='Git "to" organization/user/project.', required=True)
parser.add_argument(
'--to-type', help='Git "to" type (Bitbucket, Gitea, Gerrit, GitLab, GitHub, ... ; To use when cannot be detected from URL).', default='')
parser.add_argument('--to-proxy',
help='Git "to" proxy (with credentials if needed).')
parser.add_argument('--to-disable-ssl-verify',
help='Git "to" disable SSL verification.', action='store_true')
parser.add_argument('--to-description-prefix',
help='Description prefix for Git "to" repositories creation.', default='')
parser.add_argument('--repos-include',
help='Repositories names patterns to include (comma separated).', default='')
parser.add_argument('--repos-exclude',
help='Repositories names patterns to exclude (comma separated).', default='\\.')
parser.add_argument('--branches-include',
help='Branches names patterns to include (comma separated).', default='')
parser.add_argument('--branches-exclude',
help='Branches names patterns to exclude (comma separated).', default='\\.')
parser.add_argument('-d', '--dry-run',
help='Dry-run : Just analyse which branches should be synchronized, without doning it really.', action='store_true')
parser.add_argument(
'-l', '--log-level', help='Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)', default='INFO')
args = parser.parse_args()
return args
def print_args(args: argparse.Namespace):
logger.info('\n------ Input arguments ------')
logger.info('Git "from" platform URL : %s', args.from_url)
logger.info('Git "from" login or token : %s', hide(args.from_login))
logger.info('Git "from" org/project : %s', args.from_org)
logger.info('Git "from" type : %s', args.from_type)
logger.info('Git "from" proxy : %s', hide_url(args.from_proxy))
logger.info('Git "from" disable SSL : %s',
args.from_disable_ssl_verify)
logger.info('Git "to" platform URL : %s', args.to_url)
logger.info('Git "to" login or token : %s', hide(args.to_login))
logger.info('Git "to" org/project : %s', args.to_org)
logger.info('Git "to" type : %s', args.to_type)
logger.info('Git "to" proxy : %s', hide_url(args.to_proxy))
logger.info('Git "to" disable SSL : %s', args.to_disable_ssl_verify)
logger.info('Git "to" description prefix : %s', args.to_description_prefix)
logger.info('Repositories include : %s', args.repos_include)
logger.info('Repositories exclude : %s', args.repos_exclude)
logger.info('Branches include : %s', args.branches_include)
logger.info('Branches exclude : %s', args.branches_exclude)
logger.info('Dry-run : %s', args.dry_run)
logger.info('Log Level : %s', args.log_level)
def reduce(items: list, includes: str, excludes: str):
includes_combined = "(" + ")|(".join(includes.split(',')) + ")"
excludes_combined = "(" + ")|(".join(excludes.split(',')) + ")"
new_items = []
for item in items:
if re.match(excludes_combined, item) or not re.match(includes_combined, item):
continue
new_items.append(item)
return new_items