Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed May 23, 2019
1 parent 2e15272 commit 7adf973
Showing 1 changed file with 129 additions and 124 deletions.
253 changes: 129 additions & 124 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1973,106 +1973,115 @@ public void observe(@NonNull SCMHead head, @NonNull SCMRevision revision) throws
P project = observer.shouldUpdate(encodedName);
try {
Branch origBranch = getOrigBranch(project);;
if (project != null && origBranch == null) {
return;
}
boolean headActionsFetched = isHeadActionsFetched(head, branch, rawName);
Action[] revisionActions = getRevisionActions(revision, rawName);
if (project != null) {
boolean rebuild = (origBranch instanceof Branch.Dead && !(branch instanceof Branch.Dead))
|| !(source.getId().equals(origBranch.getSourceId()));
if (!headActionsFetched) {
// we didn't fetch them so replicate previous actions
branch.setActions(origBranch.getActions());
if (origBranch == null) {
return;
}
boolean needSave = !branch.equals(origBranch)
|| !branch.getActions().equals(origBranch.getActions())
|| !Util.getDigestOf(Items.XSTREAM2.toXML(branch.getScm()))
.equals(Util.getDigestOf(Items.XSTREAM2.toXML(origBranch.getScm())));
_factory.decorate(_factory.setBranch(project, branch));
if (rebuild) {
needSave = true;
listener.getLogger().format(
"%s reopened: %s (%s)%n",
StringUtils.defaultIfEmpty(head.getPronoun(), "Branch"),
rawName,
revision
);
// the previous "revision" for this head is not a revision for the current source
// either because the head was removed and then recreated, or because the head
// was taken over by a different source, thus the previous revision is null
doAutomaticBuilds(head, revision, rawName, project, revisionActions, null, null);
} else {
// get the previous revision
SCMRevision scmLastBuiltRevision = _factory.getRevision(project);

if (isChangesDetected(revision, project, scmLastBuiltRevision)) {
needSave = true;
// get the previous seen revision
SCMRevision scmLastSeenRevision = getLastSeenRevision(project, scmLastBuiltRevision);
observeProject(head, revision, branch, rawName, project, origBranch, headActionsFetched, revisionActions);
} else {
observeNew(head, revision, branch, rawName, encodedName, revisionActions);
}
} finally {
observer.completed(encodedName);
}
}

listener.getLogger()
.format("Changes detected: %s (%s → %s)%n", rawName, scmLastBuiltRevision, revision);
doAutomaticBuilds(head, revision, rawName, project, revisionActions, scmLastBuiltRevision, scmLastSeenRevision);
private void observeProject(@NonNull SCMHead head, @NonNull SCMRevision revision, @NonNull Branch branch, String rawName, P project, Branch origBranch, boolean headActionsFetched, Action[] revisionActions) {
boolean rebuild = (origBranch instanceof Branch.Dead && !(branch instanceof Branch.Dead))
|| !(source.getId().equals(origBranch.getSourceId()));
if (!headActionsFetched) {
// we didn't fetch them so replicate previous actions
branch.setActions(origBranch.getActions());
}
boolean needSave = !branch.equals(origBranch)
|| !branch.getActions().equals(origBranch.getActions())
|| !Util.getDigestOf(Items.XSTREAM2.toXML(branch.getScm()))
.equals(Util.getDigestOf(Items.XSTREAM2.toXML(origBranch.getScm())));
_factory.decorate(_factory.setBranch(project, branch));
if (rebuild) {
needSave = true;
listener.getLogger().format(
"%s reopened: %s (%s)%n",
StringUtils.defaultIfEmpty(head.getPronoun(), "Branch"),
rawName,
revision
);
// the previous "revision" for this head is not a revision for the current source
// either because the head was removed and then recreated, or because the head
// was taken over by a different source, thus the previous revision is null
doAutomaticBuilds(head, revision, rawName, project, revisionActions, null, null);
} else {
// get the previous revision
SCMRevision scmLastBuiltRevision = _factory.getRevision(project);

} else {
listener.getLogger().format("No changes detected: %s (still at %s)%n", rawName, revision);
}
if (isChangesDetected(revision, project, scmLastBuiltRevision)) {
needSave = true;
// get the previous seen revision
SCMRevision scmLastSeenRevision = getLastSeenRevision(project, scmLastBuiltRevision);

}
listener.getLogger()
.format("Changes detected: %s (%s → %s)%n", rawName, scmLastBuiltRevision, revision);
doAutomaticBuilds(head, revision, rawName, project, revisionActions, scmLastBuiltRevision, scmLastSeenRevision);

try {
if (needSave) {
project.save();
}
} catch (IOException e) {
printStackTrace(e, listener.error("Could not save changes to " + rawName));
}
return;
}
if (!observer.mayCreate(encodedName)) {
listener.getLogger().println("Ignoring duplicate branch project " + rawName);
return;
} else {
listener.getLogger().format("No changes detected: %s (still at %s)%n", rawName, revision);
}
try (ChildNameGenerator.Trace trace = ChildNameGenerator.beforeCreateItem(
MultiBranchProject.this, encodedName, branch.getName()
)) {
if (getItem(encodedName) != null) {
throw new IllegalStateException(
"JENKINS-42511: attempted to redundantly create " + encodedName + " in "
+ MultiBranchProject.this);
}
project = _factory.newInstance(branch);

}

try {
if (needSave) {
project.save();
}
if (!project.getName().equals(encodedName)) {
} catch (IOException e) {
printStackTrace(e, listener.error("Could not save changes to " + rawName));
}
}

private void observeNew(@NonNull SCMHead head, @NonNull SCMRevision revision, @NonNull Branch branch, String rawName, String encodedName, Action[] revisionActions) {
P project;
if (!observer.mayCreate(encodedName)) {
listener.getLogger().println("Ignoring duplicate branch project " + rawName);
return;
}
try (ChildNameGenerator.Trace trace = ChildNameGenerator.beforeCreateItem(
MultiBranchProject.this, encodedName, branch.getName()
)) {
if (getItem(encodedName) != null) {
throw new IllegalStateException(
"Name of created project " + project + " did not match expected " + encodedName);
"JENKINS-42511: attempted to redundantly create " + encodedName + " in "
+ MultiBranchProject.this);
}
// HACK ALERT
// ==========
// We don't want to trigger a save, so we will do some trickery to ensure that observer.created(project)
// performs the save
BulkChange bc = new BulkChange(project);
try {
if (project.getDisplayNameOrNull() == null && !rawName.equals(encodedName)) {
project.setDisplayName(rawName);
}
} catch (IOException e) {
// ignore even if it does happen we didn't want a save
} finally {
bc.abort();
project = _factory.newInstance(branch);
}
if (!project.getName().equals(encodedName)) {
throw new IllegalStateException(
"Name of created project " + project + " did not match expected " + encodedName);
}
// HACK ALERT
// ==========
// We don't want to trigger a save, so we will do some trickery to ensure that observer.created(project)
// performs the save
BulkChange bc = new BulkChange(project);
try {
if (project.getDisplayNameOrNull() == null && !rawName.equals(encodedName)) {
project.setDisplayName(rawName);
}
// decorate contract is to ensure it dowes not trigger a save
_factory.decorate(project);
// ok it is now up to the observer to ensure it does the actual save.
observer.created(project);
doAutomaticBuilds(head, revision, rawName, project, revisionActions, null, null);
} catch (IOException e) {
// ignore even if it does happen we didn't want a save
} finally {
observer.completed(encodedName);
bc.abort();
}
// decorate contract is to ensure it dowes not trigger a save
_factory.decorate(project);
// ok it is now up to the observer to ensure it does the actual save.
observer.created(project);
doAutomaticBuilds(head, revision, rawName, project, revisionActions, null, null);
}

private boolean isChangesDetected(@NonNull SCMRevision revision, P project, SCMRevision scmLastBuiltRevision) {
private boolean isChangesDetected(@NonNull SCMRevision revision, @NonNull P project, SCMRevision scmLastBuiltRevision) {
boolean changesDetected = false;

if (revision.isDeterministic()) {
Expand Down Expand Up @@ -2112,7 +2121,7 @@ private Action[] getRevisionActions(@NonNull SCMRevision revision, String rawNam
return revisionActions;
}

private boolean isHeadActionsFetched(@NonNull SCMHead head, Branch branch, String rawName) {
private boolean isHeadActionsFetched(@NonNull SCMHead head, @NonNull Branch branch, String rawName) {
boolean headActionsFetched = false;
try {
branch.setActions(source.fetchActions(head, event, listener));
Expand Down Expand Up @@ -2173,7 +2182,7 @@ private Branch getOrigBranch(P project) {
return origBranch;
}

private SCMRevision getLastSeenRevision(P project, SCMRevision scmLastBuiltRevision) {
private SCMRevision getLastSeenRevision(@NonNull P project, SCMRevision scmLastBuiltRevision) {
SCMRevision scmLastSeenRevision = _factory.getLastSeenRevision(project);
if (scmLastSeenRevision == null && scmLastBuiltRevision != null) {
scmLastSeenRevision = scmLastBuiltRevision;
Expand All @@ -2186,8 +2195,8 @@ private SCMRevision getLastSeenRevision(P project, SCMRevision scmLastBuiltRevis
return scmLastSeenRevision;
}

private void doAutomaticBuilds(@NonNull SCMHead head, @NonNull SCMRevision revision, String rawName, P project, Action[] revisionActions, SCMRevision scmLastBuiltRevision, SCMRevision scmLastSeenRevision) {
if (isAutomaticBuild(source, head, revision, scmLastBuiltRevision, scmLastSeenRevision, listener)) {
private void doAutomaticBuilds(@NonNull SCMHead head, @NonNull SCMRevision revision, @NonNull String rawName, @NonNull P project, Action[] revisionActions, SCMRevision scmLastBuiltRevision, SCMRevision scmLastSeenRevision) {
if (isAutomaticBuild(head, revision, scmLastBuiltRevision, scmLastSeenRevision)) {
scheduleBuild(
_factory,
project,
Expand All @@ -2198,54 +2207,50 @@ private void doAutomaticBuilds(@NonNull SCMHead head, @NonNull SCMRevision revis
revisionActions
);
} else {
listener.getLogger().format("No automatic builds for %s%n", rawName);
listener.getLogger().format("No automatic build triggered for %s%n", rawName);
}
try {
_factory.setLastSeenRevisionHash(project, revision);
} catch (IOException e) {
printStackTrace(e, listener.error("Could not update last seen revision hash"));
}
}
}

/**
* Tests if the specified {@link SCMHead} should be automatically built when discovered / modified.
* @param source the source.
* @param head the head.
* @param currRevision the current built revision.
* @param lastBuiltRevision the previous built revision
* @param listener the {@link TaskListener}
* @param lastSeenRevision the last seen revision
* @return {@code true} if the head should be automatically built when discovered / modified.
*/
private boolean isAutomaticBuild(@NonNull SCMSource source,
@NonNull SCMHead head,
@NonNull SCMRevision currRevision,
@CheckForNull SCMRevision lastBuiltRevision,
@CheckForNull SCMRevision lastSeenRevision,
@NonNull TaskListener listener) {
BranchSource branchSource = null;
for (BranchSource s: sources) {
if (s.getSource().getId().equals(source.getId())) {
branchSource = s;
break;
/**
* Tests if the specified {@link SCMHead} should be automatically built when discovered / modified.
* @param head the head.
* @param currRevision the current built revision.
* @param lastBuiltRevision the previous built revision
* @param lastSeenRevision the last seen revision
* @return {@code true} if the head should be automatically built when discovered / modified.
*/
private boolean isAutomaticBuild(@NonNull SCMHead head,
@NonNull SCMRevision currRevision,
@CheckForNull SCMRevision lastBuiltRevision,
@CheckForNull SCMRevision lastSeenRevision) {
BranchSource branchSource = null;
for (BranchSource s: MultiBranchProject.this.sources) {
if (s.getSource().getId().equals(source.getId())) {
branchSource = s;
break;
}
}
}
if (branchSource == null) {
// no match, means no build
return false;
}
List<BranchBuildStrategy> buildStrategies = branchSource.getBuildStrategies();
if (buildStrategies.isEmpty()) {
// we will use default behaviour, build anything but tags
return !(head instanceof TagSCMHead);
} else {
for (BranchBuildStrategy s: buildStrategies) {
if (s.automaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener)) {
return true;
if (branchSource == null) {
// no match, means no build
return false;
}
List<BranchBuildStrategy> buildStrategies = branchSource.getBuildStrategies();
if (buildStrategies.isEmpty()) {
// we will use default behaviour, build anything but tags
return !(head instanceof TagSCMHead);
} else {
for (BranchBuildStrategy s: buildStrategies) {
if (s.automaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener)) {
return true;
}
}
return false;
}
return false;
}
}

Expand Down

0 comments on commit 7adf973

Please sign in to comment.