Created
June 26, 2024 12:44
-
-
Save michitux/a45bff079875be1d0802f7ab8e6b16c2 to your computer and use it in GitHub Desktop.
A script to get the maven commands of all failing tests of a Jenkins build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
import collections | |
import argparse | |
def get_failing_tests_by_block(base_url): | |
url = base_url + '/testReport/api/json' | |
response = requests.get(url) | |
data = response.json() | |
failing_tests_by_block = collections.defaultdict(list) | |
for suite in data['suites']: | |
for case in suite['cases']: | |
if case['status'] != 'PASSED' and case['status'] != 'SKIPPED' and case['status'] != 'FIXED': | |
failing_tests_by_block[suite['enclosingBlocks'][0]].append(case['className'] + '.' + case['name']) | |
return failing_tests_by_block | |
def get_maven_commands(base_url, failing_tests_by_block): | |
maven_commands = [] | |
# Get information about the build to get the blocks with failures | |
for block in failing_tests_by_block.keys(): | |
url = base_url + '/execution/node/' + block + '/wfapi/describe' | |
response = requests.get(url) | |
data = response.json() | |
stages = data['stageFlowNodes'] | |
for stage in stages: | |
# Check if the parameter description starts with "mvn " | |
if stage['parameterDescription'].startswith('mvn '): | |
print("Failing stage: " + data['name']) | |
print("Failing tests:") | |
for test in failing_tests_by_block[block]: | |
print(test) | |
print('Maven command: ' + stage['parameterDescription']) | |
maven_commands.append(stage['parameterDescription']) | |
return maven_commands | |
if __name__ == '__main__': | |
# Get the build URL from the command line | |
parser = argparse.ArgumentParser(description='Get failing tests from a Jenkins build') | |
parser.add_argument('build_url', help='The URL of the Jenkins build') | |
args = parser.parse_args() | |
base_url = args.build_url | |
failing_tests_by_block = get_failing_tests_by_block(base_url) | |
maven_commands = get_maven_commands(base_url, failing_tests_by_block) | |
print('\n'.join(maven_commands)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment