Vault Automation f2872f0cc7
[VAULT-39158] actions(build-hcp-image): various small fixes (#9207) (#9230)
* [VAULT-39158] actions(build-hcp-image): various small fixes

Various small fixes to correctly trigger custom image builds and wait
for them to be available.

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2025-09-09 22:11:09 +00:00

129 lines
3.0 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package github
import (
"context"
"fmt"
"log/slog"
gh "github.com/google/go-github/v74/github"
slogctx "github.com/veqryn/slog-context"
)
// getWorkflow attempts to locate the workflow associated with our workflow name.
func getWorkflow(
ctx context.Context,
client *gh.Client,
owner string,
repo string,
name string,
) (*gh.Workflow, error) {
slog.Default().DebugContext(slogctx.Append(ctx,
slog.String("owner", owner),
slog.String("repo", repo),
slog.String("name", name),
), "getting github actions workflow")
opts := &gh.ListOptions{PerPage: PerPageMax}
for {
wfs, res, err := client.Actions.ListWorkflows(ctx, owner, repo, opts)
if err != nil {
return nil, err
}
for _, wf := range wfs.Workflows {
if wf.GetName() == name {
return wf, nil
}
}
if res.NextPage == 0 {
return nil, fmt.Errorf("no workflow matching %s could be found", name)
}
opts.Page = res.NextPage
}
}
// getWorkflowRuns gets the workflow runs associated with a workflow ID.
func getWorkflowRuns(
ctx context.Context,
client *gh.Client,
owner string,
repo string,
id int64,
opts *gh.ListWorkflowRunsOptions,
) ([]*WorkflowRun, error) {
var runs []*WorkflowRun
opts.ListOptions = gh.ListOptions{PerPage: PerPageMax}
// By default our status will be "success" which elimates in_progress runs.
// Instead, we'll try both so that we're sure to include what's actually
// running along with historical runs.
for _, status := range []string{"success", "in_progress"} {
for {
opts.Status = status
slog.Default().DebugContext(slogctx.Append(ctx,
slog.String("owner", owner),
slog.String("repo", repo),
slog.Int64("id", id),
slog.String("query-status", opts.Status),
), "getting github actions workflow runs")
wfrs, res, err := client.Actions.ListWorkflowRunsByID(ctx, owner, repo, id, opts)
if err != nil {
return nil, err
}
for _, r := range wfrs.WorkflowRuns {
runs = append(runs, &WorkflowRun{Run: r})
}
if res.NextPage == 0 {
break
}
opts.ListOptions.Page = res.NextPage
}
}
return runs, nil
}
// getWorkflowRunArtifacts gets the artifacts associated with a workflow run
func getWorkflowRunArtifacts(
ctx context.Context,
client *gh.Client,
owner string,
repo string,
id int64,
) (gh.ArtifactList, error) {
slog.Default().DebugContext(slogctx.Append(ctx,
slog.String("owner", owner),
slog.String("repo", repo),
slog.Int64("id", id),
), "getting github actions workflow run artifacts")
opts := &gh.ListOptions{PerPage: PerPageMax}
artifacts := gh.ArtifactList{}
for {
arts, res, err := client.Actions.ListWorkflowRunArtifacts(ctx, owner, repo, id, opts)
if err != nil {
return artifacts, err
}
newTotal := artifacts.GetTotalCount() + arts.GetTotalCount()
artifacts.TotalCount = &newTotal
artifacts.Artifacts = append(artifacts.Artifacts, arts.Artifacts...)
if res.NextPage == 0 {
return artifacts, nil
}
opts.Page = res.NextPage
}
}