8
8
"fmt"
9
9
"os"
10
10
"path/filepath"
11
+ "strconv"
11
12
"strings"
12
13
"time"
13
14
@@ -17,6 +18,24 @@ import (
17
18
"github.com/unknwon/com"
18
19
)
19
20
21
+ // GenerateRepoOptions contains the template units to generate
22
+ type GenerateRepoOptions struct {
23
+ Name string
24
+ Description string
25
+ Private bool
26
+ GitContent bool
27
+ Topics bool
28
+ GitHooks bool
29
+ Webhooks bool
30
+ Avatar bool
31
+ IssueLabels bool
32
+ }
33
+
34
+ // IsValid checks whether at least one option is chosen for generation
35
+ func (gro GenerateRepoOptions ) IsValid () bool {
36
+ return gro .GitContent || gro .Topics || gro .GitHooks || gro .Webhooks || gro .Avatar || gro .IssueLabels // or other items as they are added
37
+ }
38
+
20
39
// generateRepository initializes repository from template
21
40
func generateRepository (e Engine , repo , templateRepo * Repository ) (err error ) {
22
41
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
160
179
}
161
180
return nil
162
181
}
182
+
183
+ // GenerateAvatar generates the avatar from a template repository
184
+ func GenerateAvatar (ctx DBContext , templateRepo , generateRepo * Repository ) error {
185
+ generateRepo .Avatar = strings .Replace (templateRepo .Avatar , strconv .FormatInt (templateRepo .ID , 10 ), strconv .FormatInt (generateRepo .ID , 10 ), 1 )
186
+ if err := com .Copy (templateRepo .CustomAvatarPath (), generateRepo .CustomAvatarPath ()); err != nil {
187
+ return err
188
+ }
189
+
190
+ return updateRepositoryCols (ctx .e , generateRepo , "avatar" )
191
+ }
192
+
193
+ // GenerateIssueLabels generates issue labels from a template repository
194
+ func GenerateIssueLabels (ctx DBContext , templateRepo , generateRepo * Repository ) error {
195
+ templateLabels , err := getLabelsByRepoID (ctx .e , templateRepo .ID , "" )
196
+ if err != nil {
197
+ return err
198
+ }
199
+
200
+ for _ , templateLabel := range templateLabels {
201
+ generateLabel := & Label {
202
+ RepoID : generateRepo .ID ,
203
+ Name : templateLabel .Name ,
204
+ Description : templateLabel .Description ,
205
+ Color : templateLabel .Color ,
206
+ }
207
+ if err := newLabel (ctx .e , generateLabel ); err != nil {
208
+ return err
209
+ }
210
+ }
211
+ return nil
212
+ }
0 commit comments