mirror of https://github.com/go-gitea/gitea.git
Add workflow_run api + webhook (#33964)
Implements - https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#list-jobs-for-a-workflow-run--code-samples - https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#get-a-job-for-a-workflow-run--code-samples - https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository - https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#get-a-workflow-run - `/actions/runs` for global + user + org (Gitea only) - `/actions/jobs` for global + user + org + repository (Gitea only) - workflow_run webhook + action trigger - limitations - workflow id is assigned to a string, this may result into problems in strongly typed clients Fixes - workflow_job webhook url to no longer contain the `runs/<run>` part to align with api - workflow instance does now use it's name inside the file instead of filename if set Refactoring - Moved a lot of logic from workflows/workflow_job into a shared module used by both webhook and api TODO - [x] Verify Keda Compatibility - [x] Edit Webhook API bug is resolved Closes https://github.com/go-gitea/gitea/issues/23670 Closes https://github.com/go-gitea/gitea/issues/23796 Closes https://github.com/go-gitea/gitea/issues/24898 Replaces https://github.com/go-gitea/gitea/pull/28047 and is much more complete --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>pull/34787/head^2
parent
d462ce149d
commit
cda90eca31
@ -0,0 +1,93 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package admin
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/routers/api/v1/shared"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
)
|
||||
|
||||
// ListWorkflowJobs Lists all jobs
|
||||
func ListWorkflowJobs(ctx *context.APIContext) {
|
||||
// swagger:operation GET /admin/actions/jobs admin listAdminWorkflowJobs
|
||||
// ---
|
||||
// summary: Lists all jobs
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: status
|
||||
// in: query
|
||||
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
|
||||
// type: string
|
||||
// required: false
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/WorkflowJobsList"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
shared.ListJobs(ctx, 0, 0, 0)
|
||||
}
|
||||
|
||||
// ListWorkflowRuns Lists all runs
|
||||
func ListWorkflowRuns(ctx *context.APIContext) {
|
||||
// swagger:operation GET /admin/actions/runs admin listAdminWorkflowRuns
|
||||
// ---
|
||||
// summary: Lists all runs
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: event
|
||||
// in: query
|
||||
// description: workflow event name
|
||||
// type: string
|
||||
// required: false
|
||||
// - name: branch
|
||||
// in: query
|
||||
// description: workflow branch
|
||||
// type: string
|
||||
// required: false
|
||||
// - name: status
|
||||
// in: query
|
||||
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
|
||||
// type: string
|
||||
// required: false
|
||||
// - name: actor
|
||||
// in: query
|
||||
// description: triggered by user
|
||||
// type: string
|
||||
// required: false
|
||||
// - name: head_sha
|
||||
// in: query
|
||||
// description: triggering sha of the workflow run
|
||||
// type: string
|
||||
// required: false
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/WorkflowRunsList"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
shared.ListRuns(ctx, 0, 0)
|
||||
}
|
||||
@ -0,0 +1,187 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/webhook"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
)
|
||||
|
||||
// ListJobs lists jobs for api route validated ownerID and repoID
|
||||
// ownerID == 0 and repoID == 0 means all jobs
|
||||
// ownerID == 0 and repoID != 0 means all jobs for the given repo
|
||||
// ownerID != 0 and repoID == 0 means all jobs for the given user/org
|
||||
// ownerID != 0 and repoID != 0 undefined behavior
|
||||
// runID == 0 means all jobs
|
||||
// runID is used as an additional filter together with ownerID and repoID to only return jobs for the given run
|
||||
// Access rights are checked at the API route level
|
||||
func ListJobs(ctx *context.APIContext, ownerID, repoID, runID int64) {
|
||||
if ownerID != 0 && repoID != 0 {
|
||||
setting.PanicInDevOrTesting("ownerID and repoID should not be both set")
|
||||
}
|
||||
opts := actions_model.FindRunJobOptions{
|
||||
OwnerID: ownerID,
|
||||
RepoID: repoID,
|
||||
RunID: runID,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
}
|
||||
for _, status := range ctx.FormStrings("status") {
|
||||
values, err := convertToInternal(status)
|
||||
if err != nil {
|
||||
ctx.APIError(http.StatusBadRequest, fmt.Errorf("Invalid status %s", status))
|
||||
return
|
||||
}
|
||||
opts.Statuses = append(opts.Statuses, values...)
|
||||
}
|
||||
|
||||
jobs, total, err := db.FindAndCount[actions_model.ActionRunJob](ctx, opts)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
|
||||
res := new(api.ActionWorkflowJobsResponse)
|
||||
res.TotalCount = total
|
||||
|
||||
res.Entries = make([]*api.ActionWorkflowJob, len(jobs))
|
||||
|
||||
isRepoLevel := repoID != 0 && ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == repoID
|
||||
for i := range jobs {
|
||||
var repository *repo_model.Repository
|
||||
if isRepoLevel {
|
||||
repository = ctx.Repo.Repository
|
||||
} else {
|
||||
repository, err = repo_model.GetRepositoryByID(ctx, jobs[i].RepoID)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
convertedWorkflowJob, err := convert.ToActionWorkflowJob(ctx, repository, nil, jobs[i])
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
res.Entries[i] = convertedWorkflowJob
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, &res)
|
||||
}
|
||||
|
||||
func convertToInternal(s string) ([]actions_model.Status, error) {
|
||||
switch s {
|
||||
case "pending", "waiting", "requested", "action_required":
|
||||
return []actions_model.Status{actions_model.StatusBlocked}, nil
|
||||
case "queued":
|
||||
return []actions_model.Status{actions_model.StatusWaiting}, nil
|
||||
case "in_progress":
|
||||
return []actions_model.Status{actions_model.StatusRunning}, nil
|
||||
case "completed":
|
||||
return []actions_model.Status{
|
||||
actions_model.StatusSuccess,
|
||||
actions_model.StatusFailure,
|
||||
actions_model.StatusSkipped,
|
||||
actions_model.StatusCancelled,
|
||||
}, nil
|
||||
case "failure":
|
||||
return []actions_model.Status{actions_model.StatusFailure}, nil
|
||||
case "success":
|
||||
return []actions_model.Status{actions_model.StatusSuccess}, nil
|
||||
case "skipped", "neutral":
|
||||
return []actions_model.Status{actions_model.StatusSkipped}, nil
|
||||
case "cancelled", "timed_out":
|
||||
return []actions_model.Status{actions_model.StatusCancelled}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid status %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
// ListRuns lists jobs for api route validated ownerID and repoID
|
||||
// ownerID == 0 and repoID == 0 means all runs
|
||||
// ownerID == 0 and repoID != 0 means all runs for the given repo
|
||||
// ownerID != 0 and repoID == 0 means all runs for the given user/org
|
||||
// ownerID != 0 and repoID != 0 undefined behavior
|
||||
// Access rights are checked at the API route level
|
||||
func ListRuns(ctx *context.APIContext, ownerID, repoID int64) {
|
||||
if ownerID != 0 && repoID != 0 {
|
||||
setting.PanicInDevOrTesting("ownerID and repoID should not be both set")
|
||||
}
|
||||
opts := actions_model.FindRunOptions{
|
||||
OwnerID: ownerID,
|
||||
RepoID: repoID,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
}
|
||||
|
||||
if event := ctx.FormString("event"); event != "" {
|
||||
opts.TriggerEvent = webhook.HookEventType(event)
|
||||
}
|
||||
if branch := ctx.FormString("branch"); branch != "" {
|
||||
opts.Ref = string(git.RefNameFromBranch(branch))
|
||||
}
|
||||
for _, status := range ctx.FormStrings("status") {
|
||||
values, err := convertToInternal(status)
|
||||
if err != nil {
|
||||
ctx.APIError(http.StatusBadRequest, fmt.Errorf("Invalid status %s", status))
|
||||
return
|
||||
}
|
||||
opts.Status = append(opts.Status, values...)
|
||||
}
|
||||
if actor := ctx.FormString("actor"); actor != "" {
|
||||
user, err := user_model.GetUserByName(ctx, actor)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
opts.TriggerUserID = user.ID
|
||||
}
|
||||
if headSHA := ctx.FormString("head_sha"); headSHA != "" {
|
||||
opts.CommitSHA = headSHA
|
||||
}
|
||||
|
||||
runs, total, err := db.FindAndCount[actions_model.ActionRun](ctx, opts)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
|
||||
res := new(api.ActionWorkflowRunsResponse)
|
||||
res.TotalCount = total
|
||||
|
||||
res.Entries = make([]*api.ActionWorkflowRun, len(runs))
|
||||
isRepoLevel := repoID != 0 && ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == repoID
|
||||
for i := range runs {
|
||||
var repository *repo_model.Repository
|
||||
if isRepoLevel {
|
||||
repository = ctx.Repo.Repository
|
||||
} else {
|
||||
repository, err = repo_model.GetRepositoryByID(ctx, runs[i].RepoID)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
convertedRun, err := convert.ToActionWorkflowRun(ctx, repository, runs[i])
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
res.Entries[i] = convertedRun
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, &res)
|
||||
}
|
||||
@ -0,0 +1,167 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"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 TestAPIWorkflowRun(t *testing.T) {
|
||||
t.Run("AdminRuns", func(t *testing.T) {
|
||||
testAPIWorkflowRunBasic(t, "/api/v1/admin/actions", "User1", 802, auth_model.AccessTokenScopeReadAdmin, auth_model.AccessTokenScopeReadRepository)
|
||||
})
|
||||
t.Run("UserRuns", func(t *testing.T) {
|
||||
testAPIWorkflowRunBasic(t, "/api/v1/user/actions", "User2", 803, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadRepository)
|
||||
})
|
||||
t.Run("OrgRuns", func(t *testing.T) {
|
||||
testAPIWorkflowRunBasic(t, "/api/v1/orgs/org3/actions", "User1", 802, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopeReadRepository)
|
||||
})
|
||||
t.Run("RepoRuns", func(t *testing.T) {
|
||||
testAPIWorkflowRunBasic(t, "/api/v1/repos/org3/repo5/actions", "User2", 802, auth_model.AccessTokenScopeReadRepository)
|
||||
})
|
||||
}
|
||||
|
||||
func testAPIWorkflowRunBasic(t *testing.T, apiRootURL, userUsername string, runID int64, scope ...auth_model.AccessTokenScope) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
token := getUserToken(t, userUsername, scope...)
|
||||
|
||||
apiRunsURL := fmt.Sprintf("%s/%s", apiRootURL, "runs")
|
||||
req := NewRequest(t, "GET", apiRunsURL).AddTokenAuth(token)
|
||||
runnerListResp := MakeRequest(t, req, http.StatusOK)
|
||||
runnerList := api.ActionWorkflowRunsResponse{}
|
||||
DecodeJSON(t, runnerListResp, &runnerList)
|
||||
|
||||
foundRun := false
|
||||
|
||||
for _, run := range runnerList.Entries {
|
||||
// Verify filtering works
|
||||
verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", run.Status, "", "", "", "")
|
||||
verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, run.Conclusion, "", "", "", "", "")
|
||||
verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", "", run.HeadBranch, "", "")
|
||||
verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", run.Event, "", "", "")
|
||||
verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", "", "", run.TriggerActor.UserName, "")
|
||||
verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", "", "", run.TriggerActor.UserName, run.HeadSha)
|
||||
|
||||
// Verify run url works
|
||||
req := NewRequest(t, "GET", run.URL).AddTokenAuth(token)
|
||||
runResp := MakeRequest(t, req, http.StatusOK)
|
||||
apiRun := api.ActionWorkflowRun{}
|
||||
DecodeJSON(t, runResp, &apiRun)
|
||||
assert.Equal(t, run.ID, apiRun.ID)
|
||||
assert.Equal(t, run.Status, apiRun.Status)
|
||||
assert.Equal(t, run.Conclusion, apiRun.Conclusion)
|
||||
assert.Equal(t, run.Event, apiRun.Event)
|
||||
|
||||
// Verify jobs list works
|
||||
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)
|
||||
|
||||
if run.ID == runID {
|
||||
foundRun = true
|
||||
assert.Len(t, jobList.Entries, 1)
|
||||
for _, job := range jobList.Entries {
|
||||
// Check the jobs list of the run
|
||||
verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", run.URL, "jobs"), token, job.ID, "", job.Status)
|
||||
verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", run.URL, "jobs"), token, job.ID, job.Conclusion, "")
|
||||
// Check the run independent job list
|
||||
verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", apiRootURL, "jobs"), token, job.ID, "", job.Status)
|
||||
verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", apiRootURL, "jobs"), token, job.ID, job.Conclusion, "")
|
||||
|
||||
// Verify job url works
|
||||
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.True(t, foundRun, "Expected to find run with ID %d", runID)
|
||||
}
|
||||
|
||||
func verifyWorkflowRunCanbeFoundWithStatusFilter(t *testing.T, runAPIURL, token string, id int64, conclusion, status, event, branch, actor, headSHA string) {
|
||||
filter := url.Values{}
|
||||
if conclusion != "" {
|
||||
filter.Add("status", conclusion)
|
||||
}
|
||||
if status != "" {
|
||||
filter.Add("status", status)
|
||||
}
|
||||
if event != "" {
|
||||
filter.Set("event", event)
|
||||
}
|
||||
if branch != "" {
|
||||
filter.Set("branch", branch)
|
||||
}
|
||||
if actor != "" {
|
||||
filter.Set("actor", actor)
|
||||
}
|
||||
if headSHA != "" {
|
||||
filter.Set("head_sha", headSHA)
|
||||
}
|
||||
req := NewRequest(t, "GET", runAPIURL+"?"+filter.Encode()).AddTokenAuth(token)
|
||||
runResp := MakeRequest(t, req, http.StatusOK)
|
||||
runList := api.ActionWorkflowRunsResponse{}
|
||||
DecodeJSON(t, runResp, &runList)
|
||||
|
||||
found := false
|
||||
for _, run := range runList.Entries {
|
||||
if conclusion != "" {
|
||||
assert.Equal(t, conclusion, run.Conclusion)
|
||||
}
|
||||
if status != "" {
|
||||
assert.Equal(t, status, run.Status)
|
||||
}
|
||||
if event != "" {
|
||||
assert.Equal(t, event, run.Event)
|
||||
}
|
||||
if branch != "" {
|
||||
assert.Equal(t, branch, run.HeadBranch)
|
||||
}
|
||||
if actor != "" {
|
||||
assert.Equal(t, actor, run.Actor.UserName)
|
||||
}
|
||||
found = found || run.ID == id
|
||||
}
|
||||
assert.True(t, found, "Expected to find run with ID %d", id)
|
||||
}
|
||||
|
||||
func verifyWorkflowJobCanbeFoundWithStatusFilter(t *testing.T, runAPIURL, token string, id int64, conclusion, status string) {
|
||||
filter := conclusion
|
||||
if filter == "" {
|
||||
filter = status
|
||||
}
|
||||
if filter == "" {
|
||||
return
|
||||
}
|
||||
req := NewRequest(t, "GET", runAPIURL+"?status="+filter).AddTokenAuth(token)
|
||||
jobListResp := MakeRequest(t, req, http.StatusOK)
|
||||
jobList := api.ActionWorkflowJobsResponse{}
|
||||
DecodeJSON(t, jobListResp, &jobList)
|
||||
|
||||
found := false
|
||||
for _, job := range jobList.Entries {
|
||||
if conclusion != "" {
|
||||
assert.Equal(t, conclusion, job.Conclusion)
|
||||
} else {
|
||||
assert.Equal(t, status, job.Status)
|
||||
}
|
||||
found = found || job.ID == id
|
||||
}
|
||||
assert.True(t, found, "Expected to find job with ID %d", id)
|
||||
}
|
||||
Loading…
Reference in New Issue