1 | """Update project metrics on The Coverage Space. |
||
2 | |||
3 | Usage: |
||
4 | coverage.space <owner/repo> <metric> [<value>] [--exit-code] |
||
5 | coverage.space (-h | --help) |
||
6 | coverage.space (-V | --version) |
||
7 | |||
8 | Options: |
||
9 | -h --help Show this help screen. |
||
10 | -V --version Show the program version. |
||
11 | --exit-code Return non-zero exit code on failures. |
||
12 | |||
13 | """ |
||
14 | |||
15 | 1 | from __future__ import unicode_literals |
|
16 | |||
17 | 1 | import os |
|
18 | 1 | import sys |
|
19 | 1 | import json |
|
20 | |||
21 | 1 | import six |
|
22 | 1 | from docopt import docopt |
|
23 | 1 | import colorama |
|
24 | 1 | from colorama import Fore, Style |
|
25 | 1 | import requests |
|
26 | |||
27 | 1 | from . import API, VERSION |
|
28 | |||
29 | 1 | from .plugins import get_coverage |
|
30 | |||
31 | |||
32 | 1 | def main(): |
|
33 | """Run the program.""" |
||
34 | 1 | colorama.init(autoreset=True) |
|
35 | 1 | arguments = docopt(__doc__, version=VERSION) |
|
36 | |||
37 | 1 | slug = arguments['<owner/repo>'] |
|
38 | 1 | metric = arguments['<metric>'] |
|
39 | 1 | value = arguments.get('<value>') or get_coverage() |
|
40 | |||
41 | 1 | success = call(slug, metric, value) |
|
42 | |||
43 | 1 | if not success and arguments['--exit-code']: |
|
44 | 1 | sys.exit(1) |
|
45 | |||
46 | |||
47 | 1 | def call(slug, metric, value): |
|
48 | """Call the API and display errors.""" |
||
49 | 1 | url = "{}/{}".format(API, slug) |
|
50 | 1 | data = {metric: value} |
|
51 | |||
52 | 1 | response = requests.put(url, data=data) |
|
53 | |||
54 | 1 | if response.status_code == 200: |
|
55 | 1 | return True |
|
56 | |||
57 | 1 | elif response.status_code == 422: |
|
58 | 1 | display("coverage decreased", response.json(), |
|
59 | Fore.YELLOW + Style.BRIGHT) |
||
0 ignored issues
–
show
|
|||
60 | 1 | return False |
|
61 | |||
62 | else: |
||
63 | display("coverage unknown", response.json(), |
||
64 | Fore.RED + Style.BRIGHT) |
||
0 ignored issues
–
show
|
|||
65 | return False |
||
66 | |||
67 | |||
68 | 1 | def display(title, data, color=""): |
|
69 | """Write colored text to the console.""" |
||
70 | 1 | width = int(os.getenv('COLUMNS', 80)) |
|
71 | 1 | six.print_(color + "{0:=^{1}}".format(' ' + title + ' ', width)) |
|
72 | 1 | six.print_(color + json.dumps(data, indent=4)) |
|
73 | six.print_(color + '=' * width) |
||
74 |
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.