Skip to content
Permalink
Browse files
Merge pull request #497 from vgaidarji/vgaidarji/improve-logs-output
[JENKINS-38241] Print commit message to log on checkout
  • Loading branch information
MarkEWaite committed Jun 23, 2017
2 parents 1ca1f49 + bba9928 commit 31101a39964c56384708b52f11bd135d492f7c69
Showing 6 changed files with 70 additions and 57 deletions.
@@ -24,6 +24,9 @@ your best to improve code coverage with tests when you submit.

Before submitting your change, please review the findbugs output to
assure that you haven't introduced new findbugs warnings.
- `mvn findbugs:check` to analyze project using [Findbugs](http://findbugs.sourceforge.net/)
- `mvn findbugs:gui` to check Findbugs report using GUI


Code formatting in the git plugin varies between files. Try to
maintain reasonable consistency with the existing files where

This file was deleted.

@@ -46,6 +46,7 @@

import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
@@ -1148,6 +1149,9 @@ public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, Tas
}

listener.getLogger().println("Checking out " + revToBuild.revision);

printCommitMessageToLog(listener, git, revToBuild);

CheckoutCommand checkoutCommand = git.checkout().branch(localBranchName).ref(revToBuild.revision.getSha1String()).deleteBranchIfExist(true);
for (GitSCMExtension ext : this.getExtensions()) {
ext.decorateCheckoutCommand(this, build, git, listener, checkoutCommand);
@@ -1179,6 +1183,16 @@ public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, Tas
}
}

private void printCommitMessageToLog(TaskListener listener, GitClient git, final Build revToBuild)
throws IOException {
try {
RevCommit commit = git.withRepository(new RevCommitRepositoryCallback(revToBuild));
listener.getLogger().println("Commit message: \"" + commit.getShortMessage() + "\"");
} catch (InterruptedException e) {
e.printStackTrace(listener.error("Unable to retrieve commit message"));
}
}

/**
* Build up change log from all the branches that we've merged into {@code revToBuild}.
*
@@ -35,7 +35,7 @@ public final URL getUrl() throws IOException {
if (req != null) {
Job job = req.findAncestorObject(Job.class);
if (job != null) {
EnvVars env = null;
EnvVars env;
try {
env = job.getEnvironment(null, TaskListener.NULL);
} catch (InterruptedException e) {
@@ -0,0 +1,28 @@
package hudson.plugins.git.util;

import hudson.remoting.VirtualChannel;
import java.io.IOException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.jenkinsci.plugins.gitclient.RepositoryCallback;

/**
* Retrieves {@link RevCommit} from given {@link Build} revision.
*/
public final class RevCommitRepositoryCallback implements RepositoryCallback<RevCommit> {
private static final long serialVersionUID = 1L;
private final Build revToBuild;

public RevCommitRepositoryCallback(Build revToBuild) {
this.revToBuild = revToBuild;
}

@Override
public RevCommit invoke(Repository repository, VirtualChannel virtualChannel)
throws IOException, InterruptedException {
try (RevWalk walk = new RevWalk(repository)) {
return walk.parseCommit(revToBuild.revision.getSha1());
}
}
}
@@ -78,8 +78,10 @@
import org.junit.Before;
import org.junit.BeforeClass;

import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@@ -2286,7 +2288,28 @@ public void testBuildEnvVarsLocalBranchNotSet() throws Exception {
verify(buildData, times(1)).hasBeenReferenced(anyString());
verify(build, times(1)).getActions(BuildData.class);
}


@Issue("JENKINS-38241")
@Test
public void testCommitMessageIsPrintedToLogs() throws Exception {
sampleRepo.init();
sampleRepo.write("file", "v1");
sampleRepo.git("commit", "--all", "--message=test commit");
FreeStyleProject p = setupSimpleProject("master");
Run<?,?> run = rule.buildAndAssertSuccess(p);
TaskListener mockListener = Mockito.mock(TaskListener.class);
Mockito.when(mockListener.getLogger()).thenReturn(Mockito.spy(StreamTaskListener.fromStdout().getLogger()));

p.getScm().checkout(run, new Launcher.LocalLauncher(listener),
new FilePath(run.getRootDir()).child("tmp-" + "master"),
mockListener, null, SCMRevisionState.NONE);

ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
verify(mockListener.getLogger(), atLeastOnce()).println(logCaptor.capture());
List<String> values = logCaptor.getAllValues();
assertThat(values, hasItem("Commit message: \"test commit\""));
}

/**
* Method performs HTTP get on "notifyCommit" URL, passing it commit by SHA1
* and tests for build data consistency.

0 comments on commit 31101a3

Please sign in to comment.