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