Skip to content

Commit

Permalink
[JENKINS-73297] Improve error handling when commit is not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Aug 19, 2024
1 parent 57957eb commit 75fd7de
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ private RemoteResultWrapper<Delta> calculateDelta(final Repository repository) t
GitDelta delta = new GitDelta(currentCommitId, referenceCommitId, Map.of(), exception.getMessage());
RemoteResultWrapper<Delta> wrapper = new RemoteResultWrapper<>(delta, "Errors from Git Delta:");

wrapper.logError("Could not find commit", exception);
wrapper.logException(exception, "Could not find the specified commit - is the SCM parameter correctly set?");

return wrapper;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Optional<Delta> calculateDelta(final Run<?, ?> build, final Run<?, ?> ref
RemoteResultWrapper<Delta> wrapped = git.withRepository(
new DeltaRepositoryCallback(currentCommit, referenceCommit));
wrapped.getInfoMessages().forEach(log::logInfo);
wrapped.getErrorMessages().forEach(log::logError);
return Optional.of(wrapped.getResult());
}
catch (IOException | InterruptedException exception) {
Expand All @@ -77,12 +78,12 @@ public Optional<Delta> calculateDelta(final Run<?, ?> build, final Run<?, ?> ref
}

/**
* Returns the latest commit of the {@link GitCommitsRecord commits record} of a Git repository.
* Returns the latest commit of the {@link GitCommitsRecord commit record} of a Git repository.
*
* @param buildName
* the name of the build the commits record corresponds to
* @param record
* the commits record
* the commit record
* @param log
* the log
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.Issue;

import edu.hm.hafner.util.FilteredLog;

import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import hudson.model.FreeStyleProject;
import hudson.model.Run;
import hudson.plugins.git.GitSCM;
Expand All @@ -34,6 +36,32 @@ class GitDeltaCalculatorITest extends GitITest {
private static final String EMPTY_SCM_KEY = "";
private static final String EMPTY_FILE_PATH = "";

private static final String GIT_FORENSICS_URL = "https://github.com/jenkinsci/git-forensics-plugin.git";
private static final String GIT_FORENSICS_COMMIT = "86503e8bc0374e05e2cd32ed3bb8b4435d5fd757";

@Test @Issue("JENKINS-73297")
void shouldShowErrorIfCommitIsNotFound() {
WorkflowJob job = createPipeline();
job.setDefinition(asStage("checkout([$class: 'GitSCM', "
+ "branches: [[name: '" + GIT_FORENSICS_COMMIT + "' ]],\n"
+ "userRemoteConfigs: [[url: '" + GIT_FORENSICS_URL + "']],\n"
+ "extensions: [[$class: 'RelativeTargetDirectory', \n"
+ " relativeTargetDir: '" + GIT_FORENSICS_COMMIT + "']]])"));

Run<?, ?> build = buildSuccessfully(job);
GitDeltaCalculator deltaCalculator = createDeltaCalculator();

var log = createLog();
Optional<Delta> result = deltaCalculator.calculateDelta(build, build, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

assertThat(log.getInfoMessages())
.contains("-> Invoking Git delta calculator for determining the changes between commits '86503e8' and '86503e8'");
assertThat(log.getErrorMessages())
.contains("Could not find the specified commit - is the SCM parameter correctly set?",
"org.eclipse.jgit.errors.MissingObjectException: Missing unknown 86503e8bc0374e05e2cd32ed3bb8b4435d5fd757");
}

@Test
void shouldCreateEmptyDeltaIfCommitsAreInvalid() {
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Expand All @@ -46,7 +74,6 @@ void shouldCreateEmptyDeltaIfCommitsAreInvalid() {
void shouldCreateDiffFile() {
FreeStyleProject job = createJobWithReferenceRecorder();

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

Run<?, ?> referenceBuild = buildSuccessfully(job);
Expand All @@ -59,6 +86,7 @@ void shouldCreateDiffFile() {
Run<?, ?> build = buildSuccessfully(job);
String currentCommit = getHead();

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand Down Expand Up @@ -86,7 +114,6 @@ void shouldCreateDiffFile() {
@Test
void shouldDetermineAddedFile() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

Run<?, ?> referenceBuild = buildSuccessfully(job);
Expand All @@ -98,6 +125,7 @@ void shouldDetermineAddedFile() {
commit("test");
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -117,7 +145,6 @@ void shouldDetermineAddedFile() {
@Test
void shouldDetermineModifiedFile() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "modified";
Expand All @@ -126,6 +153,7 @@ void shouldDetermineModifiedFile() {
commitFile(content);
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -145,7 +173,6 @@ void shouldDetermineModifiedFile() {
@Test
void shouldDetermineDeletedFile() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "content";
Expand All @@ -155,6 +182,7 @@ void shouldDetermineDeletedFile() {
commit("test");
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -174,7 +202,6 @@ void shouldDetermineDeletedFile() {
@Test
void shouldDetermineRenamedFile() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "content";
Expand All @@ -184,6 +211,7 @@ void shouldDetermineRenamedFile() {
writeFileAsAuthorFoo(content);
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -203,7 +231,6 @@ void shouldDetermineRenamedFile() {
@Test
void shouldDetermineAddedLines() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "Test\nTest\n";
Expand All @@ -213,6 +240,7 @@ void shouldDetermineAddedLines() {
commitFile(insertedContent);
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -234,7 +262,6 @@ void shouldDetermineAddedLines() {
@Test
void shouldDetermineModifiedLines() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "Test\nTest\nTest\nTest";
Expand All @@ -244,6 +271,7 @@ void shouldDetermineModifiedLines() {
commitFile(modified);
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -265,7 +293,6 @@ void shouldDetermineModifiedLines() {
@Test
void shouldDetermineDeletedLines() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "Test\nTest3\nTest";
Expand All @@ -275,6 +302,7 @@ void shouldDetermineDeletedLines() {
commitFile(modified);
Run<?, ?> build = buildSuccessfully(job);

GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand All @@ -296,8 +324,6 @@ void shouldDetermineDeletedLines() {
@Test
void shouldDetermineAllChangeTypesTogether() {
FreeStyleProject job = createJobWithReferenceRecorder();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

String content = "Test1\nTest2\nTest3\nTest4";
String newContent = "Modified\nTest2\nInserted\nTest3";
Expand All @@ -306,6 +332,8 @@ void shouldDetermineAllChangeTypesTogether() {
commitFile(newContent);
Run<?, ?> build = buildSuccessfully(job);

FilteredLog log = createLog();
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
Optional<Delta> result = deltaCalculator.calculateDelta(build, referenceBuild, EMPTY_SCM_KEY, log);
assertThat(result).isNotEmpty();

Expand Down

0 comments on commit 75fd7de

Please sign in to comment.