diff --git a/.changeset/short-steps-run.md b/.changeset/short-steps-run.md new file mode 100644 index 00000000..de1e9b58 --- /dev/null +++ b/.changeset/short-steps-run.md @@ -0,0 +1,5 @@ +--- +'grafana-github-datasource': minor +--- + +Saved **Workflow Runs** queries with a blank **Workflow** field previously failed. They now return all runs across the repository that match the query's filters. Use the new **Workflow Job Steps** query to analyze individual GitHub Actions job step durations. diff --git a/docs/sources/configure.md b/docs/sources/configure.md index 08397a5f..1c1b5adb 100644 --- a/docs/sources/configure.md +++ b/docs/sources/configure.md @@ -127,6 +127,12 @@ The following scopes are required for classic personal access tokens: | `read:project` | Read project data. | | `repo` | Full control of private repositories. Required only if you need to query private repositories. | +For fine-grained personal access tokens, grant the following repository permission to use the **Workflow Runs** and **Workflow Job Steps** query types: + +| Permission | Access level | +| ----------- | ------------ | +| **Actions** | Read-only | + ### GitHub App GitHub App authentication provides better security and fine-grained access to resources compared to personal access tokens. @@ -157,6 +163,7 @@ The following repository permissions are required: | **Packages** | Read-only | | **Repository security advisories** | Read-only | | **Projects** | Read-only | +| **Actions** (for **Workflow Runs** and **Workflow Job Steps**) | Read-only | ### Code scanning permissions diff --git a/docs/sources/query-editor.md b/docs/sources/query-editor.md index 049a3cd9..1f9b8fac 100644 --- a/docs/sources/query-editor.md +++ b/docs/sources/query-editor.md @@ -54,7 +54,8 @@ Select a query type from the **Query Type** drop-down in the query editor: - [**Vulnerabilities**](#vulnerabilities): Query security vulnerabilities detected in a repository. - [**Webhook deliveries**](#webhook-deliveries): List deliveries of an organization or repository webhook, including the response status code. - [**Workflows**](#workflows): List GitHub Actions workflows defined in a repository. -- [**Workflow runs**](#workflow-runs): List runs for a specific workflow, including status, conclusion, and timing information. +- [**Workflow runs**](#workflow-runs): List runs across a repository or filter by workflow, including status, conclusion, and timing information. +- [**Workflow job steps**](#workflow-job-steps): List individual job steps and their durations across workflow runs. - [**Workflow usage**](#workflow-usage): Retrieve usage statistics for a workflow, such as run counts and durations. ### Code scanning @@ -1045,7 +1046,7 @@ Show all workflows created within the `grafana/grafana` repository within the cu ### Workflow runs -List runs for a specific workflow, including status, conclusion, and timing information. +List runs across a repository or filter by a specific workflow. The Grafana time range selects runs by creation time. GitHub limits time-filtered results to 1,000 runs; use a narrower range for busy repositories. #### Query options @@ -1053,12 +1054,14 @@ List runs for a specific workflow, including status, conclusion, and timing info |------|-------------|----------| | Owner | GitHub user or organization that owns the repository | Yes | | Repository | Name of the repository | Yes | -| Workflow | The workflow ID or file name. Use `id` or the filename from `path` from [Workflows](#workflows) queries. | Yes | +| Workflow | Leave empty for all workflows, or enter a workflow ID or file name from [Workflows](#workflows). | No | | Branch | The head branch to filter on | No | ##### Sample queries -Show all completed runs for the `Levitate` workflow in the `grafana/grafana` repository: +To compare run outcomes across a repository, set **Owner** and **Repository**, and leave **Workflow** and **Branch** empty. In the panel, use **Group by** on `conclusion` and **Count** on `id`. To compare workflows side by side, also group by `workflow_id`. + +Show runs for the `Levitate` workflow in the `grafana/grafana` repository: - Owner: `grafana` - Repository: `grafana` @@ -1090,6 +1093,12 @@ Show workflow runs for a specific branch (useful for monitoring release branch C | workflow_id | Unique identifier for the workflow definition | | run_number | The run number for this workflow run in the repository | +### Workflow job steps + +List individual steps for the latest attempt of each matching workflow run. Use the same owner, repository, optional workflow and branch filters, and run creation time range as [Workflow runs](#workflow-runs). This detail query fetches jobs separately for each run, so it inspects at most the 50 most recent matching runs and reports a warning if the result is truncated. Use **Workflow Runs** for repository-wide run counts. + +The table contains one row per step and includes workflow, run and attempt, job and link, step number and name, status, conclusion, start and completion times, and `duration_seconds`. Skipped steps and steps without valid start and completion times have no duration. For a bottleneck panel, group by `workflow_name`, `job_name`, and `step_name`, then calculate the mean or maximum duration. + ### Workflow usage Retrieve usage statistics for a workflow, such as run counts and durations. diff --git a/pkg/github/client/client.go b/pkg/github/client/client.go index 9f8fdff0..f9849e02 100644 --- a/pkg/github/client/client.go +++ b/pkg/github/client/client.go @@ -7,6 +7,8 @@ import ( "net/http" "net/url" "strconv" + "strings" + "sync" "time" "github.com/bradleyfalzon/ghinstallation/v2" @@ -23,9 +25,11 @@ import ( // Client is a wrapper of GitHub clients that can access the GraphQL and rest API. type Client struct { - restClient *googlegithub.Client - graphqlClient *githubv4.Client - authType models.AuthType + restClient *googlegithub.Client + graphqlClient *githubv4.Client + authType models.AuthType + workflowJobsMu sync.Mutex + workflowJobs map[workflowJobsKey]workflowJobsPage } const ( @@ -407,6 +411,61 @@ func (client *Client) getWorkflowUsage(ctx context.Context, owner, repo string, return client.restClient.Actions.GetWorkflowUsageByFileName(ctx, owner, repo, workflow) } +type workflowJobsKey struct { + owner, repo string + run, attempt int64 + page, perPage int +} + +type workflowJobsPage struct { + jobs []*googlegithub.WorkflowJob + next int +} + +// ListWorkflowJobs returns a page for the observed run attempt. Only completed, +// explicitly identified attempts are cached, per authenticated data source client. +func (client *Client) ListWorkflowJobs(ctx context.Context, owner, repo string, run *googlegithub.WorkflowRun, opts googlegithub.ListOptions) ([]*googlegithub.WorkflowJob, int, error) { + id, attempt := run.GetID(), int64(run.GetRunAttempt()) + key := workflowJobsKey{strings.ToLower(owner), strings.ToLower(repo), id, attempt, opts.Page, opts.PerPage} + cacheable := attempt > 0 && run.GetStatus() == runStatusCompleted + if cacheable { + client.workflowJobsMu.Lock() + cached, found := client.workflowJobs[key] + client.workflowJobsMu.Unlock() + if found { + return cached.jobs, cached.next, nil + } + } + var jobs *googlegithub.Jobs + var response *googlegithub.Response + var err error + if attempt > 0 { + jobs, response, err = client.restClient.Actions.ListWorkflowJobsAttempt(ctx, owner, repo, id, attempt, &opts) + } else { + jobs, response, err = client.restClient.Actions.ListWorkflowJobs(ctx, owner, repo, id, &googlegithub.ListWorkflowJobsOptions{Filter: "latest", ListOptions: opts}) + } + if err != nil { + return nil, 0, addErrorSourceToError(err, response) + } + if cacheable { + client.workflowJobsMu.Lock() + if client.workflowJobs == nil { + client.workflowJobs = make(map[workflowJobsKey]workflowJobsPage) + } + // Keep memory bounded even when many dashboards query disjoint ranges. + const maxCachedJobPages = 512 + if _, exists := client.workflowJobs[key]; !exists && len(client.workflowJobs) >= maxCachedJobPages { + for old := range client.workflowJobs { + delete(client.workflowJobs, old) + break + } + } + client.workflowJobs[key] = workflowJobsPage{jobs.Jobs, response.NextPage} + client.workflowJobsMu.Unlock() + } + return jobs.Jobs, response.NextPage, nil +} + func (client *Client) GetWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, timeRange backend.TimeRange) ([]*googlegithub.WorkflowRun, error) { workflowRuns := []*googlegithub.WorkflowRun{} @@ -426,9 +485,17 @@ func (client *Client) GetWorkflowRuns(ctx context.Context, owner, repo, workflow } func (client *Client) getWorkflowRuns(ctx context.Context, owner, repo, workflow string, branch string, timeRange backend.TimeRange, page int) ([]*googlegithub.WorkflowRun, int, error) { - workflowID, _ := strconv.ParseInt(workflow, 10, 64) + runs, response, err := client.ListWorkflowRunsPage(ctx, owner, repo, workflow, branch, timeRange, googlegithub.ListOptions{Page: page, PerPage: 100}) + if err != nil { + return nil, 0, err + } + return runs.WorkflowRuns, response.NextPage, nil +} - workflowRuns := []*googlegithub.WorkflowRun{} +// ListWorkflowRunsPage fetches just the requested page, preserving the API's total and next page. +func (client *Client) ListWorkflowRunsPage(ctx context.Context, owner, repo, workflow, branch string, timeRange backend.TimeRange, page googlegithub.ListOptions) (*googlegithub.WorkflowRuns, *googlegithub.Response, error) { + workflow = strings.TrimSpace(workflow) + workflowID, _ := strconv.ParseInt(workflow, 10, 64) format := time.RFC3339 created := fmt.Sprintf("%s..%s", timeRange.From.Format(format), timeRange.To.Format(format)) @@ -439,29 +506,27 @@ func (client *Client) getWorkflowRuns(ctx context.Context, owner, repo, workflow err error ) - 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, - }) - } 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, - }) + opts := &googlegithub.ListWorkflowRunsOptions{ + Created: created, + ListOptions: page, + Branch: branch, + } + switch { + case workflow == "": + runs, response, err = client.restClient.Actions.ListRepositoryWorkflowRuns(ctx, owner, repo, opts) + case workflowID > 0: + runs, response, err = client.restClient.Actions.ListWorkflowRunsByID(ctx, owner, repo, workflowID, opts) + default: + runs, response, err = client.restClient.Actions.ListWorkflowRunsByFileName(ctx, owner, repo, workflow, opts) } if err != nil { // If the workflow is not found, return a specific error. - if response != nil && response.StatusCode == http.StatusNotFound { - return nil, 0, backend.DownstreamError(errWorkflowNotFound) + if workflow != "" && response != nil && response.StatusCode == http.StatusNotFound { + return nil, response, backend.DownstreamError(errWorkflowNotFound) } - return nil, 0, addErrorSourceToError(fmt.Errorf("fetching workflow runs: %w", err), response) + return nil, response, addErrorSourceToError(err, response) } - workflowRuns = append(workflowRuns, runs.WorkflowRuns...) - - return workflowRuns, response.NextPage, nil + return runs, response, nil } diff --git a/pkg/github/client/workflow_runs_test.go b/pkg/github/client/workflow_runs_test.go new file mode 100644 index 00000000..bb5ebb06 --- /dev/null +++ b/pkg/github/client/workflow_runs_test.go @@ -0,0 +1,115 @@ +package githubclient + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + "time" + + googlegithub "github.com/google/go-github/v84/github" + "github.com/grafana/grafana-plugin-sdk-go/backend" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestListWorkflowJobsCachesCompletedAttempts(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + assert.Equal(t, "100", r.URL.Query().Get("per_page")) + switch r.URL.Path { + case "/api/v3/repos/acme/widget/actions/runs/11/attempts/1/jobs": + w.Header().Set("Link", fmt.Sprintf("; rel=\"next\"", r.Host, r.URL.Path)) + fmt.Fprint(w, `{"jobs":[{"id":20,"run_attempt":1}]}`) + case "/api/v3/repos/acme/widget/actions/runs/11/attempts/2/jobs": + fmt.Fprint(w, `{"jobs":[{"id":21,"run_attempt":2}]}`) + case "/api/v3/repos/acme/widget/actions/runs/11/jobs": + assert.Equal(t, "latest", r.URL.Query().Get("filter")) + fmt.Fprint(w, `{"jobs":[{"id":21,"run_attempt":2}]}`) + default: + t.Errorf("unexpected path: %s", r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) + defer server.Close() + rest, err := googlegithub.NewClient(server.Client()).WithEnterpriseURLs(server.URL, server.URL) + require.NoError(t, err) + client := &Client{restClient: rest} + opts := googlegithub.ListOptions{Page: 1, PerPage: 100} + run := &googlegithub.WorkflowRun{ID: googlegithub.Ptr(int64(11)), RunAttempt: googlegithub.Ptr(1), Status: googlegithub.Ptr("completed")} + for range 2 { + jobs, next, err := client.ListWorkflowJobs(t.Context(), "acme", "widget", run, opts) + require.NoError(t, err) + require.Equal(t, 2, next) + require.Len(t, jobs, 1) + require.Equal(t, int64(20), jobs[0].GetID()) + } + require.Equal(t, 1, calls, "completed attempt should be served from cache on refresh") + + run.RunAttempt = googlegithub.Ptr(2) + run.Status = googlegithub.Ptr("in_progress") + for range 2 { + jobs, next, err := client.ListWorkflowJobs(t.Context(), "acme", "widget", run, opts) + require.NoError(t, err) + require.Zero(t, next) + require.Len(t, jobs, 1) + require.Equal(t, int64(21), jobs[0].GetID()) + } + require.Equal(t, 3, calls, "new and in-progress attempts must bypass old cached results") + + run.RunAttempt = nil + run.Status = googlegithub.Ptr("completed") + for range 2 { + _, _, err := client.ListWorkflowJobs(t.Context(), "acme", "widget", run, opts) + require.NoError(t, err) + } + require.Equal(t, 5, calls, "the mutable latest endpoint must not be cached") +} + +func TestGetWorkflowRunsEndpoints(t *testing.T) { + const base = "/api/v3/repos/acme/widget/actions/" + for _, tc := range []struct { + name, workflow, path string + }{ + {"all workflows", "", base + "runs"}, + {"blank workflow", " ", base + "runs"}, + {"by id", "42", base + "workflows/42/runs"}, + {"by filename", "ci.yml", base + "workflows/ci.yml/runs"}, + } { + t.Run(tc.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != tc.path { + t.Errorf("request path = %q, want %q", r.URL.Path, tc.path) + w.WriteHeader(http.StatusNotFound) + return + } + params := r.URL.Query() + assert.Equal(t, "main", params.Get("branch")) + assert.Equal(t, "100", params.Get("per_page")) + assert.Equal(t, "2026-11-16T00:00:00Z..2026-11-17T00:00:00Z", params.Get("created")) + w.Header().Set("Content-Type", "application/json") + switch params.Get("page") { + case "1": + w.Header().Set("Link", fmt.Sprintf("; rel=\"next\"", r.Host, r.URL.Path)) + fmt.Fprint(w, `{"total_count":2,"workflow_runs":[{"id":11,"workflow_id":42}]}`) + case "2": + fmt.Fprint(w, `{"total_count":2,"workflow_runs":[{"id":12,"workflow_id":43}]}`) + default: + t.Errorf("unexpected page %q", params.Get("page")) + w.WriteHeader(http.StatusBadRequest) + } + })) + defer server.Close() + rest, err := googlegithub.NewClient(server.Client()).WithEnterpriseURLs(server.URL, server.URL) + require.NoError(t, err) + client := &Client{restClient: rest} + from := time.Date(2026, 11, 16, 0, 0, 0, 0, time.UTC) + runs, err := client.GetWorkflowRuns(t.Context(), "acme", "widget", tc.workflow, "main", backend.TimeRange{From: from, To: from.Add(24 * time.Hour)}) + require.NoError(t, err) + require.Len(t, runs, 2) + assert.Equal(t, int64(11), runs[0].GetID()) + assert.Equal(t, int64(43), runs[1].GetWorkflowID()) + }) + } +} diff --git a/pkg/github/codescanning_test.go b/pkg/github/codescanning_test.go index 985a509d..1c92a502 100644 --- a/pkg/github/codescanning_test.go +++ b/pkg/github/codescanning_test.go @@ -17,6 +17,7 @@ type mockAlertPage struct { } type mockClient struct { + models.Client mockAlerts []*googlegithub.Alert mockResponse *googlegithub.Response // pages, when set, makes successive ListAlertsFor* calls return successive diff --git a/pkg/github/commit_files_test.go b/pkg/github/commit_files_test.go index f3794367..a25f0bd9 100644 --- a/pkg/github/commit_files_test.go +++ b/pkg/github/commit_files_test.go @@ -19,6 +19,7 @@ type prFilePage struct { // commitFilesMockClient satisfies models.Client for commit file tests type commitFilesMockClient struct { + models.Client commitFiles []*googlegithub.CommitFile prFilePages []prFilePage prPageIdx int diff --git a/pkg/github/commits_test.go b/pkg/github/commits_test.go index 0d03634d..6b0ad02c 100644 --- a/pkg/github/commits_test.go +++ b/pkg/github/commits_test.go @@ -17,6 +17,7 @@ import ( // Query populates QueryListCommitsInRange with a fixed set of commits; GetCommitFiles // returns files keyed by commit SHA. All other methods panic if called unexpectedly. type commitsWithFilesMockClient struct { + models.Client commits []Commit filesBySHA map[string][]*googlegithub.CommitFile } diff --git a/pkg/github/datasource.go b/pkg/github/datasource.go index 32329a68..7661bcb4 100644 --- a/pkg/github/datasource.go +++ b/pkg/github/datasource.go @@ -265,6 +265,13 @@ func (d *Datasource) HandleWorkflowRunsQuery(ctx context.Context, query *models. return GetWorkflowRuns(ctx, d.client, opt, req.TimeRange) } +// HandleWorkflowJobStepsQuery lists job steps and their durations for a repository. +func (d *Datasource) HandleWorkflowJobStepsQuery(ctx context.Context, query *models.WorkflowRunsQuery, req backend.DataQuery) (dfutil.Framer, error) { + return GetWorkflowJobSteps(ctx, d.client, models.WorkflowRunsOptions{ + Owner: query.Owner, Repository: query.Repository, Workflow: query.Options.Workflow, Branch: query.Options.Branch, + }, req.TimeRange) +} + // HandleDeploymentsQuery is the query handler for listing GitHub Deployments func (d *Datasource) HandleDeploymentsQuery(ctx context.Context, query *models.DeploymentsQuery, req backend.DataQuery) (dfutil.Framer, error) { opt := models.ListDeploymentsOptions{ diff --git a/pkg/github/deployments_test.go b/pkg/github/deployments_test.go index 90fa4df2..311dbb76 100644 --- a/pkg/github/deployments_test.go +++ b/pkg/github/deployments_test.go @@ -11,6 +11,7 @@ import ( ) type mockDeploymentsClient struct { + models.Client mockDeployments []*googlegithub.Deployment mockResponse *googlegithub.Response expectedOwner string diff --git a/pkg/github/query_handler.go b/pkg/github/query_handler.go index fa2728b6..b1894d2c 100644 --- a/pkg/github/query_handler.go +++ b/pkg/github/query_handler.go @@ -64,6 +64,7 @@ func GetQueryHandlers(s *QueryHandler) *datasource.QueryTypeMux { register(models.QueryTypeWorkflows, s.HandleWorkflows) register(models.QueryTypeWorkflowUsage, s.HandleWorkflowUsage) register(models.QueryTypeWorkflowRuns, s.HandleWorkflowRuns) + register(models.QueryTypeWorkflowJobSteps, s.HandleWorkflowJobSteps) register(models.QueryTypeCodeScanning, s.HandleCodeScanning) register(models.QueryTypeDeployments, s.HandleDeployments) register(models.QueryTypeOrganizations, s.HandleOrganizations) diff --git a/pkg/github/workflow_job_steps.go b/pkg/github/workflow_job_steps.go new file mode 100644 index 00000000..24dd0257 --- /dev/null +++ b/pkg/github/workflow_job_steps.go @@ -0,0 +1,115 @@ +package github + +import ( + "context" + "fmt" + "time" + + googlegithub "github.com/google/go-github/v84/github" + "github.com/grafana/grafana-plugin-sdk-go/backend" + "github.com/grafana/grafana-plugin-sdk-go/data" + + "github.com/grafana/github-datasource/pkg/models" +) + +const workflowJobStepsRunLimit = 50 + +type workflowJob struct { + run *googlegithub.WorkflowRun + job *googlegithub.WorkflowJob +} + +type workflowJobSteps []workflowJob + +func (jobs workflowJobSteps) Frames() data.Frames { + duration := data.NewField("duration_seconds", nil, []*float64{}) + duration.Config = &data.FieldConfig{Unit: "s"} + frame := data.NewFrame("workflow_job_steps", + data.NewField("workflow_id", nil, []int64{}), + data.NewField("workflow_name", nil, []string{}), + data.NewField("run_id", nil, []int64{}), + data.NewField("run_attempt", nil, []int64{}), + data.NewField("job_id", nil, []int64{}), + data.NewField("job_name", nil, []string{}), + data.NewField("job_url", nil, []string{}), + data.NewField("step_number", nil, []int64{}), + data.NewField("step_name", nil, []string{}), + data.NewField("status", nil, []string{}), + data.NewField("conclusion", nil, []string{}), + data.NewField("started_at", nil, []*time.Time{}), + data.NewField("completed_at", nil, []*time.Time{}), + duration, + ) + frame.Meta = &data.FrameMeta{PreferredVisualization: data.VisTypeTable} + for _, row := range jobs { + for _, step := range row.job.Steps { + if step == nil { + continue + } + start, end := step.StartedAt.GetTime(), step.CompletedAt.GetTime() + var seconds *float64 + if step.GetConclusion() != "skipped" && start != nil && end != nil && !start.IsZero() && !end.Before(*start) { + seconds = googlegithub.Ptr(end.Sub(*start).Seconds()) + } + frame.AppendRow(row.run.GetWorkflowID(), row.run.GetName(), row.run.GetID(), row.job.GetRunAttempt(), + row.job.GetID(), row.job.GetName(), row.job.GetHTMLURL(), step.GetNumber(), step.GetName(), + step.GetStatus(), step.GetConclusion(), start, end, seconds) + } + } + return data.Frames{frame} +} + +type workflowJobStepsResult struct { + jobs workflowJobSteps + truncated bool +} + +func (r workflowJobStepsResult) Frames() data.Frames { + frames := r.jobs.Frames() + if r.truncated { + frames[0].Meta.Notices = []data.Notice{{ + Severity: data.NoticeSeverityWarning, + Text: fmt.Sprintf("Showing job steps for the %d most recent matching workflow runs. Narrow the time range or set a branch or workflow filter.", workflowJobStepsRunLimit), + }} + } + return frames +} + +// GetWorkflowJobSteps gets the jobs and steps for the most recent matching runs. +func GetWorkflowJobSteps(ctx context.Context, client models.Client, opts models.WorkflowRunsOptions, timeRange backend.TimeRange) (workflowJobStepsResult, error) { + return getWorkflowJobSteps(ctx, client, opts.Owner, opts.Repository, opts.Workflow, opts.Branch, timeRange) +} + +func getWorkflowJobSteps(ctx context.Context, client models.Client, owner, repo, workflow, branch string, timeRange backend.TimeRange) (workflowJobStepsResult, error) { + var result workflowJobStepsResult + if owner == "" || repo == "" { + return result, nil + } + page, response, err := client.ListWorkflowRunsPage(ctx, owner, repo, workflow, branch, timeRange, googlegithub.ListOptions{Page: 1, PerPage: workflowJobStepsRunLimit}) + if err != nil { + return result, fmt.Errorf("listing workflow runs for job steps: %w", err) + } + runs := page.WorkflowRuns + result.truncated = page.GetTotalCount() > workflowJobStepsRunLimit || response.NextPage != 0 || len(runs) > workflowJobStepsRunLimit + if len(runs) > workflowJobStepsRunLimit { + runs = runs[:workflowJobStepsRunLimit] + } + for _, run := range runs { + if run == nil || run.GetID() == 0 { + continue + } + for page := 1; page != 0; { + jobs, next, err := client.ListWorkflowJobs(ctx, owner, repo, run, googlegithub.ListOptions{Page: page, PerPage: 100}) + if err != nil { + return workflowJobStepsResult{}, fmt.Errorf("listing jobs for workflow run %d: %w", run.GetID(), err) + } + for _, job := range jobs { + if job != nil { + result.jobs = append(result.jobs, workflowJob{run: run, job: job}) + } + } + page = next + } + } + return result, nil +} diff --git a/pkg/github/workflow_job_steps_test.go b/pkg/github/workflow_job_steps_test.go new file mode 100644 index 00000000..ab102d1f --- /dev/null +++ b/pkg/github/workflow_job_steps_test.go @@ -0,0 +1,96 @@ +package github + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "testing" + + "github.com/grafana/github-datasource/pkg/models" + "github.com/grafana/grafana-plugin-sdk-go/backend" + "github.com/grafana/grafana-plugin-sdk-go/data" + + googlegithub "github.com/google/go-github/v84/github" + "github.com/stretchr/testify/require" +) + +type jobStepsTestClient struct { + models.Client + runs []*googlegithub.WorkflowRun + calls []int + jobsErr error + runCalls []googlegithub.ListOptions + total int +} + +func (c *jobStepsTestClient) ListWorkflowRunsPage(_ context.Context, _, _, _, _ string, _ backend.TimeRange, opts googlegithub.ListOptions) (*googlegithub.WorkflowRuns, *googlegithub.Response, error) { + c.runCalls = append(c.runCalls, opts) + return &googlegithub.WorkflowRuns{WorkflowRuns: c.runs, TotalCount: googlegithub.Ptr(c.total)}, &googlegithub.Response{}, nil +} + +func (c *jobStepsTestClient) ListWorkflowJobs(_ context.Context, _, _ string, run *googlegithub.WorkflowRun, opts googlegithub.ListOptions) ([]*googlegithub.WorkflowJob, int, error) { + c.calls = append(c.calls, opts.Page) + if c.jobsErr != nil { + return nil, 0, c.jobsErr + } + next := 0 + if opts.Page == 1 { + next = 2 + } + job := &googlegithub.WorkflowJob{Steps: []*googlegithub.TaskStep{{Name: googlegithub.Ptr(fmt.Sprint(run.GetID()))}}} + return []*googlegithub.WorkflowJob{job}, next, nil +} + +func TestGetWorkflowJobSteps(t *testing.T) { + t.Run("truncates and warns", func(t *testing.T) { + client := &jobStepsTestClient{total: workflowJobStepsRunLimit + 1} + for i := int64(1); i <= workflowJobStepsRunLimit; i++ { + client.runs = append(client.runs, &googlegithub.WorkflowRun{ID: googlegithub.Ptr(i)}) + } + result, err := getWorkflowJobSteps(t.Context(), client, "owner", "repo", "", "", backend.TimeRange{}) + require.NoError(t, err) + frame := result.Frames()[0] + require.Equal(t, 2*workflowJobStepsRunLimit, frame.Rows()) + require.Equal(t, []googlegithub.ListOptions{{Page: 1, PerPage: 50}}, client.runCalls) + require.Len(t, frame.Meta.Notices, 1) + require.Equal(t, data.NoticeSeverityWarning, frame.Meta.Notices[0].Severity) + }) + t.Run("job pagination", func(t *testing.T) { + client := &jobStepsTestClient{runs: []*googlegithub.WorkflowRun{{ID: googlegithub.Ptr(int64(12))}}} + result, err := getWorkflowJobSteps(t.Context(), client, "owner", "repo", "", "", backend.TimeRange{}) + require.NoError(t, err) + require.Equal(t, []int{1, 2}, client.calls) + require.Equal(t, 2, result.Frames()[0].Rows()) + }) + t.Run("wraps job error with run ID", func(t *testing.T) { + sentinel := errors.New("request failed") + client := &jobStepsTestClient{jobsErr: sentinel, runs: []*googlegithub.WorkflowRun{{ID: googlegithub.Ptr(int64(12))}}} + _, err := getWorkflowJobSteps(t.Context(), client, "owner", "repo", "", "", backend.TimeRange{}) + require.ErrorIs(t, err, sentinel) + require.ErrorContains(t, err, "workflow run 12") + }) +} + +func TestWorkflowJobStepsFrame(t *testing.T) { + var job googlegithub.WorkflowJob + require.NoError(t, json.Unmarshal([]byte(`{"id":45,"name":"Build (linux)","run_attempt":2,"steps":[{"name":"Build","number":1,"status":"completed","conclusion":"success","started_at":"2026-11-17T08:00:00Z","completed_at":"2026-11-17T08:02:30Z"},{"name":"Publish","number":2,"status":"in_progress","started_at":"2026-11-17T08:03:00Z"},{"name":"Optional","number":3,"status":"completed","conclusion":"skipped","started_at":"2026-11-17T08:03:00Z","completed_at":"2026-11-17T08:03:00Z"}]}`), &job)) + run := &googlegithub.WorkflowRun{ID: googlegithub.Ptr(int64(12)), WorkflowID: googlegithub.Ptr(int64(34)), Name: googlegithub.Ptr("CI")} + steps := workflowJobSteps{{run: run, job: &job}} + frame := steps.Frames()[0] + require.Equal(t, "workflow_job_steps", frame.Name) + require.Equal(t, 3, frame.Rows()) + fields := map[string]int{} + for i, field := range frame.Fields { + fields[field.Name] = i + } + require.Equal(t, int64(34), frame.Fields[fields["workflow_id"]].At(0)) + require.Equal(t, int64(12), frame.Fields[fields["run_id"]].At(0)) + require.Equal(t, int64(2), frame.Fields[fields["run_attempt"]].At(0)) + require.Equal(t, "Build (linux)", frame.Fields[fields["job_name"]].At(0)) + duration := frame.Fields[fields["duration_seconds"]] + require.Equal(t, "s", duration.Config.Unit) + require.Equal(t, 150.0, *duration.At(0).(*float64)) + require.Nil(t, duration.At(1)) + require.Nil(t, duration.At(2)) +} diff --git a/pkg/github/workflows_handler.go b/pkg/github/workflows_handler.go index 8215ca85..88b7c4e8 100644 --- a/pkg/github/workflows_handler.go +++ b/pkg/github/workflows_handler.go @@ -55,3 +55,15 @@ func (s *QueryHandler) HandleWorkflowRuns(ctx context.Context, req *backend.Quer Responses: processQueries(ctx, req, s.handleWorkflowRunsQuery), }, nil } + +// HandleWorkflowJobSteps handles queries for Actions job step durations. +func (s *QueryHandler) HandleWorkflowJobSteps(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { + responses := processQueries(ctx, req, func(ctx context.Context, q backend.DataQuery) backend.DataResponse { + var query models.WorkflowRunsQuery + if err := UnmarshalQuery(q.JSON, &query); err != nil { + return *err + } + return dfutil.FrameResponseWithError(s.Datasource.HandleWorkflowJobStepsQuery(ctx, &query, q)) + }) + return &backend.QueryDataResponse{Responses: responses}, nil +} diff --git a/pkg/models/client.go b/pkg/models/client.go index 0523e20f..e7c99445 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -14,6 +14,8 @@ type Client interface { 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) + ListWorkflowRunsPage(context.Context, string, string, string, string, backend.TimeRange, googlegithub.ListOptions) (*googlegithub.WorkflowRuns, *googlegithub.Response, error) + ListWorkflowJobs(context.Context, string, string, *googlegithub.WorkflowRun, googlegithub.ListOptions) ([]*googlegithub.WorkflowJob, int, error) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *googlegithub.AlertListOptions) ([]*googlegithub.Alert, *googlegithub.Response, error) ListAlertsForOrg(ctx context.Context, owner string, opts *googlegithub.AlertListOptions) ([]*googlegithub.Alert, *googlegithub.Response, error) ListAllOrgRepositories(ctx context.Context, opts *googlegithub.ListOptions) ([]*googlegithub.Repository, *googlegithub.Response, error) diff --git a/pkg/models/query.go b/pkg/models/query.go index ecaf9d6f..5f4c273f 100644 --- a/pkg/models/query.go +++ b/pkg/models/query.go @@ -45,6 +45,8 @@ const ( QueryTypeWorkflowUsage QueryType = "Workflow_Usage" // QueryTypeWorkflowRuns is used when querying workflow runs for a repository QueryTypeWorkflowRuns QueryType = "Workflow_Runs" + // QueryTypeWorkflowJobSteps is used when querying workflow job step durations. + QueryTypeWorkflowJobSteps QueryType = "Workflow_Job_Steps" // QueryTypeCodeScanning is used when querying code scanning alerts for a repository QueryTypeCodeScanning QueryType = "Code_Scanning" // QueryTypeDeployments is used when querying deployments for a repository diff --git a/pkg/testutil/client.go b/pkg/testutil/client.go index fe64732e..1a4e76a1 100644 --- a/pkg/testutil/client.go +++ b/pkg/testutil/client.go @@ -67,6 +67,14 @@ func (c *TestClient) GetWorkflowRuns(ctx context.Context, owner, repo, workflow panic("unimplemented") } +func (c *TestClient) ListWorkflowRunsPage(context.Context, string, string, string, string, backend.TimeRange, googlegithub.ListOptions) (*googlegithub.WorkflowRuns, *googlegithub.Response, error) { + panic("unimplemented") +} + +func (c *TestClient) ListWorkflowJobs(context.Context, string, string, *googlegithub.WorkflowRun, googlegithub.ListOptions) ([]*googlegithub.WorkflowJob, int, error) { + panic("unimplemented") +} + // ListAlertsForRepo is not implemented because it is not being used in tests at the moment. func (c *TestClient) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *googlegithub.AlertListOptions) ([]*googlegithub.Alert, *googlegithub.Response, error) { panic("unimplemented") diff --git a/src/constants.ts b/src/constants.ts index 19ecb368..b0037bdb 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -27,6 +27,7 @@ export const QueryTypes = [ 'Workflows', 'Workflow_Usage', 'Workflow_Runs', + 'Workflow_Job_Steps', 'Deployments', 'Webhook_Deliveries', ] as const; diff --git a/src/types/query.ts b/src/types/query.ts index 16998b28..e5c62bab 100644 --- a/src/types/query.ts +++ b/src/types/query.ts @@ -153,10 +153,11 @@ type StargazersQuery = BaseQuery<'Stargazers', {}> //#region Workflow_Runs Query export type WorkflowRunsOptions = Options & { - workflowID?: string; + workflow?: string; branch?: string; } type Workflow_RunsQuery = BaseQuery<'Workflow_Runs', WorkflowRunsOptions> +type Workflow_Job_StepsQuery = BaseQuery<'Workflow_Job_Steps', WorkflowRunsOptions> //#endregion //#region Workflow_Usage Query @@ -227,6 +228,7 @@ export type GitHubQuery = ProjectItemsQuery | StargazersQuery | Workflow_RunsQuery | + Workflow_Job_StepsQuery | Workflow_UsageQuery | WorkflowsQuery | DeploymentsQuery | diff --git a/src/validation.ts b/src/validation.ts index 1c32149c..f4f9058b 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -12,6 +12,8 @@ export const isValid = (query: GitHubQuery): boolean => { query.queryType === "Commits" || query.queryType === 'Commit_Files' || query.queryType === 'Check_Runs' || + query.queryType === 'Workflow_Runs' || + query.queryType === 'Workflow_Job_Steps' || query.queryType === "Contributors" || query.queryType === "Tags" || query.queryType === "Releases" || diff --git a/src/views/QueryEditor.tsx b/src/views/QueryEditor.tsx index d7bb80cc..8de291ac 100644 --- a/src/views/QueryEditor.tsx +++ b/src/views/QueryEditor.tsx @@ -139,6 +139,11 @@ const queryEditors: Record ), }, + ['Workflow_Job_Steps']: { + component: (props: Props, onChange: (val: any) => void) => ( + + ), + }, ['Deployments']: { component: (props: Props, onChange: (val: any) => void) => ( diff --git a/src/views/QueryEditorWorkflowRuns.tsx b/src/views/QueryEditorWorkflowRuns.tsx index 2d28801b..f41bd1bd 100644 --- a/src/views/QueryEditorWorkflowRuns.tsx +++ b/src/views/QueryEditorWorkflowRuns.tsx @@ -14,9 +14,10 @@ export const QueryEditorWorkflowRuns = (props: Props) => { return ( - + setWorkflow(el.currentTarget.value)} onBlur={(el) =>