Skip to content

Commit

Permalink
gh-128 Fix polling issue. NOT-BUILT is also saved for the record
Browse files Browse the repository at this point in the history
  • Loading branch information
Claus Schneider (Praqma) committed Aug 21, 2018
1 parent 1b83e1b commit 39c7d3e
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,50 +126,44 @@ public boolean endBuild() throws InterruptedException, IOException {
@Override
public void perform(Run<?, ?> run, FilePath ws, Launcher launcher, TaskListener listener) throws InterruptedException {
Result result = run.getResult();
String triggeredBranch = "n/a";
String integrationBranch = "n/a";
String integrationRepo = null;
if( ! run.getActions(PretestTriggerCommitAction.class).isEmpty()) {
triggeredBranch = run.getAction(PretestTriggerCommitAction.class).triggerBranch.getName();
integrationBranch = run.getAction(PretestTriggerCommitAction.class).integrationBranch;
integrationRepo = run.getAction(PretestTriggerCommitAction.class).integrationRepo;
String ucCredentialsId = run.getAction(PretestTriggerCommitAction.class).ucCredentialsId;
try {
// The choice of 'jgit' or 'git'. It must be set though..
GitClient client = Git.with(listener, run.getEnvironment(listener)).in(ws).using("git").getClient();

/**
* This was added because we want to fail if the plugin performed more than 1 merge during run.
* This can only happen with the Pipeline job types.
* The action is only added when Pretested Integration merges.
* We do not support this scenario with more than 1 merge.
*/
/**
if(run.getActions(PretestTriggerCommitAction.class).size() > 1) {
throw new InterruptedException("[PREINT] Error: More than 1 integration configured with the Pretested Integration Plugin. Did you use the Pretested Integration Plugin git scm extension on more than one repository? It is not supported.");
}
*/
String triggeredBranch = run.getActions(PretestTriggerCommitAction.class).get(
run.getActions(PretestTriggerCommitAction.class).size()-1).triggerBranch.getName();
String integrationBranch = run.getActions(PretestTriggerCommitAction.class).get(
run.getActions(PretestTriggerCommitAction.class).size()-1).integrationBranch;
String integrationRepo = run.getActions(PretestTriggerCommitAction.class).get(
run.getActions(PretestTriggerCommitAction.class).size()-1).integrationRepo;
String ucCredentialsId = run.getActions(PretestTriggerCommitAction.class).get(
run.getActions(PretestTriggerCommitAction.class).size()-1).ucCredentialsId;

try {
// The choice of 'jgit' or 'git'. It must be set though..
GitClient client = Git.with(listener, run.getEnvironment(listener)).in(ws).using("git").getClient();

if (ucCredentialsId != null) {
StandardUsernameCredentials credentials = CredentialsProvider.findCredentialById(ucCredentialsId, StandardUsernameCredentials.class, run, Collections.EMPTY_LIST);

if (credentials != null) {
listener.getLogger().println("[PREINT] Found credentials");
client.setCredentials(credentials);
if (ucCredentialsId != null) {
StandardUsernameCredentials credentials = CredentialsProvider.findCredentialById(ucCredentialsId, StandardUsernameCredentials.class, run, Collections.EMPTY_LIST);

if (credentials != null) {
listener.getLogger().println("[PREINT] Found credentials");
client.setCredentials(credentials);
}
}
}

if (result == null || result.isBetterOrEqualTo(GitBridge.getRequiredResult())) {
GitBridge.pushToIntegrationBranchGit(run, listener, client, integrationRepo, integrationBranch);
GitBridge.deleteBranch(run, listener, client, triggeredBranch, integrationRepo);
} else {
LOGGER.log(Level.WARNING, "Build result not satisfied - skipped post-build step.");
listener.getLogger().println(LOG_PREFIX + "Build result not satisfied - skipped post-build step.");
if (result == null || result.isBetterOrEqualTo(GitBridge.getRequiredResult())) {
// Make sure we accidently do not try to delete the integration branch - nor push when not needed. Consider moving this to the two functions
if (triggeredBranch.replaceFirst(integrationRepo + "/" , "").equals(integrationBranch)) {
LOGGER.log(Level.WARNING, LOG_PREFIX + "The development/triggered branch: " + triggeredBranch.replaceFirst(integrationRepo + "/" , "") + " equals the integration branch: " + integrationBranch + " SKIP the push to integration branch and deletion of the development branch");
listener.getLogger().println(LOG_PREFIX + "The development/triggered branch: " + triggeredBranch.replaceFirst(integrationRepo + "/" , "") + " equals the integration branch: " + integrationBranch + " SKIP the push to integration branch and deletion of the development branch");
} else {
GitBridge.pushToIntegrationBranchGit(run, listener, client, integrationRepo, integrationBranch);
GitBridge.deleteBranch(run, listener, client, triggeredBranch, integrationRepo);
}
} else {
LOGGER.log(Level.WARNING, "Build result not satisfied - skipped post-build step.");
listener.getLogger().println(LOG_PREFIX + "Build result not satisfied - skipped post-build step.");
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Cannot launch the Git Client.." + ex);
listener.getLogger().println(LOG_PREFIX + "Cannot launch the Git Client.." + ex);
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Cannot launch the Git Client.." + ex);
listener.getLogger().println(LOG_PREFIX + "Cannot launch the Git Client.." + ex);
}
GitBridge.updateBuildDescription(run, listener, integrationBranch, triggeredBranch.replace(integrationRepo + "/", ""));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void doTheIntegration(Run build, TaskListener listener, GitBridge gitbri
listener.getLogger().println(logMessage);
try {
client.merge()
.setMessage("Merge of " + triggerBranch.getName() + " into "+ expandedIntegrationBranch + " (temporary)")
.setMessage("Merge of " + triggerBranch.getName() + " into "+ expandedIntegrationBranch + " (temporary message)")
.setCommit(false)
.setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwardMode.NO_FF)
.setRevisionToMerge(commitId).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ public static void updateBuildDescription(Run<?, ?> run, TaskListener listener,
if (triggeredBranch != null) {
String postfixText = "";
Result result = run.getResult();

if (result == null || result.isBetterOrEqualTo(getRequiredResult())) {
postfixText = " -> " + integrationBranch;
}
String finalDescription;
if (!StringUtils.isBlank(run.getDescription())) {
finalDescription = String.format("%s%n%s",
run.getDescription(),
triggeredBranch + postfixText);
finalDescription = String.format("%s%n%s",run.getDescription(),triggeredBranch + postfixText);
} else {
finalDescription = String.format("%s", triggeredBranch + postfixText);
}
try {
listener.getLogger().println(LOG_PREFIX + "Updating build description");
run.setDescription(finalDescription);
} catch (Exception ex) {
LOGGER.log(Level.FINE, "Failed to update description", ex); /* Dont care */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,10 @@ public Revision decorateRevisionToBuild(

EnvVars environment = run.getEnvironment(listener);

// TODO: Should this be last branch in stead of?
Branch triggeredBranchDraft = triggeredRevision.getBranches().iterator().next();
Branch triggeredBranch = new Branch(triggeredBranchDraft.getName().replaceFirst("refs/remotes/", ""), triggeredBranchDraft.getSHA1());
String expandedIntegrationBranch = gitBridge.getExpandedIntegrationBranch(environment);
String expandedRepo = gitBridge.getExpandedRepository(environment);
String ucCredentialsId = "";

for (UserRemoteConfig uc : scm.getUserRemoteConfigs()) {
String credentialsRepoName = StringUtils.isBlank(uc.getName()) ? "origin" : uc.getName();
if (credentialsRepoName != null && credentialsRepoName.equals(expandedRepo)) {
Expand All @@ -115,42 +112,64 @@ public Revision decorateRevisionToBuild(
}
}

try {
gitBridge.evalBranchConfigurations(triggeredBranch, expandedIntegrationBranch, expandedRepo);
listener.getLogger().println(String.format(LOG_PREFIX + "Checking out integration branch %s:", expandedIntegrationBranch));
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
((GitIntegrationStrategy) gitBridge.integrationStrategy).integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge);


} catch (NothingToDoException e) {
Branch triggeredBranch = null;
if ( triggeredRevision.getBranches().isEmpty() ) {
run.setResult(Result.NOT_BUILT);
String logMessage = LOG_PREFIX + String.format("%s - setUp() - NothingToDoException - %s", LOG_PREFIX, e.getMessage());
String logMessage = String.format("%s - No branch on revision hence we cannot built - leaving workspace: %s but dont build", LOG_PREFIX, expandedIntegrationBranch);
listener.getLogger().println(logMessage);
LOGGER.log(Level.SEVERE, logMessage, e);
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
} catch (IntegrationFailedException | EstablishingWorkspaceFailedException | UnsupportedConfigurationException e) {
run.setResult(Result.FAILURE);
String logMessage = String.format("%s - setUp() - %s - %s", LOG_PREFIX, e.getClass().getSimpleName(), e.getMessage());
listener.getLogger().println(logMessage);
LOGGER.log(Level.SEVERE, logMessage, e);
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
} catch (IOException | InterruptedException e) {
} else {
// TODO: Should this be last branch in stead of?
Branch triggeredBranchDraft = triggeredRevision.getBranches().iterator().next();
triggeredBranch = new Branch(triggeredBranchDraft.getName().replaceFirst("refs/remotes/", ""), triggeredBranchDraft.getSHA1());
}

if( ! run.getActions(PretestTriggerCommitAction.class).isEmpty() ) {
run.setResult(Result.FAILURE);
String logMessage = String.format("%s - Unexpected error. %n%s", LOG_PREFIX, e.getMessage());
LOGGER.log(Level.SEVERE, logMessage, e);
String logMessage = String.format("%s ERROR Likely misconfigered. Currently it is not supported to integrate twice in a build. It is likely because of Pipeline preSCM step or multiSCM. Please see https://github.com/Praqma/pretested-integration-plugin/issues/133 for details about Pipeline preSCM support. If it is neither scenarios, please report it", LOG_PREFIX );
listener.getLogger().println(logMessage);
e.printStackTrace(listener.getLogger());
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
}

if (run.getResult() == null || run.getResult() == Result.SUCCESS ) {

try {
gitBridge.evalBranchConfigurations(triggeredBranch, expandedIntegrationBranch, expandedRepo);
listener.getLogger().println(String.format(LOG_PREFIX + "Checking out integration branch %s:", expandedIntegrationBranch));
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
((GitIntegrationStrategy) gitBridge.integrationStrategy).integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge);
} catch (NothingToDoException e) {
run.setResult(Result.NOT_BUILT);
String logMessage = String.format("%s - setUp() - NothingToDoException - %s", LOG_PREFIX, e.getMessage());
listener.getLogger().println(logMessage);
LOGGER.log(Level.SEVERE, logMessage, e);
// Leave the workspace as we were triggered, so postbuild step can report the correct branch
scm.getBuildData(run).saveBuild(new Build(marked, triggeredRevision, run.getNumber(), run.getResult()));
git.checkout().ref(triggeredBranch.getName()).execute();
} catch (IntegrationFailedException | EstablishingWorkspaceFailedException | UnsupportedConfigurationException e) {
run.setResult(Result.FAILURE);
String logMessage = String.format("%s - setUp() - %s - %s", LOG_PREFIX, e.getClass().getSimpleName(), e.getMessage());
listener.getLogger().println(logMessage);
LOGGER.log(Level.SEVERE, logMessage, e);
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
} catch (IOException | InterruptedException e) {
run.setResult(Result.FAILURE);
String logMessage = String.format("%s - Unexpected error. %n%s", LOG_PREFIX, e.getMessage());
LOGGER.log(Level.SEVERE, logMessage, e);
listener.getLogger().println(logMessage);
e.printStackTrace(listener.getLogger());
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) {
if (run.getResult() == null || run.getResult() == Result.SUCCESS || run.getResult() == Result.NOT_BUILT) {
Revision mergeRevision = new GitUtils(listener, git).getRevisionForSHA1(git.revParse(HEAD));
return mergeRevision;
} else {
// We could not integrate, but we must return a revision for recording it so it does not retrigger
// reset the workspace to the triggered revision
git.checkout().ref(triggeredBranch.getName()).execute();
// We could not integrate, but we must return a revision for recording it so it does not retrigger
scm.getBuildData(run).saveBuild(new Build(marked, triggeredRevision, run.getNumber(), Result.FAILURE));
// throwing the AbortException will result in a status=FAILURE
throw new AbortException(String.format("%s Unable to integrate revision: %s", LOG_PREFIX, triggeredRevision.getSha1String()));
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/test/resources/EndUserTesting/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@

def pattern = 'MultiBranchPipePreSCM'

def triggerBranchPattern = '*/ready' + pattern + '/*'
def integrationBranch = 'master' + pattern


node {
stage ('Checkout'){
checkout scm
checkout scm
/**
checkout([
$class: 'GitSCM',
branches: [[name: '*/readyMultiBranchPipe/*']],
checkout([
$class: 'GitSCM',
branches: [[name: triggerBranchPattern ]],
userRemoteConfigs: [[
name: 'origin',
url: 'git@github.com:bicschneider/test-git-phlow-plugin.git'
]],
extensions: [
pretestedIntegration(
gitIntegrationStrategy: accumulated(),
integrationBranch: 'masterMultiBranchPipe',
integrationBranch: integrationBranch,
repoName: 'origin')
,
[ $class: 'ChangelogToBranch',
options: [compareRemote: 'origin', compareTarget: 'masterMultiBranchPipe']
options: [compareRemote: 'origin', compareTarget: integrationBranch]
],
[$class: 'PruneStaleBranch']
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
def triggerBranchPattern = '*/readyPipeDeclSCM/*'
def integrationBranch = 'masterPipeDeclSCM'

def pattern = 'PipeDeclCheckoutSCM'

def triggerBranchPattern = '*/ready' + pattern + '/*'
def integrationBranch = 'master' + pattern
pipeline{
agent none
stages {
Expand All @@ -8,8 +11,6 @@ pipeline{
label 'master'
}
steps {
checkout scm // just reference SCM in the job executing the Pipeline script.
/**
checkout([
$class: 'GitSCM',
branches: [[name: triggerBranchPattern ]],
Expand All @@ -29,7 +30,6 @@ pipeline{
[$class: 'PruneStaleBranch']
]
])
*/
// sh '''
// if [ -e ./build_failed.md ]; then
// exit 1
Expand Down
28 changes: 28 additions & 0 deletions src/test/resources/EndUserTesting/JenkinsfileDeclPreSCM
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

def pattern = 'PipeDeclPreSCM'

def triggerBranchPattern = '*/ready' + pattern + '/*'
def integrationBranch = 'master' + pattern
pipeline{
agent none
stages {
stage ('Checkout') {
agent {
label 'master'
}
steps {
checkout scm // just reference SCM in the job executing the Pipeline script.
// sh '''
// if [ -e ./build_failed.md ]; then
// exit 1
// fi
// '''
}
post {
always {
pretestedIntegrationPublisher()
}
}
}
}
}
43 changes: 43 additions & 0 deletions src/test/resources/EndUserTesting/JenkinsfileScriptedCheckoutSCM
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

def pattern = 'PipeScriptedCheckoutSCM'

def triggerBranchPattern = '*/ready' + pattern + '/*'
def integrationBranch = 'master' + pattern
node {
stage ('Checkout'){

try {
checkout([
$class: 'GitSCM',
branches: [[name: triggerBranchPattern ]],
userRemoteConfigs: [[
name: 'origin',
url: '[email protected]:bicschneider/test-git-phlow-plugin.git'
]],
extensions: [
pretestedIntegration(
gitIntegrationStrategy: accumulated(),
integrationBranch: integrationBranch,
repoName: 'origin')
,
[ $class: 'ChangelogToBranch',
options: [compareRemote: 'origin', compareTarget: integrationBranch]
],
[$class: 'PruneStaleBranch']
]
])
} catch(Exception e) {
echo "Leave further processing to publisher"
}
// try {
// sh '''
// if [ -e ./build_failed.md ]; then
// exit 1
// fi
// '''
// } catch(Exception e) {
// currentBuild.result = 'UNSTABLE'
// }
pretestedIntegrationPublisher()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
def triggerBranchPattern = '*/readyPipeScriptedSCM/*'
def integrationBranch = 'masterPipeScriptedSCM'

def pattern = 'PipeScriptedSCM'

def triggerBranchPattern = '*/ready' + pattern + '/*'
def integrationBranch = 'master' + pattern
node {
stage ('Checkout'){
checkout scm // just reference SCM in the job executing the Pipeline script.
Expand Down
Binary file not shown.
Loading

0 comments on commit 39c7d3e

Please sign in to comment.