Skip to content

Commit

Permalink
Merge pull request #55 from Jochen-A-Fuerbacher/JENKINS-34151
Browse files Browse the repository at this point in the history
[JENKINS-34151] Solution to show badges on pipeline projects.
  • Loading branch information
Jochen-A-Fuerbacher committed Apr 18, 2016
2 parents 114a7bb + 457cfef commit 0b97aaa
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 278 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -58,6 +58,12 @@
<email>iordan.boev@gmail.com</email>
<timezone>+1</timezone>
</developer>
<developer>
<id>Jochen-A-Fuerbacher</id>
<name>Jochen A. Fuerbacher</name>
<email>jochen.fuerbacher@1und1.de</email>
<timezone>+1</timezone>
</developer>
</developers>


Expand Down
266 changes: 133 additions & 133 deletions src/main/java/hudson/plugins/jobConfigHistory/HistoryDescr.java
@@ -1,133 +1,133 @@
/*
* The MIT License
*
* Copyright 2013 Stefan Brausch.
*
* 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 hudson.plugins.jobConfigHistory;

import java.util.Date;

/**
* Holder for information about an altering operation saved to {@link JobConfigHistoryConsts#HISTORY_FILE}.
*
* @author Stefan Brausch
*/
public class HistoryDescr implements ParsedDate {

public static final HistoryDescr EMPTY_HISTORY_DESCR = new HistoryDescr(null, null, null, null, null, null);

/** Display name of the user doing the operation. */
private final String user;

/** Id of the user doing the operation. */
private final String userId;

/** Name of the operation. */
private final String operation;

/** Timestamp of the operation, see {@link JobConfigHistoryConsts#ID_FORMATTER}. */
private final String timestamp;

/** Current name of the job after renaming. */
private final String currentName;

/** Old name of the job before renaming.*/
private final String oldName;

/**
* @param user
* display name of the user doing the operation
* @param userId
* id of the user doing the operation
* @param operation
* name of the operation
* @param timestamp
* timestamp of the operation
*/
public HistoryDescr(String user, String userId, String operation, String timestamp, String currentName, String oldName) {
this.user = user;
this.userId = userId;
this.operation = operation;
this.timestamp = timestamp;
this.currentName = currentName;
this.oldName = oldName;
}

/**
* Returns display name of the user doing the operation.
*
* @return display name of the user
*/
public String getUser() {
return user;
}

/**
* Returns id of the user doing the operation.
*
* @return id of the user
*/
public String getUserID() {
return userId;
}

/**
* Returns name of the operation.
*
* @return name of the operation
*/
public String getOperation() {
return operation;
}

/**
* Returns timestamp of the operation.
*
* @return timestamp
*/
public String getTimestamp() {
return timestamp;
}

/**
* Returns a {@link Date}.
*
* @return The parsed date as a java.util.Date.
*/
@Override
public Date parsedDate() {
return PluginUtils.parsedDate(getTimestamp());
}

/**
* Returns the current job name after renaming.
*/
public String getCurrentName() {
return currentName;
}

/**
* Returns the old job name before renaming.
*/
public String getOldName() {
return oldName;
}
}
/*
* The MIT License
*
* Copyright 2013 Stefan Brausch.
*
* 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 hudson.plugins.jobConfigHistory;

import java.util.Date;

/**
* Holder for information about an altering operation saved to {@link JobConfigHistoryConsts#HISTORY_FILE}.
*
* @author Stefan Brausch
*/
public class HistoryDescr implements ParsedDate {

public static final HistoryDescr EMPTY_HISTORY_DESCR = new HistoryDescr(null, null, null, null, null, null);

/** Display name of the user doing the operation. */
private final String user;

/** Id of the user doing the operation. */
private final String userId;

/** Name of the operation. */
private final String operation;

/** Timestamp of the operation, see {@link JobConfigHistoryConsts#ID_FORMATTER}. */
private final String timestamp;

/** Current name of the job after renaming. */
private final String currentName;

/** Old name of the job before renaming.*/
private final String oldName;

/**
* @param user
* display name of the user doing the operation
* @param userId
* id of the user doing the operation
* @param operation
* name of the operation
* @param timestamp
* timestamp of the operation
*/
public HistoryDescr(String user, String userId, String operation, String timestamp, String currentName, String oldName) {
this.user = user;
this.userId = userId;
this.operation = operation;
this.timestamp = timestamp;
this.currentName = currentName;
this.oldName = oldName;
}

/**
* Returns display name of the user doing the operation.
*
* @return display name of the user
*/
public String getUser() {
return user;
}

/**
* Returns id of the user doing the operation.
*
* @return id of the user
*/
public String getUserID() {
return userId;
}

/**
* Returns name of the operation.
*
* @return name of the operation
*/
public String getOperation() {
return operation;
}

/**
* Returns timestamp of the operation.
*
* @return timestamp
*/
public String getTimestamp() {
return timestamp;
}

/**
* Returns a {@link Date}.
*
* @return The parsed date as a java.util.Date.
*/
@Override
public Date parsedDate() {
return PluginUtils.parsedDate(getTimestamp());
}

/**
* Returns the current job name after renaming.
*/
public String getCurrentName() {
return currentName;
}

/**
* Returns the old job name before renaming.
*/
public String getOldName() {
return oldName;
}
}
Expand Up @@ -28,10 +28,9 @@
import java.util.Date;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildBadgeAction;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
Expand All @@ -53,7 +52,7 @@ public class JobConfigBadgeAction implements BuildBadgeAction, RunAction2 {
/**
* We need the build in order to get the project name.
*/
private transient AbstractBuild<?, ?> build;
private transient Run<?, ?> build;

/**
* A transient field, which allows to workaround the JENKINS-20511 issue
Expand All @@ -75,31 +74,31 @@ public class JobConfigBadgeAction implements BuildBadgeAction, RunAction2 {

@Override
public void onAttached(Run<?, ?> r) {
build = (AbstractBuild<?, ?>) r;
build = r;

}

@Override
public void onLoad(Run<?, ?> r) {
build = (AbstractBuild<?, ?>) r;
build = r;
}

/**
* Listener.
*/
@Extension
public static class Listener extends RunListener<AbstractBuild<?, ?>> {
public static class Listener extends RunListener<Run <?, ?>> {

@Override
public void onStarted(AbstractBuild<?, ?> build, TaskListener listener) {
final AbstractProject<?, ?> project = build.getProject();
public void onStarted(Run<?, ?> build, TaskListener listener) {
final Job<?, ?> project = build.getParent();
if (project.getNextBuildNumber() <= 2) {
super.onStarted(build, listener);
return;
}

Date lastBuildDate = null;
final AbstractBuild lastBuild = project.getLastBuild();
final Run<?,?> lastBuild = project.getLastBuild();
if (lastBuild != null && lastBuild.getPreviousBuild() != null) {
lastBuildDate = lastBuild.getPreviousBuild().getTime();
}
Expand All @@ -124,7 +123,7 @@ public void onStarted(AbstractBuild<?, ?> build, TaskListener listener) {
* @param project to inspect.
* @return list of revisions
*/
List<HistoryDescr> getRevisions(final AbstractProject<?, ?> project) {
List<HistoryDescr> getRevisions(final Job<?, ?> project) {
final HistoryDao historyDao = PluginUtils.getHistoryDao();
final ArrayList<HistoryDescr> historyDescriptions = new ArrayList<HistoryDescr>(
historyDao.getRevisions(project.getConfigFile()).values());
Expand Down Expand Up @@ -158,7 +157,7 @@ private String findLastRelevantConfigChangeDate(List<HistoryDescr> historyDescri
* @return True if badges should appear.
*/
public boolean showBadge() {
return getPlugin().showBuildBadges(build.getProject());
return getPlugin().showBuildBadges(build.getParent());
}

/**
Expand All @@ -168,7 +167,7 @@ public boolean showBadge() {
*/
public boolean oldConfigsExist() {
final HistoryDao historyDao = getHistoryDao();
final AbstractProject<?, ?> project = build.getProject();
final Job<?, ?> project = build.getParent();
for (String timestamp : configDates) {
if (!historyDao.hasOldRevision(project.getConfigFile(), timestamp)) {
return false;
Expand All @@ -183,7 +182,7 @@ public boolean oldConfigsExist() {
* @return Link target as String.
*/
public String createLink() {
return getRootUrl() + build.getProject().getUrl()
return getRootUrl() + build.getParent().getUrl()
+ JobConfigHistoryConsts.URLNAME + "/showDiffFiles?timestamp1=" + configDates[1]
+ "&timestamp2=" + configDates[0];
}
Expand Down
Expand Up @@ -39,6 +39,7 @@
import hudson.model.Descriptor.FormException;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Saveable;
import hudson.model.TopLevelItem;
import hudson.util.FormValidation;
Expand Down Expand Up @@ -279,7 +280,7 @@ public void setShowBuildBadges(String showBadges) {
* @param project The project to which the build history belongs.
* @return False if the option is set to 'never' or the user doesn't have the required permissions.
*/
public boolean showBuildBadges(AbstractProject<?, ?> project) {
public boolean showBuildBadges(Job<?, ?> project) {
if ("always".equals(showBuildBadges)) {
return true;
} else if ("userWithConfigPermission".equals(showBuildBadges) && project.hasPermission(Item.CONFIGURE)) {
Expand Down

0 comments on commit 0b97aaa

Please sign in to comment.