Files

279 lines
7.6 KiB
Go
Raw Permalink Normal View History

2017-04-21 13:32:31 +02:00
// Copyright 2017 Gitea. All rights reserved.
// SPDX-License-Identifier: MIT
2017-04-21 13:32:31 +02:00
package repo
import (
"fmt"
2019-12-20 18:07:12 +01:00
"net/http"
2017-04-21 13:32:31 +02:00
2024-01-15 15:07:32 +01:00
"code.gitea.io/gitea/models/db"
2022-06-12 23:51:54 +08:00
git_model "code.gitea.io/gitea/models/git"
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-24 19:00:29 +00:00
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
2017-04-21 13:32:31 +02:00
)
// NewCommitStatus creates a new CommitStatus
2021-01-26 23:36:53 +08:00
func NewCommitStatus(ctx *context.APIContext) {
2017-11-12 23: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-19 15:46:53 -06:00
// "201":
// "$ref": "#/responses/CommitStatus"
2019-12-20 18:07:12 +01:00
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.CreateStatusOption)
2024-06-19 06:32:45 +08:00
sha := ctx.PathParam("sha")
2017-04-21 13:32:31 +02:00
if len(sha) == 0 {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusBadRequest, nil)
2017-04-21 13:32:31 +02:00
return
}
2022-06-12 23:51:54 +08:00
status := &git_model.CommitStatus{
State: form.State,
2017-04-21 13:32:31 +02:00
TargetURL: form.TargetURL,
Description: form.Description,
Context: form.Context,
}
if err := commitstatus_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.Doer, sha, status); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2017-04-21 13:32:31 +02:00
return
}
ctx.JSON(http.StatusCreated, convert.ToCommitStatus(ctx, 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-12 23: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
// - 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 19:00:29 +00:00
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
2020-01-24 19:00:29 +00:00
// type: integer
2017-11-12 23:02:25 -08:00
// responses:
// "200":
// "$ref": "#/responses/CommitStatusList"
2019-12-20 18:07:12 +01:00
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
2024-06-19 06:32:45 +08:00
getCommitStatuses(ctx, ctx.PathParam("sha"))
2017-11-12 23:02:25 -08:00
}
// 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
// - 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 19:00:29 +00:00
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
2020-01-24 19:00:29 +00:00
// type: integer
2017-11-12 23:02:25 -08:00
// responses:
// "200":
// "$ref": "#/responses/CommitStatusList"
2019-12-20 18:07:12 +01:00
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
refCommit := resolveRefCommit(ctx, ctx.PathParam("ref"), 7)
if ctx.Written() {
return
}
getCommitStatuses(ctx, refCommit.CommitID)
}
func getCommitStatuses(ctx *context.APIContext, commitID string) {
2017-04-21 13:32:31 +02:00
repo := ctx.Repo.Repository
listOptions := utils.GetListOptions(ctx)
2024-01-15 15:07:32 +01:00
statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](ctx, &git_model.CommitStatusOptions{
ListOptions: listOptions,
2024-01-15 15:07:32 +01:00
RepoID: repo.ID,
SHA: commitID,
SortType: ctx.FormTrim("sort"),
State: ctx.FormTrim("state"),
})
2017-04-21 13:32:31 +02:00
if err != nil {
ctx.APIErrorInternal(fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %w", repo.FullName(), commitID, ctx.FormInt("page"), err))
return
2017-04-21 13:32:31 +02:00
}
apiStatuses := make([]*api.CommitStatus, 0, len(statuses))
2017-04-21 13:32:31 +02:00
for _, status := range statuses {
apiStatuses = append(apiStatuses, convert.ToCommitStatus(ctx, status))
2017-04-21 13:32:31 +02:00
}
2026-03-08 15:35:50 +01:00
ctx.SetLinkHeader(maxResults, listOptions.PageSize)
2021-08-12 14:43:08 +02:00
ctx.SetTotalCountHeader(maxResults)
2019-12-20 18:07:12 +01:00
ctx.JSON(http.StatusOK, apiStatuses)
2017-04-21 13:32:31 +02:00
}
2017-11-12 23:02:25 -08:00
// GetCombinedCommitStatusByRef returns the combined status for any given commit hash
func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/status repository repoGetCombinedStatusByRef
2017-11-12 23: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
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
2017-11-12 23:02:25 -08:00
// responses:
// "200":
// "$ref": "#/responses/CombinedStatus"
2019-12-20 18:07:12 +01:00
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
refCommit := resolveRefCommit(ctx, ctx.PathParam("ref"), 7)
if ctx.Written() {
2017-04-21 13:32:31 +02:00
return
}
2017-04-21 13:32:31 +02:00
repo := ctx.Repo.Repository
2026-02-06 14:12:05 +01:00
listOptions := utils.GetListOptions(ctx)
statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, refCommit.Commit.ID.String(), listOptions)
2017-04-21 13:32:31 +02:00
if err != nil {
ctx.APIErrorInternal(fmt.Errorf("GetLatestCommitStatus[%s, %s]: %w", repo.FullName(), refCommit.CommitID, err))
2017-04-21 13:32:31 +02:00
return
}
count, err := git_model.CountLatestCommitStatus(ctx, repo.ID, refCommit.Commit.ID.String())
if err != nil {
ctx.APIErrorInternal(fmt.Errorf("CountLatestCommitStatus[%s, %s]: %w", repo.FullName(), refCommit.CommitID, err))
return
}
2026-03-08 15:35:50 +01:00
ctx.SetLinkHeader(count, listOptions.PageSize)
ctx.SetTotalCountHeader(count)
combiStatus := convert.ToCombinedStatus(ctx, refCommit.Commit.ID.String(), statuses,
convert.ToRepo(ctx, repo, ctx.Repo.Permission))
ctx.JSON(http.StatusOK, combiStatus)
2017-04-21 13:32:31 +02:00
}