Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some updates to the API for archived repos #27149

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions models/git/protected_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ type WhitelistOptions struct {
// This function also performs check if whitelist user and team's IDs have been changed
// to avoid unnecessary whitelist delete and regenerate.
func UpdateProtectBranch(ctx context.Context, repo *repo_model.Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
err = repo.GetIsArchivedError()
if err != nil {
return err
}

if err = repo.LoadOwner(ctx); err != nil {
return fmt.Errorf("LoadOwner: %v", err)
}
Expand Down Expand Up @@ -445,9 +450,14 @@ func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, curre
}

// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
func DeleteProtectedBranch(ctx context.Context, repoID, id int64) (err error) {
func DeleteProtectedBranch(ctx context.Context, repo *repo_model.Repository, id int64) (err error) {
err = repo.GetIsArchivedError()
if err != nil {
return err
}

protectedBranch := &ProtectedBranch{
RepoID: repoID,
RepoID: repo.ID,
ID: id,
}

Expand Down
18 changes: 18 additions & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func (err ErrUserDoesNotHaveAccessToRepo) Unwrap() error {
return util.ErrPermissionDenied
}

type ErrRepoIsArchived struct {
ID int64
Name string
Owner string
}

func (err ErrRepoIsArchived) Error() string {
return fmt.Sprintf("repo %s/%s is archived", err.Owner, err.Name)
}

var (
reservedRepoNames = []string{".", "..", "-"}
reservedRepoPatterns = []string{"*.git", "*.wiki", "*.rss", "*.atom"}
Expand Down Expand Up @@ -654,6 +664,14 @@ func (repo *Repository) GetTrustModel() TrustModelType {
return trustModel
}

// GetIsArchivedError returns ErrRepoIsArchived is the repo is archived
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
func (repo *Repository) GetIsArchivedError() error {
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
if repo.IsArchived {
return ErrRepoIsArchived{ID: repo.ID, Name: repo.Name, Owner: repo.OwnerName}
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

// __________ .__ __
// \______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
Expand Down
6 changes: 6 additions & 0 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ type APIRedirect struct{}
// swagger:response string
type APIString string

// APIRepoArchivedError is error that is raised when a repo is archived
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
// swagger:response repoArchived
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
type APIRepoArchivedError struct {
APIError
}

// ServerError responds with error message, status is 500
func (ctx *APIContext) ServerError(title string, err error) {
ctx.Error(http.StatusInternalServerError, title, err)
Expand Down
24 changes: 12 additions & 12 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func mustEnableWiki(ctx *context.APIContext) {

func mustNotBeArchived(ctx *context.APIContext) {
if ctx.Repo.Repository.IsArchived {
ctx.NotFound()
ctx.Error(http.StatusLocked, "RepoArchived", fmt.Errorf("repo %s/%s is archived", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name))
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
return
}
}
Expand Down Expand Up @@ -1108,23 +1108,23 @@ func Routes() *web.Route {
m.Group("/branches", func() {
m.Get("", repo.ListBranches)
m.Get("/*", repo.GetBranch)
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), repo.DeleteBranch)
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch)
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteBranch)
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateBranchRepoOption{}), repo.CreateBranch)
}, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode))
m.Group("/branch_protections", func() {
m.Get("", repo.ListBranchProtections)
m.Post("", bind(api.CreateBranchProtectionOption{}), repo.CreateBranchProtection)
m.Post("", bind(api.CreateBranchProtectionOption{}), mustNotBeArchived, repo.CreateBranchProtection)
m.Group("/{name}", func() {
m.Get("", repo.GetBranchProtection)
m.Patch("", bind(api.EditBranchProtectionOption{}), repo.EditBranchProtection)
m.Patch("", bind(api.EditBranchProtectionOption{}), mustNotBeArchived, repo.EditBranchProtection)
m.Delete("", repo.DeleteBranchProtection)
})
}, reqToken(), reqAdmin())
m.Group("/tags", func() {
m.Get("", repo.ListTags)
m.Get("/*", repo.GetTag)
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateTagOption{}), repo.CreateTag)
m.Delete("/*", reqToken(), repo.DeleteTag)
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateTagOption{}), repo.CreateTag)
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteTag)
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true))
m.Group("/keys", func() {
m.Combo("").Get(repo.ListDeployKeys).
Expand Down Expand Up @@ -1245,15 +1245,15 @@ func Routes() *web.Route {
m.Get("/tags/{sha}", repo.GetAnnotatedTag)
m.Get("/notes/{sha}", repo.GetNote)
}, context.ReferencesGitRepo(true), reqRepoReader(unit.TypeCode))
m.Post("/diffpatch", reqRepoWriter(unit.TypeCode), reqToken(), bind(api.ApplyDiffPatchFileOptions{}), repo.ApplyDiffPatch)
m.Post("/diffpatch", reqRepoWriter(unit.TypeCode), reqToken(), bind(api.ApplyDiffPatchFileOptions{}), mustNotBeArchived, repo.ApplyDiffPatch)
m.Group("/contents", func() {
m.Get("", repo.GetContentsList)
m.Post("", reqToken(), bind(api.ChangeFilesOptions{}), reqRepoBranchWriter, repo.ChangeFiles)
m.Post("", reqToken(), bind(api.ChangeFilesOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.ChangeFiles)
m.Get("/*", repo.GetContents)
m.Group("/*", func() {
m.Post("", bind(api.CreateFileOptions{}), reqRepoBranchWriter, repo.CreateFile)
m.Put("", bind(api.UpdateFileOptions{}), reqRepoBranchWriter, repo.UpdateFile)
m.Delete("", bind(api.DeleteFileOptions{}), reqRepoBranchWriter, repo.DeleteFile)
m.Post("", bind(api.CreateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.CreateFile)
m.Put("", bind(api.UpdateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.UpdateFile)
m.Delete("", bind(api.DeleteFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.DeleteFile)
}, reqToken())
}, reqRepoReader(unit.TypeCode))
m.Get("/signing-key.gpg", misc.SigningKey)
Expand Down
25 changes: 9 additions & 16 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,13 @@ func DeleteBranch(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"

// "423":
// "$ref": "#/responses/repoArchived"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a description, here and everywhere else:

Suggested change
// "$ref": "#/responses/repoArchived"
// "$ref": "#/responses/repoArchived"
// "description": "The repo is archived"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description is added by swagger automatically

if ctx.Repo.Repository.IsEmpty {
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
return
}

if ctx.Repo.Repository.IsArchived {
ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
return
}

if ctx.Repo.Repository.IsMirror {
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
return
Expand Down Expand Up @@ -157,10 +153,6 @@ func DeleteBranch(ctx *context.APIContext) {
}
}

if ctx.Repo.Repository.IsArchived {
ctx.Error(http.StatusForbidden, "IsArchived", fmt.Errorf("can not delete branch of an archived repository"))
return
}
if ctx.Repo.Repository.IsMirror {
ctx.Error(http.StatusForbidden, "IsMirrored", fmt.Errorf("can not delete branch of an mirror repository"))
return
Expand Down Expand Up @@ -216,17 +208,14 @@ func CreateBranch(ctx *context.APIContext) {
// description: The old branch does not exist.
// "409":
// description: The branch with the same name already exists.
// "423":
// "$ref": "#/responses/repoArchived"

if ctx.Repo.Repository.IsEmpty {
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
return
}

if ctx.Repo.Repository.IsArchived {
ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
return
}

if ctx.Repo.Repository.IsMirror {
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
return
Expand Down Expand Up @@ -519,6 +508,8 @@ func CreateBranchProtection(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchived"

form := web.GetForm(ctx).(*api.CreateBranchProtectionOption)
repo := ctx.Repo.Repository
Expand Down Expand Up @@ -727,6 +718,8 @@ func EditBranchProtection(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchived"
form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
repo := ctx.Repo.Repository
bpName := ctx.Params(":name")
Expand Down Expand Up @@ -1004,7 +997,7 @@ func DeleteBranchProtection(ctx *context.APIContext) {
return
}

if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository.ID, bp.ID); err != nil {
if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository, bp.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err)
return
}
Expand Down
8 changes: 8 additions & 0 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ func ChangeFiles(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

apiOpts := web.GetForm(ctx).(*api.ChangeFilesOptions)

Expand Down Expand Up @@ -550,6 +552,8 @@ func CreateFile(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

apiOpts := web.GetForm(ctx).(*api.CreateFileOptions)

Expand Down Expand Up @@ -646,6 +650,8 @@ func UpdateFile(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"
apiOpts := web.GetForm(ctx).(*api.UpdateFileOptions)
if ctx.Repo.Repository.IsEmpty {
ctx.Error(http.StatusUnprocessableEntity, "RepoIsEmpty", fmt.Errorf("repo is empty"))
Expand Down Expand Up @@ -806,6 +812,8 @@ func DeleteFile(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

apiOpts := web.GetForm(ctx).(*api.DeleteFileOptions)
if !canWriteFiles(ctx, apiOpts.BranchName) {
Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ func CreateIssue(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchived"
form := web.GetForm(ctx).(*api.CreateIssueOption)
var deadlineUnix timeutil.TimeStamp
if form.Deadline != nil && ctx.Repo.CanWrite(unit.TypeIssues) {
Expand Down
6 changes: 6 additions & 0 deletions routers/api/v1/repo/issue_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func CreateIssueAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

issue := getIssueFromContext(ctx)
if issue == nil {
Expand Down Expand Up @@ -238,6 +240,8 @@ func EditIssueAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/Attachment"
// "404":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

attachment := getIssueAttachmentSafeWrite(ctx)
if attachment == nil {
Expand Down Expand Up @@ -292,6 +296,8 @@ func DeleteIssueAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

attachment := getIssueAttachmentSafeWrite(ctx)
if attachment == nil {
Expand Down
5 changes: 4 additions & 1 deletion routers/api/v1/repo/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ func CreateIssueComment(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "423":
// "$ref": "#/responses/repoArchived"
form := web.GetForm(ctx).(*api.CreateIssueCommentOption)
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
Expand Down Expand Up @@ -486,7 +488,8 @@ func EditIssueComment(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"

// "423":
// "$ref": "#/responses/repoArchived"
form := web.GetForm(ctx).(*api.EditIssueCommentOption)
editIssueComment(ctx, *form)
}
Expand Down
8 changes: 6 additions & 2 deletions routers/api/v1/repo/issue_comment_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

// Check if comment exists and load comment
comment := getIssueCommentSafe(ctx)
Expand Down Expand Up @@ -245,7 +247,8 @@ func EditIssueCommentAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/Attachment"
// "404":
// "$ref": "#/responses/error"

// "423":
// "$ref": "#/responses/repoArchived"
attach := getIssueCommentAttachmentSafeWrite(ctx)
if attach == nil {
return
Expand Down Expand Up @@ -297,7 +300,8 @@ func DeleteIssueCommentAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/error"

// "423":
// "$ref": "#/responses/repoArchived"
attach := getIssueCommentAttachmentSafeWrite(ctx)
if attach == nil {
return
Expand Down
4 changes: 4 additions & 0 deletions routers/api/v1/repo/issue_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func CreateIssueDependency(ctx *context.APIContext) {
// "$ref": "#/responses/Issue"
// "404":
// description: the issue does not exist
// "423":
// "$ref": "#/responses/repoArchived"

// We want to make <:index> depend on <Form>, i.e. <:index> is the target
target := getParamsIssue(ctx)
Expand Down Expand Up @@ -246,6 +248,8 @@ func RemoveIssueDependency(ctx *context.APIContext) {
// "$ref": "#/responses/Issue"
// "404":
// "$ref": "#/responses/notFound"
// "423":
// "$ref": "#/responses/repoArchived"

// We want to make <:index> depend on <Form>, i.e. <:index> is the target
target := getParamsIssue(ctx)
Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func ApplyDiffPatch(ctx *context.APIContext) {
// "$ref": "#/responses/FileResponse"
// "404":
// "$ref": "#/responses/notFound"
// "423":
// "$ref": "#/responses/repoArchived"
apiOpts := web.GetForm(ctx).(*api.ApplyDiffPatchFileOptions)

opts := &files.ApplyDiffPatchOptions{
Expand Down
6 changes: 6 additions & 0 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ func CreatePullRequest(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchived"

form := *web.GetForm(ctx).(*api.CreatePullRequestOption)
if form.Head == form.Base {
Expand Down Expand Up @@ -741,6 +743,8 @@ func MergePullRequest(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "409":
// "$ref": "#/responses/error"
// "423":
// "$ref": "#/responses/repoArchived"

form := web.GetForm(ctx).(*forms.MergePullRequestForm)

Expand Down Expand Up @@ -1196,6 +1200,8 @@ func CancelScheduledAutoMerge(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "423":
// "$ref": "#/responses/repoArchived"

pullIndex := ctx.ParamsInt64(":index")
pull, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, pullIndex)
Expand Down
Loading