#3290 better code structure and batch minor improvements

release/v1.15
Unknwon 2016-08-03 11:51:22 -07:00
parent 2eeb0ec9b0
commit e7fd65f0cf
10 changed files with 196 additions and 228 deletions

View File

@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
##### Current tip version: 0.9.59 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) ##### Current tip version: 0.9.60 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
| Web | UI | Preview | | Web | UI | Preview |
|:-------------:|:-------:|:-------:| |:-------------:|:-------:|:-------:|

View File

@ -88,7 +88,7 @@ func checkVersion() {
{"gopkg.in/ini.v1", ini.Version, "1.8.4"}, {"gopkg.in/ini.v1", ini.Version, "1.8.4"},
{"gopkg.in/macaron.v1", macaron.Version, "1.1.4"}, {"gopkg.in/macaron.v1", macaron.Version, "1.1.4"},
{"github.com/gogits/git-module", git.Version, "0.3.3"}, {"github.com/gogits/git-module", git.Version, "0.3.3"},
{"github.com/gogits/go-gogs-client", gogs.Version, "0.7.4"}, {"github.com/gogits/go-gogs-client", gogs.Version, "0.10.1"},
} }
for _, c := range checkers { for _, c := range checkers {
if !version.Compare(c.Version(), c.Expected, ">=") { if !version.Compare(c.Version(), c.Expected, ">=") {

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.9.59.0730" const APP_VER = "0.9.60.0803"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -5,7 +5,6 @@
package models package models
import ( import (
"bytes"
"fmt" "fmt"
) )
@ -35,30 +34,6 @@ func (err ErrNamePatternNotAllowed) Error() string {
return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern) return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern)
} }
type ErrMultipleErrors struct {
Errors []error
}
func IsErrMultipleErrors(err error) bool {
_, ok := err.(ErrMultipleErrors)
return ok
}
func (err ErrMultipleErrors) Error() string {
var message bytes.Buffer
message.WriteString("Multiple errors encountered: ")
for i := range err.Errors {
message.WriteString(err.Errors[i].Error())
if i < len(err.Errors)-1 {
message.WriteString("; ")
}
}
return message.String()
}
// ____ ___ // ____ ___
// | | \______ ___________ // | | \______ ___________
// | | / ___// __ \_ __ \ // | | / ___// __ \_ __ \
@ -558,7 +533,8 @@ func (err ErrCommentNotExist) Error() string {
// \/ \/ \/ \/ // \/ \/ \/ \/
type ErrLabelNotExist struct { type ErrLabelNotExist struct {
ID int64 LabelID int64
RepoID int64
} }
func IsErrLabelNotExist(err error) bool { func IsErrLabelNotExist(err error) bool {
@ -567,21 +543,7 @@ func IsErrLabelNotExist(err error) bool {
} }
func (err ErrLabelNotExist) Error() string { func (err ErrLabelNotExist) Error() string {
return fmt.Sprintf("label does not exist [id: %d]", err.ID) return fmt.Sprintf("label does not exist [label_id: %d, repo_id: %d]", err.LabelID, err.RepoID)
}
type ErrLabelNotValidForRepository struct {
ID int64
RepoID int64
}
func IsErrLabelNotValidForRepository(err error) bool {
_, ok := err.(ErrLabelNotValidForRepository)
return ok
}
func (err ErrLabelNotValidForRepository) Error() string {
return fmt.Sprintf("label is not valid for repository [label_id: %d, repo_id: %d]", err.ID, err.RepoID)
} }
// _____ .__.__ __ // _____ .__.__ __

View File

@ -181,7 +181,7 @@ func (i *Issue) addLabel(e *xorm.Session, label *Label) error {
return newIssueLabel(e, i, label) return newIssueLabel(e, i, label)
} }
// AddLabel adds new label to issue by given ID. // AddLabel adds a new label to the issue.
func (i *Issue) AddLabel(label *Label) (err error) { func (i *Issue) AddLabel(label *Label) (err error) {
sess := x.NewSession() sess := x.NewSession()
defer sessionRelease(sess) defer sessionRelease(sess)
@ -196,52 +196,76 @@ func (i *Issue) AddLabel(label *Label) (err error) {
return sess.Commit() return sess.Commit()
} }
func (i *Issue) getLabels(e Engine) (err error) { func (issue *Issue) addLabels(e *xorm.Session, labels []*Label) error {
if len(i.Labels) > 0 { return newIssueLabels(e, issue, labels)
}
// AddLabels adds a list of new labels to the issue.
func (issue *Issue) AddLabels(labels []*Label) error {
return NewIssueLabels(issue, labels)
}
func (issue *Issue) getLabels(e Engine) (err error) {
if len(issue.Labels) > 0 {
return nil return nil
} }
i.Labels, err = getLabelsByIssueID(e, i.ID) issue.Labels, err = getLabelsByIssueID(e, issue.ID)
if err != nil { if err != nil {
return fmt.Errorf("getLabelsByIssueID: %v", err) return fmt.Errorf("getLabelsByIssueID: %v", err)
} }
return nil return nil
} }
func (i *Issue) removeLabel(e *xorm.Session, label *Label) error { func (issue *Issue) removeLabel(e *xorm.Session, label *Label) error {
return deleteIssueLabel(e, i, label) return deleteIssueLabel(e, issue, label)
} }
// RemoveLabel removes a label from issue by given ID. // RemoveLabel removes a label from issue by given ID.
func (i *Issue) RemoveLabel(label *Label) (err error) { func (issue *Issue) RemoveLabel(label *Label) (err error) {
return DeleteIssueLabel(issue, label)
}
func (issue *Issue) clearLabels(e *xorm.Session) (err error) {
if err = issue.getLabels(e); err != nil {
return fmt.Errorf("getLabels: %v", err)
}
for i := range issue.Labels {
if err = issue.removeLabel(e, issue.Labels[i]); err != nil {
return fmt.Errorf("removeLabel: %v", err)
}
}
return nil
}
func (issue *Issue) ClearLabels() (err error) {
sess := x.NewSession() sess := x.NewSession()
defer sessionRelease(sess) defer sessionRelease(sess)
if err = sess.Begin(); err != nil { if err = sess.Begin(); err != nil {
return err return err
} }
if err = i.removeLabel(sess, label); err != nil { if err = issue.clearLabels(sess); err != nil {
return err return err
} }
return sess.Commit() return sess.Commit()
} }
func (i *Issue) ClearLabels() (err error) { // ReplaceLabels removes all current labels and add new labels to the issue.
func (issue *Issue) ReplaceLabels(labels []*Label) (err error) {
sess := x.NewSession() sess := x.NewSession()
defer sessionRelease(sess) defer sessionRelease(sess)
if err = sess.Begin(); err != nil { if err = sess.Begin(); err != nil {
return err return err
} }
if err = i.getLabels(sess); err != nil { if err = issue.clearLabels(sess); err != nil {
return err return fmt.Errorf("clearLabels: %v", err)
} } else if err = issue.addLabels(sess, labels); err != nil {
return fmt.Errorf("addLabels: %v", err)
for idx := range i.Labels {
if err = i.removeLabel(sess, i.Labels[idx]); err != nil {
return err
}
} }
return sess.Commit() return sess.Commit()

View File

@ -11,6 +11,8 @@ import (
"strings" "strings"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/base"
) )
// Label represents a label of repository for issues. // Label represents a label of repository for issues.
@ -56,48 +58,65 @@ func NewLabel(l *Label) error {
return err return err
} }
func getLabelByID(e Engine, id int64) (*Label, error) { // getLabelInRepoByID returns a label by ID in given repository.
if id <= 0 { // If pass repoID as 0, then ORM will ignore limitation of repository
return nil, ErrLabelNotExist{id} // and can return arbitrary label with any valid ID.
func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
if labelID <= 0 {
return nil, ErrLabelNotExist{labelID, repoID}
} }
l := &Label{ID: id} l := &Label{
ID: labelID,
RepoID: repoID,
}
has, err := x.Get(l) has, err := x.Get(l)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrLabelNotExist{l.ID} return nil, ErrLabelNotExist{l.ID, l.RepoID}
} }
return l, nil return l, nil
} }
// GetLabelByID returns a label by given ID. // GetLabelByID returns a label by given ID.
func GetLabelByID(id int64) (*Label, error) { func GetLabelByID(id int64) (*Label, error) {
return getLabelByID(x, id) return getLabelInRepoByID(x, 0, id)
}
// GetLabelInRepoByID returns a label by ID in given repository.
func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
return getLabelInRepoByID(x, repoID, labelID)
}
// GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
// it silently ignores label IDs that are not belong to the repository.
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
return labels, x.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Find(&labels)
} }
// GetLabelsByRepoID returns all labels that belong to given repository by ID. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
func GetLabelsByRepoID(repoID int64) ([]*Label, error) { func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
labels := make([]*Label, 0, 10) labels := make([]*Label, 0, 10)
return labels, x.Where("repo_id=?", repoID).Find(&labels) return labels, x.Where("repo_id = ?", repoID).Find(&labels)
} }
func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) { func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
issueLabels, err := getIssueLabels(e, issueID) issueLabels, err := getIssueLabels(e, issueID)
if err != nil { if err != nil {
return nil, fmt.Errorf("getIssueLabels: %v", err) return nil, fmt.Errorf("getIssueLabels: %v", err)
} else if len(issueLabels) == 0 {
return []*Label{}, nil
} }
var label *Label labelIDs := make([]int64, len(issueLabels))
labels := make([]*Label, 0, len(issueLabels)) for i := range issueLabels {
for idx := range issueLabels { labelIDs[i] = issueLabels[i].LabelID
label, err = getLabelByID(e, issueLabels[idx].LabelID)
if err != nil && !IsErrLabelNotExist(err) {
return nil, fmt.Errorf("getLabelByID: %v", err)
}
labels = append(labels, label)
} }
return labels, nil
labels := make([]*Label, 0, len(labelIDs))
return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Find(&labels)
} }
// GetLabelsByIssueID returns all labels that belong to given issue by ID. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
@ -117,7 +136,7 @@ func UpdateLabel(l *Label) error {
// DeleteLabel delete a label of given repository. // DeleteLabel delete a label of given repository.
func DeleteLabel(repoID, labelID int64) error { func DeleteLabel(repoID, labelID int64) error {
l, err := GetLabelByID(labelID) _, err := GetLabelInRepoByID(repoID, labelID)
if err != nil { if err != nil {
if IsErrLabelNotExist(err) { if IsErrLabelNotExist(err) {
return nil return nil
@ -131,11 +150,12 @@ func DeleteLabel(repoID, labelID int64) error {
return err return err
} }
if _, err = x.Where("label_id=?", labelID).Delete(new(IssueLabel)); err != nil { if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
return err return err
} else if _, err = sess.Delete(l); err != nil { } else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil {
return err return err
} }
return sess.Commit() return sess.Commit()
} }
@ -154,7 +174,7 @@ type IssueLabel struct {
} }
func hasIssueLabel(e Engine, issueID, labelID int64) bool { func hasIssueLabel(e Engine, issueID, labelID int64) bool {
has, _ := e.Where("issue_id=? AND label_id=?", issueID, labelID).Get(new(IssueLabel)) has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
return has return has
} }
@ -180,6 +200,10 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
// NewIssueLabel creates a new issue-label relation. // NewIssueLabel creates a new issue-label relation.
func NewIssueLabel(issue *Issue, label *Label) (err error) { func NewIssueLabel(issue *Issue, label *Label) (err error) {
if HasIssueLabel(issue.ID, label.ID) {
return nil
}
sess := x.NewSession() sess := x.NewSession()
defer sessionRelease(sess) defer sessionRelease(sess)
if err = sess.Begin(); err != nil { if err = sess.Begin(); err != nil {
@ -193,6 +217,35 @@ func NewIssueLabel(issue *Issue, label *Label) (err error) {
return sess.Commit() return sess.Commit()
} }
func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
for i := range labels {
if hasIssueLabel(e, issue.ID, labels[i].ID) {
continue
}
if err = newIssueLabel(e, issue, labels[i]); err != nil {
return fmt.Errorf("newIssueLabel: %v", err)
}
}
return nil
}
// NewIssueLabels creates a list of issue-label relations.
func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
sess := x.NewSession()
defer sessionRelease(sess)
if err = sess.Begin(); err != nil {
return err
}
if err = newIssueLabels(sess, issue, labels); err != nil {
return err
}
return sess.Commit()
}
func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) { func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
issueLabels := make([]*IssueLabel, 0, 10) issueLabels := make([]*IssueLabel, 0, 10)
return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels) return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)

View File

@ -244,7 +244,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/:index", func() { m.Group("/:index", func() {
m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue) m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
m.Group("/labels", func() { m.Group("/labels", func() {
m.Combo("").Get(repo.GetIssueLabels). m.Combo("").Get(repo.ListIssueLabels).
Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels). Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels). Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
Delete(repo.ClearIssueLabels) Delete(repo.ClearIssueLabels)
@ -255,8 +255,8 @@ func RegisterRoutes(m *macaron.Macaron) {
}) })
m.Group("/labels", func() { m.Group("/labels", func() {
m.Combo("").Get(repo.ListLabels). m.Combo("").Get(repo.ListLabels).
Post(bind(api.LabelOption{}), repo.CreateLabel) Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.LabelOption{}), repo.EditLabel). m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
Delete(repo.DeleteLabel) Delete(repo.DeleteLabel)
}) })
}, RepoAssignment()) }, RepoAssignment())

View File

@ -12,7 +12,7 @@ import (
"github.com/gogits/gogs/routers/api/v1/convert" "github.com/gogits/gogs/routers/api/v1/convert"
) )
func GetIssueLabels(ctx *context.APIContext) { func ListIssueLabels(ctx *context.APIContext) {
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil { if err != nil {
if models.IsErrIssueNotExist(err) { if models.IsErrIssueNotExist(err) {
@ -27,7 +27,6 @@ func GetIssueLabels(ctx *context.APIContext) {
for i := range issue.Labels { for i := range issue.Labels {
apiLabels[i] = convert.ToLabel(issue.Labels[i]) apiLabels[i] = convert.ToLabel(issue.Labels[i])
} }
ctx.JSON(200, &apiLabels) ctx.JSON(200, &apiLabels)
} }
@ -47,90 +46,27 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
return return
} }
var labels []*models.Label labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
if labels, err = filterLabelsByRepoID(form.Labels, issue.RepoID); err != nil { if err != nil {
ctx.Error(400, "filterLabelsByRepoID", err) ctx.Error(500, "GetLabelsInRepoByIDs", err)
return return
} }
if err = issue.AddLabels(labels); err != nil {
ctx.Error(500, "AddLabels", err)
return
}
labels, err = models.GetLabelsByIssueID(issue.ID)
if err != nil {
ctx.Error(500, "GetLabelsByIssueID", err)
return
}
apiLabels := make([]*api.Label, len(labels))
for i := range labels { for i := range labels {
if !models.HasIssueLabel(issue.ID, labels[i].ID) { apiLabels[i] = convert.ToLabel(labels[i])
if err := models.NewIssueLabel(issue, labels[i]); err != nil {
ctx.Error(500, "NewIssueLabel", err)
return
}
}
} }
// Refresh issue to get the updated list of labels from the DB
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
}
apiLabels := make([]*api.Label, len(issue.Labels))
for i := range issue.Labels {
apiLabels[i] = convert.ToLabel(issue.Labels[i])
}
ctx.JSON(200, &apiLabels)
}
func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
if !ctx.Repo.IsWriter() {
ctx.Status(403)
return
}
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
}
var labels []*models.Label
if labels, err = filterLabelsByRepoID(form.Labels, issue.RepoID); err != nil {
ctx.Error(400, "filterLabelsByRepoID", err)
return
}
if err := issue.ClearLabels(); err != nil {
ctx.Error(500, "ClearLabels", err)
return
}
for i := range labels {
if err := models.NewIssueLabel(issue, labels[i]); err != nil {
ctx.Error(500, "NewIssueLabel", err)
return
}
}
// Refresh issue to get the updated list of labels from the DB
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
}
apiLabels := make([]*api.Label, len(issue.Labels))
for i := range issue.Labels {
apiLabels[i] = convert.ToLabel(issue.Labels[i])
}
ctx.JSON(200, &apiLabels) ctx.JSON(200, &apiLabels)
} }
@ -150,12 +86,12 @@ func DeleteIssueLabel(ctx *context.APIContext) {
return return
} }
label, err := models.GetLabelByID(ctx.ParamsInt64(":id")) label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
if err != nil { if err != nil {
if models.IsErrLabelNotExist(err) { if models.IsErrLabelNotExist(err) {
ctx.Status(400) ctx.Error(422, "", err)
} else { } else {
ctx.Error(500, "GetLabelByID", err) ctx.Error(500, "GetLabelInRepoByID", err)
} }
return return
} }
@ -168,6 +104,46 @@ func DeleteIssueLabel(ctx *context.APIContext) {
ctx.Status(204) ctx.Status(204)
} }
func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
if !ctx.Repo.IsWriter() {
ctx.Status(403)
return
}
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
}
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
if err != nil {
ctx.Error(500, "GetLabelsInRepoByIDs", err)
return
}
if err := issue.ReplaceLabels(labels); err != nil {
ctx.Error(500, "ReplaceLabels", err)
return
}
labels, err = models.GetLabelsByIssueID(issue.ID)
if err != nil {
ctx.Error(500, "GetLabelsByIssueID", err)
return
}
apiLabels := make([]*api.Label, len(labels))
for i := range labels {
apiLabels[i] = convert.ToLabel(labels[i])
}
ctx.JSON(200, &apiLabels)
}
func ClearIssueLabels(ctx *context.APIContext) { func ClearIssueLabels(ctx *context.APIContext) {
if !ctx.Repo.IsWriter() { if !ctx.Repo.IsWriter() {
ctx.Status(403) ctx.Status(403)
@ -191,29 +167,3 @@ func ClearIssueLabels(ctx *context.APIContext) {
ctx.Status(204) ctx.Status(204)
} }
func filterLabelsByRepoID(labelIDs []int64, repoID int64) ([]*models.Label, error) {
labels := make([]*models.Label, 0, len(labelIDs))
errors := make([]error, 0, len(labelIDs))
for i := range labelIDs {
label, err := models.GetLabelByID(labelIDs[i])
if err != nil {
errors = append(errors, err)
} else if label.RepoID != repoID {
errors = append(errors, models.ErrLabelNotValidForRepository{label.ID, repoID})
} else {
labels = append(labels, label)
}
}
errorCount := len(errors)
if errorCount == 1 {
return labels, errors[0]
} else if errorCount > 1 {
return labels, models.ErrMultipleErrors{errors}
}
return labels, nil
}

View File

@ -9,14 +9,13 @@ import (
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/routers/api/v1/convert" "github.com/gogits/gogs/routers/api/v1/convert"
) )
func ListLabels(ctx *context.APIContext) { func ListLabels(ctx *context.APIContext) {
labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.Error(500, "Labels", err) ctx.Error(500, "GetLabelsByRepoID", err)
return return
} }
@ -24,17 +23,16 @@ func ListLabels(ctx *context.APIContext) {
for i := range labels { for i := range labels {
apiLabels[i] = convert.ToLabel(labels[i]) apiLabels[i] = convert.ToLabel(labels[i])
} }
ctx.JSON(200, &apiLabels) ctx.JSON(200, &apiLabels)
} }
func GetLabel(ctx *context.APIContext) { func GetLabel(ctx *context.APIContext) {
label, err := models.GetLabelByID(ctx.ParamsInt64(":id")) label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
if err != nil { if err != nil {
if models.IsErrLabelNotExist(err) { if models.IsErrLabelNotExist(err) {
ctx.Status(404) ctx.Status(404)
} else { } else {
ctx.Error(500, "GetLabelByID", err) ctx.Error(500, "GetLabelByRepoID", err)
} }
return return
} }
@ -42,7 +40,7 @@ func GetLabel(ctx *context.APIContext) {
ctx.JSON(200, convert.ToLabel(label)) ctx.JSON(200, convert.ToLabel(label))
} }
func CreateLabel(ctx *context.APIContext, form api.LabelOption) { func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
if !ctx.Repo.IsWriter() { if !ctx.Repo.IsWriter() {
ctx.Status(403) ctx.Status(403)
return return
@ -53,43 +51,35 @@ func CreateLabel(ctx *context.APIContext, form api.LabelOption) {
Color: form.Color, Color: form.Color,
RepoID: ctx.Repo.Repository.ID, RepoID: ctx.Repo.Repository.ID,
} }
err := models.NewLabel(label) if err := models.NewLabel(label); err != nil {
if err != nil {
ctx.Error(500, "NewLabel", err) ctx.Error(500, "NewLabel", err)
return return
} }
label, err = models.GetLabelByID(label.ID)
if err != nil {
ctx.Error(500, "GetLabelByID", err)
return
}
ctx.JSON(201, convert.ToLabel(label)) ctx.JSON(201, convert.ToLabel(label))
} }
func EditLabel(ctx *context.APIContext, form api.LabelOption) { func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
if !ctx.Repo.IsWriter() { if !ctx.Repo.IsWriter() {
ctx.Status(403) ctx.Status(403)
return return
} }
label, err := models.GetLabelByID(ctx.ParamsInt64(":id")) label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
if err != nil { if err != nil {
if models.IsErrLabelNotExist(err) { if models.IsErrLabelNotExist(err) {
ctx.Status(404) ctx.Status(404)
} else { } else {
ctx.Error(500, "GetLabelByID", err) ctx.Error(500, "GetLabelByRepoID", err)
} }
return return
} }
if len(form.Name) > 0 { if form.Name != nil {
label.Name = form.Name label.Name = *form.Name
} }
if len(form.Color) > 0 { if form.Color != nil {
label.Color = form.Color label.Color = *form.Color
} }
if err := models.UpdateLabel(label); err != nil { if err := models.UpdateLabel(label); err != nil {
ctx.Handle(500, "UpdateLabel", err) ctx.Handle(500, "UpdateLabel", err)
return return
@ -103,21 +93,10 @@ func DeleteLabel(ctx *context.APIContext) {
return return
} }
label, err := models.GetLabelByID(ctx.ParamsInt64(":id"))
if err != nil {
if models.IsErrLabelNotExist(err) {
ctx.Status(404)
} else {
ctx.Error(500, "GetLabelByID", err)
}
return
}
if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
ctx.Error(500, "DeleteLabel", err) ctx.Error(500, "DeleteLabel", err)
return return
} }
log.Trace("Label deleted: %s %s", label.ID, label.Name)
ctx.Status(204) ctx.Status(204)
} }

View File

@ -1 +1 @@
0.9.59.0730 0.9.60.0803