Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honour "allowExternalStatus" in TeamCity, be more generous with all-green, fixing fatal bug, work with Intellij 13 #30

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions ci-eye.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<sourceFolder url="file://$MODULE_DIR$/src/immersiontest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/webapp" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/webapp" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.netmelody.cieye.core.utility;

import java.util.regex.Pattern;

public class ProjectAbbreviator {

public String abbreviate(String projectName) {
StringBuilder out = new StringBuilder();
String[] nameParts = Pattern.compile("[-\\s]+").split(projectName);
if (nameParts.length == 1) {
return projectName;
} else {
for (String s : nameParts) {
out.append(s.charAt(0));
}
return out.toString();
}
}


public static String nameWithProjectAbbreviation(String projectName, String buildName) {
return new ProjectAbbreviator().abbreviate(projectName) + ": " + buildName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private PollingSpyHandler spyFor(Feature feature) {
private PollingSpyHandler createSpyFor(Feature feature) {
final ObservationAgency agency = foreignAgencies.agencyFor(feature.type());
final CiSpy spy = agency.provideSpyFor(feature, network, directory);
agencies.put(feature.type(), agency);
return new PollingSpyHandler(spy, feature);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package org.netmelody.cieye.spies.teamcity;

import static org.netmelody.cieye.core.domain.Percentage.percentageOf;
import org.netmelody.cieye.core.domain.RunningBuild;
import org.netmelody.cieye.core.domain.Sponsor;
import org.netmelody.cieye.core.domain.Status;
import org.netmelody.cieye.core.domain.TargetDetail;
import org.netmelody.cieye.core.observation.KnownOffendersDirectory;
import org.netmelody.cieye.spies.teamcity.jsondomain.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.netmelody.cieye.core.domain.RunningBuild;
import org.netmelody.cieye.core.domain.Sponsor;
import org.netmelody.cieye.core.domain.Status;
import org.netmelody.cieye.core.domain.TargetDetail;
import org.netmelody.cieye.core.observation.KnownOffendersDirectory;
import org.netmelody.cieye.spies.teamcity.jsondomain.Build;
import org.netmelody.cieye.spies.teamcity.jsondomain.BuildDetail;
import org.netmelody.cieye.spies.teamcity.jsondomain.BuildType;
import org.netmelody.cieye.spies.teamcity.jsondomain.BuildTypeDetail;
import org.netmelody.cieye.spies.teamcity.jsondomain.Change;
import org.netmelody.cieye.spies.teamcity.jsondomain.ChangeDetail;
import org.netmelody.cieye.spies.teamcity.jsondomain.Investigation;
import static org.netmelody.cieye.core.domain.Percentage.percentageOf;
import static org.netmelody.cieye.core.utility.ProjectAbbreviator.nameWithProjectAbbreviation;

public final class BuildTypeAnalyser {

Expand All @@ -29,12 +24,12 @@ public BuildTypeAnalyser(TeamCityCommunicator communicator, KnownOffendersDirect
this.communicator = communicator;
this.detective = detective;
}

public TargetDetail targetFrom(BuildType buildType) {
final BuildTypeDetail buildTypeDetail = communicator.detailsFor(buildType);

if (buildTypeDetail.paused) {
return new TargetDetail(communicator.endpoint() + buildType.href, buildType.webUrl(), buildType.name, Status.DISABLED, 0L);
if (buildTypeDetail.paused || buildTypeDetail.externalStatusDisabled()) {
return new TargetDetail(communicator.endpoint() + buildType.href, buildType.webUrl(), nameWithProjectAbbreviation(buildType.projectName, buildType.name), Status.DISABLED, 0L);
}

final Set<Sponsor> sponsors = new HashSet<Sponsor>();
Expand Down Expand Up @@ -67,7 +62,7 @@ public TargetDetail targetFrom(BuildType buildType) {
}
}

return new TargetDetail(communicator.endpoint() + buildType.href, buildType.webUrl(), buildType.name, currentStatus, startTime, runningBuilds, sponsors);
return new TargetDetail(communicator.endpoint() + buildType.href, buildType.webUrl(), nameWithProjectAbbreviation(buildType.projectName, buildType.name), currentStatus, startTime, runningBuilds, sponsors);
}

private Set<Sponsor> sponsorsOf(BuildDetail build) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class TeamCityCommunicator {
public TeamCityCommunicator(Contact contact, String endpoint) {
this.contact = contact;
this.endpoint = endpoint;
this.prefix = (contact.privileged() ? "/httpAuth" : "/guestAuth") + "/app/rest";
this.prefix = (contact.privileged() ? "/httpAuth" : "/guestAuth") + "/app/rest/7.0";
}

public String endpoint() {
Expand Down Expand Up @@ -91,18 +91,18 @@ public void commentOn(Build lastCompletedBuild, String note) {
public List<Change> changesOf(BuildDetail buildDetail) {
final JsonElement json = contact.makeJsonRestCall(endpoint + buildDetail.changes.href);
final JsonElement change = json.isJsonObject() ? json.getAsJsonObject().get("change") : JsonNull.INSTANCE;

if (null == change || !(change.isJsonArray() || change.isJsonObject())) {
return ImmutableList.of();
}

final Gson gson = new Gson();
final List<Change> changes = new ArrayList<Change>();
final Iterable<JsonElement> changesJson = change.isJsonArray() ? change.getAsJsonArray() : ImmutableList.of(change);
for (JsonElement jsonElement : changesJson) {
changes.add(gson.fromJson(jsonElement, Change.class));
}

return changes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
import static org.netmelody.cieye.core.domain.Status.UNKNOWN;
import static org.netmelody.cieye.core.utility.ProjectAbbreviator.nameWithProjectAbbreviation;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -42,7 +43,7 @@ public TargetDigestGroup targetsConstituting(Feature feature) {
final List<TargetDigest> digests = newArrayList();

for (BuildType buildType : buildTypes) {
final TargetDigest targetDigest = new TargetDigest(communicator.endpoint() + buildType.href, buildType.webUrl(), buildType.name, UNKNOWN);
final TargetDigest targetDigest = new TargetDigest(communicator.endpoint() + buildType.href, buildType.webUrl(), nameWithProjectAbbreviation(buildType.projectName, buildType.name), UNKNOWN);
digests.add(targetDigest);
recognisedBuildTypes.put(targetDigest.id(), buildType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.netmelody.cieye.spies.teamcity.jsondomain;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

import java.util.List;

import static java.lang.Boolean.parseBoolean;

public class BuildSettings {
public List<BuildSettingsProperty> property;

public boolean externalStatusDisabled() {
Optional<BuildSettingsProperty> allowExternalStatus = Iterables.tryFind(property, new Predicate<BuildSettingsProperty>() {
@Override
public boolean apply(BuildSettingsProperty input) {
return "allowExternalStatus".equals(input.name);
}
});

if (allowExternalStatus.isPresent()) {
return !Boolean.parseBoolean(allowExternalStatus.get().value);
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.netmelody.cieye.spies.teamcity.jsondomain;

public class BuildSettingsProperty {
public String name;
public String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public final class BuildTypeDetail extends BuildType {
public boolean paused;
public Project project;
public BuildsHref builds;
public BuildSettings settings;

public boolean externalStatusDisabled() {
return settings != null && settings.externalStatusDisabled();
}
//vcs-root
//parameters
//runParameters
Expand Down
12 changes: 10 additions & 2 deletions src/main/webapp/resources/cieye.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,19 @@ ORG.NETMELODY.CIEYE.newRadiatorWidget = function() {
}

var result = true;
$.each(targetsJson, function(index, targetJson) {
if (targetJson.builds.length !== 0 || (targetJson.status !== "GREEN" && targetJson.status !== "DISABLED")) {
$.each(targetsJson, function(i, targetJson) {
if (targetJson.status !== "GREEN" && targetJson.status !== "DISABLED") {
result = false;
return false;
}
if (targetJson.builds.length > 0) {
$.each(targetJson.builds, function (i2, build) {
if (build.status !== "GREEN") {
result = false;
return false;
}
});
}
});
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.netmelody.cieye.core.utility;

import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;

public class ProjectAbbreviatorTest {

@Test
public void doesNotAbbreviateIfNoSpaceInName() throws Throwable {
Assert.assertThat("myProjectName", equalTo(new ProjectAbbreviator().abbreviate("myProjectName")));
}

@Test
public void abbreviatesToFirstLettersOfWordsIfThereAreSpaces() throws Throwable {
Assert.assertThat("MpN", equalTo(new ProjectAbbreviator().abbreviate("My project Name")));
}

@Test
public void abbreviatesToFirstLettersOfWordsIfThereAreHyphens() throws Throwable {
Assert.assertThat("MpNh", equalTo(new ProjectAbbreviator().abbreviate("My-project Name-hyphens")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.netmelody.cieye.spies.teamcity.jsondomain;

import org.junit.Test;

import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class BuildSettingsTest {

@Test
public void isDisabledWhenAllowExternalStatusIsFalse() {
BuildSettings buildSettings = createBuildSettingsWithAllowExternalStatus("false");

assertThat(buildSettings.externalStatusDisabled(), equalTo(true));
}

@Test
public void isNotDisabledWhenAllowExternalStatusIsTrue() {
BuildSettings buildSettings = createBuildSettingsWithAllowExternalStatus("true");

assertThat(buildSettings.externalStatusDisabled(), equalTo(false));
}

@Test
public void isNotDisabledWhenAllowExternalStatusIsNotPresent() {
BuildSettings buildSettings = new BuildSettings();
buildSettings.property = newArrayList();

assertThat(buildSettings.externalStatusDisabled(), equalTo(false));
}

private BuildSettings createBuildSettingsWithAllowExternalStatus(String allowExternalStatus) {
BuildSettings buildSettings = new BuildSettings();
final BuildSettingsProperty property = new BuildSettingsProperty();
property.name = "allowExternalStatus";
property.value = allowExternalStatus;
buildSettings.property = newArrayList(property);
return buildSettings;
}
}