|
7 | 7 |
|
8 | 8 | record_mode = 'once'
|
9 | 9 |
|
| 10 | +from git_repo.services.service import RepositoryService |
| 11 | +services = list(RepositoryService.service_map.keys()) |
| 12 | + |
10 | 13 | if os.environ.get('TRAVIS_GH3'):
|
11 | 14 | # create default bogus values for tokens and namespaces if missing for pytest
|
12 | 15 | # to run without environment values
|
13 | 16 | # also if an environment variable is not set, then we don't want to record cassettes
|
14 | 17 | record_mode = 'never'
|
15 |
| - for service_name in ('github', 'gitlab', 'bitbucket'): |
16 |
| - token_name = 'PRIVATE_KEY_{}'.format(service_name.upper()) |
17 |
| - namespace_name = '{}_NAMESPACE'.format(service_name.upper()) |
| 18 | + for service in services: |
| 19 | + token_name = 'PRIVATE_KEY_{}'.format(service.upper()) |
| 20 | + namespace_name = '{}_NAMESPACE'.format(service.upper()) |
18 | 21 | if token_name not in os.environ:
|
19 |
| - os.environ[token_name] = 'not_configured:test' # using a : for bitbucket's case |
| 22 | + os.environ[token_name] = '_namespace_{}_:_private_'.format(service) # using a : for bitbucket's case |
20 | 23 | if namespace_name not in os.environ:
|
21 |
| - os.environ[namespace_name] = 'not_configured' |
| 24 | + os.environ[namespace_name] = '_namespace_{}_'.format(service) |
22 | 25 | else:
|
| 26 | + # if running tests "locally" and not in travis, let's try to extract the keys from |
| 27 | + # the local configuration if there is some local configuration. And exposes them as |
| 28 | + # environment variables. |
23 | 29 | import git, getpass
|
24 | 30 | config = git.config.GitConfigParser(os.path.join(os.environ['HOME'], '.gitconfig'))
|
25 |
| - conf_section = list(filter(lambda n: 'gitrepo' in n, config.sections())) |
26 |
| - key_dict = {section.split('"')[1] :config.get_value(section, 'privatekey') for section in conf_section} |
27 |
| - for service, key in key_dict.items(): |
| 31 | + |
| 32 | + # handle the different forms of token configuration item (yup, technical debt bites here) |
| 33 | + get_section = lambda s: 'gitrepo "{}"'.format(s) |
| 34 | + get_token = lambda s: config.get_value(get_section(s), 'token', |
| 35 | + config.get_value(get_section(s), 'private_token', |
| 36 | + config.get_value(get_section(s), 'privatekey', |
| 37 | + '_namespace_{}_:_private_'.format(s) # using a : for bitbucket's case |
| 38 | + ))) |
| 39 | + # XXX temporary fix that should not be necessary when refactoring with pybitbucket |
| 40 | + get_default_namespace = lambda s: os.environ[token_name].split(':')[0] if s == 'bitbucket' else '_namespace_{}_'.format(s) |
| 41 | + |
| 42 | + for service in services: |
28 | 43 | token_name = 'PRIVATE_KEY_{}'.format(service.upper())
|
29 | 44 | namespace_name = '{}_NAMESPACE'.format(service.upper())
|
30 |
| - os.environ[token_name] = key |
31 |
| - os.environ[namespace_name] = os.environ.get('GITREPO_NAMESPACE', getpass.getuser()) |
32 |
| - |
33 |
| -api_token_github = os.environ['PRIVATE_KEY_GITHUB'] |
34 |
| -api_token_gitlab = os.environ['PRIVATE_KEY_GITLAB'] |
35 |
| -api_token_bitbucket = os.environ['PRIVATE_KEY_BITBUCKET'] |
36 |
| - |
37 |
| -github_namespace = os.environ['GITHUB_NAMESPACE'] |
38 |
| -gitlab_namespace = os.environ['GITLAB_NAMESPACE'] |
39 |
| -bitbucket_namespace = os.environ['BITBUCKET_NAMESPACE'] |
| 45 | + if token_name not in os.environ: |
| 46 | + os.environ[token_name] = get_token(service) |
| 47 | + if namespace_name not in os.environ: |
| 48 | + os.environ[namespace_name] = os.environ.get('GITREPO_NAMESPACE', get_default_namespace(service)) |
40 | 49 |
|
41 | 50 | betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
|
42 | 51 |
|
43 | 52 | with betamax.Betamax.configure() as config:
|
44 | 53 | config.default_cassette_options['record_mode'] = record_mode
|
45 | 54 | config.cassette_library_dir = 'tests/integration/cassettes'
|
46 | 55 | config.default_cassette_options['serialize_with'] = 'prettyjson'
|
47 |
| - config.define_cassette_placeholder('<PRIVATE_KEY_GITHUB>', api_token_github) |
48 |
| - config.define_cassette_placeholder('<PRIVATE_KEY_GITLAB>', api_token_gitlab) |
49 |
| - config.define_cassette_placeholder('<PRIVATE_KEY_BITBUCKET>', api_token_bitbucket) |
50 |
| - config.define_cassette_placeholder('<GITHUB_NAMESPACE>', github_namespace) |
51 |
| - config.define_cassette_placeholder('<GITLAB_NAMESPACE>', gitlab_namespace) |
52 |
| - config.define_cassette_placeholder('<BITBUCKET_NAMESPACE>', bitbucket_namespace) |
53 |
| - |
54 |
| - |
| 56 | + # generating placeholders in betamax configuration for each service's key and default namespace |
| 57 | + for service in services: |
| 58 | + config.define_cassette_placeholder('<PRIVATE_KEY_{}>'.format(service.upper()), os.environ.get('PRIVATE_KEY_{}'.format(service.upper()))) |
| 59 | + config.define_cassette_placeholder('<{}_NAMESPACE>'.format(service.upper()), os.environ.get('{}_NAMESPACE'.format(service.upper()))) |
55 | 60 |
|
0 commit comments