mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-19 23:50:51 +00:00
Swagger info corrections (#9441)
* use numbers and not http.Status___ enum
* fix test
* add many missing swagger responses
* code format
* Deletion Sould return 204 ...
* error handling improvements
* if special error type ... then add it to swagger too
* one smal nit
* invalidTopicsError is []string
* valid swagger specification 2.0
- if you add responses swagger can tell you if you do it right 👍
* use ctx.InternalServerError
* Revert "use numbers and not http.Status___ enum"
This reverts commit b1ff386e2418ed6a7f183e756b13277d701278ef.
* use http.Status* enum everywhere
This commit is contained in:
parent
050a8af424
commit
2848c5eb8f
52 changed files with 1262 additions and 648 deletions
|
@ -70,6 +70,7 @@ func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions)
|
|||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/PullRequestList"
|
||||
|
||||
prs, maxResults, err := models.PullRequests(ctx.Repo.Repository.ID, &models.PullRequestsOptions{
|
||||
Page: ctx.QueryInt("page"),
|
||||
State: ctx.QueryTrim("state"),
|
||||
|
@ -79,33 +80,33 @@ func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions)
|
|||
})
|
||||
|
||||
if err != nil {
|
||||
ctx.Error(500, "PullRequests", err)
|
||||
ctx.Error(http.StatusInternalServerError, "PullRequests", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiPrs := make([]*api.PullRequest, len(prs))
|
||||
for i := range prs {
|
||||
if err = prs[i].LoadIssue(); err != nil {
|
||||
ctx.Error(500, "LoadIssue", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
|
||||
return
|
||||
}
|
||||
if err = prs[i].LoadAttributes(); err != nil {
|
||||
ctx.Error(500, "LoadAttributes", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
|
||||
return
|
||||
}
|
||||
if err = prs[i].GetBaseRepo(); err != nil {
|
||||
ctx.Error(500, "GetBaseRepo", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetBaseRepo", err)
|
||||
return
|
||||
}
|
||||
if err = prs[i].GetHeadRepo(); err != nil {
|
||||
ctx.Error(500, "GetHeadRepo", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetHeadRepo", err)
|
||||
return
|
||||
}
|
||||
apiPrs[i] = prs[i].APIFormat()
|
||||
}
|
||||
|
||||
ctx.SetLinkHeader(int(maxResults), models.ItemsPerPage)
|
||||
ctx.JSON(200, &apiPrs)
|
||||
ctx.JSON(http.StatusOK, &apiPrs)
|
||||
}
|
||||
|
||||
// GetPullRequest returns a single PR based on index
|
||||
|
@ -135,25 +136,26 @@ func GetPullRequest(ctx *context.APIContext) {
|
|||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/PullRequest"
|
||||
|
||||
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
||||
if err != nil {
|
||||
if models.IsErrPullRequestNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(500, "GetPullRequestByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = pr.GetBaseRepo(); err != nil {
|
||||
ctx.Error(500, "GetBaseRepo", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetBaseRepo", err)
|
||||
return
|
||||
}
|
||||
if err = pr.GetHeadRepo(); err != nil {
|
||||
ctx.Error(500, "GetHeadRepo", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetHeadRepo", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, pr.APIFormat())
|
||||
ctx.JSON(http.StatusOK, pr.APIFormat())
|
||||
}
|
||||
|
||||
// CreatePullRequest does what it says
|
||||
|
@ -183,6 +185,11 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
|
|||
// responses:
|
||||
// "201":
|
||||
// "$ref": "#/responses/PullRequest"
|
||||
// "409":
|
||||
// "$ref": "#/responses/error"
|
||||
// "422":
|
||||
// "$ref": "#/responses/validationError"
|
||||
|
||||
var (
|
||||
repo = ctx.Repo.Repository
|
||||
labelIDs []int64
|
||||
|
@ -201,7 +208,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
|
|||
existingPr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
|
||||
if err != nil {
|
||||
if !models.IsErrPullRequestNotExist(err) {
|
||||
ctx.Error(500, "GetUnmergedPullRequest", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetUnmergedPullRequest", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
|
@ -213,14 +220,14 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
|
|||
HeadBranch: existingPr.HeadBranch,
|
||||
BaseBranch: existingPr.BaseBranch,
|
||||
}
|
||||
ctx.Error(409, "GetUnmergedPullRequest", err)
|
||||
ctx.Error(http.StatusConflict, "GetUnmergedPullRequest", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(form.Labels) > 0 {
|
||||
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetLabelsInRepoByIDs", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -236,7 +243,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
|
|||
if models.IsErrMilestoneNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(500, "GetMilestoneByRepoID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetMilestoneByRepoID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -275,9 +282,9 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
|
|||
assigneeIDs, err := models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
|
||||
if err != nil {
|
||||
if models.IsErrUserNotExist(err) {
|
||||
ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
||||
} else {
|
||||
ctx.Error(500, "AddAssigneeByName", err)
|
||||
ctx.Error(http.StatusInternalServerError, "AddAssigneeByName", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -285,34 +292,34 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
|
|||
for _, aID := range assigneeIDs {
|
||||
assignee, err := models.GetUserByID(aID)
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetUserByID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
|
||||
return
|
||||
}
|
||||
|
||||
valid, err := models.CanBeAssigned(assignee, repo, true)
|
||||
if err != nil {
|
||||
ctx.Error(500, "canBeAssigned", err)
|
||||
ctx.Error(http.StatusInternalServerError, "canBeAssigned", err)
|
||||
return
|
||||
}
|
||||
if !valid {
|
||||
ctx.Error(422, "canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: repo.Name})
|
||||
ctx.Error(http.StatusUnprocessableEntity, "canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: repo.Name})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, assigneeIDs); err != nil {
|
||||
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
|
||||
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
|
||||
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err)
|
||||
return
|
||||
}
|
||||
ctx.Error(500, "NewPullRequest", err)
|
||||
ctx.Error(http.StatusInternalServerError, "NewPullRequest", err)
|
||||
return
|
||||
}
|
||||
|
||||
notification.NotifyNewPullRequest(pr)
|
||||
|
||||
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
|
||||
ctx.JSON(201, pr.APIFormat())
|
||||
ctx.JSON(http.StatusCreated, pr.APIFormat())
|
||||
}
|
||||
|
||||
// EditPullRequest does what it says
|
||||
|
@ -348,12 +355,19 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
// responses:
|
||||
// "201":
|
||||
// "$ref": "#/responses/PullRequest"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
// "412":
|
||||
// "$ref": "#/responses/error"
|
||||
// "422":
|
||||
// "$ref": "#/responses/validationError"
|
||||
|
||||
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
||||
if err != nil {
|
||||
if models.IsErrPullRequestNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(500, "GetPullRequestByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -367,7 +381,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
issue.Repo = ctx.Repo.Repository
|
||||
|
||||
if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypePullRequests) {
|
||||
ctx.Status(403)
|
||||
ctx.Status(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -388,7 +402,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
}
|
||||
|
||||
if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
|
||||
ctx.Error(500, "UpdateIssueDeadline", err)
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateIssueDeadline", err)
|
||||
return
|
||||
}
|
||||
issue.DeadlineUnix = deadlineUnix
|
||||
|
@ -406,9 +420,9 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
err = issue_service.UpdateAssignees(issue, form.Assignee, form.Assignees, ctx.User)
|
||||
if err != nil {
|
||||
if models.IsErrUserNotExist(err) {
|
||||
ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
||||
} else {
|
||||
ctx.Error(500, "UpdateAssignees", err)
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateAssignees", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -419,7 +433,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
oldMilestoneID := issue.MilestoneID
|
||||
issue.MilestoneID = form.Milestone
|
||||
if err = issue_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
|
||||
ctx.Error(500, "ChangeMilestoneAssign", err)
|
||||
ctx.Error(http.StatusInternalServerError, "ChangeMilestoneAssign", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -427,17 +441,17 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Labels != nil {
|
||||
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetLabelsInRepoByIDsError", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDsError", err)
|
||||
return
|
||||
}
|
||||
if err = issue.ReplaceLabels(labels, ctx.User); err != nil {
|
||||
ctx.Error(500, "ReplaceLabelsError", err)
|
||||
ctx.Error(http.StatusInternalServerError, "ReplaceLabelsError", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = models.UpdateIssue(issue); err != nil {
|
||||
ctx.Error(500, "UpdateIssue", err)
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateIssue", err)
|
||||
return
|
||||
}
|
||||
if form.State != nil {
|
||||
|
@ -446,7 +460,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
|
||||
return
|
||||
}
|
||||
ctx.Error(500, "ChangeStatus", err)
|
||||
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -457,13 +471,13 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
|||
if models.IsErrPullRequestNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(500, "GetPullRequestByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// TODO this should be 200, not 201
|
||||
ctx.JSON(201, pr.APIFormat())
|
||||
ctx.JSON(http.StatusCreated, pr.APIFormat())
|
||||
}
|
||||
|
||||
// IsPullRequestMerged checks if a PR exists given an index
|
||||
|
@ -495,18 +509,19 @@ func IsPullRequestMerged(ctx *context.APIContext) {
|
|||
// description: pull request has been merged
|
||||
// "404":
|
||||
// description: pull request has not been merged
|
||||
|
||||
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
||||
if err != nil {
|
||||
if models.IsErrPullRequestNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(500, "GetPullRequestByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if pr.HasMerged {
|
||||
ctx.Status(204)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
ctx.NotFound()
|
||||
}
|
||||
|
@ -544,12 +559,15 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
|
|||
// "$ref": "#/responses/empty"
|
||||
// "405":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "409":
|
||||
// "$ref": "#/responses/error"
|
||||
|
||||
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
||||
if err != nil {
|
||||
if models.IsErrPullRequestNotExist(err) {
|
||||
ctx.NotFound("GetPullRequestByIndex", err)
|
||||
} else {
|
||||
ctx.Error(500, "GetPullRequestByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -569,7 +587,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
|
|||
if ctx.IsSigned {
|
||||
// Update issue-user.
|
||||
if err = pr.Issue.ReadBy(ctx.User.ID); err != nil {
|
||||
ctx.Error(500, "ReadBy", err)
|
||||
ctx.Error(http.StatusInternalServerError, "ReadBy", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -580,18 +598,18 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
|
|||
}
|
||||
|
||||
if !pr.CanAutoMerge() || pr.HasMerged || pr.IsWorkInProgress() {
|
||||
ctx.Status(405)
|
||||
ctx.Status(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
isPass, err := pull_service.IsPullCommitStatusPass(pr)
|
||||
if err != nil {
|
||||
ctx.Error(500, "IsPullCommitStatusPass", err)
|
||||
ctx.Error(http.StatusInternalServerError, "IsPullCommitStatusPass", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !isPass && !ctx.IsUserRepoAdmin() {
|
||||
ctx.Status(405)
|
||||
ctx.Status(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -616,7 +634,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
|
|||
|
||||
if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
|
||||
if models.IsErrInvalidMergeStyle(err) {
|
||||
ctx.Status(405)
|
||||
ctx.Status(http.StatusMethodNotAllowed)
|
||||
return
|
||||
} else if models.IsErrMergeConflicts(err) {
|
||||
conflictError := err.(models.ErrMergeConflicts)
|
||||
|
@ -628,15 +646,15 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
|
|||
conflictError := err.(models.ErrMergeUnrelatedHistories)
|
||||
ctx.JSON(http.StatusConflict, conflictError)
|
||||
} else if models.IsErrMergePushOutOfDate(err) {
|
||||
ctx.Status(http.StatusConflict)
|
||||
ctx.Error(http.StatusConflict, "Merge", "merge push out of date")
|
||||
return
|
||||
}
|
||||
ctx.Error(500, "Merge", err)
|
||||
ctx.Error(http.StatusInternalServerError, "Merge", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Trace("Pull request merged: %d", pr.ID)
|
||||
ctx.Status(200)
|
||||
ctx.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) {
|
||||
|
@ -706,7 +724,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
|
|||
} else {
|
||||
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
|
||||
if err != nil {
|
||||
ctx.Error(500, "OpenRepository", err)
|
||||
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
||||
return nil, nil, nil, nil, "", ""
|
||||
}
|
||||
}
|
||||
|
@ -759,7 +777,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
|
|||
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
|
||||
if err != nil {
|
||||
headGitRepo.Close()
|
||||
ctx.Error(500, "GetCompareInfo", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
|
||||
return nil, nil, nil, nil, "", ""
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue