-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathtemplate.go
More file actions
193 lines (169 loc) · 5.79 KB
/
template.go
File metadata and controls
193 lines (169 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"context"
"fmt"
"strings"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
notify_service "code.gitea.io/gitea/services/notify"
)
// GenerateIssueLabels generates issue labels from a template repository
func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
templateLabels, err := issues_model.GetLabelsByRepoID(ctx, templateRepo.ID, "", db.ListOptions{})
if err != nil {
return err
}
// Prevent insert being called with an empty slice which would result in
// err "no element on slice when insert".
if len(templateLabels) == 0 {
return nil
}
newLabels := make([]*issues_model.Label, 0, len(templateLabels))
for _, templateLabel := range templateLabels {
newLabels = append(newLabels, &issues_model.Label{
RepoID: generateRepo.ID,
Name: templateLabel.Name,
Exclusive: templateLabel.Exclusive,
Description: templateLabel.Description,
Color: templateLabel.Color,
})
}
return db.Insert(ctx, newLabels)
}
func GenerateProtectedBranch(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
templateBranches, err := git_model.FindRepoProtectedBranchRules(ctx, templateRepo.ID)
if err != nil {
return err
}
// Prevent insert being called with an empty slice which would result in
// err "no element on slice when insert".
if len(templateBranches) == 0 {
return nil
}
newBranches := make([]*git_model.ProtectedBranch, 0, len(templateBranches))
for _, templateBranch := range templateBranches {
templateBranch.ID = 0
templateBranch.RepoID = generateRepo.ID
templateBranch.UpdatedUnix = 0
templateBranch.CreatedUnix = 0
newBranches = append(newBranches, templateBranch)
}
return db.Insert(ctx, newBranches)
}
// GenerateRepository generates a repository from a template
func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templateRepo *repo_model.Repository, opts GenerateRepoOptions) (_ *repo_model.Repository, err error) {
if !doer.CanCreateRepoIn(owner) {
return nil, repo_model.ErrReachLimitOfRepo{
Limit: owner.MaxRepoCreation,
}
}
generateRepo := &repo_model.Repository{
OwnerID: owner.ID,
Owner: owner,
OwnerName: owner.Name,
Name: opts.Name,
LowerName: strings.ToLower(opts.Name),
Description: opts.Description,
DefaultBranch: opts.DefaultBranch,
IsPrivate: opts.Private,
IsEmpty: !opts.GitContent || templateRepo.IsEmpty,
IsFsckEnabled: templateRepo.IsFsckEnabled,
TemplateID: templateRepo.ID,
TrustModel: templateRepo.TrustModel,
ObjectFormatName: templateRepo.ObjectFormatName,
Status: repo_model.RepositoryBeingMigrated,
}
// 1 - Create the repository in the database
if err := db.WithTx(ctx, func(ctx context.Context) error {
return createRepositoryInDB(ctx, doer, owner, generateRepo, false)
}); err != nil {
return nil, err
}
// last - clean up the repository if something goes wrong
defer func() {
if err != nil {
// we can not use `ctx` because it may be canceled or timed out
cleanupRepository(generateRepo)
}
}()
// 2 - check whether the repository with the same storage exists
isExist, err := gitrepo.IsRepositoryExist(ctx, generateRepo)
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", generateRepo.FullName(), err)
return nil, err
}
if isExist {
// Don't return directly, we need err in defer to cleanupRepository
err = repo_model.ErrRepoFilesAlreadyExist{
Uname: generateRepo.OwnerName,
Name: generateRepo.Name,
}
return nil, err
}
// 3 -Init git bare new repository.
if err = gitrepo.InitRepository(ctx, generateRepo, generateRepo.ObjectFormatName); err != nil {
return nil, fmt.Errorf("git.InitRepository: %w", err)
} else if err = gitrepo.CreateDelegateHooks(ctx, generateRepo); err != nil {
return nil, fmt.Errorf("createDelegateHooks: %w", err)
}
// 4 - Update the git repository
if err = updateGitRepoAfterCreate(ctx, generateRepo); err != nil {
return nil, fmt.Errorf("updateGitRepoAfterCreate: %w", err)
}
// 5 - generate the repository contents according to the template
// Git Content
if opts.GitContent && !templateRepo.IsEmpty {
if err = GenerateGitContent(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
// Topics
if opts.Topics {
if err = repo_model.GenerateTopics(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
// Git Hooks
if opts.GitHooks {
if err = GenerateGitHooks(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
// Webhooks
if opts.Webhooks {
if err = GenerateWebhooks(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
// Avatar
if opts.Avatar && len(templateRepo.Avatar) > 0 {
if err = generateAvatar(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
// Issue Labels
if opts.IssueLabels {
if err = GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
if opts.ProtectedBranch {
if err = GenerateProtectedBranch(ctx, templateRepo, generateRepo); err != nil {
return nil, err
}
}
// 6 - update repository status to be ready
generateRepo.Status = repo_model.RepositoryReady
if err = repo_model.UpdateRepositoryColsWithAutoTime(ctx, generateRepo, "status"); err != nil {
return nil, fmt.Errorf("UpdateRepositoryCols: %w", err)
}
notify_service.CreateRepository(ctx, doer, owner, generateRepo)
return generateRepo, nil
}