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