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..b25f1f2e 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; @@ -25,11 +27,13 @@ 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.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 +126,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()); @@ -147,7 +171,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