1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-23 02:38:35 +00:00

#1692 add CRUD issue APIs

- Fix go-gogs-client#10
- Related to #809
This commit is contained in:
Unknwon
2016-03-13 23:20:22 -04:00
parent dd6faf7f9b
commit 9bd9ad4205
23 changed files with 406 additions and 140 deletions

View File

@@ -25,7 +25,7 @@ func GetBranch(ctx *context.APIContext) {
return
}
ctx.JSON(200, convert.ToApiBranch(branch, c))
ctx.JSON(200, convert.ToBranch(branch, c))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-branches
@@ -43,7 +43,7 @@ func ListBranches(ctx *context.APIContext) {
ctx.Error(500, "GetCommit", err)
return
}
apiBranches[i] = convert.ToApiBranch(branches[i], c)
apiBranches[i] = convert.ToBranch(branches[i], c)
}
ctx.JSON(200, &apiBranches)

View File

@@ -26,9 +26,8 @@ func ListHooks(ctx *context.APIContext) {
apiHooks := make([]*api.Hook, len(hooks))
for i := range hooks {
apiHooks[i] = convert.ToApiHook(ctx.Repo.RepoLink, hooks[i])
apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
}
ctx.JSON(200, &apiHooks)
}
@@ -94,7 +93,7 @@ func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
return
}
ctx.JSON(201, convert.ToApiHook(ctx.Repo.RepoLink, w))
ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
@@ -161,5 +160,5 @@ func EditHook(ctx *context.APIContext, form api.EditHookOption) {
return
}
ctx.JSON(200, convert.ToApiHook(ctx.Repo.RepoLink, w))
ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w))
}

View File

@@ -0,0 +1,165 @@
// Copyright 2016 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
"fmt"
"strings"
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers/api/v1/convert"
"github.com/gogits/gogs/routers/repo"
)
func ListIssues(ctx *context.APIContext) {
issues, err := models.Issues(&models.IssuesOptions{
RepoID: ctx.Repo.Repository.ID,
Page: ctx.QueryInt("page"),
})
if err != nil {
ctx.Error(500, "Issues", err)
return
}
apiIssues := make([]*api.Issue, len(issues))
for i := range issues {
apiIssues[i] = convert.ToIssue(issues[i])
}
ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.IssuePagingNum)
ctx.JSON(200, &apiIssues)
}
func GetIssue(ctx *context.APIContext) {
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.Status(404)
} else {
ctx.Error(500, "GetIssueByIndex", err)
}
return
}
ctx.JSON(200, convert.ToIssue(issue))
}
func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
issue := &models.Issue{
RepoID: ctx.Repo.Repository.ID,
Name: form.Title,
PosterID: ctx.User.Id,
Poster: ctx.User,
Content: form.Body,
}
if ctx.Repo.IsWriter() {
if len(form.Assignee) > 0 {
assignee, err := models.GetUserByName(form.Assignee)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee))
} else {
ctx.Error(500, "GetUserByName", err)
}
return
}
issue.AssigneeID = assignee.Id
}
issue.MilestoneID = form.Milestone
} else {
form.Labels = nil
}
if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, nil); err != nil {
ctx.Error(500, "NewIssue", err)
return
} else if err := repo.MailWatchersAndMentions(ctx.Context, issue); err != nil {
ctx.Error(500, "MailWatchersAndMentions", err)
return
}
// Refetch from database to assign some automatic values
var err error
issue, err = models.GetIssueByID(issue.ID)
if err != nil {
ctx.Error(500, "GetIssueByID", err)
return
}
ctx.JSON(201, convert.ToIssue(issue))
}
func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.Status(404)
} else {
ctx.Error(500, "GetIssueByIndex", err)
}
return
}
if !issue.IsPoster(ctx.User.Id) && !ctx.Repo.IsWriter() {
ctx.Status(403)
return
}
if len(form.Title) > 0 {
issue.Name = form.Title
}
if form.Body != nil {
issue.Content = *form.Body
}
if ctx.Repo.IsWriter() && form.Assignee != nil &&
(issue.Assignee == nil || issue.Assignee.LowerName != strings.ToLower(*form.Assignee)) {
if len(*form.Assignee) == 0 {
issue.AssigneeID = 0
} else {
assignee, err := models.GetUserByName(*form.Assignee)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", *form.Assignee))
} else {
ctx.Error(500, "GetUserByName", err)
}
return
}
issue.AssigneeID = assignee.Id
}
if err = models.UpdateIssueUserByAssignee(issue); err != nil {
ctx.Error(500, "UpdateIssueUserByAssignee", err)
return
}
}
if ctx.Repo.IsWriter() && form.Milestone != nil &&
issue.MilestoneID != *form.Milestone {
oldMid := issue.MilestoneID
issue.MilestoneID = *form.Milestone
if err = models.ChangeMilestoneAssign(oldMid, issue); err != nil {
ctx.Error(500, "ChangeMilestoneAssign", err)
return
}
}
if err = models.UpdateIssue(issue); err != nil {
ctx.Error(500, "UpdateIssue", err)
return
}
// Refetch from database to assign some automatic values
issue, err = models.GetIssueByID(issue.ID)
if err != nil {
ctx.Error(500, "GetIssueByID", err)
return
}
ctx.JSON(201, convert.ToIssue(issue))
}

View File

@@ -34,7 +34,7 @@ func ListDeployKeys(ctx *context.APIContext) {
ctx.Error(500, "GetContent", err)
return
}
apiKeys[i] = convert.ToApiDeployKey(apiLink, keys[i])
apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
}
ctx.JSON(200, &apiKeys)
@@ -58,7 +58,7 @@ func GetDeployKey(ctx *context.APIContext) {
}
apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
ctx.JSON(200, convert.ToApiDeployKey(apiLink, key))
ctx.JSON(200, convert.ToDeployKey(apiLink, key))
}
func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
@@ -96,7 +96,7 @@ func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) {
key.Content = content
apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
ctx.JSON(201, convert.ToApiDeployKey(apiLink, key))
ctx.JSON(201, convert.ToDeployKey(apiLink, key))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key

View File

@@ -69,7 +69,7 @@ func Search(ctx *context.APIContext) {
return
}
results[i] = &api.Repository{
Id: repos[i].ID,
ID: repos[i].ID,
FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
}
}
@@ -97,12 +97,12 @@ func ListMyRepos(ctx *context.APIContext) {
repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
for i := range ownRepos {
repos[i] = convert.ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
repos[i] = convert.ToRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
}
i := numOwnRepos
for repo, access := range accessibleRepos {
repos[i] = convert.ToApiRepository(repo.Owner, repo, api.Permission{
repos[i] = convert.ToRepository(repo.Owner, repo, api.Permission{
Admin: access >= models.ACCESS_MODE_ADMIN,
Push: access >= models.ACCESS_MODE_WRITE,
Pull: true,
@@ -139,7 +139,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
return
}
ctx.JSON(201, convert.ToApiRepository(owner, repo, api.Permission{true, true, true}))
ctx.JSON(201, convert.ToRepository(owner, repo, api.Permission{true, true, true}))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create
@@ -239,7 +239,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
}
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
ctx.JSON(201, convert.ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
ctx.JSON(201, convert.ToRepository(ctxUser, repo, api.Permission{true, true, true}))
}
func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
@@ -273,7 +273,7 @@ func Get(ctx *context.APIContext) {
return
}
ctx.JSON(200, convert.ToApiRepository(owner, repo, api.Permission{true, true, true}))
ctx.JSON(200, convert.ToRepository(owner, repo, api.Permission{true, true, true}))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#delete