|
| 1 | +// Copyright 2025 l3montree GmbH. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +package webhook |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "log/slog" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/l3montree-dev/devguard/internal/common" |
| 12 | + "github.com/l3montree-dev/devguard/internal/core" |
| 13 | + "github.com/l3montree-dev/devguard/internal/database/models" |
| 14 | + "github.com/l3montree-dev/devguard/internal/database/repositories" |
| 15 | +) |
| 16 | + |
| 17 | +type WebhookIntegration struct { |
| 18 | + webhookRepository core.WebhookIntegrationRepository |
| 19 | +} |
| 20 | + |
| 21 | +var _ core.ThirdPartyIntegration = &WebhookIntegration{} |
| 22 | + |
| 23 | +func NewWebhookIntegration(db core.DB) *WebhookIntegration { |
| 24 | + webhookRepository := repositories.NewWebhookRepository(db) |
| 25 | + return &WebhookIntegration{ |
| 26 | + webhookRepository: webhookRepository, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func (w *WebhookIntegration) Delete(ctx core.Context) error { |
| 31 | + id := ctx.Param("id") |
| 32 | + if id == "" { |
| 33 | + return ctx.JSON(400, "id is required") |
| 34 | + } |
| 35 | + |
| 36 | + uuidID, err := uuid.Parse(id) |
| 37 | + if err != nil { |
| 38 | + return ctx.JSON(400, "invalid id format") |
| 39 | + } |
| 40 | + |
| 41 | + if err := w.webhookRepository.Delete(nil, uuidID); err != nil { |
| 42 | + slog.Error("failed to delete webhook integration", "err", err) |
| 43 | + return ctx.JSON(500, "failed to delete webhook integration") |
| 44 | + } |
| 45 | + return ctx.JSON(200, "Webhook integration deleted successfully") |
| 46 | +} |
| 47 | + |
| 48 | +func (w *WebhookIntegration) Update(ctx core.Context) error { |
| 49 | + var data struct { |
| 50 | + ID string `json:"id"` |
| 51 | + Name string `json:"name"` |
| 52 | + Description string `json:"description"` |
| 53 | + URL string `json:"url"` |
| 54 | + Secret string `json:"secret"` |
| 55 | + SbomEnabled bool `json:"sbomEnabled"` |
| 56 | + VulnEnabled bool `json:"vulnEnabled"` |
| 57 | + } |
| 58 | + |
| 59 | + if err := ctx.Bind(&data); err != nil { |
| 60 | + return ctx.JSON(400, "invalid request data") |
| 61 | + } |
| 62 | + if data.URL == "" { |
| 63 | + return ctx.JSON(400, "url is required") |
| 64 | + } |
| 65 | + |
| 66 | + uuidID, err := uuid.Parse(data.ID) |
| 67 | + if err != nil { |
| 68 | + return ctx.JSON(400, "invalid id format") |
| 69 | + } |
| 70 | + webhookIntegration := &models.WebhookIntegration{ |
| 71 | + |
| 72 | + Model: models.Model{ |
| 73 | + ID: uuidID, |
| 74 | + }, |
| 75 | + Name: &data.Name, |
| 76 | + Description: &data.Description, |
| 77 | + URL: data.URL, |
| 78 | + Secret: &data.Secret, |
| 79 | + SbomEnabled: data.SbomEnabled, |
| 80 | + VulnEnabled: data.VulnEnabled, |
| 81 | + OrgID: core.GetOrg(ctx).GetID(), |
| 82 | + } |
| 83 | + |
| 84 | + if err := w.webhookRepository.Save(nil, webhookIntegration); err != nil { |
| 85 | + slog.Error("failed to update webhook integration", "err", err) |
| 86 | + return ctx.JSON(500, "failed to update webhook integration") |
| 87 | + } |
| 88 | + return ctx.JSON(200, common.WebhookIntegrationDTO{ |
| 89 | + ID: webhookIntegration.ID.String(), |
| 90 | + Name: *webhookIntegration.Name, |
| 91 | + Description: *webhookIntegration.Description, |
| 92 | + URL: webhookIntegration.URL, |
| 93 | + Secret: *webhookIntegration.Secret, |
| 94 | + SbomEnabled: webhookIntegration.SbomEnabled, |
| 95 | + VulnEnabled: webhookIntegration.VulnEnabled, |
| 96 | + }) |
| 97 | +} |
| 98 | +func (w *WebhookIntegration) TestAndSave(ctx core.Context) error { |
| 99 | + var data struct { |
| 100 | + Name string `json:"name"` |
| 101 | + Description string `json:"description"` |
| 102 | + URL string `json:"url"` |
| 103 | + Secret string `json:"secret"` |
| 104 | + SbomEnabled bool `json:"sbomEnabled"` |
| 105 | + VulnEnabled bool `json:"vulnEnabled"` |
| 106 | + } |
| 107 | + |
| 108 | + if err := ctx.Bind(&data); err != nil { |
| 109 | + return ctx.JSON(400, "invalid request data") |
| 110 | + } |
| 111 | + if data.URL == "" { |
| 112 | + return ctx.JSON(400, "url is required") |
| 113 | + } |
| 114 | + |
| 115 | + /* projectID := uuid.Nil |
| 116 | + if ok := core.HasProject(ctx); ok { |
| 117 | + project := core.GetProject(ctx) |
| 118 | + projectID = project.ID |
| 119 | + } */ |
| 120 | + |
| 121 | + webhookIntegration := &models.WebhookIntegration{ |
| 122 | + Name: &data.Name, |
| 123 | + Description: &data.Description, |
| 124 | + URL: data.URL, |
| 125 | + Secret: &data.Secret, |
| 126 | + SbomEnabled: data.SbomEnabled, |
| 127 | + VulnEnabled: data.VulnEnabled, |
| 128 | + OrgID: core.GetOrg(ctx).GetID(), |
| 129 | + /* ProjectID: &projectID, */ |
| 130 | + } |
| 131 | + |
| 132 | + if err := w.webhookRepository.Save(nil, webhookIntegration); err != nil { |
| 133 | + slog.Error("failed to save webhook integration", "err", err) |
| 134 | + return ctx.JSON(500, "failed to save webhook integration") |
| 135 | + } |
| 136 | + return ctx.JSON(200, common.WebhookIntegrationDTO{ |
| 137 | + ID: webhookIntegration.ID.String(), |
| 138 | + Name: *webhookIntegration.Name, |
| 139 | + Description: *webhookIntegration.Description, |
| 140 | + URL: webhookIntegration.URL, |
| 141 | + Secret: *webhookIntegration.Secret, |
| 142 | + SbomEnabled: webhookIntegration.SbomEnabled, |
| 143 | + VulnEnabled: webhookIntegration.VulnEnabled, |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +func (w *WebhookIntegration) HandleEvent(event any) error { |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func (w *WebhookIntegration) WantsToHandleWebhook(ctx core.Context) bool { |
| 152 | + // Logic to determine if this integration wants to handle the webhook |
| 153 | + return true |
| 154 | +} |
| 155 | + |
| 156 | +func (w *WebhookIntegration) HandleWebhook(ctx core.Context) error { |
| 157 | + // Logic to handle the webhook |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func (w *WebhookIntegration) ListOrgs(ctx core.Context) ([]models.Org, error) { |
| 162 | + // Logic to list organizations |
| 163 | + return nil, nil |
| 164 | +} |
| 165 | + |
| 166 | +func (w *WebhookIntegration) ListGroups(ctx core.Context, userID string, providerID string) ([]models.Project, error) { |
| 167 | + // Logic to list groups |
| 168 | + return nil, nil |
| 169 | +} |
| 170 | + |
| 171 | +func (w *WebhookIntegration) ListProjects(ctx core.Context, userID string, providerID string, groupID string) ([]models.Asset, error) { |
| 172 | + // Logic to list projects |
| 173 | + return nil, nil |
| 174 | +} |
| 175 | + |
| 176 | +func (w *WebhookIntegration) ListRepositories(ctx core.Context) ([]core.Repository, error) { |
| 177 | + // Logic to list repositories |
| 178 | + return nil, nil |
| 179 | +} |
| 180 | + |
| 181 | +func (w *WebhookIntegration) HasAccessToExternalEntityProvider(ctx core.Context, externalEntityProviderID string) (bool, error) { |
| 182 | + // Logic to check access to external entity provider |
| 183 | + return false, nil |
| 184 | +} |
| 185 | + |
| 186 | +func (w *WebhookIntegration) GetRoleInGroup(ctx context.Context, userID string, providerID string, groupID string) (string, error) { |
| 187 | + // Logic to get role in group |
| 188 | + return "", nil |
| 189 | +} |
| 190 | + |
| 191 | +func (w *WebhookIntegration) GetRoleInProject(ctx context.Context, userID string, providerID string, projectID string) (string, error) { |
| 192 | + // Logic to get role in project |
| 193 | + return "", nil |
| 194 | +} |
| 195 | + |
| 196 | +func (w *WebhookIntegration) CreateIssue(ctx context.Context, asset models.Asset, assetVersionName string, vuln models.Vuln, projectSlug string, orgSlug string, justification string, userID string) error { |
| 197 | + // Logic to create an issue |
| 198 | + return nil |
| 199 | +} |
| 200 | + |
| 201 | +func (w *WebhookIntegration) UpdateIssue(ctx context.Context, asset models.Asset, vuln models.Vuln) error { |
| 202 | + // Logic to update an issue |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +func (w *WebhookIntegration) GetUsers(org models.Org) []core.User { |
| 207 | + // Logic to get users in an organization |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func (w *WebhookIntegration) GetID() core.IntegrationID { |
| 212 | + // Return the integration ID for this webhook integration |
| 213 | + return core.WebhookIntegrationID |
| 214 | +} |
0 commit comments