-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_api_reporter.py
199 lines (161 loc) · 6.96 KB
/
build_api_reporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
import os
import glob
import json
import requests
import subprocess
from pathlib import Path
from typing import List, Dict, Optional, Union
from dataclasses import dataclass
import xml.etree.ElementTree as ET
import yaml
from dotenv import load_dotenv
import argparse
@dataclass
class KetryxConfig:
ketryx_url: str
project: str
api_key: str
commit_sha: Optional[str]
version: Optional[str]
class KetryxReporter:
def __init__(self, config: KetryxConfig):
self.config = config
self.base_url = config.ketryx_url.rstrip('/')
self.headers = {
'Authorization': f'Bearer {config.api_key}',
'Content-Type': 'application/json'
}
def upload_artifact(self, file_path: str, content_type: str = 'application/octet-stream') -> str:
"""Upload a single artifact file to Ketryx."""
url = f"{self.base_url}/api/v1/build-artifacts"
params = {'project': self.config.project}
with open(file_path, 'rb') as f:
files = {'file': (os.path.basename(file_path), f, content_type)}
response = requests.post(url, params=params, headers={'Authorization': self.headers['Authorization']}, files=files)
if response.status_code != 200:
raise Exception(f"Failed to upload artifact: {response.text}")
return response.json()['id']
def upload_test_results(self, junit_paths: List[str], cucumber_paths: List[str]) -> List[Dict]:
"""Upload all test results and return artifact data."""
artifacts = []
# Upload JUnit XML results
for pattern in junit_paths:
for file_path in glob.glob(pattern):
artifact_id = self.upload_artifact(file_path, 'application/xml')
artifacts.append({
'id': artifact_id,
'type': 'junit-xml'
})
# Upload Cucumber JSON results
for pattern in cucumber_paths:
for file_path in glob.glob(pattern):
artifact_id = self.upload_artifact(file_path, 'application/json')
artifacts.append({
'id': artifact_id,
'type': 'cucumber-json'
})
return artifacts
def upload_sbom(self, sbom_paths: List[str], sbom_type: str) -> List[Dict]:
"""Upload SBOM files.
Args:
sbom_paths: List of file paths to SBOM files
sbom_type: Type of SBOM (e.g., 'cyclonedx', 'spdx')
"""
artifacts = []
for pattern in sbom_paths:
for file_path in glob.glob(pattern):
artifact_id = self.upload_artifact(file_path, 'application/json')
artifacts.append({
'id': artifact_id,
'type': f'{sbom_type}-json'
})
return artifacts
def report_build(self, build_name: str, artifacts: List[Dict]) -> Dict:
"""Report build data to Ketryx."""
url = f"{self.base_url}/api/v1/builds"
data = {
'project': self.config.project,
'buildName': build_name,
'artifacts': artifacts,
'sourceUrl': os.getenv('GITHUB_SERVER_URL', ''),
'repositoryUrls': [f"{os.getenv('GITHUB_SERVER_URL', '')}/{os.getenv('GITHUB_REPOSITORY', '')}"],
}
# If version is specified, it takes precedence over commitSha
if self.config.version:
data['version'] = self.config.version
elif self.config.commit_sha:
data['commitSha'] = self.config.commit_sha
response = requests.post(url, headers=self.headers, json=data)
if response.status_code != 200:
raise Exception(f"Failed to report build: {response.text}")
return response.json()
def process_build_config(config_file: str) -> List[Dict]:
"""Process the YAML config file and return build artifacts."""
with open(config_file, 'r') as f:
build_config = yaml.safe_load(f)
if not build_config or 'builds' not in build_config:
raise ValueError("Invalid config file: missing 'builds' section")
# Collect all missing files
missing_files = []
for build in build_config['builds']:
if build['type'] == 'test-results':
for junit_path in build['artifacts'].get('junit', []):
if not glob.glob(junit_path):
missing_files.append(f"JUnit file not found: {junit_path}")
for cucumber_path in build['artifacts'].get('cucumber', []):
if not glob.glob(cucumber_path):
missing_files.append(f"Cucumber file not found: {cucumber_path}")
elif build['type'] == 'sbom':
for artifact in build['artifacts']:
if not glob.glob(artifact['file']):
missing_files.append(f"SBOM file not found: {artifact['file']}")
if missing_files:
print("\nMissing files:")
for file in missing_files:
print(f" - {file}")
import sys; sys.exit(1)
return build_config['builds']
def main():
# Add argument parser
parser = argparse.ArgumentParser(description='Process build configuration and report to Ketryx')
parser.add_argument('config_file', help='Path to the YAML configuration file')
args = parser.parse_args()
# Load environment variables from .env file
load_dotenv()
# Configuration from environment variables
config = KetryxConfig(
ketryx_url=os.getenv('KETRYX_URL'),
project=os.getenv('KETRYX_PROJECT'),
api_key=os.getenv('KETRYX_API_KEY'),
version=os.getenv('KETRYX_VERSION'),
commit_sha=os.getenv('GITHUB_SHA')
)
if not all([config.project, config.api_key, config.version]):
raise ValueError("Missing required environment variables")
reporter = KetryxReporter(config)
# Run tests and collect results
try:
builds = process_build_config(args.config_file)
for build in builds:
if build['type'] == 'test-results':
artifacts = reporter.upload_test_results(
junit_paths=build['artifacts'].get('junit', []),
cucumber_paths=build['artifacts'].get('cucumber', [])
)
elif build['type'] == 'sbom':
for artifact in build['artifacts']:
artifacts = reporter.upload_sbom(
sbom_paths=[artifact['file']],
sbom_type=artifact['type']
)
else:
print(f"Warning: Unknown build type '{build['type']}'")
continue
build_result = reporter.report_build(build['name'], artifacts)
print(f"Reported {build['name']} to Ketryx: {build_result.get('buildId')}")
except Exception as e:
print(f"Error: {e}")
raise
if __name__ == '__main__':
main()