Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoints for getting action jobs status #26673

Merged
merged 48 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
b8a61a6
Add API endpoints for getting action jobs status
chesteripz Aug 22, 2023
8b5443a
Fix template
chesteripz Aug 23, 2023
6870e1a
Use the run index instead, since it's the number used in FE
chesteripz Aug 23, 2023
8f787df
Update copyright message
chesteripz Aug 23, 2023
246873a
Update routers/api/v1/api.go
chesteripz Aug 23, 2023
5ebefc8
align with GitHub Actions
chesteripz Aug 23, 2023
fdfc2f4
Merge branch 'api-repo-actions' of github.com:chesteripz/gitea into a…
chesteripz Aug 23, 2023
38d1197
Merge branch 'main' of github.com:go-gitea/gitea into api-repo-actions
chesteripz Aug 23, 2023
5625bc5
Update Swagger template
chesteripz Aug 23, 2023
80a2c8e
Merge remote-tracking branch 'origin/main' into api-repo-actions
chesteripz Aug 30, 2023
1ba5701
Fix var-naming
chesteripz Aug 30, 2023
4d9a231
Fix template
chesteripz Aug 30, 2023
a9cdb7e
Fix template
chesteripz Aug 30, 2023
6928b37
Update template using generate swagger
chesteripz Aug 30, 2023
7617e9e
Merge branch 'main' into api-repo-actions
chesteripz Aug 30, 2023
083d718
Merge branch 'main' into api-repo-actions
chesteripz Sep 5, 2023
a348da4
Update modules/structs/repo_actions.go
chesteripz Sep 11, 2023
b94cbda
Update modules/structs/repo_actions.go
chesteripz Sep 11, 2023
878eeed
Update modules/structs/repo_actions.go
chesteripz Sep 11, 2023
5ababc5
Merge branch 'main' into api-repo-actions
chesteripz Sep 11, 2023
e61a33e
Merge branch 'main' into api-repo-actions
chesteripz Oct 20, 2023
a0be04f
Merge branch 'main' into api-repo-actions
chesteripz Apr 3, 2024
e05fc9a
Fix broken json after resolving merge conflict
chesteripz Apr 3, 2024
2c8249b
More fixes after mreging from main
chesteripz Apr 3, 2024
1028eef
move to db.find
chesteripz Apr 3, 2024
4310284
Reducing queries
chesteripz Apr 3, 2024
ad16583
Add error res to swagger
chesteripz Apr 4, 2024
ae25e04
:Merge branch 'main' of github.com:go-gitea/gitea into api-repo-actions
chesteripz Apr 4, 2024
3477737
Use GetListOptions() and panic()
chesteripz Apr 19, 2024
0aabe97
Merge branch 'main' of github.com:go-gitea/gitea into api-repo-actions
chesteripz Apr 19, 2024
0e61648
Merge branch 'main' into api-repo-actions
chesteripz Apr 19, 2024
87ef8b7
Fix list options
chesteripz Apr 23, 2024
8d45013
Merge branch 'main' into api-repo-actions
chesteripz Apr 23, 2024
e0e0941
Fix URL
chesteripz Apr 23, 2024
5f26e8a
Fix format
chesteripz Apr 24, 2024
2d53041
Return err in ToActionTask
chesteripz Apr 24, 2024
f954f20
Update modules/structs/repo_actions.go
chesteripz Apr 26, 2024
86493c2
Fix as suggested
chesteripz Apr 26, 2024
e7bcc1b
Merge branch 'main' of github.com:go-gitea/gitea into api-repo-actions
chesteripz Apr 26, 2024
04abcc7
Fix letter case
chesteripz Apr 26, 2024
fc3dc33
Fix template
chesteripz Apr 26, 2024
105f20c
Merge branch 'main' into api-repo-actions
GiteaBot Apr 30, 2024
d574b18
Merge branch 'main' into api-repo-actions
GiteaBot Apr 30, 2024
0735ccf
Merge branch 'main' into api-repo-actions
GiteaBot Apr 30, 2024
c1acd7d
Merge branch 'main' into api-repo-actions
GiteaBot Apr 30, 2024
28b0c4e
Merge branch 'main' into api-repo-actions
GiteaBot Apr 30, 2024
0922a5a
Merge branch 'main' into api-repo-actions
GiteaBot Apr 30, 2024
c9ae661
Merge branch 'main' into api-repo-actions
GiteaBot May 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions modules/structs/repo_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package structs

import (
"time"
)

// ActionTask represents a ActionTask
type ActionTask struct {
ID int64 `json:"id"`
Name string `json:"name"`
HeadBranch string `json:"head_branch"`
HeadSHA string `json:"head_sha"`
RunNumber int64 `json:"run_number"`
Event string `json:"event"`
DisplayTitle string `json:"display_title"`
Status string `json:"status"`
WorkflowID string `json:"workflow_id"`
URL string `json:"url"`
// swagger:strfmt date-time
CreatedAt time.Time `json:"created_at"`
// swagger:strfmt date-time
UpdatedAt time.Time `json:"updated_at"`
// swagger:strfmt date-time
RunStartedAt time.Time `json:"run_started_at"`
lunny marked this conversation as resolved.
Show resolved Hide resolved
}

// ActionTaskResponse returns a ActionTask
type ActionTaskResponse struct {
Entries []*ActionTask `json:"workflow_runs"`
TotalCount int64 `json:"total_count"`
}
3 changes: 3 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,9 @@ func Routes() *web.Route {
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateTagOption{}), repo.CreateTag)
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteTag)
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true))
m.Group("/actions", func() {
m.Get("/tasks", repo.ListActionTasks)
chesteripz marked this conversation as resolved.
Show resolved Hide resolved
}, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true))
m.Group("/keys", func() {
m.Combo("").Get(repo.ListDeployKeys).
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
Expand Down
80 changes: 80 additions & 0 deletions routers/api/v1/repo/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
"net/http"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/convert"
)

// ListActionTasks list all the actions of a repository
func ListActionTasks(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/actions/tasks repository ListActionTasks
// ---
// summary: List a repository's action tasks
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results, default maximum page size is 50
// type: integer
// responses:
// "200":
// "$ref": "#/responses/TasksList"
chesteripz marked this conversation as resolved.
Show resolved Hide resolved
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/conflict"
// "422":
// "$ref": "#/responses/validationError"
lunny marked this conversation as resolved.
Show resolved Hide resolved

tasks, total, err := db.FindAndCount[actions_model.ActionTask](ctx, &actions_model.FindTaskOptions{
ListOptions: utils.GetListOptions(ctx),
RepoID: ctx.Repo.Repository.ID,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListActionTasks", err)
return
}

res := new(api.ActionTaskResponse)
res.TotalCount = total

res.Entries = make([]*api.ActionTask, len(tasks))
for i := range tasks {
convertedTask, err := convert.ToActionTask(ctx, tasks[i])
if err != nil {
ctx.Error(http.StatusInternalServerError, "ToActionTask", err)
return
}
res.Entries[i] = convertedTask
}

ctx.JSON(http.StatusOK, &res)
}
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ type swaggerRepoNewIssuePinsAllowed struct {
Body api.NewIssuePinsAllowed `json:"body"`
}

// TasksList
// swagger:response TasksList
type swaggerRepoTasksList struct {
// in:body
Body api.ActionTaskResponse `json:"body"`
}

// swagger:response Compare
type swaggerCompare struct {
// in:body
Expand Down
27 changes: 27 additions & 0 deletions services/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

actions_model "code.gitea.io/gitea/models/actions"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/auth"
git_model "code.gitea.io/gitea/models/git"
Expand All @@ -24,6 +25,7 @@ import (
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/gitdiff"
Expand Down Expand Up @@ -193,6 +195,31 @@ func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
}
}

// ToActionTask convert a actions_model.ActionTask to an api.ActionTask
func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.ActionTask, error) {
if err := t.LoadAttributes(ctx); err != nil {
return nil, err
}

url := strings.TrimSuffix(setting.AppURL, "/") + t.GetRunLink()

return &api.ActionTask{
ID: t.ID,
Name: t.Job.Name,
HeadBranch: t.Job.Run.PrettyRef(),
HeadSHA: t.Job.CommitSHA,
RunNumber: t.Job.Run.Index,
Event: t.Job.Run.TriggerEvent,
DisplayTitle: t.Job.Run.Title,
Status: t.Status.String(),
WorkflowID: t.Job.Run.WorkflowID,
URL: url,
CreatedAt: t.Created.AsLocalTime(),
UpdatedAt: t.Updated.AsLocalTime(),
RunStartedAt: t.Started.AsLocalTime(),
}, nil
}

// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
func ToVerification(ctx context.Context, c *git.Commit) *api.PayloadCommitVerification {
verif := asymkey_model.ParseCommitWithSignature(ctx, c)
Expand Down
149 changes: 149 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.