MCPcopy
hub / github.com/apache/devlake / getOrCreateConnection

Function getOrCreateConnection

backend/plugins/webhook/api/deployments.go:137–211  ·  view source on GitHub ↗
(input *plugin.ApiResourceInput)

Source from the content-addressed store, hash-verified

135}
136
137func getOrCreateConnection(input *plugin.ApiResourceInput) (*models.WebhookConnection, errors.Error, bool) {
138 connection := &models.WebhookConnection{}
139 projectName := input.Params["projectName"]
140 webhookName := fmt.Sprintf("%s_deployments", projectName)
141 err := findByProjectName(connection, input.Params, pluginName, webhookName)
142 dal := basicRes.GetDal()
143 if err != nil {
144 // if not found, we will attempt to create a new connection
145 // Use direct comparison against the package sentinel; only treat other errors as fatal.
146 if !dal.IsErrorNotFound(err) {
147 logger.Error(err, "failed to find webhook connection for project", "projectName", projectName)
148 return nil, err, true
149 }
150
151 // create the connection
152 logger.Debug("creating webhook connection for project %s", projectName)
153 connection.Name = webhookName
154
155 // find the project and blueprint with which we will associate this connection
156 projectOutput, err := services.GetProject(projectName)
157 if err != nil {
158 logger.Error(err, "failed to find project for webhook connection", "projectName", projectName)
159 return nil, err, true
160 }
161
162 if projectOutput == nil {
163 logger.Error(err, "project not found for webhook connection", "projectName", projectName)
164 return nil, errors.NotFound.New("project not found: " + projectName), true
165 }
166
167 if projectOutput.Blueprint == nil {
168 logger.Error(err, "unable to create webhook as the project has no blueprint", "projectName", projectName)
169 return nil, errors.BadInput.New("project has no blueprint: " + projectName), true
170 }
171
172 connectionInput := &plugin.ApiResourceInput{
173 Params: map[string]string{
174 "plugin": "webhook",
175 },
176 Body: map[string]interface{}{
177 "name": webhookName,
178 },
179 }
180
181 err = connectionHelper.Create(connection, connectionInput)
182 if err != nil {
183 logger.Error(err, "failed to create webhook connection for project", "projectName", projectName)
184 return nil, err, true
185 }
186
187 // get the blueprint
188 blueprintId := projectOutput.Blueprint.ID
189 blueprint, err := services.GetBlueprint(blueprintId, true)
190
191 if err != nil {
192 logger.Error(err, "failed to find blueprint for webhook connection", "blueprintId", blueprintId)
193 return nil, err, true
194 }

Callers 1

Calls 10

findByProjectNameFunction · 0.85
GetProjectMethod · 0.80
GetBlueprintMethod · 0.80
GetDalMethod · 0.65
IsErrorNotFoundMethod · 0.65
ErrorMethod · 0.65
DebugMethod · 0.65
NewMethod · 0.65
CreateMethod · 0.65
InfoMethod · 0.65

Tested by

no test coverage detected