diff options
author | Thibault Meyer <0xbaadf00d@users.noreply.github.com> | 2016-08-30 04:02:49 +0200 |
---|---|---|
committer | 无闻 <u@gogs.io> | 2016-08-29 19:02:49 -0700 |
commit | 92fb30c5260cc067da28ae3cd031b76f7b7976db (patch) | |
tree | 50855703a1044069861620ca32f98e812627ea59 /routers | |
parent | 9f44c267899fabd0b180cdf6cd41e6d77b2fb423 (diff) | |
download | gitea-92fb30c5260cc067da28ae3cd031b76f7b7976db.tar.gz gitea-92fb30c5260cc067da28ae3cd031b76f7b7976db.zip |
Load a set of predefined labels (#3459)
* Can use a predefined set of labels
* Change UI
* Fix HTML file indentation
* Avoid reading file from other directory (security issue)
* Apply a better fix
* Remove not used variable
* Merge upstream/develop
* Do modifications
* Raname
* remove binding + rename variable
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/issue.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 450143112f..46b513b265 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -11,6 +11,8 @@ import ( "io/ioutil" "net/http" "net/url" + "path" + "regexp" "strings" "time" @@ -20,6 +22,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/bindata" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/markdown" @@ -938,9 +941,58 @@ func Labels(ctx *context.Context) { ctx.Data["PageIsIssueList"] = true ctx.Data["PageIsLabels"] = true ctx.Data["RequireMinicolors"] = true + ctx.Data["LabelTemplates"] = models.LabelTemplates ctx.HTML(200, LABELS) } +func getLabelTemplateFile(name string) ([]byte, error) { + relPath := path.Join("conf/label", strings.TrimLeft(name, "./")) + + // Use custom file when available. + customPath := path.Join(setting.CustomPath, relPath) + if com.IsFile(customPath) { + return ioutil.ReadFile(customPath) + } + return bindata.Asset(relPath) +} + +func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) { + if ctx.HasError() { + ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) + ctx.Redirect(ctx.Repo.RepoLink + "/labels") + return + } + data, err := getLabelTemplateFile(form.TemplateName) + if err != nil { + ctx.Redirect(ctx.Repo.RepoLink + "/labels") + return + } + r, _ := regexp.Compile("#([a-fA-F0-9]{6})") + for i, line := range strings.Split(string(data), "\n") { + if len(line) > 0 { + line_x := strings.SplitN(strings.Trim(line, " \t"), " ", 2) + if len(line_x) == 2 && len(line_x[1]) > 0 { + if r.MatchString(line_x[0]) { + l := &models.Label{ + RepoID: ctx.Repo.Repository.ID, + Name: line_x[1], + Color: line_x[0], + } + if err := models.NewLabel(l); err != nil { + ctx.Handle(500, "InitializeLabelsFromTemplate", err) + return + } + } else { + log.Warn("Line %d on the label template file '%s': Bad HTML color code", i+1, form.TemplateName) + } + } else { + log.Warn("Line %d on the label template file '%s': Line is malformed", i+1, form.TemplateName) + } + } + } + ctx.Redirect(ctx.Repo.RepoLink + "/labels") +} + func NewLabel(ctx *context.Context, form auth.CreateLabelForm) { ctx.Data["Title"] = ctx.Tr("repo.labels") ctx.Data["PageIsLabels"] = true |