Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENTERPRISE-728: Use base revision if no previous successful commit is available. #6

Merged
merged 1 commit into from Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/main/java/org/jenkinsci/plugins/codescene/CodeSceneBuilder.java
Expand Up @@ -37,7 +37,6 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -250,6 +249,23 @@ private CodeSceneBuildActionEntry runDeltaAnalysisOnBranchDiff(Configuration con
result.getRiskDescription());
}

/**
* Find the previous successful commit in the environment,
* or use the base revision (usually reference to the base branch like 'origin/master')
* if there's no previous commit set.
* This is important in case we are executing the first build of of a branch.
*/
private CommitRange getRangeFromPreviousSuccessfulCommit(EnvVars env, Commit currentCommit) {
// This can be null: see https://issues.jenkins-ci.org/browse/JENKINS-51324
final String previousCommitFromEnv = env.get("GIT_PREVIOUS_SUCCESSFUL_COMMIT");
if (previousCommitFromEnv != null) {
return new CommitRange(new Commit(previousCommitFromEnv), currentCommit);
} else {
// fallback to the base revision
return new CommitRange(new Branch(getBaseRevision()), currentCommit);
}
}

@Override
public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) {
if (isDeltaAnalysisConfigured()) {
Expand All @@ -263,13 +279,14 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task
couplingThresholdPercent, useBiomarkers, letBuildPassOnFailedAnalysis);
EnvVars env = build.getEnvironment(listener);

Commit previousCommit = new Commit(env.get("GIT_PREVIOUS_SUCCESSFUL_COMMIT"));
Commit currentCommit = new Commit(env.get("GIT_COMMIT"));
String branch = env.get("GIT_BRANCH");

final Commit currentCommit = new Commit(env.get("GIT_COMMIT"));

if (isAnalyzeLatestIndividually()) {
analyzeLatestIndividualCommitFor(build, workspace, launcher, listener, codesceneConfig, previousCommit, currentCommit);
analyzeLatestIndividualCommitFor(build, workspace, launcher, listener, codesceneConfig,
getRangeFromPreviousSuccessfulCommit(env, currentCommit));
}
final String branch = env.get("GIT_BRANCH");
if (isAnalyzeBranchDiff() && getBaseRevision() != null) {
analyzeWorkOnBranchFor(build, workspace, launcher, listener, codesceneConfig, currentCommit, branch);
}
Expand All @@ -283,7 +300,9 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task
}
}

private void analyzeWorkOnBranchFor(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, Configuration codesceneConfig, Commit currentCommit, String branch) throws IOException, InterruptedException, RemoteAnalysisException {
private void analyzeWorkOnBranchFor(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener,
Configuration codesceneConfig, Commit currentCommit, String branch)
throws IOException, InterruptedException, RemoteAnalysisException {
final Branch branchBase = new Branch(getBaseRevision());
final CommitRange rangeToAnalyse = new CommitRange(branchBase, currentCommit);
List<String> revisions = getCommitRange(build, workspace, launcher, listener, rangeToAnalyse);
Expand All @@ -297,8 +316,9 @@ private void analyzeWorkOnBranchFor(Run<?, ?> build, FilePath workspace, Launche
}
}

private void analyzeLatestIndividualCommitFor(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, Configuration codesceneConfig, Commit previousCommit, Commit currentCommit) throws IOException, InterruptedException, RemoteAnalysisException {
final CommitRange rangeToAnalyse = new CommitRange(previousCommit, currentCommit);
private void analyzeLatestIndividualCommitFor(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener,
Configuration codesceneConfig, CommitRange rangeToAnalyse)
throws IOException, InterruptedException, RemoteAnalysisException {
List<String> revisions = getCommitRange(build, workspace, launcher, listener, rangeToAnalyse);
if (revisions.isEmpty()) {
listener.getLogger().println("No new commits to analyze individually for this build.");
Expand Down Expand Up @@ -408,13 +428,13 @@ public boolean isApplicable(Class type) {
}

@Override
public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws FormException {
public boolean configure(StaplerRequest staplerRequest, JSONObject json) {
save();
return true; // indicate that everything is good so far
}

public FormValidation doCheckBaseRevision(@QueryParameter boolean analyzeBranchDiff,
@QueryParameter String baseRevision) throws IOException, ServletException {
@QueryParameter String baseRevision) {
if (analyzeBranchDiff && (baseRevision == null || baseRevision.isEmpty())) {
return FormValidation.error("Base revision cannot be empty.");
} else {
Expand Down
Expand Up @@ -12,7 +12,7 @@ public class Commit {

public Commit(final String hash) {
if (hash == null) {
throw new IllegalArgumentException("A commit hash cannot be null - just don't do that");
throw new IllegalArgumentException("A commit hash cannot be null!");
}

final Matcher m = hashPattern.matcher(hash);
Expand Down