mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	Add API endpoints for getting action jobs status
This commit is contained in:
		
							
								
								
									
										23
									
								
								modules/structs/repo_actions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								modules/structs/repo_actions.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | // Copyright 2019 The Gitea Authors. All rights reserved. | ||||||
|  | // SPDX-License-Identifier: MIT | ||||||
|  |  | ||||||
|  | package structs | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Tag represents a repository tag | ||||||
|  | type ActionTask struct { | ||||||
|  | 	ID         int64  `json:"id"` | ||||||
|  | 	JobName    string `json:"job_name"` | ||||||
|  | 	WorkflowID string `json:"workflow_id"` | ||||||
|  | 	Title      string `json:"title"` | ||||||
|  | 	Status     string `json:"status"` | ||||||
|  | 	Commit     string `json:"commit"` | ||||||
|  | 	Duration   string `json:"duration"` | ||||||
|  | 	// swagger:strfmt date-time | ||||||
|  | 	Started time.Time `json:"started,omitempty"` | ||||||
|  | 	// swagger:strfmt date-time | ||||||
|  | 	Stopped time.Time `json:"stopped,omitempty"` | ||||||
|  | } | ||||||
| @@ -994,6 +994,9 @@ func Routes() *web.Route { | |||||||
| 					m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateTagOption{}), repo.CreateTag) | 					m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateTagOption{}), repo.CreateTag) | ||||||
| 					m.Delete("/*", reqToken(), repo.DeleteTag) | 					m.Delete("/*", reqToken(), repo.DeleteTag) | ||||||
| 				}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true)) | 				}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true)) | ||||||
|  | 				m.Group("/actions", func() { | ||||||
|  | 					m.Get("/tasks", repo.ListActionTasks) | ||||||
|  | 				}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true)) | ||||||
| 				m.Group("/keys", func() { | 				m.Group("/keys", func() { | ||||||
| 					m.Combo("").Get(repo.ListDeployKeys). | 					m.Combo("").Get(repo.ListDeployKeys). | ||||||
| 						Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey) | 						Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey) | ||||||
|   | |||||||
							
								
								
									
										69
									
								
								routers/api/v1/repo/actions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								routers/api/v1/repo/actions.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | // 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" | ||||||
|  | 	"code.gitea.io/gitea/modules/context" | ||||||
|  | 	api "code.gitea.io/gitea/modules/structs" | ||||||
|  | 	"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" | ||||||
|  | 	page := ctx.FormInt("page") | ||||||
|  | 	limit := convert.ToCorrectPageSize(ctx.FormInt("limit")) | ||||||
|  |  | ||||||
|  | 	opts := actions_model.FindTaskOptions{ | ||||||
|  | 		RepoID: ctx.Repo.Repository.ID, | ||||||
|  | 		ListOptions: db.ListOptions{ | ||||||
|  | 			Page:     page, | ||||||
|  | 			PageSize: limit, | ||||||
|  | 		}, | ||||||
|  | 		Status:      actions_model.StatusUnknown, // Unknown means all | ||||||
|  | 		IDOrderDesc: true, | ||||||
|  | 	} | ||||||
|  | 	tasks, err := actions_model.FindTasks(ctx, opts) | ||||||
|  | 	if err != nil { | ||||||
|  | 		ctx.Error(http.StatusInternalServerError, "GetTags", err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	apiActionTasks := make([]*api.ActionTask, len(tasks)) | ||||||
|  | 	for i := range tasks { | ||||||
|  | 		apiActionTasks[i] = convert.ToActionTask(ctx, ctx.Repo.Repository, tasks[i]) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	ctx.JSON(http.StatusOK, &apiActionTasks) | ||||||
|  | } | ||||||
| @@ -11,6 +11,7 @@ import ( | |||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	actions_model "code.gitea.io/gitea/models/actions" | ||||||
| 	asymkey_model "code.gitea.io/gitea/models/asymkey" | 	asymkey_model "code.gitea.io/gitea/models/asymkey" | ||||||
| 	"code.gitea.io/gitea/models/auth" | 	"code.gitea.io/gitea/models/auth" | ||||||
| 	"code.gitea.io/gitea/models/db" | 	"code.gitea.io/gitea/models/db" | ||||||
| @@ -179,6 +180,24 @@ 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, repo *repo_model.Repository, t *actions_model.ActionTask) *api.ActionTask { | ||||||
|  | 	if err := t.LoadAttributes(ctx); err != nil { | ||||||
|  | 		log.Warn("LoadAttributes of ActionTask: %v", err) | ||||||
|  | 	} | ||||||
|  | 	return &api.ActionTask{ | ||||||
|  | 		ID:         t.ID, | ||||||
|  | 		JobName:    t.Job.Name, | ||||||
|  | 		WorkflowID: t.Job.Run.WorkflowID, | ||||||
|  | 		Title:      t.Job.Run.Title, | ||||||
|  | 		Status:     t.Status.String(), | ||||||
|  | 		Commit:     t.CommitSHA, | ||||||
|  | 		Duration:   t.Duration().String(), | ||||||
|  | 		Started:    t.Started.AsLocalTime(), | ||||||
|  | 		Stopped:    t.Stopped.AsLocalTime(), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| // ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification | // ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification | ||||||
| func ToVerification(ctx context.Context, c *git.Commit) *api.PayloadCommitVerification { | func ToVerification(ctx context.Context, c *git.Commit) *api.PayloadCommitVerification { | ||||||
| 	verif := asymkey_model.ParseCommitWithSignature(ctx, c) | 	verif := asymkey_model.ParseCommitWithSignature(ctx, c) | ||||||
|   | |||||||
							
								
								
									
										99
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										99
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							| @@ -12313,6 +12313,51 @@ | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     }, |     }, | ||||||
|  |     "/repos/{owner}/{repo}/actions/tasks": { | ||||||
|  |       "get": { | ||||||
|  |         "produces": [ | ||||||
|  |           "application/json" | ||||||
|  |         ], | ||||||
|  |         "tags": [ | ||||||
|  |           "repository" | ||||||
|  |         ], | ||||||
|  |         "summary": "List a repository's actions tasks", | ||||||
|  |         "operationId": "ListActionTasks", | ||||||
|  |         "parameters": [ | ||||||
|  |           { | ||||||
|  |             "type": "string", | ||||||
|  |             "description": "owner of the repo", | ||||||
|  |             "name": "owner", | ||||||
|  |             "in": "path", | ||||||
|  |             "required": true | ||||||
|  |           }, | ||||||
|  |           { | ||||||
|  |             "type": "string", | ||||||
|  |             "description": "name of the repo", | ||||||
|  |             "name": "repo", | ||||||
|  |             "in": "path", | ||||||
|  |             "required": true | ||||||
|  |           }, | ||||||
|  |           { | ||||||
|  |             "type": "integer", | ||||||
|  |             "description": "page number of results to return (1-based)", | ||||||
|  |             "name": "page", | ||||||
|  |             "in": "query" | ||||||
|  |           }, | ||||||
|  |           { | ||||||
|  |             "type": "integer", | ||||||
|  |             "description": "page size of results, default maximum page size is 50", | ||||||
|  |             "name": "limit", | ||||||
|  |             "in": "query" | ||||||
|  |           } | ||||||
|  |         ], | ||||||
|  |         "responses": { | ||||||
|  |           "200": { | ||||||
|  |             "$ref": "#/responses/TasksList" | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     }, | ||||||
|     "/repos/{owner}/{repo}/teams": { |     "/repos/{owner}/{repo}/teams": { | ||||||
|       "get": { |       "get": { | ||||||
|         "produces": [ |         "produces": [ | ||||||
| @@ -21414,6 +21459,51 @@ | |||||||
|       }, |       }, | ||||||
|       "x-go-package": "code.gitea.io/gitea/modules/structs" |       "x-go-package": "code.gitea.io/gitea/modules/structs" | ||||||
|     }, |     }, | ||||||
|  |     "Task": { | ||||||
|  |       "description": "Task represents a task", | ||||||
|  |       "type": "object", | ||||||
|  |       "properties": { | ||||||
|  |         "id": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "ID" | ||||||
|  |         }, | ||||||
|  |         "job_name": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "JobName" | ||||||
|  |         }, | ||||||
|  |         "workflow_id": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "WorkflowID" | ||||||
|  |         }, | ||||||
|  |         "title": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "Title" | ||||||
|  |         }, | ||||||
|  |         "status": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "Status" | ||||||
|  |         }, | ||||||
|  |         "commit": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "Commit" | ||||||
|  |         }, | ||||||
|  |         "duration": { | ||||||
|  |           "type": "string", | ||||||
|  |           "x-go-name": "Duration" | ||||||
|  |         }, | ||||||
|  |         "started": { | ||||||
|  |           "type": "string", | ||||||
|  |           "format": "date-time", | ||||||
|  |           "x-go-name": "Started" | ||||||
|  |         }, | ||||||
|  |         "stopped": { | ||||||
|  |           "type": "string", | ||||||
|  |           "format": "date-time", | ||||||
|  |           "x-go-name": "Stopped" | ||||||
|  |         } | ||||||
|  |       }, | ||||||
|  |       "x-go-package": "code.gitea.io/gitea/modules/structs" | ||||||
|  |     }, | ||||||
|     "ServerVersion": { |     "ServerVersion": { | ||||||
|       "description": "ServerVersion wraps the version of the server", |       "description": "ServerVersion wraps the version of the server", | ||||||
|       "type": "object", |       "type": "object", | ||||||
| @@ -23000,6 +23090,15 @@ | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     }, |     }, | ||||||
|  |     "TasksList": { | ||||||
|  |       "description": "TasksList", | ||||||
|  |       "schema": { | ||||||
|  |         "type": "array", | ||||||
|  |         "items": { | ||||||
|  |           "$ref": "#/definitions/Task" | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     }, | ||||||
|     "ServerVersion": { |     "ServerVersion": { | ||||||
|       "description": "ServerVersion", |       "description": "ServerVersion", | ||||||
|       "schema": { |       "schema": { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user