MCPcopy Index your code
hub / github.com/apache/devlake / CreateProject

Function CreateProject

backend/server/services/project.go:103–178  ·  view source on GitHub ↗

CreateProject accepts a project instance and insert it to database

(projectInput *models.ApiInputProject)

Source from the content-addressed store, hash-verified

101
102// CreateProject accepts a project instance and insert it to database
103func CreateProject(projectInput *models.ApiInputProject) (*models.ApiOutputProject, errors.Error) {
104 // verify input
105 if err := VerifyStruct(projectInput); err != nil {
106 return nil, err
107 }
108
109 // create transaction to update multiple tables
110 var err errors.Error
111 tx := db.Begin()
112 defer func() {
113 if r := recover(); r != nil || err != nil {
114 err = tx.Rollback()
115 if err != nil {
116 logger.Error(err, "PatchProject: failed to rollback")
117 }
118 }
119 }()
120
121 // create project first
122 project := &models.Project{}
123 project.BaseProject = projectInput.BaseProject
124 err = db.Create(project)
125 if err != nil {
126 if db.IsDuplicationError(err) {
127 return nil, errors.BadInput.New(fmt.Sprintf("A project with name [%s] already exists", project.Name))
128 }
129 return nil, errors.Default.Wrap(err, "error creating DB project")
130 }
131
132 // check if we need flush the Metrics
133 if len(projectInput.Metrics) > 0 {
134 err = refreshProjectMetrics(tx, projectInput)
135 if err != nil {
136 return nil, err
137 }
138 }
139
140 // create blueprint
141 blueprint := &models.Blueprint{
142 Name: project.Name + "-Blueprint",
143 ProjectName: project.Name,
144 Mode: "NORMAL",
145 Enable: true,
146 CronConfig: "0 0 * * *",
147 IsManual: false,
148 SyncPolicy: models.SyncPolicy{
149 TimeAfter: func() *time.Time {
150 t := time.Now().AddDate(0, -6, 0)
151 t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
152 return &t
153 }(),
154 },
155 Connections: nil,
156 }
157 if projectInput.Blueprint != nil {
158 blueprint = projectInput.Blueprint
159 }
160 err = tx.Create(blueprint)

Callers

nothing calls this directly

Calls 12

VerifyStructFunction · 0.85
refreshProjectMetricsFunction · 0.85
reloadBlueprintFunction · 0.85
makeProjectOutputFunction · 0.85
WrapMethod · 0.80
BeginMethod · 0.65
RollbackMethod · 0.65
ErrorMethod · 0.65
CreateMethod · 0.65
IsDuplicationErrorMethod · 0.65
NewMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected