2017-04-21 19:32:31 +08:00
|
|
|
// Copyright 2017 Gitea. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-21 01:07:12 +08:00
|
|
|
"net/http"
|
2017-04-21 19:32:31 +08:00
|
|
|
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2017-04-21 19:32:31 +08:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-10-17 12:23:08 +08:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-05-11 18:21:34 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-01-25 03:00:29 +08:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2021-11-24 15:56:24 +08:00
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2017-04-21 19:32:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewCommitStatus creates a new CommitStatus
|
2021-01-26 23:36:53 +08:00
|
|
|
func NewCommitStatus(ctx *context.APIContext) {
|
2017-11-13 15:02:25 +08:00
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/statuses/{sha} repository repoCreateStatus
|
|
|
|
// ---
|
|
|
|
// summary: Create a commit status
|
|
|
|
// 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: sha
|
|
|
|
// in: path
|
|
|
|
// description: sha of the commit
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/CreateStatusOption"
|
|
|
|
// responses:
|
2019-12-20 05:46:53 +08:00
|
|
|
// "201":
|
2020-12-18 11:33:32 +08:00
|
|
|
// "$ref": "#/responses/CommitStatus"
|
2019-12-21 01:07:12 +08:00
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
2021-01-26 23:36:53 +08:00
|
|
|
form := web.GetForm(ctx).(*api.CreateStatusOption)
|
2017-04-21 19:32:31 +08:00
|
|
|
sha := ctx.Params("sha")
|
|
|
|
if len(sha) == 0 {
|
2019-12-21 01:07:12 +08:00
|
|
|
ctx.Error(http.StatusBadRequest, "sha not given", nil)
|
2017-04-21 19:32:31 +08:00
|
|
|
return
|
|
|
|
}
|
2022-06-12 23:51:54 +08:00
|
|
|
status := &git_model.CommitStatus{
|
2022-06-20 18:02:49 +08:00
|
|
|
State: form.State,
|
2017-04-21 19:32:31 +08:00
|
|
|
TargetURL: form.TargetURL,
|
|
|
|
Description: form.Description,
|
|
|
|
Context: form.Context,
|
|
|
|
}
|
2022-03-22 15:03:22 +08:00
|
|
|
if err := files_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.Doer, sha, status); err != nil {
|
2019-12-21 01:07:12 +08:00
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err)
|
2017-04-21 19:32:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-17 12:23:08 +08:00
|
|
|
ctx.JSON(http.StatusCreated, convert.ToCommitStatus(status))
|
2017-04-21 19:32:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommitStatuses returns all statuses for any given commit hash
|
|
|
|
func GetCommitStatuses(ctx *context.APIContext) {
|
2017-11-13 15:02:25 +08:00
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/statuses/{sha} repository repoListStatuses
|
|
|
|
// ---
|
|
|
|
// summary: Get a commit's statuses
|
|
|
|
// 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: sha
|
|
|
|
// in: path
|
|
|
|
// description: sha of the commit
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2019-07-25 18:55:06 +08:00
|
|
|
// - name: sort
|
|
|
|
// in: query
|
|
|
|
// description: type of sort
|
|
|
|
// type: string
|
|
|
|
// enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
|
|
|
|
// required: false
|
|
|
|
// - name: state
|
|
|
|
// in: query
|
|
|
|
// description: type of state
|
|
|
|
// type: string
|
|
|
|
// enum: [pending, success, error, failure, warning]
|
|
|
|
// required: false
|
2020-01-25 03:00:29 +08:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 12:57:38 +08:00
|
|
|
// description: page size of results
|
2020-01-25 03:00:29 +08:00
|
|
|
// type: integer
|
2017-11-13 15:02:25 +08:00
|
|
|
// responses:
|
|
|
|
// "200":
|
2020-12-18 11:33:32 +08:00
|
|
|
// "$ref": "#/responses/CommitStatusList"
|
2019-12-21 01:07:12 +08:00
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
2017-11-13 15:02:25 +08:00
|
|
|
getCommitStatuses(ctx, ctx.Params("sha"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommitStatusesByRef returns all statuses for any given commit ref
|
|
|
|
func GetCommitStatusesByRef(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
|
|
|
|
// ---
|
|
|
|
// summary: Get a commit's statuses, by branch/tag/commit reference
|
|
|
|
// 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: ref
|
|
|
|
// in: path
|
|
|
|
// description: name of branch/tag/commit
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2019-07-25 18:55:06 +08:00
|
|
|
// - name: sort
|
|
|
|
// in: query
|
|
|
|
// description: type of sort
|
|
|
|
// type: string
|
|
|
|
// enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
|
|
|
|
// required: false
|
|
|
|
// - name: state
|
|
|
|
// in: query
|
|
|
|
// description: type of state
|
|
|
|
// type: string
|
|
|
|
// enum: [pending, success, error, failure, warning]
|
|
|
|
// required: false
|
2020-01-25 03:00:29 +08:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 12:57:38 +08:00
|
|
|
// description: page size of results
|
2020-01-25 03:00:29 +08:00
|
|
|
// type: integer
|
2017-11-13 15:02:25 +08:00
|
|
|
// responses:
|
|
|
|
// "200":
|
2020-12-18 11:33:32 +08:00
|
|
|
// "$ref": "#/responses/CommitStatusList"
|
2019-12-21 01:07:12 +08:00
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
2019-08-09 10:13:03 +08:00
|
|
|
|
2021-07-13 15:14:14 +08:00
|
|
|
filter := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
|
|
|
|
if ctx.Written() {
|
2019-08-09 10:13:03 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
getCommitStatuses(ctx, filter) // By default filter is maybe the raw SHA
|
2019-08-09 10:13:03 +08:00
|
|
|
}
|
|
|
|
|
2017-11-13 15:02:25 +08:00
|
|
|
func getCommitStatuses(ctx *context.APIContext, sha string) {
|
2017-04-21 19:32:31 +08:00
|
|
|
if len(sha) == 0 {
|
2019-12-21 01:07:12 +08:00
|
|
|
ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
|
2017-04-21 19:32:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
2020-06-21 16:22:06 +08:00
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
|
|
|
2022-06-12 23:51:54 +08:00
|
|
|
statuses, maxResults, err := git_model.GetCommitStatuses(repo, sha, &git_model.CommitStatusOptions{
|
2020-06-21 16:22:06 +08:00
|
|
|
ListOptions: listOptions,
|
2021-07-29 09:42:15 +08:00
|
|
|
SortType: ctx.FormTrim("sort"),
|
|
|
|
State: ctx.FormTrim("state"),
|
2019-07-25 18:55:06 +08:00
|
|
|
})
|
2017-04-21 19:32:31 +08:00
|
|
|
if err != nil {
|
2021-07-29 09:42:15 +08:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, ctx.FormInt("page"), err))
|
2019-06-30 15:57:59 +08:00
|
|
|
return
|
2017-04-21 19:32:31 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 11:33:32 +08:00
|
|
|
apiStatuses := make([]*api.CommitStatus, 0, len(statuses))
|
2017-04-21 19:32:31 +08:00
|
|
|
for _, status := range statuses {
|
2020-10-17 12:23:08 +08:00
|
|
|
apiStatuses = append(apiStatuses, convert.ToCommitStatus(status))
|
2017-04-21 19:32:31 +08:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:22:06 +08:00
|
|
|
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
|
2021-08-12 20:43:08 +08:00
|
|
|
ctx.SetTotalCountHeader(maxResults)
|
2020-06-21 16:22:06 +08:00
|
|
|
|
2019-12-21 01:07:12 +08:00
|
|
|
ctx.JSON(http.StatusOK, apiStatuses)
|
2017-04-21 19:32:31 +08:00
|
|
|
}
|
|
|
|
|
2017-11-13 15:02:25 +08:00
|
|
|
// GetCombinedCommitStatusByRef returns the combined status for any given commit hash
|
|
|
|
func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
|
2020-12-18 11:33:32 +08:00
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/status repository repoGetCombinedStatusByRef
|
2017-11-13 15:02:25 +08:00
|
|
|
// ---
|
|
|
|
// summary: Get a commit's combined status, by branch/tag/commit reference
|
|
|
|
// 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: ref
|
|
|
|
// in: path
|
|
|
|
// description: name of branch/tag/commit
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2019-07-25 18:55:06 +08:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
2020-12-18 11:33:32 +08:00
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results
|
2019-07-25 18:55:06 +08:00
|
|
|
// type: integer
|
2017-11-13 15:02:25 +08:00
|
|
|
// responses:
|
|
|
|
// "200":
|
2020-12-18 11:33:32 +08:00
|
|
|
// "$ref": "#/responses/CombinedStatus"
|
2019-12-21 01:07:12 +08:00
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
2021-07-13 15:14:14 +08:00
|
|
|
sha := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
|
|
|
|
if ctx.Written() {
|
2017-04-21 19:32:31 +08:00
|
|
|
return
|
|
|
|
}
|
2021-07-13 15:14:14 +08:00
|
|
|
|
2017-04-21 19:32:31 +08:00
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
2022-06-12 23:51:54 +08:00
|
|
|
statuses, count, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, utils.GetListOptions(ctx))
|
2017-04-21 19:32:31 +08:00
|
|
|
if err != nil {
|
2020-12-18 11:33:32 +08:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s]: %v", repo.FullName(), sha, err))
|
2017-04-21 19:32:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(statuses) == 0 {
|
2020-12-18 11:33:32 +08:00
|
|
|
ctx.JSON(http.StatusOK, &api.CombinedStatus{})
|
2017-04-21 19:32:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-18 11:33:32 +08:00
|
|
|
combiStatus := convert.ToCombinedStatus(statuses, convert.ToRepo(repo, ctx.Repo.AccessMode))
|
2017-04-21 19:32:31 +08:00
|
|
|
|
2021-12-15 13:39:34 +08:00
|
|
|
ctx.SetTotalCountHeader(count)
|
2020-12-18 11:33:32 +08:00
|
|
|
ctx.JSON(http.StatusOK, combiStatus)
|
2017-04-21 19:32:31 +08:00
|
|
|
}
|