Skip to content

Commit

Permalink
Refactor Notifications send to Gerrit
Browse files Browse the repository at this point in the history
  • Loading branch information
ckreisl committed Jul 10, 2022
1 parent 61fddd8 commit 8403378
Show file tree
Hide file tree
Showing 11 changed files with 570 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier;

import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.notification.INotification;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.notification.Notification;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.notification.NotificationCommands;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.notification.NotificationBuildCompleted;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.notification.NotificationBuildStarted;
import com.sonymobile.tools.gerrit.gerritevents.GerritCmdRunner;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.PatchSet;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeBasedEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.rest.Topic;

import java.util.Collections;
import java.util.Map;

import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.extensions.GerritTriggeredBuildListener;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.MemoryImprint;
Expand All @@ -53,8 +49,8 @@
public class GerritNotifier {

private static final Logger logger = LoggerFactory.getLogger(GerritNotifier.class);
private GerritCmdRunner cmdRunner;
private ParameterExpander parameterExpander;
private final GerritCmdRunner cmdRunner;
private final ParameterExpander parameterExpander;

/**
* Constructor.
Expand All @@ -78,99 +74,30 @@ public GerritNotifier(IGerritHudsonTriggerConfig config, GerritCmdRunner cmdRunn
}

/**
* Wrapper for sending a command to Gerrit
* Send Notification to gerrit.
*
* @param command The command send to Gerrit
* @param notification The notifications.
*/
private void sendCommandToGerrit(String command) {
if (command != null) {
if (!command.isEmpty()) {
logger.debug("Notifying BuildStarted to gerrit: {}", command);
cmdRunner.sendCommand(command);
} else {
logger.debug("BuildStarted command is empty. Gerrit will not be notified of BuildStarted");
}
} else {
logger.error("Something wrong during parameter extraction. "
+ "Gerrit will not be notified of BuildStarted");
}
}
private void send(INotification notification) {

/**
* Returns all assigned changes related to a Gerrit topic.
*
* @param event the event.
* @return Returns map of changes, if no changes an empty map is returned.
*/
private Map<Change, PatchSet> queryTopicChanges(ChangeBasedEvent event) {
Topic topic = event.getChange().getTopicObject();
if (topic != null) {
GerritServer server = PluginImpl.getServer_(event);
if (server != null) {
return topic.getChanges(server.getQueryHandler());
} else {
logger.error("Could get gerrit server based on event!");
}
if (notification == null) {
logger.error("NotificationObject is null not sending command!");
return;
}
return Collections.emptyMap();
}

/**
* Send build started message Gerrit changes assigned to a topic.
*
* @param build the build.
* @param taskListener the task listener
* @param event the event.
* @param stats the build stats.
*/
private void notifySameTopicBuildStarted(Run build, TaskListener taskListener,
GerritTriggeredEvent event, BuildsStartedStats stats) {
ChangeBasedEvent changeBasedEvent = (ChangeBasedEvent)event;
IGerritHudsonTriggerConfig config = PluginImpl.getServerConfig(event);
if (config != null) {
if (config.isVoteSameTopic()) {
Map<Change, PatchSet> changes = queryTopicChanges(changeBasedEvent);
for (Map.Entry<Change, PatchSet> entry : changes.entrySet()) {
Change change = entry.getKey();
if (change.equals(changeBasedEvent.getChange())) {
continue;
}
PatchSet patchSet = entry.getValue();
String command = parameterExpander.getBuildStartedCommand(
build, taskListener, changeBasedEvent, stats, change, patchSet);
sendCommandToGerrit(command);
}
}
} else {
logger.error("Could not get server config.");
if (!notification.isValid()) {
logger.error("Something wrong during parameter extraction. "
+ "Gerrit will not be notified of BuildStarted!");
return;
}
}

/**
* Sends build completed notification to all topic assigned patches.
*
* @param memoryImprint build memory imprint.
* @param listener the listener.
*/
private void notifySameTopicBuildCompleted(MemoryImprint memoryImprint, TaskListener listener) {
ChangeBasedEvent changeBasedEvent = (ChangeBasedEvent)memoryImprint.getEvent();
IGerritHudsonTriggerConfig config = PluginImpl.getServerConfig(changeBasedEvent);
if (config != null) {
if (config.isVoteSameTopic()) {
Map<Change, PatchSet> changes = queryTopicChanges(changeBasedEvent);
for (Map.Entry<Change, PatchSet> entry : changes.entrySet()) {
Change change = entry.getKey();
if (change.equals(changeBasedEvent.getChange())) {
continue;
}
PatchSet patchSet = entry.getValue();
String command = parameterExpander.getBuildCompletedCommand(
memoryImprint, listener, change, patchSet);
sendCommandToGerrit(command);
}
NotificationCommands notifyCommands = notification.getCommands();
cmdRunner.sendCommand(notifyCommands.getCommand());

if (notification.isInformTopicChangesEnabled()) {
for (String command : notifyCommands.getCommandsTopicChanges()) {
cmdRunner.sendCommand(command);
}
} else {
logger.error("Could not get server config.");
}
}

Expand All @@ -186,12 +113,16 @@ public void buildStarted(Run build, TaskListener taskListener,
try {
/* Without a change, it doesn't make sense to notify gerrit */
if (event instanceof ChangeBasedEvent) {
ChangeBasedEvent changeBasedEvent = (ChangeBasedEvent)event;
String command = parameterExpander.getBuildStartedCommand(
build, taskListener, changeBasedEvent, stats);
sendCommandToGerrit(command);
notifySameTopicBuildStarted(build, taskListener, changeBasedEvent, stats);
GerritTriggeredBuildListener.fireOnStarted(event, command);
Notification notification = new NotificationBuildStarted(
build, taskListener, event, stats, parameterExpander);
if (notification.isValid()) {
send(notification);
String command = notification.getCommands().getCommand();
GerritTriggeredBuildListener.fireOnStarted(event, command);
} else {
logger.error("Something wrong during parameter extraction. "
+ "Gerrit will not be notified of BuildStarted");
}
}
} catch (Exception ex) {
logger.error("Could not complete BuildStarted notification!", ex);
Expand All @@ -204,14 +135,19 @@ public void buildStarted(Run build, TaskListener taskListener,
* @param listener the taskListener.
*/
public void buildCompleted(MemoryImprint memoryImprint, TaskListener listener) {

try {
/* Without a change, it doesn't make sense to notify gerrit */
if (memoryImprint.getEvent() instanceof ChangeBasedEvent) {
String command = parameterExpander.getBuildCompletedCommand(memoryImprint, listener);
sendCommandToGerrit(command);
notifySameTopicBuildCompleted(memoryImprint, listener);
GerritTriggeredBuildListener.fireOnCompleted(memoryImprint, command);
Notification notification = new NotificationBuildCompleted(
memoryImprint, listener, parameterExpander);
if (notification.isValid()) {
send(notification);
String command = notification.getCommands().getCommand();
GerritTriggeredBuildListener.fireOnCompleted(memoryImprint, command);
} else {
logger.error("Something wrong during parameter extraction. "
+ "Gerrit will not be notified of BuildCompleted");
}
}
} catch (Exception ex) {
logger.error("Could not complete BuildCompleted notification!", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@
*
* @author Robert Sandell &lt;robert.sandell@sonyericsson.com&gt;
*/
public class NotificationFactory {
private static final Logger logger = LoggerFactory.getLogger(NotificationFactory.class);
private static NotificationFactory instance;
public class GerritNotifierFactory {
private static final Logger logger = LoggerFactory.getLogger(GerritNotifierFactory.class);
private static GerritNotifierFactory instance;

/**
* Gets the singleton instance of the NotificationFactory.
*
* @return the NotificationFactory.
*/
public static NotificationFactory getInstance() {
public static GerritNotifierFactory getInstance() {
if (instance == null) {
instance = new NotificationFactory();
instance = new GerritNotifierFactory();
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public synchronized void allBuildsCompleted(GerritTriggeredEvent event, GerritCa
if (event instanceof GerritEventLifecycle) {
((GerritEventLifecycle)event).fireAllBuildsCompleted();
}
NotificationFactory.getInstance().queueBuildCompleted(memory.getMemoryImprint(event), listener);
GerritNotifierFactory.getInstance().queueBuildCompleted(memory.getMemoryImprint(event), listener);
} finally {
memory.forget(event);
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public synchronized void onStarted(Run r, TaskListener listener) {
}
if (!silentStartMode) {
BuildsStartedStats stats = memory.getBuildsStartedStats(cause.getEvent());
NotificationFactory.getInstance().queueBuildStarted(r, listener, cause.getEvent(), stats);
GerritNotifierFactory.getInstance().queueBuildStarted(r, listener, cause.getEvent(), stats);
}
}
logger.info("Gerrit build [{}] Started for cause: [{}].", r, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.sonymobile.tools.gerrit.gerritevents.workers.cmd.AbstractSendCommandJob;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.GerritNotifier;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.NotificationFactory;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.GerritNotifierFactory;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory;

import hudson.model.TaskListener;
Expand Down Expand Up @@ -63,7 +63,7 @@ public BuildCompletedCommandJob(IGerritHudsonTriggerConfig config,
@Override
public void run() {
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
GerritNotifier notifier = NotificationFactory.getInstance()
GerritNotifier notifier = GerritNotifierFactory.getInstance()
.createGerritNotifier((IGerritHudsonTriggerConfig)getConfig(), this);
notifier.buildCompleted(memoryImprint, listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.sonymobile.tools.gerrit.gerritevents.workers.cmd.AbstractSendCommandJob;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.GerritNotifier;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.NotificationFactory;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.GerritNotifierFactory;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildsStartedStats;

import hudson.model.TaskListener;
Expand Down Expand Up @@ -73,7 +73,7 @@ public BuildStartedCommandJob(IGerritHudsonTriggerConfig config, Run build,
@Override
public void run() {
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
GerritNotifier notifier = NotificationFactory.getInstance()
GerritNotifier notifier = GerritNotifierFactory.getInstance()
.createGerritNotifier((IGerritHudsonTriggerConfig)getConfig(), this);
notifier.buildStarted(build, taskListener, event, stats);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2012 Sony Mobile Communications AB. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.notification;

/**
* Interface for a Notification.
*/
public interface INotification {
/**
* Returns the NotificationCommands object.
*
* @return NotificationsCommands object
*/
NotificationCommands getCommands();

/**
* Returns true of 'isVoteSameTopic()' in Gerrit config is enabled.
* Otherwise, false.
*
* @return true or false depending on isVoteSameTopic in Gerrit config.
*/
boolean isInformTopicChangesEnabled();

/**
* Returns true or false depending on whether the notification is valid or not.
*
* @return true or false
*/
boolean isValid();
}

0 comments on commit 8403378

Please sign in to comment.