|
1
|
|
|
"""Update project metrics on The Coverage Space. |
|
2
|
|
|
|
|
3
|
|
|
Usage: |
|
4
|
|
|
coveragespace <owner/repo> <metric> [<value>] [--verbose] [--exit-code] |
|
5
|
|
|
coveragespace <owner/repo> --reset [--verbose] |
|
6
|
|
|
coveragespace (-h | --help) |
|
7
|
|
|
coveragespace (-V | --version) |
|
8
|
|
|
|
|
9
|
|
|
Options: |
|
10
|
|
|
-h --help Show this help screen. |
|
11
|
|
|
-V --version Show the program version. |
|
12
|
|
|
-v --verbose Always display the coverage metrics. |
|
13
|
|
|
-x --exit-code Return non-zero exit code on failures. |
|
14
|
|
|
|
|
15
|
|
|
""" |
|
16
|
|
|
|
|
17
|
1 |
|
from __future__ import unicode_literals |
|
18
|
|
|
|
|
19
|
1 |
|
import json |
|
20
|
1 |
|
import logging |
|
21
|
1 |
|
import sys |
|
22
|
|
|
|
|
23
|
1 |
|
import colorama |
|
24
|
1 |
|
import six |
|
25
|
1 |
|
from backports.shutil_get_terminal_size import \ |
|
26
|
1 |
|
get_terminal_size # pylint: disable=relative-import |
|
27
|
|
|
from docopt import DocoptExit, docopt |
|
28
|
1 |
|
|
|
29
|
1 |
|
from . import API, VERSION, client, services |
|
30
|
1 |
|
from .plugins import get_coverage, launch_report |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
1 |
|
log = logging.getLogger(__name__) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
1 |
|
def main(): |
|
37
|
|
|
"""Parse command-line arguments, configure logging, and run the program.""" |
|
38
|
1 |
|
colorama.init(autoreset=True) |
|
39
|
1 |
|
arguments = docopt(__doc__, version=VERSION) |
|
40
|
|
|
|
|
41
|
1 |
|
slug = arguments['<owner/repo>'] |
|
42
|
1 |
|
metric = arguments['<metric>'] |
|
43
|
1 |
|
reset = arguments['--reset'] |
|
44
|
1 |
|
value = arguments['<value>'] |
|
45
|
1 |
|
verbose = arguments['--verbose'] |
|
46
|
1 |
|
hardfail = arguments['--exit-code'] |
|
47
|
|
|
|
|
48
|
1 |
|
logging.basicConfig( |
|
49
|
|
|
level=logging.DEBUG if verbose else logging.WARNING, |
|
50
|
|
|
format="%(levelname)s: %(name)s: %(message)s", |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
1 |
|
if '/' not in slug: |
|
54
|
1 |
|
raise DocoptExit("<owner/repo> slug must contain a slash" + '\n') |
|
55
|
|
|
|
|
56
|
1 |
|
success = run(slug, metric, value, reset, verbose, hardfail) |
|
57
|
|
|
|
|
58
|
1 |
|
if not success and hardfail: |
|
59
|
1 |
|
sys.exit(1) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
1 |
|
def run(*args, **kwargs): |
|
63
|
|
|
"""Run the program.""" |
|
64
|
1 |
|
if services.detected(): |
|
65
|
1 |
|
log.info("Coverage check skipped when running on CI service") |
|
66
|
1 |
|
return True |
|
67
|
|
|
|
|
68
|
1 |
|
return call(*args, **kwargs) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
1 |
|
def call(slug, metric, value, reset=False, verbose=False, hardfail=False): |
|
72
|
|
|
"""Call the API and display errors.""" |
|
73
|
1 |
|
url = "{}/{}".format(API, slug) |
|
74
|
1 |
|
if reset: |
|
75
|
1 |
|
data = {metric: None} |
|
76
|
1 |
|
response = client.delete(url, data) |
|
77
|
|
|
else: |
|
78
|
1 |
|
data = {metric: value or get_coverage()} |
|
79
|
1 |
|
response = client.get(url, data) |
|
80
|
|
|
|
|
81
|
1 |
|
if response.status_code == 200: |
|
82
|
1 |
|
if verbose: |
|
83
|
1 |
|
display("coverage increased", response.json(), colorama.Fore.GREEN) |
|
84
|
1 |
|
return True |
|
85
|
|
|
|
|
86
|
1 |
|
elif response.status_code == 202: |
|
87
|
1 |
|
display("coverage reset", response.json(), colorama.Fore.BLUE) |
|
88
|
1 |
|
return True |
|
89
|
|
|
|
|
90
|
1 |
|
elif response.status_code == 422: |
|
91
|
1 |
|
color = colorama.Fore.RED if hardfail else colorama.Fore.YELLOW |
|
92
|
1 |
|
data = response.json() |
|
93
|
1 |
|
data['help'] = \ |
|
94
|
|
|
"To reset metrics, run: coveragespace {} --reset".format(slug) |
|
95
|
1 |
|
display("coverage decreased", data, color) |
|
96
|
1 |
|
launch_report() |
|
97
|
1 |
|
return False |
|
98
|
|
|
|
|
99
|
|
|
else: |
|
100
|
1 |
|
try: |
|
101
|
1 |
|
data = response.json() |
|
102
|
1 |
|
display("coverage unknown", data, colorama.Fore.RED) |
|
103
|
1 |
|
except (TypeError, ValueError) as exc: |
|
104
|
1 |
|
data = response.data.decode('utf-8') |
|
105
|
1 |
|
log.error("%s\n\nwhen decoding response:\n\n%s\n", exc, data) |
|
106
|
1 |
|
return False |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
1 |
|
def display(title, data, color=""): |
|
110
|
|
|
"""Write colored text to the console.""" |
|
111
|
1 |
|
color += colorama.Style.BRIGHT |
|
112
|
1 |
|
width, _ = get_terminal_size() |
|
113
|
1 |
|
six.print_(color + "{0:=^{1}}".format(' ' + title + ' ', width)) |
|
114
|
1 |
|
six.print_(color + json.dumps(data, indent=4)) |
|
115
|
|
|
six.print_(color + '=' * width) |
|
116
|
|
|
|