Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #106 from stephenc/jenkins-45322
[JENKINS-45322] Prevent event processing when disabled
  • Loading branch information
stephenc committed Jul 14, 2017
2 parents 0608c06 + 7f970b8 commit 40370fd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>6.0.4</version>
<version>6.1.0-20170713.111738-1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/jenkins/branch/MetadataActionFolderIcon.java
Expand Up @@ -70,6 +70,9 @@ protected void setOwner(AbstractFolder<?> folder) {
@Override
public String getIconClassName() {
if (owner != null) {
if (owner.isDisabled()) {
return "icon-folder-disabled";
}
AvatarMetadataAction action = owner.getAction(AvatarMetadataAction.class);
if (action != null) {
String result = action.getAvatarIconClassName();
Expand Down
52 changes: 49 additions & 3 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Expand Up @@ -548,7 +548,9 @@ public SCMSourceCriteria getSCMSourceCriteria(@NonNull SCMSource source) {
* {@inheritDoc}
*/
public void onSCMSourceUpdated(@NonNull SCMSource source) {
scheduleBuild(0, new BranchIndexingCause());
if (isBuildable()) {
scheduleBuild(0, new BranchIndexingCause());
}
}

/**
Expand Down Expand Up @@ -644,6 +646,11 @@ protected void computeChildren(final ChildObserver<P> observer, final TaskListen

private void scheduleBuild(BranchProjectFactory<P, R> factory, final P item, SCMRevision revision,
TaskListener listener, String name, Cause[] cause, Action... actions) {
if (!isBuildable()) {
listener.getLogger().printf("Did not schedule build for branch: %s (%s is disabled)%n",
name, getDisplayName());
return;
}
Action[] _actions = new Action[actions.length + 1];
_actions[0] = new CauseAction(cause);
System.arraycopy(actions, 0, _actions, 1, actions.length);
Expand Down Expand Up @@ -978,6 +985,17 @@ public void run() {
}
}

/**
* {@inheritDoc}
*/
@Override
protected boolean supportsMakeDisabled() {
if (sources == null || sources.isEmpty()) {
return false;
}
return super.supportsMakeDisabled();
}

/**
* {@inheritDoc}
*/
Expand All @@ -986,7 +1004,7 @@ public boolean isBuildable() {
if (sources == null) {
return false; // still loading
}
return !sources.isEmpty();
return super.isBuildable() && !sources.isEmpty();
}

/**
Expand Down Expand Up @@ -1160,8 +1178,16 @@ private int processHeadCreate(SCMHeadEvent<?> event, TaskListener global, String
throws IOException, InterruptedException {
Set<String> sourceIds = new HashSet<>();
for (MultiBranchProject<?, ?> p : Jenkins.getActiveInstance().getAllItems(MultiBranchProject.class)) {
sourceIds.clear();
String pFullName = p.getFullName();
if (!p.isBuildable()) {
LOGGER.log(Level.FINER, "{0} {1} {2,date} {2,time}: Ignoring {3} because it is disabled",
new Object[]{
eventDescription, eventType, eventTimestamp, pFullName
}
);
continue;
}
sourceIds.clear();
LOGGER.log(Level.FINER, "{0} {1} {2,date} {2,time}: Checking {3} for a match",
new Object[]{
eventDescription, eventType, eventTimestamp, pFullName
Expand Down Expand Up @@ -1328,6 +1354,14 @@ private int processHeadUpdate(SCMHeadEvent<?> event, TaskListener global, String
Set<Job<?, ?>> jobs = new HashSet<>();
for (MultiBranchProject<?, ?> p : Jenkins.getActiveInstance().getAllItems(MultiBranchProject.class)) {
String pFullName = p.getFullName();
if (!p.isBuildable()) {
LOGGER.log(Level.FINER, "{0} {1} {2,date} {2,time}: Ignoring {3} because it is disabled",
new Object[]{
eventDescription, eventType, eventTimestamp, pFullName
}
);
continue;
}
LOGGER.log(Level.FINER, "{0} {1} {2,date} {2,time}: Checking {3} for a match",
new Object[]{
eventDescription, eventType, eventTimestamp, pFullName
Expand Down Expand Up @@ -1671,6 +1705,18 @@ public void onSCMSourceEvent(SCMSourceEvent<?> event) {
try {
for (MultiBranchProject<?, ?> p : Jenkins.getActiveInstance()
.getAllItems(MultiBranchProject.class)) {
if (!p.isBuildable()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER,
"{0} {1} {2,date} {2,time}: Ignoring {3} because it is disabled",
new Object[]{
eventDescription, event.getType().name(), event.getTimestamp(),
p.getFullName()
}
);
}
continue;
}
boolean haveMatch = false;
List<SCMSource> scmSources = p.getSCMSources();
for (SCMSource s : scmSources) {
Expand Down
39 changes: 37 additions & 2 deletions src/main/java/jenkins/branch/OrganizationFolder.java
Expand Up @@ -320,11 +320,30 @@ protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOExceptio
return new OrganizationScan(OrganizationFolder.this, previous);
}

/**
* {@inheritDoc}
*/
@Override
public boolean isHasEvents() {
return true;
}

/**
* {@inheritDoc}
*/
@Override
protected boolean supportsMakeDisabled() {
return !navigators.isEmpty() && !projectFactories.isEmpty();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isBuildable() {
return super.isBuildable() && !navigators.isEmpty() && !projectFactories.isEmpty();
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -926,6 +945,18 @@ public void onSCMHeadEvent(SCMHeadEvent<?> event) {
if (CREATED == event.getType() || UPDATED == event.getType()) {
try {
for (OrganizationFolder p : Jenkins.getActiveInstance().getAllItems(OrganizationFolder.class)) {
if (!p.isBuildable()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER,
"{0} {1} {2,date} {2,time}: Ignoring {3} because it is disabled",
new Object[]{
globalEventDescription,
event.getType().name(), event.getTimestamp(), p.getFullName()
}
);
}
continue;
}
// we want to catch when a branch is created / updated and consequently becomes eligible
// against the criteria. First check if the event matches one of the navigators
SCMNavigator navigator = null;
Expand Down Expand Up @@ -1312,7 +1343,9 @@ public void complete() throws IllegalStateException, IOException, InterruptedExc
} finally {
bc.commit();
}
existing.scheduleBuild(cause());
if (isBuildable()) {
existing.scheduleBuild(cause());
}
return;
}
if (!observer.mayCreate(folderName)) {
Expand Down Expand Up @@ -1350,7 +1383,9 @@ public void complete() throws IllegalStateException, IOException, InterruptedExc
bc.commit();
}
observer.created(project);
project.scheduleBuild(cause());
if (isBuildable()) {
project.scheduleBuild(cause());
}
} finally {
observer.completed(folderName);
}
Expand Down

0 comments on commit 40370fd

Please sign in to comment.