mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.3 KiB
111 lines
2.3 KiB
9 years ago
|
// 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 (
|
||
8 years ago
|
"github.com/Unknwon/com"
|
||
|
|
||
9 years ago
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
func ListLabels(c *context.APIContext) {
|
||
|
labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "GetLabelsByRepoID", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
apiLabels := make([]*api.Label, len(labels))
|
||
|
for i := range labels {
|
||
8 years ago
|
apiLabels[i] = labels[i].APIFormat()
|
||
9 years ago
|
}
|
||
8 years ago
|
c.JSON(200, &apiLabels)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func GetLabel(c *context.APIContext) {
|
||
8 years ago
|
var label *models.Label
|
||
|
var err error
|
||
|
idStr := c.Params(":id")
|
||
|
if id := com.StrTo(idStr).MustInt64(); id > 0 {
|
||
|
label, err = models.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
|
||
|
} else {
|
||
|
label, err = models.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
|
||
|
}
|
||
9 years ago
|
if err != nil {
|
||
|
if models.IsErrLabelNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetLabelByRepoID", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, label.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
|
||
|
if !c.Repo.IsWriter() {
|
||
|
c.Status(403)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
label := &models.Label{
|
||
|
Name: form.Name,
|
||
|
Color: form.Color,
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
9 years ago
|
}
|
||
8 years ago
|
if err := models.NewLabels(label); err != nil {
|
||
8 years ago
|
c.Error(500, "NewLabel", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.JSON(201, label.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditLabel(c *context.APIContext, form api.EditLabelOption) {
|
||
|
if !c.Repo.IsWriter() {
|
||
|
c.Status(403)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
||
9 years ago
|
if err != nil {
|
||
|
if models.IsErrLabelNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetLabelByRepoID", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if form.Name != nil {
|
||
|
label.Name = *form.Name
|
||
9 years ago
|
}
|
||
9 years ago
|
if form.Color != nil {
|
||
|
label.Color = *form.Color
|
||
9 years ago
|
}
|
||
|
if err := models.UpdateLabel(label); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateLabel", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.JSON(200, label.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func DeleteLabel(c *context.APIContext) {
|
||
|
if !c.Repo.IsWriter() {
|
||
|
c.Status(403)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if err := models.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
|
||
|
c.Error(500, "DeleteLabel", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Status(204)
|
||
9 years ago
|
}
|