From b329f1138be04e090408d935c4db665fc588b8ff Mon Sep 17 00:00:00 2001 From: "Claus Schneider (Praqma)" Date: Wed, 15 Aug 2018 09:47:01 +0200 Subject: [PATCH] gh-133 Integrate Jenkinsfile during preSCM step and reuse the SHA1 - part 1 --- .../git/GetAllCommitsFromBranchCallback.java | 1 + .../scm/git/GitBridge.java | 7 ++--- .../PretestedIntegrationAsGitPluginExt.java | 29 +++++++++++++++++-- .../EndUserTesting/JenkinsfileScripted | 4 +-- .../createRepoForIntegrationSituations.sh | 9 ++++-- .../jobs/test-PipeScriptedSCM/config.xml | 18 ++++++++---- 6 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GetAllCommitsFromBranchCallback.java b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GetAllCommitsFromBranchCallback.java index d240642f..cb575e8c 100644 --- a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GetAllCommitsFromBranchCallback.java +++ b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GetAllCommitsFromBranchCallback.java @@ -72,6 +72,7 @@ public String invoke(Repository repo, VirtualChannel channel) throws IOException sb.append(String.format("%n")); Integer secondsSinceUnixEpoch = rev.getCommitTime(); + // Note that the git log shows different date formats, depending on configuration. // The choices in the git commit message below matches the squashed commit message // that git generates on a Ubuntu Linux 14.04 with default git installation. diff --git a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java index c381c538..e64a15fe 100644 --- a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java +++ b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java @@ -69,9 +69,9 @@ public static void pushToIntegrationBranchGit(Run run, TaskListener listen try { pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo, 0); } catch (IOException ex) { - LOGGER.log(Level.SEVERE, "Failed to push changes to integration branch. Exception:", ex); - listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to integration branch. Exception %s", ex)); - throw new PushFailedException(String.format("Failed to push changes to integration branch, message was:%n%s", ex)); + LOGGER.log(Level.SEVERE, "Failed to push changes to branch: " + expandedBranch +". Exception:", ex); + listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to branch: \" + expandedBranch +\". Exception: %s", ex)); + throw new PushFailedException(String.format("Failed to push changes to branch: " + expandedBranch + ", message was:%n%s", ex)); } } @@ -239,7 +239,6 @@ public void pushToIntegrationBranch(AbstractBuild build, BuildListener lis EnvVars environment = build.getEnvironment(listener); String expandedRepo = getExpandedRepository(environment); String expandedBranch = getExpandedIntegrationBranch(environment); - GitSCM gitSCM = findScm(build, listener); GitClient client = gitSCM.createClient(listener, build.getEnvironment(listener), build, build.getWorkspace()); pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo, 0); diff --git a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java index d16ac9f0..3b7c1d0a 100644 --- a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java +++ b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java @@ -15,6 +15,8 @@ import hudson.plugins.git.util.GitUtils; import jenkins.model.Jenkins; import org.apache.commons.lang.StringUtils; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.URIish; import org.jenkinsci.Symbol; import org.jenkinsci.plugins.gitclient.GitClient; import org.jenkinsci.plugins.gitclient.MergeCommand; @@ -24,9 +26,11 @@ import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegrationFailedException; import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException; import org.jenkinsci.plugins.pretestedintegration.exceptions.UnsupportedConfigurationException; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.kohsuke.stapler.DataBoundConstructor; import java.io.IOException; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -118,10 +122,30 @@ public Revision decorateRevisionToBuild( try { gitBridge.evalBranchConfigurations(triggeredBranch, expandedIntegrationBranch, expandedRepo); listener.getLogger().println(String.format(LOG_PREFIX + "Checking out integration branch %s:", expandedIntegrationBranch)); + try { + List list = new ArrayList<>(); + list.add(new RefSpec(":refs/heads" + triggeredBranch.getName().replace(expandedRepo, ""))); + git.fetch_().from(new URIish(expandedRepo), list); + } catch (URISyntaxException ex) { + String logMessage = LOG_PREFIX + "Fetch failed"; + listener.getLogger().println(logMessage); + LOGGER.log(Level.SEVERE, logMessage, ex ); + } git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute(); - ((GitIntegrationStrategy) gitBridge.integrationStrategy).integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge); - + if(run.getActions(PretestTriggerCommitAction.class).isEmpty() ) { + GitIntegrationStrategy gitStrategy = (GitIntegrationStrategy)gitBridge.integrationStrategy; + gitStrategy.integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge); + //For AccumulatedCommit and in pipeline, immediately push back the merge result + if(gitStrategy instanceof AccumulatedCommitStrategy && run instanceof WorkflowRun) { + GitBridge.pushToBranch(listener, git, expandedIntegrationBranch, triggeredBranch.getName(), repoName); + } + run.addAction(new PretestTriggerCommitAction(triggeredBranch, expandedIntegrationBranch, expandedRepo, ucCredentialsId)); + } else { + String logMessage = LOG_PREFIX + "Checking out SHA1 from first integration"; + listener.getLogger().println(logMessage); + git.checkout().branch(expandedIntegrationBranch).ref(triggeredBranch.getSHA1String()).deleteBranchIfExist(true).execute(); + } } catch (NothingToDoException e) { run.setResult(Result.NOT_BUILT); String logMessage = LOG_PREFIX + String.format("%s - setUp() - NothingToDoException - %s", LOG_PREFIX, e.getMessage()); @@ -143,7 +167,6 @@ public Revision decorateRevisionToBuild( git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute(); } - run.addAction(new PretestTriggerCommitAction(triggeredBranch, expandedIntegrationBranch, expandedRepo, ucCredentialsId)); if (run.getResult() == null || run.getResult() == Result.SUCCESS) { Revision mergeRevision = new GitUtils(listener, git).getRevisionForSHA1(git.revParse(HEAD)); return mergeRevision; diff --git a/src/test/resources/EndUserTesting/JenkinsfileScripted b/src/test/resources/EndUserTesting/JenkinsfileScripted index b7943701..b7583fcb 100644 --- a/src/test/resources/EndUserTesting/JenkinsfileScripted +++ b/src/test/resources/EndUserTesting/JenkinsfileScripted @@ -2,8 +2,7 @@ def triggerBranchPattern = '*/readyPipeScriptedSCM/*' def integrationBranch = 'masterPipeScriptedSCM' node { stage ('Checkout'){ - checkout scm // just reference SCM in the job executing the Pipeline script. -/** +// checkout scm // just reference SCM in the job executing the Pipeline script. checkout([ $class: 'GitSCM', branches: [[name: triggerBranchPattern ]], @@ -23,7 +22,6 @@ node { [$class: 'PruneStaleBranch'] ] ]) -*/ // try { // sh ''' // if [ -e ./build_failed.md ]; then diff --git a/src/test/resources/EndUserTesting/createRepoForIntegrationSituations.sh b/src/test/resources/EndUserTesting/createRepoForIntegrationSituations.sh index 608b926a..ea83aafa 100644 --- a/src/test/resources/EndUserTesting/createRepoForIntegrationSituations.sh +++ b/src/test/resources/EndUserTesting/createRepoForIntegrationSituations.sh @@ -132,8 +132,8 @@ done read -n 1 -p "Enter to continue" enter for branch_prefix in ${branch_prefixes} ; do -# git push origin refs/tags/${branch_prefix}_NOT_BUILT:refs/heads/ready${branch_prefix}/test-05.1-NOT_BUILT - publishAndBuild refs/tags/${branch_prefix}_NOT_BUILT ${branch_prefix} test-05.1-NOT_BUILT + git push origin refs/tags/${branch_prefix}_NOT_BUILT:refs/heads/ready${branch_prefix}/test-05.1-NOT_BUILT +# publishAndBuild refs/tags/${branch_prefix}_NOT_BUILT ${branch_prefix} test-05.1-NOT_BUILT done read -n 1 -p "Enter to continue" enter @@ -156,7 +156,10 @@ for branch_prefix in ${branch_prefixes} ; do publishAndBuild HEAD ${branch_prefix} ${text} done -read -n 1 -p "Enter to continue" enter +read -n 1 -p "Enter to continue and fetch and prune branches" enter checkoutMyBranch "master" && resetToInit + +git fetch -ap + git branch -r diff --git a/src/test/resources/EndUserTesting/jobs/test-PipeScriptedSCM/config.xml b/src/test/resources/EndUserTesting/jobs/test-PipeScriptedSCM/config.xml index 1c781165..7d1f36a9 100644 --- a/src/test/resources/EndUserTesting/jobs/test-PipeScriptedSCM/config.xml +++ b/src/test/resources/EndUserTesting/jobs/test-PipeScriptedSCM/config.xml @@ -1,5 +1,5 @@ - + false @@ -13,7 +13,7 @@ - + 2 @@ -29,15 +29,21 @@ false - + masterPipeScriptedSCM origin - + + false + + + + origin + masterPipeScriptedSCM + + JenkinsfileScripted - false - false \ No newline at end of file