Skip to content

Commit

Permalink
[SCM-1022] jgit push failure is not failing the build
Browse files Browse the repository at this point in the history
This closes #203
  • Loading branch information
Mattias Andersson authored and michael-o committed Apr 8, 2024
1 parent 6991a0b commit d77b92e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
package org.apache.maven.scm.provider.git.command.checkin;

import java.io.File;
import java.io.IOException;

import org.apache.maven.scm.PlexusJUnit4TestSupport;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.provider.git.GitScmTestUtils;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.tck.command.checkin.CheckInCommandTckTest;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Test;

import static org.junit.Assert.assertFalse;

/**
* @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
Expand All @@ -44,4 +52,61 @@ protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository reposi
GitScmTestUtils.setDefaulGitConfig(workingDirectory);
}
}

@Test
public void testUpToDatePush() throws Exception {
File checkedOutRepo = getWorkingCopy();

ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
checkoutRepoInto(checkedOutRepo, scmRepository);

// Add a default user to the config
GitScmTestUtils.setDefaulGitConfig(checkedOutRepo);

CheckInScmResult result =
getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "No change commit message");

assertResultIsSuccess(result);
}

@Test
public void testRejectedNonFastForwardPush() throws Exception {
File blockingRepo = PlexusJUnit4TestSupport.getTestFile("target/scm-test/blocking-repo");
File rejectedRepo = PlexusJUnit4TestSupport.getTestFile("target/scm-test/rejected-repo");

ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
checkoutRepoInto(rejectedRepo, scmRepository);
checkoutRepoInto(blockingRepo, scmRepository);

// Add a default user to the config
GitScmTestUtils.setDefaulGitConfig(rejectedRepo);
GitScmTestUtils.setDefaulGitConfig(blockingRepo);

ScmFileSet blockingFileSet = createWorkspaceChange(rejectedRepo);

CheckInScmResult blockingResult = getScmManager().checkIn(scmRepository, blockingFileSet, "Blocking commit");
assertResultIsSuccess(blockingResult);

ScmFileSet rejectedFileSet = createWorkspaceChange(blockingRepo);

CheckInScmResult checkInScmResult = getScmManager().checkIn(scmRepository, rejectedFileSet, "Rejected commit");
assertFalse(
"check-in should have been rejected since fast forward was not possible", checkInScmResult.isSuccess());
}

private CheckOutScmResult checkoutRepoInto(File workingCopy, ScmRepository scmRepository) throws Exception {
FileUtils.deleteDirectory(workingCopy);
workingCopy.mkdir();

CheckOutScmResult result = getScmManager().checkOut(scmRepository, new ScmFileSet(workingCopy), null);

assertResultIsSuccess(result);
return result;
}

private ScmFileSet createWorkspaceChange(File repo) throws IOException {
File beerFile = new File(repo.getAbsolutePath(), "beer.xml");
FileUtils.fileWrite(beerFile.getAbsolutePath(), "1 litre");
return new ScmFileSet(repo, beerFile.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.UserConfig;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteRefUpdate;

/**
* This provider uses the following strategy to discover the committer and author name/mail for a commit:
Expand Down Expand Up @@ -139,7 +141,19 @@ protected CheckInScmResult executeCheckInCommand(
}
RefSpec refSpec = new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_HEADS + branch);
logger.info("push changes to remote... " + refSpec);
JGitUtils.push(git, (GitScmProviderRepository) repo, refSpec);
Iterable<PushResult> pushResultList = JGitUtils.push(git, (GitScmProviderRepository) repo, refSpec);

for (PushResult pushResult : pushResultList) {
for (RemoteRefUpdate remoteRefUpdate : pushResult.getRemoteUpdates()) {
if (!isSuccessStatus(remoteRefUpdate.getStatus())) {
return new CheckInScmResult(
"JGit checkin",
"The git-push command failed, with status: " + remoteRefUpdate.getStatus(),
remoteRefUpdate.getMessage(),
false);
}
}
}
}

return new CheckInScmResult("JGit checkin", checkedInFiles);
Expand All @@ -150,7 +164,13 @@ protected CheckInScmResult executeCheckInCommand(
}
}

private boolean isSuccessStatus(RemoteRefUpdate.Status remoteRefUpdateStatus) {
return remoteRefUpdateStatus == RemoteRefUpdate.Status.OK
|| remoteRefUpdateStatus == RemoteRefUpdate.Status.UP_TO_DATE;
}

private static final class UserInfo {

final String name;

final String email;
Expand Down

0 comments on commit d77b92e

Please sign in to comment.