Skip to content

Commit

Permalink
JENKINS-21411 Shift (Prev/Next) for diff results
Browse files Browse the repository at this point in the history
Both sides goes to the next or previous entry (shift)
  • Loading branch information
Stefan Brausch committed Feb 6, 2014
1 parent 833cd84 commit 869921d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 20 deletions.
Expand Up @@ -15,7 +15,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;

import javax.servlet.ServletException;
import javax.xml.transform.Source;
Expand Down Expand Up @@ -174,6 +177,24 @@ public final void doDiffFiles(StaplerRequest req, StaplerResponse rsp)
rsp.sendRedirect("showDiffFiles?timestamp1=" + parser.get("timestamp1")
+ "&timestamp2=" + parser.get("timestamp2"));
}


/**
* Action when 'Prev' or 'Next' button in showDiffFiles.jelly is pressed.
* Forwards to the previous or next diff.
* @param req StaplerRequest created by pressing the button
* @param rsp Outgoing StaplerResponse
* @throws IOException If XML file can't be read
*/
public final void doDiffFilesPrevNext(StaplerRequest req, StaplerResponse rsp)
throws IOException {
final String timestamp1 = req.getParameter("timestamp1");
final String timestamp2 = req.getParameter("timestamp2");
rsp.sendRedirect("showDiffFiles?timestamp1=" + timestamp1
+ "&timestamp2=" + timestamp2);
}


/**
* Used in the Difference jelly only. Returns one of the two timestamps that
Expand Down Expand Up @@ -218,7 +239,57 @@ public final String getOperation(int timestampNumber) {
.get(getTimestamp(timestampNumber)).getOperation();
}

/**
* Used in the Difference jelly only. Returns the next timestamp of
* the next entry of the two Files A and B. timestampNumber decides which
* file exactly.
*
* @param timestampNumber
* 1 for File A, 2 for File B
* @return the timestamp of the next entry as String.
*/
public final String getNextTimestamp(int timestampNumber) {
checkConfigurePermission();
final String timestamp = this.getRequestParameter("timestamp" + timestampNumber);
final SortedMap <String, HistoryDescr> revisions = getHistoryDao().getRevisions(this.project.getConfigFile());
final Iterator<Entry<String, HistoryDescr>> itr = revisions.entrySet().iterator();
while(itr.hasNext()) {
if (itr.next().getValue().getTimestamp().equals((String)timestamp)) {
if (itr.hasNext()){
return itr.next().getValue().getTimestamp();
}
}
}
//no next entry found
return timestamp;
}

/**
* Used in the Difference jelly only. Returns the previous timestamp of
* the next entry of the two Files A and B. timestampNumber decides which
* file exactly.
*
* @param timestampNumber
* 1 for File A, 2 for File B
* @return the timestamp of the preious entry as String.
*/
public final String getPrevTimestamp(int timestampNumber) {
checkConfigurePermission();
final String timestamp = this.getRequestParameter("timestamp" + timestampNumber);
final SortedMap <String, HistoryDescr> revisions = getHistoryDao().getRevisions(this.project.getConfigFile());
final Iterator<Entry<String, HistoryDescr>> itr = revisions.entrySet().iterator();
String prevTimestamp = timestamp;
while(itr.hasNext()) {
final String checkTimestamp = itr.next().getValue().getTimestamp();
if (checkTimestamp.equals((String)timestamp)) {
return prevTimestamp;
}
else prevTimestamp = checkTimestamp;
}
//no previous entry found
return timestamp;
}

/**
* Takes the two timestamp request parameters and returns the diff between the corresponding
* config files of this project as a list of single lines.
Expand Down
Expand Up @@ -13,24 +13,32 @@
</j:when>
<j:otherwise>
<div>
<j:choose>
<j:when test="${it.getLines().size() == 0}">
<p>${%No lines changed}</p>
</j:when>
<j:otherwise>
<table style="width:100%;">
<thead>
<tr>
<td colspan="2"> <font size="3"> File A </font></td>
<td colspan="2"> <font size="3"> File B </font></td>
</tr>
<tr>
<td colspan="2"> ${it.getTimestamp(1)}; ${it.getOperation(1)}; <a href="${rootURL}/user/${it.getUser(1)}">${it.getUser(1)}</a> </td>
<td colspan="2"> ${it.getTimestamp(2)}; ${it.getOperation(2)}; <a href="${rootURL}/user/${it.getUser(2)}">${it.getUser(2)}</a> </td>
</tr>
</thead>
<j:set var="prev1" value="${it.getPrevTimestamp(1)}"/>
<j:set var="prev2" value="${it.getPrevTimestamp(2)}"/>
<j:set var="next1" value="${it.getNextTimestamp(1)}"/>
<j:set var="next2" value="${it.getNextTimestamp(2)}"/>
<table style="width:100%;">
<thead>
<tr>
<td colspan="2"> <font size="3"> File A </font></td>
<td colspan="2"> <font size="3"> File B </font></td>
</tr>
<tr>
<td colspan="2"> ${it.getTimestamp(1)}; ${it.getOperation(1)}; <a href="${rootURL}/user/${it.getUser(1)}">${it.getUser(1)}</a> </td>
<td colspan="2"> ${it.getTimestamp(2)}; ${it.getOperation(2)}; <a href="${rootURL}/user/${it.getUser(2)}">${it.getUser(2)}</a> </td>
</tr>
</thead>
<tbody style="outline: 1pt solid #B2B2B2;">
<j:forEach items="${it.getLines()}" var="line">
<j:choose>
<j:when test="${it.getLines().size() == 0}">
<tr>
<td colspan="4">
<p>${%No lines changed}</p>
</td>
</tr>
</j:when>
<j:otherwise>
<j:forEach items="${it.getLines()}" var="line">
<tr>
<j:choose>
<j:when test="${line.skipping}">
Expand All @@ -48,8 +56,31 @@
</j:choose>
</tr>
</j:forEach>
</tbody>

</j:otherwise>
</j:choose>
</tbody>
</table>
<div align="right">
<table>
<tr>
<td>
<j:if test="${prev1 != prev2}">
<f:form method="post" action="diffFilesPrevNext?timestamp1=${prev1}&amp;timestamp2=${prev2}" name="prevEntry" >
<f:submit value="&lt; ${%Prev}" />
</f:form>
</j:if>
</td>
<td>
<j:if test="${next1 != next2}">
<f:form method="post" action="diffFilesPrevNext?timestamp1=${next1}&amp;timestamp2=${next2}" name="nextEntry" >
<f:submit value="${%Next} &gt;" />
</f:form>
</j:if>
</td>
</tr>
</table>
</div>
<div align="right">
<table>
<tr>
Expand All @@ -66,8 +97,6 @@
</tr>
</table>
</div>
</j:otherwise>
</j:choose>
</div>
</j:otherwise>
</j:choose>
Expand Down

0 comments on commit 869921d

Please sign in to comment.