]> source.dussan.org Git - gitea.git/commitdiff
Add avatar and issue labels to template repositories (#9149)
authorJohn Olheiser <42128690+jolheiser@users.noreply.github.com>
Mon, 25 Nov 2019 05:17:51 +0000 (23:17 -0600)
committerLunny Xiao <xiaolunwen@gmail.com>
Mon, 25 Nov 2019 05:17:51 +0000 (13:17 +0800)
* Add avatar and issue labels

Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Fix redundant if-err

Signed-off-by: jolheiser <john.olheiser@gmail.com>
models/issue_label.go
models/repo.go
models/repo_generate.go
modules/auth/repo_form.go
options/locale/locale_en-US.ini
routers/repo/repo.go
services/repository/generate.go
templates/repo/create.tmpl

index 77281e9bd4a659fad6fc4b534672887a69c0e952..ef1f37d77538cd309da3c1302dca0a8cc954e696 100644 (file)
@@ -279,10 +279,9 @@ func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
                Find(&labels)
 }
 
-// GetLabelsByRepoID returns all labels that belong to given repository by ID.
-func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
+func getLabelsByRepoID(e Engine, repoID int64, sortType string) ([]*Label, error) {
        labels := make([]*Label, 0, 10)
-       sess := x.Where("repo_id = ?", repoID)
+       sess := e.Where("repo_id = ?", repoID)
 
        switch sortType {
        case "reversealphabetically":
@@ -298,6 +297,11 @@ func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
        return labels, sess.Find(&labels)
 }
 
+// GetLabelsByRepoID returns all labels that belong to given repository by ID.
+func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
+       return getLabelsByRepoID(x, repoID, sortType)
+}
+
 func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
        var labels []*Label
        return labels, e.Where("issue_label.issue_id = ?", issueID).
index cbafe5d5a5295d7550e4914fe0d0d1348946cff9..6c293da6f84bb1d04854eb85ea5e80869b549abd 100644 (file)
@@ -1257,22 +1257,6 @@ type CreateRepoOptions struct {
        Status      RepositoryStatus
 }
 
-// GenerateRepoOptions contains the template units to generate
-type GenerateRepoOptions struct {
-       Name        string
-       Description string
-       Private     bool
-       GitContent  bool
-       Topics      bool
-       GitHooks    bool
-       Webhooks    bool
-}
-
-// IsValid checks whether at least one option is chosen for generation
-func (gro GenerateRepoOptions) IsValid() bool {
-       return gro.GitContent || gro.Topics || gro.GitHooks || gro.Webhooks // or other items as they are added
-}
-
 func getRepoInitFile(tp, name string) ([]byte, error) {
        cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
        relPath := path.Join("options", tp, cleanedName)
@@ -2957,8 +2941,12 @@ func (repo *Repository) GetTreePathLock(treePath string) (*LFSLock, error) {
        return nil, nil
 }
 
+func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
+       _, err := e.ID(repo.ID).Cols(cols...).Update(repo)
+       return err
+}
+
 // UpdateRepositoryCols updates repository's columns
 func UpdateRepositoryCols(repo *Repository, cols ...string) error {
-       _, err := x.ID(repo.ID).Cols(cols...).Update(repo)
-       return err
+       return updateRepositoryCols(x, repo, cols...)
 }
index 6406180038d2ac2bd341175653200c4559cc3547..234bdc27f2edae0ae3e7bfe75964dd734a11b23d 100644 (file)
@@ -8,6 +8,7 @@ import (
        "fmt"
        "os"
        "path/filepath"
+       "strconv"
        "strings"
        "time"
 
@@ -17,6 +18,24 @@ import (
        "github.com/unknwon/com"
 )
 
+// GenerateRepoOptions contains the template units to generate
+type GenerateRepoOptions struct {
+       Name        string
+       Description string
+       Private     bool
+       GitContent  bool
+       Topics      bool
+       GitHooks    bool
+       Webhooks    bool
+       Avatar      bool
+       IssueLabels bool
+}
+
+// IsValid checks whether at least one option is chosen for generation
+func (gro GenerateRepoOptions) IsValid() bool {
+       return gro.GitContent || gro.Topics || gro.GitHooks || gro.Webhooks || gro.Avatar || gro.IssueLabels // or other items as they are added
+}
+
 // generateRepository initializes repository from template
 func generateRepository(e Engine, repo, templateRepo *Repository) (err error) {
        tmpDir := filepath.Join(os.TempDir(), "gitea-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond()))
@@ -160,3 +179,34 @@ func GenerateWebhooks(ctx DBContext, templateRepo, generateRepo *Repository) err
        }
        return nil
 }
+
+// GenerateAvatar generates the avatar from a template repository
+func GenerateAvatar(ctx DBContext, templateRepo, generateRepo *Repository) error {
+       generateRepo.Avatar = strings.Replace(templateRepo.Avatar, strconv.FormatInt(templateRepo.ID, 10), strconv.FormatInt(generateRepo.ID, 10), 1)
+       if err := com.Copy(templateRepo.CustomAvatarPath(), generateRepo.CustomAvatarPath()); err != nil {
+               return err
+       }
+
+       return updateRepositoryCols(ctx.e, generateRepo, "avatar")
+}
+
+// GenerateIssueLabels generates issue labels from a template repository
+func GenerateIssueLabels(ctx DBContext, templateRepo, generateRepo *Repository) error {
+       templateLabels, err := getLabelsByRepoID(ctx.e, templateRepo.ID, "")
+       if err != nil {
+               return err
+       }
+
+       for _, templateLabel := range templateLabels {
+               generateLabel := &Label{
+                       RepoID:      generateRepo.ID,
+                       Name:        templateLabel.Name,
+                       Description: templateLabel.Description,
+                       Color:       templateLabel.Color,
+               }
+               if err := newLabel(ctx.e, generateLabel); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
index dd4f83478b82487d140f55de2b1dc4103c2848fa..1c873f2ea36690e5e8f49a71273e7bd35aa3b2b9 100644 (file)
@@ -42,6 +42,8 @@ type CreateRepoForm struct {
        Topics       bool
        GitHooks     bool
        Webhooks     bool
+       Avatar       bool
+       Labels       bool
 }
 
 // Validate validates the fields
index 3a209f62149d6fdee77239363a899004e02a7a03..be4522014f375b34fb9a509ecc6951571c8a5604 100644 (file)
@@ -641,6 +641,8 @@ template.git_hooks = Git Hooks
 template.git_hooks_tooltip = You are currently unable to modify or remove git hooks once added. Select this only if you trust the template repository.
 template.webhooks = Webhooks
 template.topics = Topics
+template.avatar = Avatar
+template.issue_labels = Issue Labels
 template.one_item = Must select at least one template item
 template.invalid = Must select a template repository
 
index 2c97216406056960e7ae5c7cd0e3e1ba51af3745..969f96bfba3515a73fa1171a08e9d4023bd29994 100644 (file)
@@ -190,6 +190,8 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
                        Topics:      form.Topics,
                        GitHooks:    form.GitHooks,
                        Webhooks:    form.Webhooks,
+                       Avatar:      form.Avatar,
+                       IssueLabels: form.Labels,
                }
 
                if !opts.IsValid() {
index bd7fd0ce1179e81f54df0736fc8102dbb5ae58f5..f7e8ebd8c4dbad3d25639dda17c45afb2f3d37c7 100644 (file)
@@ -47,6 +47,20 @@ func GenerateRepository(doer, owner *models.User, templateRepo *models.Repositor
                        }
                }
 
+               // Avatar
+               if opts.Avatar && len(templateRepo.Avatar) > 0 {
+                       if err = models.GenerateAvatar(ctx, templateRepo, generateRepo); err != nil {
+                               return err
+                       }
+               }
+
+               // Issue Labels
+               if opts.IssueLabels {
+                       if err = models.GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil {
+                               return err
+                       }
+               }
+
                return nil
        }); err != nil {
                if generateRepo != nil {
index 69f2abb687f0fb5d9592832927718bc743afa35a..c71c3ab5a6afdf5c9a6002749f9de9947d9bd26f 100644 (file)
                                                                <label>{{.i18n.Tr "repo.template.topics"}}</label>
                                                        </div>
                                                </div>
+                                               <div class="inline field">
+                                                       <label></label>
+                                                       <div class="ui checkbox">
+                                                               <input class="hidden" name="avatar" type="checkbox" tabindex="0" {{if .avatar}}checked{{end}}>
+                                                               <label>{{.i18n.Tr "repo.template.avatar"}}</label>
+                                                       </div>
+                                                       <div class="ui checkbox">
+                                                               <input class="hidden" name="labels" type="checkbox" tabindex="0" {{if .labels}}checked{{end}}>
+                                                               <label>{{.i18n.Tr "repo.template.issue_labels"}}</label>
+                                                       </div>
+                                               </div>
                                        </div>
 
                                        <div id="non_template">