Skip to content

Commit

Permalink
Merge pull request #62 from jonesbusy/bugfix/ignore-workflow-jobs
Browse files Browse the repository at this point in the history
Ignore disabled workflow/pipeline jobs on widget and column
  • Loading branch information
jonesbusy authored Nov 8, 2023
2 parents d1a91e4 + 4a66a45 commit 27031be
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package hudson.plugins.nextexecutions.utils;

import hudson.model.AbstractProject;
import hudson.plugins.nextexecutions.NextBuilds;
import hudson.scheduler.CronTab;
import hudson.scheduler.CronTabList;
import hudson.scheduler.RareOrImpossibleDateException;
import hudson.triggers.Trigger;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
Expand All @@ -23,37 +24,44 @@ public static NextBuilds getNextBuild(
ParameterizedJobMixIn.ParameterizedJob project, Class<? extends Trigger> triggerClass) {
Calendar cal = null;

// Only AbstractProject has isDisabled method
if ((project instanceof AbstractProject && !((AbstractProject) project).isDisabled())
|| !(project instanceof AbstractProject)) {
for (Trigger<?> trigger : project.getTriggers().values()) {
if (trigger.getClass().equals(triggerClass)) {
try {
Field triggerTabsField = Trigger.class.getDeclaredField("tabs");
triggerTabsField.setAccessible(true);
// Skip all disabled jobs
try {
Method isDisabledMethod = project.getClass().getMethod("isDisabled");
isDisabledMethod.setAccessible(true);
if ((Boolean) isDisabledMethod.invoke(project)) {
return null;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// Do nothing
}

for (Trigger<?> trigger : project.getTriggers().values()) {
if (trigger.getClass().equals(triggerClass)) {
try {
Field triggerTabsField = Trigger.class.getDeclaredField("tabs");
triggerTabsField.setAccessible(true);

CronTabList cronTabList = (CronTabList) triggerTabsField.get(trigger);
CronTabList cronTabList = (CronTabList) triggerTabsField.get(trigger);

Field crontablistTabsField = CronTabList.class.getDeclaredField("tabs");
crontablistTabsField.setAccessible(true);
Field crontablistTabsField = CronTabList.class.getDeclaredField("tabs");
crontablistTabsField.setAccessible(true);

List<CronTab> crons = (Vector<CronTab>) crontablistTabsField.get(cronTabList);
List<CronTab> crons = (Vector<CronTab>) crontablistTabsField.get(cronTabList);

for (CronTab cronTab : crons) {
TimeZone timezone =
cronTab.getTimeZone() != null ? cronTab.getTimeZone() : TimeZone.getDefault();
try {
Calendar next = cronTab.ceil(new GregorianCalendar(timezone));
if (cal == null || cal.compareTo(next) > 0) {
cal = next;
}
} catch (RareOrImpossibleDateException ignored) {
// Ignore this crontab because its next date won't occur in the next two years
for (CronTab cronTab : crons) {
TimeZone timezone =
cronTab.getTimeZone() != null ? cronTab.getTimeZone() : TimeZone.getDefault();
try {
Calendar next = cronTab.ceil(new GregorianCalendar(timezone));
if (cal == null || cal.compareTo(next) > 0) {
cal = next;
}
} catch (RareOrImpossibleDateException ignored) {
// Ignore this crontab because its next date won't occur in the next two years
}
} catch (NoSuchFieldException | IllegalAccessException e) {
// Do nothing
}
} catch (NoSuchFieldException | IllegalAccessException e) {
// Do nothing
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package hudson.plugins.nextexecutions.utils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.AbstractProject;
import hudson.plugins.nextexecutions.NextBuilds;
import hudson.scheduler.CronTab;
import hudson.scheduler.CronTabList;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
Expand All @@ -31,44 +32,51 @@ public static NextBuilds getNextBuild(
ParameterizedJobMixIn.ParameterizedJob project, Class<? extends Trigger> triggerClass) {
Calendar cal = null;
TimeZone timezone = null;
// Only AbstractProject has isDisabled method
if ((project instanceof AbstractProject && !((AbstractProject) project).isDisabled())
|| !(project instanceof AbstractProject)) {
Map<TriggerDescriptor, Trigger<?>> triggers = project.getTriggers();
Iterator<Map.Entry<TriggerDescriptor, Trigger<?>>> iterator =
triggers.entrySet().iterator();
while (iterator.hasNext()) {
Trigger trigger = iterator.next().getValue();
if (trigger.getClass().equals(triggerClass) && triggerClass.equals(ParameterizedTimerTrigger.class)) {
try {
Field triggerTabsField = ParameterizedTimerTrigger.class.getDeclaredField("cronTabList");
triggerTabsField.setAccessible(true);

ParameterizedCronTabList parameterizedCronTabList =
(ParameterizedCronTabList) triggerTabsField.get(trigger);
// Skip all disabled jobs
try {
Method isDisabledMethod = project.getClass().getMethod("isDisabled");
isDisabledMethod.setAccessible(true);
if ((Boolean) isDisabledMethod.invoke(project)) {
return null;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// Do nothing
}

Map<TriggerDescriptor, Trigger<?>> triggers = project.getTriggers();
Iterator<Map.Entry<TriggerDescriptor, Trigger<?>>> iterator =
triggers.entrySet().iterator();
while (iterator.hasNext()) {
Trigger trigger = iterator.next().getValue();
if (trigger.getClass().equals(triggerClass) && triggerClass.equals(ParameterizedTimerTrigger.class)) {
try {
Field triggerTabsField = ParameterizedTimerTrigger.class.getDeclaredField("cronTabList");
triggerTabsField.setAccessible(true);

ParameterizedCronTabList parameterizedCronTabList =
(ParameterizedCronTabList) triggerTabsField.get(trigger);

Field crontablistTabsField = ParameterizedCronTabList.class.getDeclaredField("cronTabs");
crontablistTabsField.setAccessible(true);
List<ParameterizedCronTab> parameterizedCrons =
(ArrayList<ParameterizedCronTab>) crontablistTabsField.get(parameterizedCronTabList);
Field crontablistTabsField = ParameterizedCronTabList.class.getDeclaredField("cronTabs");
crontablistTabsField.setAccessible(true);
List<ParameterizedCronTab> parameterizedCrons =
(ArrayList<ParameterizedCronTab>) crontablistTabsField.get(parameterizedCronTabList);

for (ParameterizedCronTab parameterizedCron : parameterizedCrons) {
Field crontablistField = ParameterizedCronTab.class.getDeclaredField("cronTabList");
crontablistField.setAccessible(true);
CronTabList list = (CronTabList) crontablistField.get(parameterizedCron);
Field crontablistTabsField1 = CronTabList.class.getDeclaredField("tabs");
crontablistTabsField1.setAccessible(true);
List<CronTab> crons = (Vector<CronTab>) crontablistTabsField1.get(list);
for (CronTab cronTab : crons) {
timezone =
cronTab.getTimeZone() != null ? cronTab.getTimeZone() : TimeZone.getDefault();
Calendar now = new GregorianCalendar(timezone);
cal = (cal == null || cal.compareTo(cronTab.ceil(now)) > 0) ? cronTab.ceil(now) : cal;
}
for (ParameterizedCronTab parameterizedCron : parameterizedCrons) {
Field crontablistField = ParameterizedCronTab.class.getDeclaredField("cronTabList");
crontablistField.setAccessible(true);
CronTabList list = (CronTabList) crontablistField.get(parameterizedCron);
Field crontablistTabsField1 = CronTabList.class.getDeclaredField("tabs");
crontablistTabsField1.setAccessible(true);
List<CronTab> crons = (Vector<CronTab>) crontablistTabsField1.get(list);
for (CronTab cronTab : crons) {
timezone = cronTab.getTimeZone() != null ? cronTab.getTimeZone() : TimeZone.getDefault();
Calendar now = new GregorianCalendar(timezone);
cal = (cal == null || cal.compareTo(cronTab.ceil(now)) > 0) ? cronTab.ceil(now) : cal;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
// Do nothing
}
} catch (NoSuchFieldException | IllegalAccessException e) {
// Do nothing
}
}
}
Expand Down

0 comments on commit 27031be

Please sign in to comment.