-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBuild.PackageGuard.cs
More file actions
60 lines (54 loc) · 3.04 KB
/
Copy pathBuild.PackageGuard.cs
File metadata and controls
60 lines (54 loc) · 3.04 KB
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
using Fallout.Common;
using Fallout.Common.Git;
using Fallout.Common.IO;
using Fallout.Common.Tooling;
using Fallout.Common.Tools.PackageGuard;
using Fallout.Common.Utilities;
using Fallout.Components;
partial class Build
{
// PackageGuard's own CLI only exposes an env-var override for the risk-report path
// (see PackageGuard.json's help text on ReportRisk) — there's no `--report-risk <path>`
// argument in our wrapper, matching the repo-wide convention that bool CLI flags stay
// presence-only. Setting this process env var instead pins the SARIF/HTML pair to a
// deterministic path we can reference from CI (upload-sarif, the release asset step).
const string PackageGuardReportRiskPathOverrideEnvironmentVariable = "PACKAGEGUARD_REPORT_RISK_PATH_OVERRIDE";
AbsolutePath PackageGuardDirectory => OutputDirectory / "packageguard";
AbsolutePath PackageGuardSbomFile => PackageGuardDirectory / "sbom.json";
AbsolutePath PackageGuardSarifFile => PackageGuardDirectory / "risk-report.sarif";
// The SBOM and risk report (HTML + SARIF) are only worth generating where they're actually
// consumed: security-scan.yml uploads the SARIF on a push to one of these four branches, and
// the release workflow attaches the SBOM/HTML to the GitHub Release. Neither happens on a PR
// (build.yml checks out the contributor's own branch via github.head_ref, never one of
// these), or on a tag-triggered release checkout (detached HEAD — hence the
// GitHubActions.Workflow fallback, since that workflow's own validate-ref job already
// proved the tag is reachable from a production branch).
bool IsOnLongLivedBranch =>
GitRepository.IsOnMainBranch() ||
GitRepository.IsOnDevelopBranch() ||
GitRepository.IsOnReleaseBranch() ||
GitRepository.IsOnSupportBranch() ||
GitHubActions?.Workflow == ReleaseWorkflow;
// Runs unconditionally — this is the PR gate's policy-violation check (build.yml), so it has
// to run on every branch, including a contributor's feature branch. Only the SBOM/risk-report
// generation is restricted to the four long-lived branches, via IsOnLongLivedBranch below.
Target PackageGuard => _ => _
.DependsOn<IRestore>()
.Produces(PackageGuardSbomFile)
.Produces(PackageGuardSarifFile)
.Produces(PackageGuardDirectory / "*.html")
.Executes(() =>
{
var generateReports = IsOnLongLivedBranch;
if (generateReports)
PackageGuardDirectory.CreateOrCleanDirectory();
PackageGuardTasks.PackageGuard(_ => _
.SetProjectPath(Solution.Path)
.SetGitHubApiKey(From<ICreateGitHubRelease>().GitHubToken)
.When(generateReports, _ => _
.EnableReportRisk()
.SetSbom(SbomFormat.cyclonedx)
.SetSbomOutput(PackageGuardSbomFile)
.SetProcessEnvironmentVariable(PackageGuardReportRiskPathOverrideEnvironmentVariable, PackageGuardSarifFile)));
});
}