Skip to content

Commit 9a57133

Browse files
committed
test(integration): handle missing steps info in GitHub API response
Signed-off-by: behnazh-w <[email protected]>
1 parent 4235041 commit 9a57133

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

src/macaron/slsa_analyzer/checks/infer_artifact_pipeline_check.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from macaron.config.defaults import defaults
1414
from macaron.database.table_definitions import CheckFacts
15-
from macaron.errors import InvalidHTTPResponseError, ProvenanceError
15+
from macaron.errors import GitHubActionsValueError, InvalidHTTPResponseError, ProvenanceError
1616
from macaron.json_tools import json_extract
1717
from macaron.repo_finder.provenance_extractor import ProvenancePredicate
1818
from macaron.slsa_analyzer.analyze_context import AnalyzeContext
@@ -219,17 +219,22 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData:
219219
return CheckResultData(result_tables=[], result_type=CheckResultType.FAILED)
220220

221221
# Find the workflow runs that have potentially triggered the artifact publishing.
222-
html_urls = ci_service.workflow_run_in_date_time_range(
223-
repo_full_name=ctx.component.repository.full_name,
224-
workflow=build_entry_point,
225-
publish_date_time=artifact_published_date,
226-
commit_date_time=commit_date,
227-
job_id=job_id,
228-
step_name=step_name,
229-
step_id=step_id,
230-
time_range=publish_time_range,
231-
callee_node_type=callee_node_type,
232-
)
222+
html_urls = set()
223+
try:
224+
html_urls = ci_service.workflow_run_in_date_time_range(
225+
repo_full_name=ctx.component.repository.full_name,
226+
workflow=build_entry_point,
227+
publish_date_time=artifact_published_date,
228+
commit_date_time=commit_date,
229+
job_id=job_id,
230+
step_name=step_name,
231+
step_id=step_id,
232+
time_range=publish_time_range,
233+
callee_node_type=callee_node_type,
234+
)
235+
except GitHubActionsValueError as error:
236+
logger.debug(error)
237+
ci_run_deleted = True
233238

234239
# If provenance exists, we expect the timestamp of the reported triggered run
235240
# to be within an acceptable range, have succeeded, and called the deploy command.

src/macaron/slsa_analyzer/ci_service/github_actions/github_actions_ci.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from macaron.code_analyzer.call_graph import BaseNode, CallGraph
1414
from macaron.config.defaults import defaults
1515
from macaron.config.global_config import global_config
16-
from macaron.errors import CallGraphError, ParseError
16+
from macaron.errors import CallGraphError, GitHubActionsValueError, ParseError
1717
from macaron.parsers.bashparser import BashNode, BashScriptType
1818
from macaron.slsa_analyzer.build_tool.base_build_tool import BaseBuildTool, BuildToolCommand
1919
from macaron.slsa_analyzer.ci_service.base_ci_service import BaseCIService
@@ -333,6 +333,11 @@ def workflow_run_in_date_time_range(
333333
-------
334334
set[str]
335335
The set of URLs found for the workflow within the time range.
336+
337+
Raises
338+
------
339+
GitHubActionsValueError
340+
This error is raised when the GitHub Action workflow run misses values.
336341
"""
337342
logger.debug(
338343
"Getting the latest workflow run of %s at publishing time %s and source commit date %s within time range %s.",
@@ -377,6 +382,8 @@ def workflow_run_in_date_time_range(
377382

378383
# Find the matching step and check its `conclusion` and `started_at` attributes.
379384
html_url = None
385+
if not run_jobs["jobs"]:
386+
raise GitHubActionsValueError("GitHub Actions workflow run misses jobs information.")
380387
for job in run_jobs["jobs"]:
381388
# If the deploy step is a Reusable Workflow, there won't be any steps in the caller job.
382389
if callee_node_type == GitHubWorkflowType.REUSABLE.value:
@@ -393,6 +400,11 @@ def workflow_run_in_date_time_range(
393400
html_url = item["html_url"]
394401
break
395402

403+
if not job["steps"]:
404+
raise GitHubActionsValueError(
405+
f"GitHub Actions workflow run misses steps information for the {job_id} job ID."
406+
)
407+
396408
for step in job["steps"]:
397409
if step["name"] not in [step_name, step_id] or step["conclusion"] != "success":
398410
continue

tests/integration/cases/micronaut-projects_micronaut-test/micronaut-test.dl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@ Policy("test_policy", component_id, "") :-
1414
build_tool_check(gradle_id, "gradle", "java"),
1515
check_facts(gradle_id, _, component_id,_,_),
1616
check_passed(component_id, "mcn_provenance_level_three_1"),
17-
check_passed(component_id, "mcn_find_artifact_pipeline_1"),
1817
check_failed(component_id, "mcn_provenance_derived_commit_1"),
1918
check_failed(component_id, "mcn_provenance_witness_level_one_1"),
2019
check_failed(component_id, "mcn_trusted_builder_level_three_1"),
21-
is_repo_url(component_id, "https://github.com/micronaut-projects/micronaut-test").
20+
is_repo_url(component_id, "https://github.com/micronaut-projects/micronaut-test"),
21+
// The GitHub API for some reasons does not return the steps information anymore.
22+
// Note that mcn_find_artifact_pipeline_1 fails because it returns UNKNOWN, in this case with low confidence.
23+
check_failed_with_confidence(component_id, "mcn_find_artifact_pipeline_1", confidence),
24+
confidence = 0.4,
25+
artifact_pipeline_check(
26+
apc_check_id,
27+
"https://github.com/micronaut-projects/micronaut-test/blob/0ffa4e86ee4311f744f1a2b8ccd740a15af3a52b/.github/workflows/release.yml",
28+
"release",
29+
"publish",
30+
_,
31+
1, // From provenance.
32+
1, // Run deleted.
33+
0 // Published before the code was committed.
34+
),
35+
check_facts(apc_check_id, confidence, component_id,_,_).
2236

2337
apply_policy_to("test_policy", component_id) :-
2438
is_component(component_id, purl),

0 commit comments

Comments
 (0)