Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into JENKINS-23152
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/gerritnotifier/model/BuildMemory.java
	src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java
	src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/spec/SpecGerritTriggerHudsonTest.java

Tests compiles but still fails.
  • Loading branch information
rsandell committed Dec 8, 2014
2 parents 1eb20e4 + 19bfb01 commit 3084c3e
Show file tree
Hide file tree
Showing 56 changed files with 1,279 additions and 285 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Expand Up @@ -9,7 +9,7 @@

<groupId>com.sonyericsson.hudson.plugins.gerrit</groupId>
<artifactId>gerrit-trigger</artifactId>
<version>2.12.0-beta-5-SNAPSHOT</version>
<version>2.13.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Gerrit Trigger</name>
<description>Integrates with Gerrit code review.</description>
Expand Down Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>com.sonymobile.tools.gerrit</groupId>
<artifactId>gerrit-events</artifactId>
<version>2.4.0</version>
<version>2.4.2</version>
<!-- New source is here: https://github.com/sonyxperiadev/gerrit-events -->
</dependency>
<dependency>
Expand Down Expand Up @@ -301,5 +301,6 @@
<connection>scm:git:ssh://github.com/jenkinsci/gerrit-trigger-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/jenkinsci/gerrit-trigger-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/gerrit-trigger-plugin</url>
</scm>
<tag>HEAD</tag>
</scm>
</project>
Expand Up @@ -25,23 +25,25 @@
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier;


import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeBasedEvent;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.Config;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.MemoryImprint;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.MemoryImprint.Entry;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildsStartedStats;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import com.sonyericsson.hudson.plugins.gerrit.trigger.utils.StringUtil;
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.Notify;

import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -517,6 +519,11 @@ private String createBuildsStats(MemoryImprint memoryImprint, TaskListener liste

Entry[] entries = memoryImprint.getEntries();

/* Sort entries with worst results first so the attention is drawn on them.
* Otherwise users may e.g. miss an UNSTABLE result because they see SUCCESS first.
*/
Arrays.sort(entries, EntryByBuildResultComparator.DESCENDING);

// In Gerrit, all lines before the first empty line are used as the summary.
// For the summary all single linefeeds will be removed (only in Gerrit, not sent mails).
// Hence, for the multi-builds, we will add a double linefeed before actually listing
Expand Down Expand Up @@ -637,4 +644,42 @@ protected String findMessage(String completedCommand) {
}
return fromMessage.substring(messageStart.length(), endIndex);
}

/**
* Sorts build entries along their results.
*/
private static final class EntryByBuildResultComparator implements Comparator<Entry> {
/**
* Sorts with worse results first.
*/
static final EntryByBuildResultComparator DESCENDING = new EntryByBuildResultComparator(true);

private boolean descending;
/**
* Creates a comparator
*
* @param descending <code>true</code> for worst results first,
* <code>false</code> for best results first
*/
private EntryByBuildResultComparator(boolean descending) {
this.descending = descending;
}

@Override
public int compare(Entry e1, Entry e2) {
AbstractBuild b1 = e1.getBuild();
AbstractBuild b2 = e2.getBuild();
if (b1 != null && b2 != null) {
int o1 = b1.getResult().ordinal;
int o2 = b2.getResult().ordinal;
if (descending) {
return o2 - o1;
} else {
return o1 - o2;
}
}
return 0;
}
}

}
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2012 Sony Mobile Communications AB. All rights reserved.
* Copyright (c) 2010, 2014 Sony Mobile Communications Inc. 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
Expand Down Expand Up @@ -182,8 +181,15 @@ public synchronized void onStarted(AbstractBuild r, TaskListener listener) {
if (!cause.isSilentMode()) {
memory.started(cause.getEvent(), r);
updateTriggerContexts(r);
BuildsStartedStats stats = memory.getBuildsStartedStats(cause.getEvent());
NotificationFactory.getInstance().queueBuildStarted(r, listener, cause.getEvent(), stats);
GerritTrigger trigger = GerritTrigger.getTrigger(r.getProject());
boolean silentStartMode = false;
if (trigger != null) {
silentStartMode = trigger.isSilentStartMode();
}
if (!silentStartMode) {
BuildsStartedStats stats = memory.getBuildsStartedStats(cause.getEvent());
NotificationFactory.getInstance().queueBuildStarted(r, listener, cause.getEvent(), stats);
}
}
logger.info("Gerrit build [{}] Started for cause: [{}].", r, cause);
logger.info("MemoryStatus:\n{}", memory.getStatusReport(cause.getEvent()));
Expand Down
Expand Up @@ -28,21 +28,23 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.TriggerContext;
import static com.sonyericsson.hudson.plugins.gerrit.trigger.utils.Logic.shouldSkip;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Result;
import jenkins.model.Jenkins;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.sonyericsson.hudson.plugins.gerrit.trigger.utils.Logic.shouldSkip;

/**
* Keeps track of what builds have been triggered and if all builds are done for specific events.
Expand Down Expand Up @@ -794,6 +796,20 @@ private void setBuildCompleted(boolean buildCompleted) {
this.buildCompleted = buildCompleted;
}

@Override
public String toString() {
String s;
if (getBuild() != null) {
s = getBuild().toString();
} else {
s = getProject().getName();
}
if (isBuildCompleted()) {
s += " (completed)";
}
return s;
}

}
}
}
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2012 Sony Mobile Communications AB. All rights reserved.
* Copyright (c) 2010, 2014 Sony Mobile Communications Inc. All rights reserved.
* Copyright (c) 2014, CloudBees, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -136,6 +135,7 @@ public class GerritTrigger extends Trigger<AbstractProject> {
private Integer gerritBuildNotBuiltCodeReviewValue;
private boolean silentMode;
private String notificationLevel;
private boolean silentStartMode;
private boolean escapeQuotes;
private boolean noNameAndEmailParameters;
private String dependencyJobsNames;
Expand Down Expand Up @@ -195,6 +195,7 @@ public class GerritTrigger extends Trigger<AbstractProject> {
* Job specific Gerrit code review vote when a build is not built, null means
* that the global value should be used.
* @param silentMode Silent Mode on or off.
* @param silentStartMode Silent Start Mode on or off.
* @param escapeQuotes EscapeQuotes on or off.
* @param noNameAndEmailParameters Whether to create parameters containing name and email
* @param readableMessage Human readable message or not.
Expand All @@ -206,7 +207,7 @@ public class GerritTrigger extends Trigger<AbstractProject> {
* @param buildNotBuiltMessage Message to write to Gerrit when all builds are not built
* @param buildUnsuccessfulFilepath Filename to retrieve Gerrit comment message from, in the case of an
* unsuccessful build.
* @param customUrl Custom URL to sen to Gerrit instead of build URL
* @param customUrl Custom URL to send to Gerrit instead of build URL
* @param serverName The selected server
* @param gerritSlaveId The selected slave associated to this job, if enabled in server configs
* @param triggerOnEvents The list of event types to trigger on.
Expand All @@ -231,6 +232,7 @@ public GerritTrigger(
Integer gerritBuildNotBuiltVerifiedValue,
Integer gerritBuildNotBuiltCodeReviewValue,
boolean silentMode,
boolean silentStartMode,
boolean escapeQuotes,
boolean noNameAndEmailParameters,
boolean readableMessage,
Expand Down Expand Up @@ -262,6 +264,7 @@ public GerritTrigger(
this.gerritBuildNotBuiltVerifiedValue = gerritBuildNotBuiltVerifiedValue;
this.gerritBuildNotBuiltCodeReviewValue = gerritBuildNotBuiltCodeReviewValue;
this.silentMode = silentMode;
this.silentStartMode = silentStartMode;
this.escapeQuotes = escapeQuotes;
this.noNameAndEmailParameters = noNameAndEmailParameters;
this.readableMessage = readableMessage;
Expand Down Expand Up @@ -364,7 +367,7 @@ private void addThisTriggerAsListener(AbstractProject project) {
if (plugin != null) {
GerritHandler handler = plugin.getHandler();
if (handler != null) {
handler.addListener(new EventListener(project));
handler.addListener(createListener(project));
} else {
logger.warn("The plugin has no handler instance (BUG)! Project {} will not be triggered!",
project.getFullDisplayName());
Expand All @@ -375,6 +378,24 @@ private void addThisTriggerAsListener(AbstractProject project) {
}
}

/**
* Creates an {@link EventListener} for the provided project.
* @param project the project
* @return a new listener instance
*/
/*package*/ static EventListener createListener(AbstractProject project) {
return new EventListener(project);
}

/**
* Creates an {@link EventListener} for this trigger's job.
* @return a new listener instance.
* @see #createListener(hudson.model.AbstractProject)
*/
/*package*/ EventListener createListener() {
return createListener(job);
}

@Override
public void start(AbstractProject project, boolean newInstance) {
logger.debug("Start project: {}", project);
Expand Down Expand Up @@ -406,7 +427,7 @@ public void stop() {
GerritProjectList.removeTriggerFromProjectList(this);
super.stop();
try {
removeListener();
removeListener();
} catch (IllegalStateException e) {
logger.error("I am too late!", e);
}
Expand All @@ -422,13 +443,13 @@ private void removeListener() {
if (plugin != null) {
if (PluginImpl.getInstance().getHandler() != null) {
if (job != null) {
PluginImpl.getInstance().getHandler().removeListener(new EventListener(job));
PluginImpl.getInstance().getHandler().removeListener(createListener());
}
} else {
logger.error("The Gerrit handler has not been initialized. BUG!");
}
} else {
logger.error("The plugin instance could not be found");
logger.error("The plugin instance could not be found");
}
}

Expand Down Expand Up @@ -486,7 +507,7 @@ private boolean shouldTriggerOnEventType(GerritTriggeredEvent event) {
*/
@Deprecated
protected void schedule(GerritCause cause, GerritTriggeredEvent event) {
new EventListener(job).schedule(this, cause, event, job);
createListener().schedule(this, cause, event, job);
}

/**
Expand All @@ -500,7 +521,7 @@ protected void schedule(GerritCause cause, GerritTriggeredEvent event) {
*/
@Deprecated
protected void schedule(GerritCause cause, GerritTriggeredEvent event, AbstractProject project) {
new EventListener(job).schedule(this, cause, event, project);
createListener().schedule(this, cause, event, project);
}

/**
Expand All @@ -514,7 +535,7 @@ protected void schedule(GerritCause cause, GerritTriggeredEvent event, AbstractP
*/
@Deprecated
protected ParametersAction createParameters(GerritTriggeredEvent event, AbstractProject project) {
return new EventListener(job).createParameters(event, project);
return createListener().createParameters(event, project);
}


Expand Down Expand Up @@ -1186,6 +1207,16 @@ public boolean isSilentMode() {
return silentMode;
}

/**
* If silent start mode is on or off. When silent start mode is on there will be no 'build started' message back
* to Gerrit. Default is false.
*
* @return true if silent start mode is on.
*/
public boolean isSilentStartMode() {
return silentStartMode;
}

/**
* Returns whom to notify.
*
Expand Down Expand Up @@ -1323,6 +1354,16 @@ public void setSilentMode(boolean silentMode) {
this.silentMode = silentMode;
}

/**
* Sets silent start mode to on or off. When silent start mode is on there will be no 'silent start' message
* back to Gerrit. Default is false.
*
* @param silentStartMode true if silent start mode should be on.
*/
public void setSilentStartMode(boolean silentStartMode) {
this.silentStartMode = silentStartMode;
}

/**
* Sets the value for whom to notify.
*
Expand Down Expand Up @@ -1664,7 +1705,6 @@ public FormValidation doUrlCheck(
if (value == null || value.isEmpty()) {
return FormValidation.error(Messages.EmptyError());
}

try {
URL url = new URL(value); // Check for protocol errors
url.toURI(); // Perform some extra checking
Expand Down Expand Up @@ -1828,7 +1868,6 @@ private void cancelJob(ParametersAction parameters) {
}
}
}

// Interrupt any currently running jobs.
for (Computer c : Hudson.getInstance().getComputers()) {
List<Executor> executors = new ArrayList<Executor>();
Expand Down
Expand Up @@ -189,8 +189,9 @@ private IGerritHudsonTriggerConfig getConfig() {
*/
public void triggerUnreviewedPatches(GerritTrigger trigger) {
if (trigger != null && trigger.isAllowTriggeringUnreviewedPatches()) {
EventListener listener = trigger.createListener();
for (PatchsetCreated event : this.events) {
trigger.gerritEvent(event);
listener.gerritEvent(event);
}
}
}
Expand Down

0 comments on commit 3084c3e

Please sign in to comment.