From fc30eb23e21580cbd642f70ef1d662d71579ed60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Cl=C3=A9rice?= Date: Fri, 7 Apr 2017 11:36:40 +0200 Subject: [PATCH] From travis to hooktest --- HookTest/cmd.py | 5 +++ HookTest/test.py | 84 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/HookTest/cmd.py b/HookTest/cmd.py index 55c37e4..565d848 100644 --- a/HookTest/cmd.py +++ b/HookTest/cmd.py @@ -47,6 +47,11 @@ def parse_args(args): parser.add_argument("--allowfailure", help="Returns a passing test result as long as at least one text passes.", action="store_true", default=False) + parser.add_argument( + "--from_travis_to_hook", + help="Send results to a Hook UI endpoint", + default=False + ) args = parser.parse_args(args) if args.finder: diff --git a/HookTest/test.py b/HookTest/test.py index 228f90f..0d5add2 100644 --- a/HookTest/test.py +++ b/HookTest/test.py @@ -136,6 +136,7 @@ def __init__(self, path, repository=None, branch=None, uuid=None, workers=1, scheme="tei", verbose=False, ping=None, secret="", triggering_size=None, console=False, travis=False, finder=DefaultFinder, finderoptions=None, countwords=False, allowfailure=False, + from_travis_to_hook=False, **kwargs ): """ Create a Test object @@ -168,7 +169,10 @@ def __init__(self, path, self.uuid = uuid self.workers = workers self.ping = ping - self.secret = bytes(secret, "utf-8") + if os.environ.get("HOOK_SECRET"): + self.secret = os.environ.get("HOOK_SECRET").encode() + else: + self.secret = bytes(secret, "utf-8") self.scheme = scheme self.verbose = verbose self.countwords = countwords @@ -200,6 +204,8 @@ def __init__(self, path, else: self.finder = self.finder() + self.from_travis_to_hook = from_travis_to_hook + @property def successes(self): """ Get the number of successful tests @@ -297,17 +303,19 @@ def count_files(self): return len(self.text_files) + len(self.cts_files) def flush(self, stack): - """ Flush the remaining logs to the endpoint """ + """ Flush the remaining logs to the endpoint + + """ if len(stack) > 0: for needle in stack: needle.sent = True self.send({"units": [needle.dict for needle in stack]}) def send(self, data): - """ + """ Send data to self.ping URL - :param data: - :return: + :param data: Data to send + :return: Result of request """ if isinstance(data, dict): data = Test.dump(data) @@ -316,7 +324,7 @@ def send(self, data): data = bytes(data, "utf-8") hashed = hmac.new(self.secret, data, hashlib.sha1).hexdigest() - requests.post( + return requests.post( self.ping, data=data, headers={"HookTest-Secure-X": hashed, "HookTest-UUID": self.uuid} @@ -456,16 +464,20 @@ def start(self): }) def download(self): + """ Information to send or print during download + + """ if self.console: print("\n".join([f for f in self.progress.json if f]), flush=True) def middle(self): - """ - to print out the results for the metadata files that failed the tests + """ to print out the results for the metadata files that failed the tests + :return: :rtype: """ self.m_files = self.m_passing = len(self.results.values()) + if self.travis and self.verbose: print('', flush=True) if False not in [unit.status for unit in self.results.values()]: @@ -596,8 +608,14 @@ def end(self): for l, words in language_words.items(): results_table.add_row(["Words in {}".format(l.upper()), "{:,}".format(words)]) print(results_table, flush=True) - #print(black('#*# texts={texts} texts_passing={t_pass} metadata={meta} metadata_passing={m_pass} coverage_units={cov} total_nodes={nodes} words={words}'.format( - # texts=num_texts, t_pass=t_pass, meta=self.m_files, m_pass=self.m_passing, cov=cov, nodes="{:,}".format(total_units), words="{:,}".format(total_words)))) + + # Pushing to HOOK ! + if isinstance(self.from_travis_to_hook, str): + args = [num_texts, t_pass, self.m_files, self.m_passing, cov, total_units] + if self.countwords is True: + args.append(language_words) + print(self.send_to_hook_from_travis(*args).text) + #Manifest of passing files passing = self.create_manifest() with open('{}/manifest.txt'.format(self.path), mode="w") as f: @@ -615,6 +633,52 @@ def end(self): report["units"] = [unit.dict for unit in self.stack] self.send(report) + def send_to_hook_from_travis( + self, texts_total, texts_passing, + metadata_total, metadata_passing, + coverage, nodes_count, + words_dict=None + ): + """ Send data to travis + + :return: Request output + """ + data = dict( + # Event + event_type=os.environ.get("TRAVIS_EVENT_TYPE"), + + travis_build_id=os.environ.get("TRAVIS_BUILD_NUMBER"), + travis_uri=os.environ.get("TRAVIS_BUILD_NUMBER"), + sha=os.environ.get("TRAVIS_COMMIT_MESSAGE"), + + # Information about the test + texts_total=texts_total, + texts_passing=texts_passing, + metadata_total=metadata_total, + metadata_passing=metadata_passing, + coverage=coverage, + nodes_count=nodes_count, + units={ + unit_name: log.status for unit_name, log in self.results.items() + }, + ) + if data["event_type"] == "pull_request": + data["source"] = os.environ.get("TRAVIS_PULL_REQUEST") + else: + data["source"] = os.environ.get("TRAVIS_BRANCH") + + if words_dict is not None: + data["words_count"] = words_dict + + data = Test.dump(data) + data = bytes(data, "utf-8") + hashed = hmac.new(self.secret, data, hashlib.sha1).hexdigest() + return requests.post( + self.from_travis_to_hook, + data=data, + headers={"HookTest-Secure-X": hashed} + ) + def create_manifest(self): """ Creates a manifest.txt file in the source directory that contains an ordered list of passing files """