3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.step;
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24 import java.util.Collection;
25 import org.sonar.api.utils.log.Logger;
26 import org.sonar.api.utils.log.Loggers;
27 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
28 import org.sonar.ce.task.projectanalysis.pushevent.PushEvent;
29 import org.sonar.ce.task.projectanalysis.pushevent.PushEventRepository;
30 import org.sonar.ce.task.step.ComputationStep;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.pushevent.PushEventDto;
35 import static java.nio.charset.StandardCharsets.UTF_8;
37 public class PersistPushEventsStep implements ComputationStep {
38 private static final int MAX_BATCH_SIZE = 250;
39 private static final Logger LOGGER = Loggers.get(PersistPushEventsStep.class);
40 private static final Gson GSON = new GsonBuilder().create();
42 private final DbClient dbClient;
43 private final PushEventRepository pushEventRepository;
44 private final TreeRootHolder treeRootHolder;
46 public PersistPushEventsStep(DbClient dbClient, PushEventRepository pushEventRepository,
47 TreeRootHolder treeRootHolder) {
48 this.dbClient = dbClient;
49 this.pushEventRepository = pushEventRepository;
50 this.treeRootHolder = treeRootHolder;
54 public void execute(Context context) {
55 Collection<PushEvent<?>> issues = pushEventRepository.getEvents();
56 if (issues.isEmpty()) {
60 try (DbSession dbSession = dbClient.openSession(true)) {
62 for (PushEvent<?> event : issues) {
63 pushEvent(dbSession, event);
65 batchCounter = flushIfNeeded(dbSession, batchCounter);
67 flushSession(dbSession);
69 } catch (Exception ex) {
70 LOGGER.warn("Error during publishing push event", ex);
74 private void pushEvent(DbSession dbSession, PushEvent<?> event) {
75 PushEventDto eventDto = new PushEventDto()
76 .setProjectUuid(treeRootHolder.getRoot().getUuid())
77 .setPayload(serializeIssueToPushEvent(event));
78 dbClient.pushEventDao().insert(dbSession, eventDto);
81 private static byte[] serializeIssueToPushEvent(PushEvent<?> event) {
82 return GSON.toJson(event.getData()).getBytes(UTF_8);
85 private static int flushIfNeeded(DbSession dbSession, int batchCounter) {
86 if (batchCounter > MAX_BATCH_SIZE) {
87 flushSession(dbSession);
93 private static void flushSession(DbSession dbSession) {
94 dbSession.flushStatements();
99 public String getDescription() {
100 return "Publishing taint vulnerabilities events";