Skip to content

Commit 6709e28

Browse files
chesteripzyp05327puni9869
authored
Add API endpoints for getting action jobs status (#26673)
Sample of response, it is similar to Github actions ref https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository ``` json { "workflow_runs": [ { "id": 3, "name": "Explore-Gitea-Actions", "head_branch": "main", "head_sha": "6d8d29a9f7a01ded8f8aeb64341cb31ee1ab5f19", "run_number": 3, "event": "push", "display_title": "More job", "status": "success", "workflow_id": "demo2.yaml", "url": "/chester/test/actions/runs/3", "created_at": "2023-08-22T13:41:33-04:00", "updated_at": "2023-08-22T13:41:37-04:00", "run_started_at": "2023-08-22T13:41:33-04:00" }, { "id": 2, "name": "Explore-Gitea-Actions", "head_branch": "main", "head_sha": "6d8d29a9f7a01ded8f8aeb64341cb31ee1ab5f19", "run_number": 2, "event": "push", "display_title": "More job", "status": "success", "workflow_id": "demo.yaml", "url": "/chester/test/actions/runs/2", "created_at": "2023-08-22T13:41:30-04:00", "updated_at": "2023-08-22T13:41:33-04:00", "run_started_at": "2023-08-22T13:41:30-04:00" }, { "id": 1, "name": "Explore-Gitea-Actions", "head_branch": "main", "head_sha": "e5369ab054cae79899ba36e45ee82811a6e0acd5", "run_number": 1, "event": "push", "display_title": "Add job", "status": "failure", "workflow_id": "demo.yaml", "url": "/chester/test/actions/runs/1", "created_at": "2023-08-22T13:15:21-04:00", "updated_at": "2023-08-22T13:18:10-04:00", "run_started_at": "2023-08-22T13:15:21-04:00" } ], "total_count": 3 } ``` --------- Co-authored-by: yp05327 <[email protected]> Co-authored-by: puni9869 <[email protected]>
1 parent d8d46d1 commit 6709e28

File tree

6 files changed

+300
-0
lines changed

6 files changed

+300
-0
lines changed

modules/structs/repo_actions.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package structs
5+
6+
import (
7+
"time"
8+
)
9+
10+
// ActionTask represents a ActionTask
11+
type ActionTask struct {
12+
ID int64 `json:"id"`
13+
Name string `json:"name"`
14+
HeadBranch string `json:"head_branch"`
15+
HeadSHA string `json:"head_sha"`
16+
RunNumber int64 `json:"run_number"`
17+
Event string `json:"event"`
18+
DisplayTitle string `json:"display_title"`
19+
Status string `json:"status"`
20+
WorkflowID string `json:"workflow_id"`
21+
URL string `json:"url"`
22+
// swagger:strfmt date-time
23+
CreatedAt time.Time `json:"created_at"`
24+
// swagger:strfmt date-time
25+
UpdatedAt time.Time `json:"updated_at"`
26+
// swagger:strfmt date-time
27+
RunStartedAt time.Time `json:"run_started_at"`
28+
}
29+
30+
// ActionTaskResponse returns a ActionTask
31+
type ActionTaskResponse struct {
32+
Entries []*ActionTask `json:"workflow_runs"`
33+
TotalCount int64 `json:"total_count"`
34+
}

routers/api/v1/api.go

+3
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,9 @@ func Routes() *web.Route {
11681168
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateTagOption{}), repo.CreateTag)
11691169
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteTag)
11701170
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true))
1171+
m.Group("/actions", func() {
1172+
m.Get("/tasks", repo.ListActionTasks)
1173+
}, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true))
11711174
m.Group("/keys", func() {
11721175
m.Combo("").Get(repo.ListDeployKeys).
11731176
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)

routers/api/v1/repo/actions.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
"net/http"
8+
9+
actions_model "code.gitea.io/gitea/models/actions"
10+
"code.gitea.io/gitea/models/db"
11+
api "code.gitea.io/gitea/modules/structs"
12+
"code.gitea.io/gitea/routers/api/v1/utils"
13+
"code.gitea.io/gitea/services/context"
14+
"code.gitea.io/gitea/services/convert"
15+
)
16+
17+
// ListActionTasks list all the actions of a repository
18+
func ListActionTasks(ctx *context.APIContext) {
19+
// swagger:operation GET /repos/{owner}/{repo}/actions/tasks repository ListActionTasks
20+
// ---
21+
// summary: List a repository's action tasks
22+
// produces:
23+
// - application/json
24+
// parameters:
25+
// - name: owner
26+
// in: path
27+
// description: owner of the repo
28+
// type: string
29+
// required: true
30+
// - name: repo
31+
// in: path
32+
// description: name of the repo
33+
// type: string
34+
// required: true
35+
// - name: page
36+
// in: query
37+
// description: page number of results to return (1-based)
38+
// type: integer
39+
// - name: limit
40+
// in: query
41+
// description: page size of results, default maximum page size is 50
42+
// type: integer
43+
// responses:
44+
// "200":
45+
// "$ref": "#/responses/TasksList"
46+
// "400":
47+
// "$ref": "#/responses/error"
48+
// "403":
49+
// "$ref": "#/responses/forbidden"
50+
// "404":
51+
// "$ref": "#/responses/notFound"
52+
// "409":
53+
// "$ref": "#/responses/conflict"
54+
// "422":
55+
// "$ref": "#/responses/validationError"
56+
57+
tasks, total, err := db.FindAndCount[actions_model.ActionTask](ctx, &actions_model.FindTaskOptions{
58+
ListOptions: utils.GetListOptions(ctx),
59+
RepoID: ctx.Repo.Repository.ID,
60+
})
61+
if err != nil {
62+
ctx.Error(http.StatusInternalServerError, "ListActionTasks", err)
63+
return
64+
}
65+
66+
res := new(api.ActionTaskResponse)
67+
res.TotalCount = total
68+
69+
res.Entries = make([]*api.ActionTask, len(tasks))
70+
for i := range tasks {
71+
convertedTask, err := convert.ToActionTask(ctx, tasks[i])
72+
if err != nil {
73+
ctx.Error(http.StatusInternalServerError, "ToActionTask", err)
74+
return
75+
}
76+
res.Entries[i] = convertedTask
77+
}
78+
79+
ctx.JSON(http.StatusOK, &res)
80+
}

routers/api/v1/swagger/repo.go

+7
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,13 @@ type swaggerRepoNewIssuePinsAllowed struct {
415415
Body api.NewIssuePinsAllowed `json:"body"`
416416
}
417417

418+
// TasksList
419+
// swagger:response TasksList
420+
type swaggerRepoTasksList struct {
421+
// in:body
422+
Body api.ActionTaskResponse `json:"body"`
423+
}
424+
418425
// swagger:response Compare
419426
type swaggerCompare struct {
420427
// in:body

services/convert/convert.go

+27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14+
actions_model "code.gitea.io/gitea/models/actions"
1415
asymkey_model "code.gitea.io/gitea/models/asymkey"
1516
"code.gitea.io/gitea/models/auth"
1617
git_model "code.gitea.io/gitea/models/git"
@@ -24,6 +25,7 @@ import (
2425
"code.gitea.io/gitea/modules/container"
2526
"code.gitea.io/gitea/modules/git"
2627
"code.gitea.io/gitea/modules/log"
28+
"code.gitea.io/gitea/modules/setting"
2729
api "code.gitea.io/gitea/modules/structs"
2830
"code.gitea.io/gitea/modules/util"
2931
"code.gitea.io/gitea/services/gitdiff"
@@ -193,6 +195,31 @@ func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
193195
}
194196
}
195197

198+
// ToActionTask convert a actions_model.ActionTask to an api.ActionTask
199+
func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.ActionTask, error) {
200+
if err := t.LoadAttributes(ctx); err != nil {
201+
return nil, err
202+
}
203+
204+
url := strings.TrimSuffix(setting.AppURL, "/") + t.GetRunLink()
205+
206+
return &api.ActionTask{
207+
ID: t.ID,
208+
Name: t.Job.Name,
209+
HeadBranch: t.Job.Run.PrettyRef(),
210+
HeadSHA: t.Job.CommitSHA,
211+
RunNumber: t.Job.Run.Index,
212+
Event: t.Job.Run.TriggerEvent,
213+
DisplayTitle: t.Job.Run.Title,
214+
Status: t.Status.String(),
215+
WorkflowID: t.Job.Run.WorkflowID,
216+
URL: url,
217+
CreatedAt: t.Created.AsLocalTime(),
218+
UpdatedAt: t.Updated.AsLocalTime(),
219+
RunStartedAt: t.Started.AsLocalTime(),
220+
}, nil
221+
}
222+
196223
// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
197224
func ToVerification(ctx context.Context, c *git.Commit) *api.PayloadCommitVerification {
198225
verif := asymkey_model.ParseCommitWithSignature(ctx, c)

templates/swagger/v1_json.tmpl

+149
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)