From 6f71c92b20b918db336cb20f4b71ffa893734d9f 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 --- .../scm/git/AccumulatedCommitStrategy.java | 2 +- .../git/GetAllCommitsFromBranchCallback.java | 1 + .../scm/git/GitBridge.java | 24 +++++++++--------- .../PretestedIntegrationAsGitPluginExt.java | 25 ++++++++++++++++--- .../EndUserTesting/JenkinsfileScripted | 4 +-- .../createRepoForIntegrationSituations.sh | 9 ++++--- .../jobs/test-PipeScriptedSCM/config.xml | 18 ++++++++----- 7 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/AccumulatedCommitStrategy.java b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/AccumulatedCommitStrategy.java index de1fa781..c9e092d7 100644 --- a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/AccumulatedCommitStrategy.java +++ b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/AccumulatedCommitStrategy.java @@ -114,7 +114,7 @@ private void doTheIntegration(Run build, TaskListener listener, GitBridge gitbri listener.getLogger().println(logMessage); try { client.merge() - .setMessage(commitMessage) + .setMessage("temp") .setCommit(false) .setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwardMode.NO_FF) .setRevisionToMerge(commitId).execute(); 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 41026cd8..9540859d 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 @@ -66,25 +66,25 @@ public GitBridge(IntegrationStrategy integrationStrategy, final String integrati public static void pushToIntegrationBranchGit(Run run, TaskListener listener, GitClient client, String expandedRepo, String expandedBranch) throws PushFailedException { try { - pushToBranch(listener, client, expandedBranch, expandedRepo); + pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo); } 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)); } } - public static void pushToBranch(TaskListener listener, GitClient client, String branchToPush, String expandedRepo) throws PushFailedException { + public static void pushToBranch(TaskListener listener, GitClient client, String sourceLocalBranch, String targetRemoteBranch, String expandedRepo) throws PushFailedException { try { - LOGGER.log(Level.INFO, "Pushing changes to: " + branchToPush); - listener.getLogger().println(GitMessages.LOG_PREFIX+ "Pushing changes to integration branch:"); - client.push(expandedRepo, "HEAD:refs/heads/" + branchToPush); + LOGGER.log(Level.INFO, "Pushing changes from local branch: " + sourceLocalBranch + " to remote branch: " + targetRemoteBranch); + listener.getLogger().println(GitMessages.LOG_PREFIX+ "Pushing changes to branch:"); + client.push(expandedRepo, "refs/heads/" + sourceLocalBranch + ":refs/heads/" + targetRemoteBranch.replace(expandedRepo + "/","")); LOGGER.log(Level.INFO, "Done pushing changes"); listener.getLogger().println(GitMessages.LOG_PREFIX+ "Done pushing changes"); } catch (InterruptedException ex) { - LOGGER.log(Level.SEVERE, "Failed to push changes to: " + branchToPush + ".\nException:", ex); - listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to: " + branchToPush + ".\nException: %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: " + targetRemoteBranch + ".\nException:", ex); + listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to: " + targetRemoteBranch + ".\nException: %s", ex)); + throw new PushFailedException(String.format("Failed to push changes to branch, message was:%n%s", ex)); } } @@ -210,7 +210,7 @@ public void pushToIntegrationBranch(AbstractBuild build, BuildListener lis GitSCM gitSCM = findScm(build, listener); GitClient client = gitSCM.createClient(listener, build.getEnvironment(listener), build, build.getWorkspace()); - pushToBranch(listener, client, expandedBranch, expandedRepo); + pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo); } catch (IOException | InterruptedException 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)); 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 43b86562..4bd197da 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.BuildData; import hudson.plugins.git.util.GitUtils; import jenkins.model.Jenkins; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.URIish; import org.jenkinsci.Symbol; import org.jenkinsci.plugins.gitclient.ChangelogCommand; import org.jenkinsci.plugins.gitclient.GitClient; @@ -30,6 +32,7 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -122,10 +125,25 @@ 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) gitBridge.integrationStrategy).integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge); + 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()); @@ -147,7 +165,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