diff --git a/routers/api/v1/repo/action.go b/routers/api/v1/repo/action.go index 88769fa858..c03f735710 100644 --- a/routers/api/v1/repo/action.go +++ b/routers/api/v1/repo/action.go @@ -962,7 +962,7 @@ func GetWorkflowRuns(ctx *context.APIContext) { res.Entries = make([]*api.ActionWorkflowRun, len(runs)) for i := range runs { - convertedRun, err := convert.ToActionWorkflowRun(ctx.Repo.Repository, runs[i]) + convertedRun, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, runs[i]) if err != nil { ctx.APIErrorInternal(err) return @@ -1011,7 +1011,7 @@ func GetWorkflowRun(ctx *context.APIContext) { ctx.APIError(http.StatusNotFound, util.ErrNotExist) } - convertedArtifact, err := convert.ToActionWorkflowRun(ctx.Repo.Repository, job) + convertedArtifact, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job) if err != nil { ctx.APIErrorInternal(err) return diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 258bd80fad..0b96ee06b9 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -786,7 +786,7 @@ func (n *actionsNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *rep convertedWorkflow, err := convert.GetActionWorkflow(ctx, gitRepo, repo, run.WorkflowID) - convertedRun, err := convert.ToActionWorkflowRun(repo, run) + convertedRun, err := convert.ToActionWorkflowRun(ctx, repo, run) if err != nil { log.Error("ToActionWorkflowRun: %v", err) return diff --git a/services/convert/convert.go b/services/convert/convert.go index e73f9302ca..f4084884c3 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -234,7 +234,8 @@ func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.Action }, nil } -func ToActionWorkflowRun(repo *repo_model.Repository, run *actions_model.ActionRun) (*api.ActionWorkflowRun, error) { +func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run *actions_model.ActionRun) (*api.ActionWorkflowRun, error) { + run.LoadRepo(ctx) status, conclusion := ToActionsStatus(run.Status) return &api.ActionWorkflowRun{ ID: run.ID, @@ -243,13 +244,13 @@ func ToActionWorkflowRun(repo *repo_model.Repository, run *actions_model.ActionR RunNumber: run.Index, StartedAt: run.Started.AsLocalTime(), CompletedAt: run.Stopped.AsLocalTime(), - Event: run.TriggerEvent, + Event: string(run.Event), DisplayTitle: run.Title, HeadBranch: git.RefName(run.Ref).BranchName(), HeadSha: run.CommitSHA, Status: status, Conclusion: conclusion, - Path: fmt.Sprint("%s@%s", run.WorkflowID, run.Ref), + Path: fmt.Sprintf("%s@%s", run.WorkflowID, run.Ref), }, nil } diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index 9878dd8c62..5689748724 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -996,7 +996,7 @@ func (*webhookNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_ convertedWorkflow, err := convert.GetActionWorkflow(ctx, gitRepo, repo, run.WorkflowID) - convertedRun, err := convert.ToActionWorkflowRun(repo, run) + convertedRun, err := convert.ToActionWorkflowRun(ctx, repo, run) if err != nil { log.Error("ToActionWorkflowRun: %v", err) return diff --git a/tests/integration/workflow_run_api_check_test.go b/tests/integration/workflow_run_api_check_test.go new file mode 100644 index 0000000000..f142da7b22 --- /dev/null +++ b/tests/integration/workflow_run_api_check_test.go @@ -0,0 +1,72 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package integration + +import ( + "fmt" + "net/http" + "testing" + + auth_model "code.gitea.io/gitea/models/auth" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/tests" + + "github.com/stretchr/testify/assert" +) + +func TestAPIWorkflowRunRepoApi(t *testing.T) { + defer tests.PrepareTestEnv(t)() + userUsername := "user5" + token := getUserToken(t, userUsername, auth_model.AccessTokenScopeWriteRepository) + + req := NewRequest(t, "GET", "/api/v1/repos/user5/repo4/actions/runs").AddTokenAuth(token) + runnerListResp := MakeRequest(t, req, http.StatusOK) + runnerList := api.ActionWorkflowRunsResponse{} + DecodeJSON(t, runnerListResp, &runnerList) + + assert.Len(t, runnerList.Entries, 4) + + for _, run := range runnerList.Entries { + req := NewRequest(t, "GET", fmt.Sprintf("%s/%s", run.URL, "jobs")).AddTokenAuth(token) + jobsResp := MakeRequest(t, req, http.StatusOK) + jobList := api.ActionWorkflowJobsResponse{} + DecodeJSON(t, jobsResp, &jobList) + + // assert.NotEmpty(t, jobList.Entries) + for _, job := range jobList.Entries { + req := NewRequest(t, "GET", job.URL).AddTokenAuth(token) + jobsResp := MakeRequest(t, req, http.StatusOK) + apiJob := api.ActionWorkflowJob{} + DecodeJSON(t, jobsResp, &apiJob) + assert.Equal(t, job.ID, apiJob.ID) + assert.Equal(t, job.RunID, apiJob.RunID) + assert.Equal(t, job.Status, apiJob.Status) + assert.Equal(t, job.Conclusion, apiJob.Conclusion) + } + // assert.NotEmpty(t, run.ID) + // assert.NotEmpty(t, run.Status) + // assert.NotEmpty(t, run.Event) + // assert.NotEmpty(t, run.WorkflowID) + // assert.NotEmpty(t, run.HeadBranch) + // assert.NotEmpty(t, run.HeadSHA) + // assert.NotEmpty(t, run.CreatedAt) + // assert.NotEmpty(t, run.UpdatedAt) + // assert.NotEmpty(t, run.URL) + // assert.NotEmpty(t, run.HTMLURL) + // assert.NotEmpty(t, run.PullRequests) + // assert.NotEmpty(t, run.WorkflowURL) + // assert.NotEmpty(t, run.HeadCommit) + // assert.NotEmpty(t, run.HeadRepository) + // assert.NotEmpty(t, run.Repository) + // assert.NotEmpty(t, run.HeadRepository) + // assert.NotEmpty(t, run.HeadRepository.Owner) + // assert.NotEmpty(t, run.HeadRepository.Name) + // assert.NotEmpty(t, run.Repository.Owner) + // assert.NotEmpty(t, run.Repository.Name) + // assert.NotEmpty(t, run.HeadRepository.Owner.Login) + // assert.NotEmpty(t, run.HeadRepository.Name) + // assert.NotEmpty(t, run.Repository.Owner.Login) + // assert.NotEmpty(t, run.Repository.Name) + } +}