Skip to content

Commit

Permalink
Removed use of deprecated APIs for git BuildChooser
Browse files Browse the repository at this point in the history
Followup fix for JENKINS-16851 which is fixed in git plugin v. 1.2.0
this commit should remove any use of deprecated APIs after the
introduction of gitclient plugin.

Change-Id: Ibaa11fe9e06150555af675aae1a32f29cb15aab3
  • Loading branch information
rsandell committed Feb 21, 2013
1 parent 7e81119 commit 155ab67
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 60 deletions.
11 changes: 6 additions & 5 deletions gerrithudsontrigger/pom.xml
Expand Up @@ -63,9 +63,9 @@
<artifactId>gerrit-events</artifactId>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>1.1.10</version>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.sonyericsson.hudson.plugins.rebuild</groupId>
Expand Down Expand Up @@ -153,15 +153,16 @@
</pluginRepositories>
<build>
<plugins>
<!-- Uncomment to change contextPath -->
<!-- Uncomment to change contextPath and jenkinsHome-->
<!--<plugin>
<groupId>org.jvnet.hudson.tools</groupId>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<version>1.52</version>
<version>1.74</version>
<extensions>true</extensions>
<configuration>
<showDeprecation>true</showDeprecation>
<contextPath>/hudson</contextPath>
<hudsonHome>/tmp/ghtJenkinsHome</hudsonHome>
</configuration>
</plugin>-->
<plugin>
Expand Down
@@ -1,23 +1,49 @@
/*
* The MIT License
*
* Copyright 2010 Andrew Bayer. All rights reserved.
* Copyright 2013 Sony Mobile Communications AB. All rights reserved.
*
* 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 com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger;


import hudson.Extension;
import hudson.model.TaskListener;
import hudson.model.Result;
import hudson.plugins.git.IGitAPI;
import hudson.plugins.git.GitAPI;
import hudson.plugins.git.GitException;
import hudson.plugins.git.Revision;
import hudson.plugins.git.Branch;
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.BuildChooser;
import hudson.plugins.git.util.BuildChooserContext;
import hudson.plugins.git.util.BuildChooserDescriptor;
import hudson.plugins.git.util.BuildData;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.stapler.DataBoundConstructor;
import org.eclipse.jgit.lib.ObjectId;

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -29,7 +55,6 @@
* @author Andrew Bayer
*/
public class GerritTriggerBuildChooser extends BuildChooser {
private final String separator = "#";

/**
* Default constructor.
Expand All @@ -39,22 +64,27 @@ public GerritTriggerBuildChooser() {
}

//CS IGNORE RedundantThrows FOR NEXT 30 LINES. REASON: Informative, and could happen.

/**
* Determines which Revisions to build.
*
* <p/>
* Doesn't care about branches.
* @param isPollCall whether this is being called from Git polling
*
* @param isPollCall whether this is being called from Git polling
* @param singleBranch The branch
* @param git The GitAPI object
* @param listener TaskListener for logging, etc
* @param data the historical BuildData object
* @param git The GitClient API object
* @param listener TaskListener for logging, etc
* @param data the historical BuildData object
* @param context the remote context
* @return A Collection containing the new revision.
*
* @throws GitException in case of error
* @throws IOException In case of error
* @throws IOException In case of error
*/
@Override
public Collection<Revision> getCandidateRevisions(boolean isPollCall, String singleBranch,
IGitAPI git, TaskListener listener, BuildData data)
GitClient git, TaskListener listener,
BuildData data, BuildChooserContext context)
throws GitException, IOException {

try {
Expand All @@ -71,12 +101,13 @@ public Collection<Revision> getCandidateRevisions(boolean isPollCall, String sin
}

@Override
public Build prevBuildForChangelog(String singleBranch, BuildData data, IGitAPI git) {
public Build prevBuildForChangelog(String singleBranch, BuildData data, GitClient git,
BuildChooserContext context) {
ObjectId sha1 = git.revParse("FETCH_HEAD");

// Now we cheat and add the parent as the last build on the branch, so we can
// get the changelog working properly-ish.
ObjectId parentSha1 = getFirstParent(ObjectId.toString(sha1), git);
ObjectId parentSha1 = getFirstParent(sha1, git);
Revision parentRev = new Revision(parentSha1);
parentRev.getBranches().add(new Branch(singleBranch, parentSha1));

Expand All @@ -89,59 +120,39 @@ public Build prevBuildForChangelog(String singleBranch, BuildData data, IGitAPI
r = lastBuild.getBuildResult();
}

Build newLastBuild = new Build(parentRev, prevBuildNum, r);

return newLastBuild;
return new Build(parentRev, prevBuildNum, r);
}

//CS IGNORE RedundantThrows FOR NEXT 30 LINES. REASON: Informative, and could happen.
/**
* Gets the top parent of the given revision.
*
* @param revName Revision
* @param git GitAPI object
* @param id Revision
* @param git GitClient API object
* @return object id of Revision's parent, or of Revision itself if there is no parent
* @throws GitException In case of error in git call
*/
private ObjectId getFirstParent(String revName, IGitAPI git) throws GitException {
String result = ((GitAPI)git).launchCommand("log", "-1", "--pretty=format:%P", revName);
// If this is the first commit in the git, there is no parent and the
// git log command returns an empty string, which will cause NPE later.
if (result.isEmpty()) {
// Get the revision of this commit instead. If git log still returns
// an empty string, raise an exception.
result = ((GitAPI)git).launchCommand("log", "-1", "--pretty=format:%H");
if (result.isEmpty()) {
throw new GitException("git log returned an empty string");
}
}
String parents = firstLine(result).trim();
String firstParent = parents.split(" ")[0];
return ObjectId.fromString(firstParent);
}

/**
* Gets the first line of the string.
*
* @param result String to get first line of
* @return first line of string
*/
private String firstLine(String result) {
BufferedReader reader = new BufferedReader(new StringReader(result));
String line;
private ObjectId getFirstParent(ObjectId id, GitClient git) throws GitException {
RevWalk walk = null;
ObjectId result = null;
try {
line = reader.readLine();
if (line == null) {
return null;
Repository repository = git.getRepository();
walk = new RevWalk(repository);
RevCommit commit = walk.parseCommit(id);
if (commit.getParentCount() > 0) {
result = commit.getParent(0);
} else {
// If this is the first commit in the git, there is no parent.
result = id;
}
if (reader.readLine() != null) {
throw new GitException("Result has multiple lines");
} catch (Exception e) {
throw new GitException("Failed to find parent id. ", e);
} finally {
if (walk != null) {
walk.release();
}
} catch (IOException e) {
throw new GitException("Error parsing result", e);
}

return line;
return result;
}

/**
Expand Down

0 comments on commit 155ab67

Please sign in to comment.