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

adds feature to alert user if upstream project is blocked, disabled o… #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.JobView;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.JobViews;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor.FormException;
import hudson.model.Job;
import hudson.model.ListView;
Expand Down Expand Up @@ -142,12 +143,17 @@ private boolean isGiven(String value) {
}

private List<JobView> jobViews() {
JobViews views = new JobViews(new StaticJenkinsAPIs(), currentConfig());

//A little bit of evil to make the type system happy.
@SuppressWarnings("unchecked")
List<Job<?, ?>> projects = new ArrayList(filter(super.getItems(), Job.class));

//start of sin
@SuppressWarnings("unchecked")
List<AbstractProject<?, ?>> ExportedProjectsToJobViews = new ArrayList(filter(super.getItems(), Job.class));
//end if sin.
List<JobView> jobs = new ArrayList<JobView>();
//Dear code reviewer, forgive me for what I have done.
JobViews views = new JobViews(new StaticJenkinsAPIs(), currentConfig(),ExportedProjectsToJobViews);

Collections.sort(projects, currentConfig().getOrder());

Expand Down Expand Up @@ -192,7 +198,6 @@ private boolean deserailisingFromAnOlderFormat() {
private void migrateFromOldToNewConfigFormat() {
Config c = new Config();
c.setOrder(order);

config = c;
order = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel;

import com.smartcodeltd.jenkinsci.plugins.buildmonitor.Config;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.facade.StaticJenkinsAPIs;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.features.*;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.features.headline.HeadlineConfig;
import hudson.model.AbstractProject;
import hudson.model.Job;

import java.util.List;
Expand All @@ -18,20 +20,22 @@ public class JobViews {
public static final String Groovy_Post_Build = "groovy-postbuild";

private final StaticJenkinsAPIs jenkins;
private final List<AbstractProject<?, ?>> projects;
private final com.smartcodeltd.jenkinsci.plugins.buildmonitor.Config config;

public JobViews(StaticJenkinsAPIs jenkins, com.smartcodeltd.jenkinsci.plugins.buildmonitor.Config config) {
public JobViews(StaticJenkinsAPIs jenkins, Config config, List<AbstractProject<?, ?>> project) {
this.jenkins = jenkins;
this.config = config;
this.projects = project;
}

public JobView viewOf(Job<?, ?> job) {
List<Feature> viewFeatures = newArrayList();

// todo: a more elegant way of assembling the features would be nice
viewFeatures.add(new HasHeadline(new HeadlineConfig(config.shouldDisplayCommitters())));
viewFeatures.add(new KnowsLastCompletedBuildDetails());
viewFeatures.add(new KnowsCurrentBuildsDetails());
viewFeatures.add(new showIfUpstreamProjectFails(projects));

if (jenkins.hasPlugin(Claim)) {
viewFeatures.add(new CanBeClaimed());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.features;

import org.codehaus.jackson.annotate.JsonValue;

/**
* @author Vincent & Robert
*/
public class UpStreamError {
private final String value;

public UpStreamError(String value) {
this.value = value;
}
@JsonValue
public String value() {
return value;
}

public UpStreamError asJson() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.features;

import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.JobView;
import hudson.model.AbstractProject;

import java.util.List;

/**
* Created by vincent on 2017-04-28.
*/

public class showIfUpstreamProjectFails implements Feature<UpStreamError> {
private List<AbstractProject<?, ?>> project;

private JobView job;

public showIfUpstreamProjectFails(List<AbstractProject<?, ?>> project) {
this.project = project;
}

public UpStreamError asJson() {
return new UpStreamError(checkAllUpstreamProjects(project));
}

@Override
public showIfUpstreamProjectFails of(JobView jobView) {
this.job = jobView;
return this;
}

private String RecursiveUpstreamCheck(List<AbstractProject> upstreamProjects) {
for (AbstractProject upstreamProject : upstreamProjects) {
if (!upstreamProject.getUpstreamProjects().isEmpty()) {
return RecursiveUpstreamCheck(upstreamProject.getUpstreamProjects());
}
if (upstreamProject.isBuilding()) {
return "";
} else if (upstreamProject.isDisabled()) {
return "project has build in chain that is disabled. name: '"+upstreamProject.getName()+"'";
} else if (upstreamProject.isBuildBlocked()) {
return "Project can't run due to project with name: '"+upstreamProject.getName()+"' in upstream chain being blocked, Reason: " + upstreamProject.getWhyBlocked();
}
//because jenkins doesn't natively have a way to check if a build has failed so I have improvised.
else if (upstreamProject.getBuildStatusUrl().equals("red.png")) {
return "project in upstream chain has failed, check Build: '" + upstreamProject.getName() + "'";
}
}
return "";
}

private String checkAllUpstreamProjects(List<AbstractProject<?, ?>> project) {
String checkRed = "";
for (AbstractProject<?, ?> aProject : project) {
if (!aProject.getUpstreamProjects().isEmpty() && aProject.getFullName().equals(job.name())) {
//getUpstreamProjects happened to not check them recursively, so I made my own.
List<AbstractProject> ups = aProject.getUpstreamProjects();
checkRed = RecursiveUpstreamCheck(ups);
}
}
return checkRed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
id="settings-colour-blind" type="checkbox" />
<label for="settings-colour-blind" title="Applies a colour blind-friendly colour scheme">Colour blind mode?</label>
</li>
<li class="settings-option">
<input ng-model="settings.displayWarnings"
ng-false-value="'0'"
ng-true-value="'1'"
id="settings-displayWarnings" type="checkbox" />
<label for="settings-displayWarnings" title="shows wanrings if a job in upstream has failed or is blocked to its children.">Alert if upstream job has error?</label>
</li>
<li class="settings-option">
<input ng-model="settings.reduceMotion"
ng-false-value="'0'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
>
<span class="value">{{project.progress}}%</span>
</div>

<header>
<h2>
<a title="{{project.name}}"
Expand Down Expand Up @@ -42,6 +41,9 @@
{{ badge.text }}
</span>
</li>
<li style="color:#EC644B" data-ng-show="project.upStreamError.length >0 &amp;&amp; settings.displayWarnings == 1">
{{project.upStreamError}}
</li>
</ul>
</header>

Expand Down
1 change: 1 addition & 0 deletions build-monitor-plugin/src/main/webapp/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ angular.
$scope.settings.colourBlind = cookieJar.get('colourBlind', 0);
$scope.settings.reduceMotion = cookieJar.get('reduceMotion', 0);
$scope.settings.showBadges = cookieJar.get('showBadges', 0);
$scope.settings.showBadges = cookieJar.get('displayWarnings', 0);

angular.forEach($scope.settings, function(value, name) {
$scope.$watch('settings.' + name, function(currentValue) {
Expand Down