Skip to content

Commit

Permalink
analyse the full git commit message where present.
Browse files Browse the repository at this point in the history
  • Loading branch information
scarytom committed Jul 14, 2017
1 parent b34914b commit cea811e
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 37 deletions.
23 changes: 12 additions & 11 deletions src/main/java/org/netmelody/cieye/spies/jenkins/JobAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public TargetDetail analyse(Job jobDigest) {
return new TargetDetail(job.url, job.url, job.name, statusOf(job), startTimeOf(job), buildsFor(job), sponsorsOf(job));
}

public String lastBadBuildUrl() {
return communicator.lastBadBuildFor(jobEndpoint);
}

private long startTimeOf(JobDetail job) {
return buildStartTimeFetcher.lastStartTimeOf(job);
}
Expand Down Expand Up @@ -141,20 +137,25 @@ private String commitMessagesOf(BuildDetail build) {

final StringBuilder result = new StringBuilder();
for (ChangeSetItem changeSetItem : build.changeSet.items) {
result.append(changeSetItem.user);
result.append(' ');
result.append(changeSetItem.msg);
result.append(' ');
appendIfNotNull(result, changeSetItem.user);
appendIfNotNull(result, changeSetItem.msg);
appendIfNotNull(result, changeSetItem.comment);
}

for (User user : build.culprits()) {
result.append(user.fullName);
result.append(' ');
appendIfNotNull(result, user.fullName);
}

return result.toString();
}


private static void appendIfNotNull(StringBuilder buffer, String string) {
if (string != null) {
buffer.append(string);
buffer.append(' ');
}
}

private List<RunningBuild> buildsFor(final JobDetail job) {
if (!job.building() || job.lastBuild == null) {
return newArrayList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.netmelody.cieye.spies.jenkins.test;

import com.google.common.base.Functions;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Matchers;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;
import org.netmelody.cieye.core.domain.Sponsor;
import org.netmelody.cieye.core.domain.TargetDetail;
import org.netmelody.cieye.core.observation.KnownOffendersDirectory;
import org.netmelody.cieye.server.observation.protocol.JsonRestRequester;
import org.netmelody.cieye.server.observation.test.StubGrapeVine;
import org.netmelody.cieye.spies.jenkins.JenkinsCommunicator;
import org.netmelody.cieye.spies.jenkins.JobAnalyser;
import org.netmelody.cieye.spies.jenkins.jsondomain.Job;
import org.netmelody.cieye.spies.jenkins.jsondomain.JobDetail;
import org.netmelody.cieye.spies.jenkins.jsondomain.Server;
import org.netmelody.cieye.spies.jenkins.jsondomain.ViewDetail;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

public final class JobAnalyserTest {
private final Mockery context = new Mockery();

private final KnownOffendersDirectory detective = context.mock(KnownOffendersDirectory.class);
private final StubGrapeVine channel = new StubGrapeVine();

private JobAnalyser analyser;

@Before
public void setup() {
analyser = new JobAnalyser(new JenkinsCommunicator("http://jenkins", new JsonRestRequester(new Gson(), Functions.<String>identity(), channel)),
"http://jenkins/job/foo", detective);
}

@Test public void
canAnalyseAJenkinsJob() {
final Job job = jobDigest();

channel.respondingWith("http://jenkins/job/foo/api/json", contentFrom("jenkins_2.60.1_jobdetail_1.json"));
channel.respondingWith("http://jenkins/job/foo/1/api/json", contentFrom("jenkins_2.60.1_builddetail_1.json"));
channel.respondingWith("http://jenkins/job/foo/2/api/json", contentFrom("jenkins_2.60.1_builddetail_2.json"));

context.checking(new Expectations() {{
allowing(detective).search(with(containsString("magic")));
will(returnValue(Sets.newHashSet(new Sponsor("Bob"))));
}});

TargetDetail analysis = analyser.analyse(job);
context.assertIsSatisfied();

Set<Sponsor> sponsors = analysis.sponsors();
assertThat(sponsors, is(Matchers.<Sponsor>iterableWithSize(1)));
assertThat(sponsors.iterator().next().name(), is("Bob"));
}

private Job jobDigest() {
final Job job = new Job();
job.url = "http://jenkins/job/foo";
return job;
}

private String contentFrom(String resourceName) {
try {
return IOUtils.toString(JobAnalyserTest.class.getResourceAsStream(resourceName));
}
catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"number": 1,
"result": "SUCCESS",
"timestamp": 1500026020084,
"url": "http://localhost:8080/job/foo/1/",
"url": "http://jenkins/job/foo/1/",
"builtOn": "",
"changeSet": {
"items": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
"number": 2,
"result": "SUCCESS",
"timestamp": 1500026152671,
"url": "http://localhost:8080/job/foo/2/",
"url": "http://jenkins/job/foo/2/",
"builtOn": "",
"changeSet": {
"items": [
{
"author": {
"absoluteUrl": "http://localhost:8080/user/tom.denley",
"absoluteUrl": "http://jenkins/user/tom.denley",
"fullName": "tom.denley"
},
"comment": "add some example json from jenkins for testing\n",
Expand Down Expand Up @@ -88,7 +88,7 @@
},
"culprits": [
{
"absoluteUrl": "http://localhost:8080/user/tom.denley",
"absoluteUrl": "http://jenkins/user/tom.denley",
"fullName": "tom.denley"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"queueId": 81942,
"result": "SUCCESS",
"timestamp": 1500028059527,
"url": "http://jenkins.youdevise.com/job/tomd_test_svn/1/",
"url": "http://jenkins/job/tomd_test_svn/1/",
"builtOn": "ci-jenkinsslavea-004",
"changeSet": {
"items": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"queueId": 82000,
"result": "SUCCESS",
"timestamp": 1500028480116,
"url": "http://jenkins.youdevise.com/job/tomd_test_svn/2/",
"url": "http://jenkins/job/tomd_test_svn/2/",
"builtOn": "ci-jenkinsslavee-002",
"changeSet": {
"items": [
Expand All @@ -39,7 +39,7 @@
"README.md"
],
"author": {
"absoluteUrl": "http://jenkins.youdevise.com/user/tdenley",
"absoluteUrl": "http://jenkins/user/tdenley",
"fullName": "tdenley"
},
"commitId": "133042",
Expand All @@ -66,7 +66,7 @@
},
"culprits": [
{
"absoluteUrl": "http://jenkins.youdevise.com/user/tdenley",
"absoluteUrl": "http://jenkins/user/tdenley",
"fullName": "tdenley"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"queueId": 1,
"result": "SUCCESS",
"timestamp": 1500023778296,
"url": "http://localhost:8080/job/foo/1/",
"url": "http://jenkins/job/foo/1/",
"builtOn": "",
"changeSet": {
"_class": "hudson.plugins.git.GitChangeSetList",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"queueId": 2,
"result": "SUCCESS",
"timestamp": 1500024430269,
"url": "http://localhost:8080/job/foo/2/",
"url": "http://jenkins/job/foo/2/",
"builtOn": "",
"changeSet": {
"_class": "hudson.plugins.git.GitChangeSetList",
Expand All @@ -85,11 +85,11 @@
"commitId": "cd90b94b651158d2ce923067bad99738b741228c",
"timestamp": 1500024223000,
"author": {
"absoluteUrl": "http://localhost:8080/user/tom.denley",
"absoluteUrl": "http://jenkins/user/tom.denley",
"fullName": "tom.denley"
},
"authorEmail": "tom.denley@timgroup.com",
"comment": "add some example json from jenkins for testing\n",
"comment": "add some example json from jenkins for testing\nand a second line of magic\n",
"date": "2017-07-14 10:23:43 +0100",
"id": "cd90b94b651158d2ce923067bad99738b741228c",
"msg": "add some example json from jenkins for testing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
"fullDisplayName": "foo",
"fullName": "foo",
"name": "foo",
"url": "http://localhost:8080/job/foo/",
"url": "http://jenkins/job/foo/",
"buildable": true,
"builds": [
{
"_class": "hudson.model.FreeStyleBuild",
"number": 2,
"url": "http://localhost:8080/job/foo/2/"
"url": "http://jenkins/job/foo/2/"
},
{
"_class": "hudson.model.FreeStyleBuild",
"number": 1,
"url": "http://localhost:8080/job/foo/1/"
"url": "http://jenkins/job/foo/1/"
}
],
"color": "blue",
"color": "red",
"firstBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 1,
"url": "http://localhost:8080/job/foo/1/"
"url": "http://jenkins/job/foo/1/"
},
"healthReport": [
{
Expand All @@ -46,26 +46,34 @@
"lastBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 2,
"url": "http://localhost:8080/job/foo/2/"
"url": "http://jenkins/job/foo/2/"
},
"lastCompletedBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 2,
"url": "http://localhost:8080/job/foo/2/"
"url": "http://jenkins/job/foo/2/"
},
"lastFailedBuild": null,
"lastStableBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 2,
"url": "http://localhost:8080/job/foo/2/"
"number": 1,
"url": "http://jenkins/job/foo/1/"
},
"lastSuccessfulBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 1,
"url": "http://jenkins/job/foo/1/"
},
"lastUnstableBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 2,
"url": "http://jenkins/job/foo/2/"
},
"lastUnsuccessfulBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 2,
"url": "http://localhost:8080/job/foo/2/"
"url": "http://jenkins/job/foo/2/"
},
"lastUnstableBuild": null,
"lastUnsuccessfulBuild": null,
"nextBuildNumber": 3,
"property": [],
"queueItem": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
{
"_class": "hudson.model.FreeStyleProject",
"name": "foo",
"url": "http://localhost:8080/job/foo/",
"url": "http://jenkins/job/foo/",
"color": "blue"
}
],
"name": "baz",
"property": [],
"url": "http://localhost:8080/view/baz/"
"url": "http://jenkins/view/baz/"
}

0 comments on commit cea811e

Please sign in to comment.