2016-01-29 01:19:05 +05:30
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2019-04-19 17:47:27 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2016-01-15 23:54:03 +05:30
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-06-07 20:22:59 +05:30
|
|
|
"errors"
|
2020-04-19 08:08:09 +05:30
|
|
|
"fmt"
|
2019-12-20 22:37:12 +05:30
|
|
|
"net/http"
|
|
|
|
|
2020-02-13 04:49:35 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2023-12-11 14:26:48 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-04-19 17:47:27 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2024-02-23 07:48:33 +05:30
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2023-06-29 15:33:20 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
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"
|
2021-02-04 00:36:13 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2020-10-14 00:20:57 +05:30
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
2020-09-11 19:44:48 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2016-01-15 23:54:03 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// GetBranch get a branch of a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
func GetBranch(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
|
|
|
|
// ---
|
2019-11-17 01:09:18 +05:30
|
|
|
// summary: Retrieve a specific branch from a repository, including its effective branch protection
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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: branch
|
|
|
|
// in: path
|
|
|
|
// description: branch to get
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Branch"
|
2020-11-14 21:43:55 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2020-11-14 21:43:55 +05:30
|
|
|
branchName := ctx.Params("*")
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
|
2016-01-15 23:54:03 +05:30
|
|
|
if err != nil {
|
2019-04-19 17:47:27 +05:30
|
|
|
if git.IsErrBranchNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound(err)
|
2017-06-11 08:27:28 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
|
2017-06-11 08:27:28 +05:30
|
|
|
}
|
2016-01-15 23:54:03 +05:30
|
|
|
return
|
|
|
|
}
|
2016-02-03 03:37:40 +05:30
|
|
|
|
2016-01-15 23:54:03 +05:30
|
|
|
c, err := branch.GetCommit()
|
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
2016-01-15 23:54:03 +05:30
|
|
|
return
|
|
|
|
}
|
2016-02-03 03:37:40 +05:30
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branchName)
|
2019-11-17 01:09:18 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
|
2019-11-17 01:09:18 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 15:33:20 +05:30
|
|
|
br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
2020-03-21 09:11:33 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, br)
|
2016-01-15 23:54:03 +05:30
|
|
|
}
|
|
|
|
|
2020-04-19 08:08:09 +05:30
|
|
|
// DeleteBranch get a branch of a repository
|
|
|
|
func DeleteBranch(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/branches/{branch} repository repoDeleteBranch
|
|
|
|
// ---
|
|
|
|
// summary: Delete a specific branch from a repository
|
|
|
|
// 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: branch
|
|
|
|
// in: path
|
|
|
|
// description: branch to delete
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/error"
|
2020-11-14 21:43:55 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2023-09-22 05:13:29 +05:30
|
|
|
// "423":
|
|
|
|
// "$ref": "#/responses/repoArchivedError"
|
2023-07-01 08:22:52 +05:30
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Repo.Repository.IsMirror {
|
|
|
|
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:43:55 +05:30
|
|
|
branchName := ctx.Params("*")
|
2020-04-19 08:08:09 +05:30
|
|
|
|
2023-06-29 15:33:20 +05:30
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
ctx.Error(http.StatusForbidden, "", "Git Repository is empty.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether branches of this repository has been synced
|
2023-12-11 14:26:48 +05:30
|
|
|
totalNumOfBranches, err := db.Count[git_model.Branch](ctx, git_model.FindBranchOptions{
|
2023-06-29 15:33:20 +05:30
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2024-02-23 07:48:33 +05:30
|
|
|
IsDeletedBranch: optional.Some(false),
|
2023-06-29 15:33:20 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "CountBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if totalNumOfBranches == 0 { // sync branches immediately because non-empty repository should have at least 1 branch
|
|
|
|
_, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SyncRepoBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Repo.Repository.IsMirror {
|
|
|
|
ctx.Error(http.StatusForbidden, "IsMirrored", fmt.Errorf("can not delete branch of an mirror repository"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-01 03:47:51 +05:30
|
|
|
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
|
2021-06-07 20:22:59 +05:30
|
|
|
switch {
|
|
|
|
case git.IsErrBranchNotExist(err):
|
2020-04-19 08:08:09 +05:30
|
|
|
ctx.NotFound(err)
|
2021-06-07 20:22:59 +05:30
|
|
|
case errors.Is(err, repo_service.ErrBranchIsDefault):
|
|
|
|
ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
|
2023-01-16 13:30:22 +05:30
|
|
|
case errors.Is(err, git_model.ErrBranchIsProtected):
|
2021-06-07 20:22:59 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
|
|
|
|
default:
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
|
2020-04-19 08:08:09 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:46:20 +05:30
|
|
|
// CreateBranch creates a branch for a user's repository
|
2021-01-26 21:06:53 +05:30
|
|
|
func CreateBranch(ctx *context.APIContext) {
|
2020-05-29 23:46:20 +05:30
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/branches repository repoCreateBranch
|
|
|
|
// ---
|
|
|
|
// summary: Create a branch
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// 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: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/CreateBranchRepoOption"
|
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/Branch"
|
2023-07-01 08:22:52 +05:30
|
|
|
// "403":
|
|
|
|
// description: The branch is archived or a mirror.
|
2020-05-29 23:46:20 +05:30
|
|
|
// "404":
|
|
|
|
// description: The old branch does not exist.
|
|
|
|
// "409":
|
|
|
|
// description: The branch with the same name already exists.
|
2023-09-22 05:13:29 +05:30
|
|
|
// "423":
|
|
|
|
// "$ref": "#/responses/repoArchivedError"
|
2020-05-29 23:46:20 +05:30
|
|
|
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-01 08:22:52 +05:30
|
|
|
if ctx.Repo.Repository.IsMirror {
|
|
|
|
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
|
|
|
|
|
2023-05-09 15:52:32 +05:30
|
|
|
var oldCommit *git.Commit
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(opt.OldRefName) > 0 {
|
|
|
|
oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if len(opt.OldBranchName) > 0 { //nolint
|
|
|
|
if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint
|
|
|
|
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
|
|
|
|
return
|
|
|
|
}
|
2020-05-29 23:46:20 +05:30
|
|
|
}
|
|
|
|
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 19:00:56 +05:30
|
|
|
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, oldCommit.ID.String(), opt.BranchName)
|
2020-05-29 23:46:20 +05:30
|
|
|
if err != nil {
|
2023-06-29 15:33:20 +05:30
|
|
|
if git_model.IsErrBranchNotExist(err) {
|
2020-05-29 23:46:20 +05:30
|
|
|
ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 19:00:56 +05:30
|
|
|
} else if models.IsErrTagAlreadyExists(err) {
|
2020-05-29 23:46:20 +05:30
|
|
|
ctx.Error(http.StatusConflict, "", "The branch with the same tag already exists.")
|
2023-06-29 15:33:20 +05:30
|
|
|
} else if git_model.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
|
2020-05-29 23:46:20 +05:30
|
|
|
ctx.Error(http.StatusConflict, "", "The branch already exists.")
|
2023-06-29 15:33:20 +05:30
|
|
|
} else if git_model.IsErrBranchNameConflict(err) {
|
2020-05-29 23:46:20 +05:30
|
|
|
ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.")
|
|
|
|
} else {
|
2023-05-09 15:52:32 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateNewBranchFromCommit", err)
|
2020-05-29 23:46:20 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName)
|
2020-05-29 23:46:20 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := branch.GetCommit()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branch.Name)
|
2020-05-29 23:46:20 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 15:33:20 +05:30
|
|
|
br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, commit, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
2020-05-29 23:46:20 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusCreated, br)
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ListBranches list all the branches of a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
func ListBranches(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
|
|
|
|
// ---
|
|
|
|
// summary: List a repository's branches
|
|
|
|
// 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
|
2021-02-04 00:36:13 +05:30
|
|
|
// - 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-13 12:32:25 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/BranchList"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2023-06-29 15:33:20 +05:30
|
|
|
var totalNumOfBranches int64
|
2022-12-04 14:27:17 +05:30
|
|
|
var apiBranches []*api.Branch
|
|
|
|
|
2021-02-04 00:36:13 +05:30
|
|
|
listOptions := utils.GetListOptions(ctx)
|
2016-02-03 03:37:40 +05:30
|
|
|
|
2023-07-01 08:22:52 +05:30
|
|
|
if !ctx.Repo.Repository.IsEmpty {
|
|
|
|
if ctx.Repo.GitRepo == nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Load git repository failed", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 15:33:20 +05:30
|
|
|
branchOpts := git_model.FindBranchOptions{
|
|
|
|
ListOptions: listOptions,
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2024-02-23 07:48:33 +05:30
|
|
|
IsDeletedBranch: optional.Some(false),
|
2023-06-29 15:33:20 +05:30
|
|
|
}
|
|
|
|
var err error
|
2023-12-11 14:26:48 +05:30
|
|
|
totalNumOfBranches, err = db.Count[git_model.Branch](ctx, branchOpts)
|
2023-06-29 15:33:20 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "CountBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if totalNumOfBranches == 0 { // sync branches immediately because non-empty repository should have at least 1 branch
|
|
|
|
totalNumOfBranches, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SyncRepoBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "FindMatchedProtectedBranchRules", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-11 14:26:48 +05:30
|
|
|
branches, err := db.Find[git_model.Branch](ctx, branchOpts)
|
2019-11-17 01:09:18 +05:30
|
|
|
if err != nil {
|
2022-12-04 14:27:17 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
|
2019-11-17 01:09:18 +05:30
|
|
|
return
|
|
|
|
}
|
2022-12-04 14:27:17 +05:30
|
|
|
|
|
|
|
apiBranches = make([]*api.Branch, 0, len(branches))
|
|
|
|
for i := range branches {
|
2023-06-29 15:33:20 +05:30
|
|
|
c, err := ctx.Repo.GitRepo.GetBranchCommit(branches[i].Name)
|
2022-12-04 14:27:17 +05:30
|
|
|
if err != nil {
|
|
|
|
// Skip if this branch doesn't exist anymore.
|
|
|
|
if git.IsErrNotExist(err) {
|
2023-06-29 15:33:20 +05:30
|
|
|
totalNumOfBranches--
|
2022-12-04 14:27:17 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-16 13:30:22 +05:30
|
|
|
|
|
|
|
branchProtection := rules.GetFirstMatched(branches[i].Name)
|
2023-06-29 15:33:20 +05:30
|
|
|
apiBranch, err := convert.ToBranch(ctx, ctx.Repo.Repository, branches[i].Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
2022-12-04 14:27:17 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiBranches = append(apiBranches, apiBranch)
|
2020-03-21 09:11:33 +05:30
|
|
|
}
|
2016-01-15 23:54:03 +05:30
|
|
|
}
|
2016-02-03 03:37:40 +05:30
|
|
|
|
2023-06-29 15:33:20 +05:30
|
|
|
ctx.SetLinkHeader(int(totalNumOfBranches), listOptions.PageSize)
|
|
|
|
ctx.SetTotalCountHeader(totalNumOfBranches)
|
2022-12-04 14:27:17 +05:30
|
|
|
ctx.JSON(http.StatusOK, apiBranches)
|
2016-01-15 23:54:03 +05:30
|
|
|
}
|
2020-02-13 04:49:35 +05:30
|
|
|
|
|
|
|
// GetBranchProtection gets a branch protection
|
|
|
|
func GetBranchProtection(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/branch_protections/{name} repository repoGetBranchProtection
|
|
|
|
// ---
|
|
|
|
// summary: Get a specific branch protection for the repository
|
|
|
|
// 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: name
|
|
|
|
// in: path
|
|
|
|
// description: name of protected branch
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/BranchProtection"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
bpName := ctx.Params(":name")
|
2023-01-16 13:30:22 +05:30
|
|
|
bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bp == nil || bp.RepoID != repo.ID {
|
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:54:07 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp, repo))
|
2020-02-13 04:49:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ListBranchProtections list branch protections for a repo
|
|
|
|
func ListBranchProtections(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/branch_protections repository repoListBranchProtection
|
|
|
|
// ---
|
|
|
|
// summary: List branch protections for a repository
|
|
|
|
// 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
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/BranchProtectionList"
|
|
|
|
|
|
|
|
repo := ctx.Repo.Repository
|
2023-01-16 13:30:22 +05:30
|
|
|
bps, err := git_model.FindRepoProtectedBranchRules(ctx, repo.ID)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiBps := make([]*api.BranchProtection, len(bps))
|
|
|
|
for i := range bps {
|
2024-04-17 18:54:07 +05:30
|
|
|
apiBps[i] = convert.ToBranchProtection(ctx, bps[i], repo)
|
2020-02-13 04:49:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, apiBps)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBranchProtection creates a branch protection for a repo
|
2021-01-26 21:06:53 +05:30
|
|
|
func CreateBranchProtection(ctx *context.APIContext) {
|
2020-02-13 04:49:35 +05:30
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/branch_protections repository repoCreateBranchProtection
|
|
|
|
// ---
|
|
|
|
// summary: Create a branch protections for a repository
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// 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: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/CreateBranchProtectionOption"
|
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/BranchProtection"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
2023-09-22 05:13:29 +05:30
|
|
|
// "423":
|
|
|
|
// "$ref": "#/responses/repoArchivedError"
|
2020-02-13 04:49:35 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.CreateBranchProtectionOption)
|
2020-02-13 04:49:35 +05:30
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
ruleName := form.RuleName
|
|
|
|
if ruleName == "" {
|
|
|
|
ruleName = form.BranchName //nolint
|
|
|
|
}
|
2023-04-10 08:22:16 +05:30
|
|
|
if len(ruleName) == 0 {
|
|
|
|
ctx.Error(http.StatusBadRequest, "both rule_name and branch_name are empty", "both rule_name and branch_name are empty")
|
|
|
|
return
|
|
|
|
}
|
2023-01-16 13:30:22 +05:30
|
|
|
|
|
|
|
isPlainRule := !git_model.IsRuleNameSpecial(ruleName)
|
|
|
|
var isBranchExist bool
|
|
|
|
if isPlainRule {
|
|
|
|
isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), ruleName)
|
2020-02-13 04:49:35 +05:30
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, ruleName)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectBranchOfRepoByName", err)
|
|
|
|
return
|
|
|
|
} else if protectBranch != nil {
|
|
|
|
ctx.Error(http.StatusForbidden, "Create branch protection", "Branch protection already exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var requiredApprovals int64
|
|
|
|
if form.RequiredApprovals > 0 {
|
|
|
|
requiredApprovals = form.RequiredApprovals
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
whitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.PushWhitelistUsernames, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
mergeWhitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.MergeWhitelistUsernames, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
approvalsWhitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.ApprovalsWhitelistUsernames, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
|
|
|
|
if repo.Owner.IsOrganization() {
|
2023-10-03 16:00:41 +05:30
|
|
|
whitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.PushWhitelistTeams, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2022-03-29 11:59:02 +05:30
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-03 16:00:41 +05:30
|
|
|
mergeWhitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.MergeWhitelistTeams, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2022-03-29 11:59:02 +05:30
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-03 16:00:41 +05:30
|
|
|
approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.ApprovalsWhitelistTeams, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2022-03-29 11:59:02 +05:30
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
protectBranch = &git_model.ProtectedBranch{
|
2020-11-29 01:00:46 +05:30
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2023-04-10 08:22:16 +05:30
|
|
|
RuleName: ruleName,
|
2020-11-29 01:00:46 +05:30
|
|
|
CanPush: form.EnablePush,
|
|
|
|
EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
|
|
|
|
EnableMergeWhitelist: form.EnableMergeWhitelist,
|
|
|
|
WhitelistDeployKeys: form.EnablePush && form.EnablePushWhitelist && form.PushWhitelistDeployKeys,
|
|
|
|
EnableStatusCheck: form.EnableStatusCheck,
|
|
|
|
StatusCheckContexts: form.StatusCheckContexts,
|
|
|
|
EnableApprovalsWhitelist: form.EnableApprovalsWhitelist,
|
|
|
|
RequiredApprovals: requiredApprovals,
|
|
|
|
BlockOnRejectedReviews: form.BlockOnRejectedReviews,
|
|
|
|
BlockOnOfficialReviewRequests: form.BlockOnOfficialReviewRequests,
|
|
|
|
DismissStaleApprovals: form.DismissStaleApprovals,
|
2024-01-15 12:50:01 +05:30
|
|
|
IgnoreStaleApprovals: form.IgnoreStaleApprovals,
|
2020-11-29 01:00:46 +05:30
|
|
|
RequireSignedCommits: form.RequireSignedCommits,
|
|
|
|
ProtectedFilePatterns: form.ProtectedFilePatterns,
|
2021-09-11 19:51:17 +05:30
|
|
|
UnprotectedFilePatterns: form.UnprotectedFilePatterns,
|
2020-11-29 01:00:46 +05:30
|
|
|
BlockOnOutdatedBranch: form.BlockOnOutdatedBranch,
|
2024-03-29 02:11:52 +05:30
|
|
|
ApplyToAdmins: form.ApplyToAdmins,
|
2020-02-13 04:49:35 +05:30
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
|
2020-02-13 04:49:35 +05:30
|
|
|
UserIDs: whitelistUsers,
|
|
|
|
TeamIDs: whitelistTeams,
|
|
|
|
MergeUserIDs: mergeWhitelistUsers,
|
|
|
|
MergeTeamIDs: mergeWhitelistTeams,
|
|
|
|
ApprovalsUserIDs: approvalsWhitelistUsers,
|
|
|
|
ApprovalsTeamIDs: approvalsWhitelistTeams,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
if isBranchExist {
|
2023-07-22 19:44:27 +05:30
|
|
|
if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, ruleName); err != nil {
|
2023-01-16 13:30:22 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !isPlainRule {
|
|
|
|
if ctx.Repo.GitRepo == nil {
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
|
2023-01-16 13:30:22 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
ctx.Repo.GitRepo.Close()
|
|
|
|
ctx.Repo.GitRepo = nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
// FIXME: since we only need to recheck files protected rules, we could improve this
|
2023-06-29 15:33:20 +05:30
|
|
|
matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, ruleName)
|
2023-01-16 13:30:22 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, branchName := range matchedBranches {
|
2023-07-22 19:44:27 +05:30
|
|
|
if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil {
|
2023-01-16 13:30:22 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 00:20:57 +05:30
|
|
|
}
|
|
|
|
|
2020-02-13 04:49:35 +05:30
|
|
|
// Reload from db to get all whitelists
|
2023-04-10 08:22:16 +05:30
|
|
|
bp, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, ruleName)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:54:07 +05:30
|
|
|
ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp, repo))
|
2020-02-13 04:49:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// EditBranchProtection edits a branch protection for a repo
|
2021-01-26 21:06:53 +05:30
|
|
|
func EditBranchProtection(ctx *context.APIContext) {
|
2020-02-13 04:49:35 +05:30
|
|
|
// swagger:operation PATCH /repos/{owner}/{repo}/branch_protections/{name} repository repoEditBranchProtection
|
|
|
|
// ---
|
|
|
|
// summary: Edit a branch protections for a repository. Only fields that are set will be changed
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// 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: name
|
|
|
|
// in: path
|
|
|
|
// description: name of protected branch
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditBranchProtectionOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/BranchProtection"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
2023-09-22 05:13:29 +05:30
|
|
|
// "423":
|
|
|
|
// "$ref": "#/responses/repoArchivedError"
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
|
2020-02-13 04:49:35 +05:30
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
bpName := ctx.Params(":name")
|
2023-01-16 13:30:22 +05:30
|
|
|
protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if protectBranch == nil || protectBranch.RepoID != repo.ID {
|
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.EnablePush != nil {
|
|
|
|
if !*form.EnablePush {
|
|
|
|
protectBranch.CanPush = false
|
|
|
|
protectBranch.EnableWhitelist = false
|
|
|
|
protectBranch.WhitelistDeployKeys = false
|
|
|
|
} else {
|
|
|
|
protectBranch.CanPush = true
|
|
|
|
if form.EnablePushWhitelist != nil {
|
|
|
|
if !*form.EnablePushWhitelist {
|
|
|
|
protectBranch.EnableWhitelist = false
|
|
|
|
protectBranch.WhitelistDeployKeys = false
|
|
|
|
} else {
|
|
|
|
protectBranch.EnableWhitelist = true
|
|
|
|
if form.PushWhitelistDeployKeys != nil {
|
|
|
|
protectBranch.WhitelistDeployKeys = *form.PushWhitelistDeployKeys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.EnableMergeWhitelist != nil {
|
|
|
|
protectBranch.EnableMergeWhitelist = *form.EnableMergeWhitelist
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.EnableStatusCheck != nil {
|
|
|
|
protectBranch.EnableStatusCheck = *form.EnableStatusCheck
|
|
|
|
}
|
2023-08-24 11:06:04 +05:30
|
|
|
|
|
|
|
if form.StatusCheckContexts != nil {
|
2020-02-13 04:49:35 +05:30
|
|
|
protectBranch.StatusCheckContexts = form.StatusCheckContexts
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.RequiredApprovals != nil && *form.RequiredApprovals >= 0 {
|
|
|
|
protectBranch.RequiredApprovals = *form.RequiredApprovals
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.EnableApprovalsWhitelist != nil {
|
|
|
|
protectBranch.EnableApprovalsWhitelist = *form.EnableApprovalsWhitelist
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.BlockOnRejectedReviews != nil {
|
|
|
|
protectBranch.BlockOnRejectedReviews = *form.BlockOnRejectedReviews
|
|
|
|
}
|
|
|
|
|
2020-11-29 01:00:46 +05:30
|
|
|
if form.BlockOnOfficialReviewRequests != nil {
|
|
|
|
protectBranch.BlockOnOfficialReviewRequests = *form.BlockOnOfficialReviewRequests
|
|
|
|
}
|
|
|
|
|
2020-02-13 04:49:35 +05:30
|
|
|
if form.DismissStaleApprovals != nil {
|
|
|
|
protectBranch.DismissStaleApprovals = *form.DismissStaleApprovals
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:50:01 +05:30
|
|
|
if form.IgnoreStaleApprovals != nil {
|
|
|
|
protectBranch.IgnoreStaleApprovals = *form.IgnoreStaleApprovals
|
|
|
|
}
|
|
|
|
|
2020-02-13 04:49:35 +05:30
|
|
|
if form.RequireSignedCommits != nil {
|
|
|
|
protectBranch.RequireSignedCommits = *form.RequireSignedCommits
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:56:34 +05:30
|
|
|
if form.ProtectedFilePatterns != nil {
|
|
|
|
protectBranch.ProtectedFilePatterns = *form.ProtectedFilePatterns
|
|
|
|
}
|
|
|
|
|
2021-09-11 19:51:17 +05:30
|
|
|
if form.UnprotectedFilePatterns != nil {
|
|
|
|
protectBranch.UnprotectedFilePatterns = *form.UnprotectedFilePatterns
|
|
|
|
}
|
|
|
|
|
2020-04-17 06:30:36 +05:30
|
|
|
if form.BlockOnOutdatedBranch != nil {
|
|
|
|
protectBranch.BlockOnOutdatedBranch = *form.BlockOnOutdatedBranch
|
|
|
|
}
|
|
|
|
|
2024-03-29 02:11:52 +05:30
|
|
|
if form.ApplyToAdmins != nil {
|
|
|
|
protectBranch.ApplyToAdmins = *form.ApplyToAdmins
|
|
|
|
}
|
|
|
|
|
2020-02-13 04:49:35 +05:30
|
|
|
var whitelistUsers []int64
|
|
|
|
if form.PushWhitelistUsernames != nil {
|
2022-11-19 13:42:33 +05:30
|
|
|
whitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.PushWhitelistUsernames, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
whitelistUsers = protectBranch.WhitelistUserIDs
|
|
|
|
}
|
|
|
|
var mergeWhitelistUsers []int64
|
|
|
|
if form.MergeWhitelistUsernames != nil {
|
2022-11-19 13:42:33 +05:30
|
|
|
mergeWhitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.MergeWhitelistUsernames, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mergeWhitelistUsers = protectBranch.MergeWhitelistUserIDs
|
|
|
|
}
|
|
|
|
var approvalsWhitelistUsers []int64
|
|
|
|
if form.ApprovalsWhitelistUsernames != nil {
|
2022-11-19 13:42:33 +05:30
|
|
|
approvalsWhitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.ApprovalsWhitelistUsernames, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
approvalsWhitelistUsers = protectBranch.ApprovalsWhitelistUserIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
|
|
|
|
if repo.Owner.IsOrganization() {
|
|
|
|
if form.PushWhitelistTeams != nil {
|
2023-10-03 16:00:41 +05:30
|
|
|
whitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.PushWhitelistTeams, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2022-03-29 11:59:02 +05:30
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
whitelistTeams = protectBranch.WhitelistTeamIDs
|
|
|
|
}
|
|
|
|
if form.MergeWhitelistTeams != nil {
|
2023-10-03 16:00:41 +05:30
|
|
|
mergeWhitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.MergeWhitelistTeams, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2022-03-29 11:59:02 +05:30
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mergeWhitelistTeams = protectBranch.MergeWhitelistTeamIDs
|
|
|
|
}
|
|
|
|
if form.ApprovalsWhitelistTeams != nil {
|
2023-10-03 16:00:41 +05:30
|
|
|
approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.ApprovalsWhitelistTeams, false)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
2022-03-29 11:59:02 +05:30
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
approvalsWhitelistTeams = protectBranch.ApprovalsWhitelistTeamIDs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
|
2020-02-13 04:49:35 +05:30
|
|
|
UserIDs: whitelistUsers,
|
|
|
|
TeamIDs: whitelistTeams,
|
|
|
|
MergeUserIDs: mergeWhitelistUsers,
|
|
|
|
MergeTeamIDs: mergeWhitelistTeams,
|
|
|
|
ApprovalsUserIDs: approvalsWhitelistUsers,
|
|
|
|
ApprovalsTeamIDs: approvalsWhitelistTeams,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
isPlainRule := !git_model.IsRuleNameSpecial(bpName)
|
|
|
|
var isBranchExist bool
|
|
|
|
if isPlainRule {
|
|
|
|
isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isBranchExist {
|
2023-07-22 19:44:27 +05:30
|
|
|
if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, bpName); err != nil {
|
2023-01-16 13:30:22 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !isPlainRule {
|
|
|
|
if ctx.Repo.GitRepo == nil {
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
|
2023-01-16 13:30:22 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
ctx.Repo.GitRepo.Close()
|
|
|
|
ctx.Repo.GitRepo = nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: since we only need to recheck files protected rules, we could improve this
|
2023-06-29 15:33:20 +05:30
|
|
|
matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, protectBranch.RuleName)
|
2023-01-16 13:30:22 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, branchName := range matchedBranches {
|
2023-07-22 19:44:27 +05:30
|
|
|
if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil {
|
2023-01-16 13:30:22 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 00:20:57 +05:30
|
|
|
}
|
|
|
|
|
2020-02-13 04:49:35 +05:30
|
|
|
// Reload from db to ensure get all whitelists
|
2023-01-16 13:30:22 +05:30
|
|
|
bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:54:07 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp, repo))
|
2020-02-13 04:49:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBranchProtection deletes a branch protection for a repo
|
|
|
|
func DeleteBranchProtection(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/branch_protections/{name} repository repoDeleteBranchProtection
|
|
|
|
// ---
|
|
|
|
// summary: Delete a specific branch protection for the repository
|
|
|
|
// 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: name
|
|
|
|
// in: path
|
|
|
|
// description: name of protected branch
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
bpName := ctx.Params(":name")
|
2023-01-16 13:30:22 +05:30
|
|
|
bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
|
2020-02-13 04:49:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bp == nil || bp.RepoID != repo.ID {
|
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:13:29 +05:30
|
|
|
if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository, bp.ID); err != nil {
|
2020-02-13 04:49:35 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|