|
5 | 5 |
|
6 | 6 | class GogsUtils { |
7 | 7 |
|
8 | | - private GogsUtils() { |
9 | | - } |
| 8 | + private GogsUtils() { |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Search in Jenkins for a item with type T based on the job name |
| 13 | + * |
| 14 | + * @param jobName job to find, for jobs inside a folder use : {@literal <folder>/<folder>/<jobName>} |
| 15 | + * @return the Job matching the given name, or {@code null} when not found |
| 16 | + */ |
| 17 | + static <T extends Item> T find(String jobName, Class<T> type) { |
| 18 | + Jenkins jenkins = Jenkins.getActiveInstance(); |
| 19 | + // direct search, can be used to find folder based items <folder>/<folder>/<jobName> |
| 20 | + T item = jenkins.getItemByFullName(jobName, type); |
| 21 | + if (item == null) { |
| 22 | + // not found in a direct search, search in all items since the item might be in a folder but given without folder structure |
| 23 | + // (to keep it backwards compatible) |
| 24 | + item = jenkins.getAllItems(type).stream().filter(i -> i.getName().equals(jobName)).findFirst().get(); |
| 25 | + } |
| 26 | + return item; |
| 27 | + } |
10 | 28 |
|
11 | | - /** |
12 | | - * Search in Jenkins for a item with type T based on the job name |
13 | | - * @param jobName job to find, for jobs inside a folder use : {@literal <folder>/<folder>/<jobName>} |
14 | | - * @return the Job matching the given name, or {@code null} when not found |
15 | | - */ |
16 | | - static <T extends Item> T find(String jobName, Class<T> type) { |
17 | | - Jenkins jenkins = Jenkins.getActiveInstance(); |
18 | | - // direct search, can be used to find folder based items <folder>/<folder>/<jobName> |
19 | | - T item = jenkins.getItemByFullName(jobName, type); |
20 | | - if (item == null) { |
21 | | - // not found in a direct search, search in all items since the item might be in a folder but given without folder structure |
22 | | - // (to keep it backwards compatible) |
23 | | - for (T allItem : jenkins.getAllItems(type)) { |
24 | | - if (allItem.getName().equals(jobName)) { |
25 | | - item = allItem; |
26 | | - break; |
27 | | - } |
28 | | - } |
29 | | - } |
30 | | - return item; |
31 | | - } |
32 | 29 | } |
0 commit comments