summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorJohn Olheiser <42128690+jolheiser@users.noreply.github.com>2019-11-24 23:17:51 -0600
committerLunny Xiao <xiaolunwen@gmail.com>2019-11-25 13:17:51 +0800
commit62bcb2b7f1ce1a64f828d94f045d06bd8605455a (patch)
tree05e9c5c46e08ca787bc08326af6cadb130ded0fd /models
parent95c3dc856a7d4199232549e3b4d76769ef868bda (diff)
downloadgitea-62bcb2b7f1ce1a64f828d94f045d06bd8605455a.tar.gz
gitea-62bcb2b7f1ce1a64f828d94f045d06bd8605455a.zip
Add avatar and issue labels to template repositories (#9149)
* 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>
Diffstat (limited to 'models')
-rw-r--r--models/issue_label.go10
-rw-r--r--models/repo.go24
-rw-r--r--models/repo_generate.go50
3 files changed, 63 insertions, 21 deletions
diff --git a/models/issue_label.go b/models/issue_label.go
index 77281e9bd4..ef1f37d775 100644
--- a/models/issue_label.go
+++ b/models/issue_label.go
@@ -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).
diff --git a/models/repo.go b/models/repo.go
index cbafe5d5a5..6c293da6f8 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -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...)
}
diff --git a/models/repo_generate.go b/models/repo_generate.go
index 6406180038..234bdc27f2 100644
--- a/models/repo_generate.go
+++ b/models/repo_generate.go
@@ -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
+}