aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java
blob: a7b03e3d028389388ffcd5c7c79f1e8dff29c483 (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.webhook;

import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.audit.AuditPersister;
import org.sonar.db.audit.model.SecretNewValue;
import org.sonar.db.audit.model.WebhookNewValue;
import org.sonar.db.project.ProjectDto;

public class WebhookDao implements Dao {

  private final System2 system2;
  private final AuditPersister auditPersister;

  public WebhookDao(System2 system2, AuditPersister auditPersister) {
    this.system2 = system2;
    this.auditPersister = auditPersister;
  }

  public List<WebhookDto> selectGlobalWebhooks(DbSession dbSession) {
    return mapper(dbSession).selectGlobalWebhooksOrderedByName();
  }

  public Optional<WebhookDto> selectByUuid(DbSession dbSession, String uuid) {
    return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid));
  }

  public List<WebhookDto> selectByProject(DbSession dbSession, ProjectDto projectDto) {
    return mapper(dbSession).selectForProjectUuidOrderedByName(projectDto.getUuid());
  }

  public void insert(DbSession dbSession, WebhookDto dto, @Nullable String projectKey, @Nullable String projectName) {
    mapper(dbSession).insert(dto.setCreatedAt(system2.now()).setUpdatedAt(system2.now()));
    auditPersister.addWebhook(dbSession, new WebhookNewValue(dto, projectKey, projectName));
  }

  public void update(DbSession dbSession, WebhookDto dto, @Nullable String projectKey, @Nullable String projectName) {
    mapper(dbSession).update(dto.setUpdatedAt(system2.now()));
    if (dto.getSecret() != null) {
      auditPersister.updateWebhookSecret(dbSession, new SecretNewValue("webhook_name", dto.getName()));
    }
    auditPersister.updateWebhook(dbSession, new WebhookNewValue(dto, projectKey, projectName));
  }

  public void delete(DbSession dbSession, String uuid, String webhookName) {
    int deletedRows = mapper(dbSession).delete(uuid);

    if (deletedRows > 0) {
      auditPersister.deleteWebhook(dbSession, new WebhookNewValue(uuid, webhookName));
    }
  }

  public void deleteByProject(DbSession dbSession, ProjectDto projectDto) {
    int deletedRows = mapper(dbSession).deleteForProjectUuid(projectDto.getUuid());

    if (deletedRows > 0) {
      auditPersister.deleteWebhook(dbSession, new WebhookNewValue(projectDto));
    }
  }

  private static WebhookMapper mapper(DbSession dbSession) {
    return dbSession.getMapper(WebhookMapper.class);
  }

}