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

Function PostConnections

backend/plugins/webhook/api/connection.go:42–80  ·  view source on GitHub ↗

PostConnections @Summary create webhook connection @Description Create webhook connection, example: {"name":"Webhook data connection name"} @Tags plugins/webhook @Param body body WebhookConnectionResponse true "json body" @Success 200 {object} WebhookConnectionResponse @Failure 400 {string} errcod

(input *plugin.ApiResourceInput)

Source from the content-addressed store, hash-verified

40// @Failure 500 {string} errcode.Error "Internal Error"
41// @Router /plugins/webhook/connections [POST]
42func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
43 // update from request and save to database
44 connection := &models.WebhookConnection{}
45 tx := basicRes.GetDal().Begin()
46 err := connectionHelper.CreateWithTx(tx, connection, input)
47 if err != nil {
48 if err := tx.Rollback(); err != nil {
49 logger.Error(err, "transaction Rollback")
50 }
51 if strings.Contains(err.Error(), "the connection name already exists (400)") {
52 return nil, errors.BadInput.New(fmt.Sprintf("A webhook with name %s already exists.", connection.Name))
53 }
54 return nil, err
55 }
56 logger.Info("connection: %+v", connection)
57 name := apiKeyHelper.GenApiKeyNameForPlugin(pluginName, connection.ID)
58 allowedPath := fmt.Sprintf("/plugins/%s/connections/%d/.*", pluginName, connection.ID)
59 extra := fmt.Sprintf("connectionId:%d", connection.ID)
60 apiKeyRecord, err := apiKeyHelper.CreateForPlugin(tx, input.User, name, pluginName, allowedPath, extra)
61 if err != nil {
62 if err := tx.Rollback(); err != nil {
63 logger.Error(err, "transaction Rollback")
64 }
65 logger.Error(err, "CreateForPlugin")
66 return nil, err
67 }
68 if err := tx.Commit(); err != nil {
69 logger.Info("transaction commit: %s", err)
70 }
71
72 webhookConnectionResponse, err := formatConnection(connection, false)
73 if err != nil {
74 return nil, err
75 }
76 webhookConnectionResponse.ApiKey = apiKeyRecord
77 logger.Info("api output connection: %+v", webhookConnectionResponse)
78
79 return &plugin.ApiResourceOutput{Body: webhookConnectionResponse, Status: http.StatusOK}, nil
80}
81
82// PatchConnection
83// @Summary patch webhook connection

Callers

nothing calls this directly

Calls 11

formatConnectionFunction · 0.85
CreateWithTxMethod · 0.80
CreateForPluginMethod · 0.80
BeginMethod · 0.65
GetDalMethod · 0.65
RollbackMethod · 0.65
ErrorMethod · 0.65
NewMethod · 0.65
InfoMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected