From aa5a1e9b835fd1fab6f898a0d7569aaee131a524 Mon Sep 17 00:00:00 2001 From: summerguo-protectai Date: Mon, 31 Mar 2025 17:56:14 -0700 Subject: [PATCH] add status in workflow runs --- pkg/github/client/client.go | 29 ++++++++++++++------------- pkg/github/datasource.go | 1 + pkg/github/workflows.go | 2 +- pkg/models/client.go | 2 +- pkg/models/workflows.go | 3 +++ src/views/QueryEditorWorkflowRuns.tsx | 18 +++++++++++++++++ 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/pkg/github/client/client.go b/pkg/github/client/client.go index bba6e6b8..463ed364 100644 --- a/pkg/github/client/client.go +++ b/pkg/github/client/client.go @@ -178,7 +178,7 @@ func (client *Client) GetWorkflowUsage(ctx context.Context, owner, repo, workflo } var workflowRuns []*googlegithub.WorkflowRun var err error - workflowRuns, page, err = client.getWorkflowRuns(ctx, owner, repo, workflow, "", timeRange, page) + workflowRuns, page, err = client.getWorkflowRuns(ctx, owner, repo, workflow, "", timeRange, page, runStatusCompleted) if err != nil { return models.WorkflowUsage{}, fmt.Errorf("fetching workflow runs: %w", err) } @@ -281,7 +281,7 @@ func (client *Client) getWorkflowUsage(ctx context.Context, owner, repo string, return client.restClient.Actions.GetWorkflowUsageByFileName(ctx, owner, repo, workflow) } -func (client *Client) GetWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, timeRange backend.TimeRange) ([]*googlegithub.WorkflowRun, error) { +func (client *Client) GetWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, status string, timeRange backend.TimeRange) ([]*googlegithub.WorkflowRun, error) { workflowRuns := []*googlegithub.WorkflowRun{} page := 1 @@ -290,7 +290,7 @@ func (client *Client) GetWorkflowRuns(ctx context.Context, owner, repo, workflow break } - workflowRunsPage, nextPage, err := client.getWorkflowRuns(ctx, owner, repo, workflow, branch, timeRange, page) + workflowRunsPage, nextPage, err := client.getWorkflowRuns(ctx, owner, repo, workflow, branch, status, timeRange, page) if err != nil { return nil, fmt.Errorf("fetching workflow runs: %w", err) } @@ -303,7 +303,7 @@ func (client *Client) GetWorkflowRuns(ctx context.Context, owner, repo, workflow return workflowRuns, nil } -func (client *Client) getWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, timeRange backend.TimeRange, page int) ([]*googlegithub.WorkflowRun, int, error) { +func (client *Client) getWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, status string, timeRange backend.TimeRange, page int) ([]*googlegithub.WorkflowRun, int, error) { workflowID, _ := strconv.ParseInt(workflow, 10, 64) workflowRuns := []*googlegithub.WorkflowRun{} @@ -317,18 +317,19 @@ func (client *Client) getWorkflowRuns(ctx context.Context, owner, repo, workflow err error ) + listOptions := &googlegithub.ListWorkflowRunsOptions{ + Created: created, + ListOptions: googlegithub.ListOptions{Page: page, PerPage: 100}, + Branch: branch, + } + if status != "" { + listOptions.Status = status + } + if workflowID > 0 { - runs, response, err = client.restClient.Actions.ListWorkflowRunsByID(ctx, owner, repo, workflowID, &googlegithub.ListWorkflowRunsOptions{ - Created: created, - ListOptions: googlegithub.ListOptions{Page: page, PerPage: 100}, - Branch: branch, - }) + runs, response, err = client.restClient.Actions.ListWorkflowRunsByID(ctx, owner, repo, workflowID, listOptions) } else { - runs, response, err = client.restClient.Actions.ListWorkflowRunsByFileName(ctx, owner, repo, workflow, &googlegithub.ListWorkflowRunsOptions{ - Created: created, - ListOptions: googlegithub.ListOptions{Page: page, PerPage: 100}, - Branch: branch, - }) + runs, response, err = client.restClient.Actions.ListWorkflowRunsByFileName(ctx, owner, repo, workflow, listOptions) } if err != nil { diff --git a/pkg/github/datasource.go b/pkg/github/datasource.go index 209c0f9c..fc75e979 100644 --- a/pkg/github/datasource.go +++ b/pkg/github/datasource.go @@ -190,6 +190,7 @@ func (d *Datasource) HandleWorkflowRunsQuery(ctx context.Context, query *models. Owner: query.Owner, Workflow: query.Options.Workflow, Branch: query.Options.Branch, + Status: query.Options.Status, } return GetWorkflowRuns(ctx, d.client, opt, req.TimeRange) diff --git a/pkg/github/workflows.go b/pkg/github/workflows.go index 3d333acc..1c3e071d 100644 --- a/pkg/github/workflows.go +++ b/pkg/github/workflows.go @@ -237,7 +237,7 @@ func GetWorkflowRuns(ctx context.Context, client models.Client, opts models.Work return nil, nil } - workflowRuns, err := client.GetWorkflowRuns(ctx, opts.Owner, opts.Repository, opts.Workflow, opts.Branch, timeRange) + workflowRuns, err := client.GetWorkflowRuns(ctx, opts.Owner, opts.Repository, opts.Workflow, opts.Branch, opts.Status, timeRange) if err != nil { return nil, err } diff --git a/pkg/models/client.go b/pkg/models/client.go index 92c12874..a162f2db 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -13,5 +13,5 @@ type Client interface { Query(ctx context.Context, q interface{}, variables map[string]interface{}) error ListWorkflows(ctx context.Context, owner, repo string, opts *googlegithub.ListOptions) (*googlegithub.Workflows, *googlegithub.Response, error) GetWorkflowUsage(ctx context.Context, owner, repo, workflow string, timeRange backend.TimeRange) (WorkflowUsage, error) - GetWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, timeRange backend.TimeRange) ([]*googlegithub.WorkflowRun, error) + GetWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, status string, timeRange backend.TimeRange) ([]*googlegithub.WorkflowRun, error) } diff --git a/pkg/models/workflows.go b/pkg/models/workflows.go index 6afdaccf..bc07ff08 100644 --- a/pkg/models/workflows.go +++ b/pkg/models/workflows.go @@ -37,6 +37,9 @@ type WorkflowUsageOptions struct { // Branch is the branch to filter the runs by. Branch string `json:"branch"` + + // Status is the status to filter the runs by (e.g. completed, in_progress, etc.) + Status string `json:"status"` } type WorkflowRunsOptions = WorkflowUsageOptions diff --git a/src/views/QueryEditorWorkflowRuns.tsx b/src/views/QueryEditorWorkflowRuns.tsx index 55072671..8f3a99cd 100644 --- a/src/views/QueryEditorWorkflowRuns.tsx +++ b/src/views/QueryEditorWorkflowRuns.tsx @@ -10,6 +10,7 @@ interface Props extends WorkflowRunsOptions { const QueryEditorWorkflowRuns = (props: Props) => { const [workflow, setWorkflow] = useState(props.workflow); const [branch, setBranch] = useState(props.branch); + const [status, setStatus] = useState(props.status); return ( <> @@ -47,6 +48,23 @@ const QueryEditorWorkflowRuns = (props: Props) => { } /> + + setStatus(el.currentTarget.value)} + onBlur={(el) => + props.onChange({ + ...props, + status: el.currentTarget.value, + }) + } + /> + ); };