-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 34af24a
Showing
19 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @jenkinsci/jobselector-plugin-developers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuring-dependabot-version-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: maven | ||
directory: / | ||
schedule: | ||
interval: monthly | ||
- package-ecosystem: github-actions | ||
directory: / | ||
schedule: | ||
interval: monthly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_extends: .github | ||
tag-template: jobselector-$NEXT_MINOR_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/ | ||
|
||
name: Jenkins Security Scan | ||
on: | ||
push: | ||
branches: | ||
- "master" | ||
- "main" | ||
pull_request: | ||
types: [ opened, synchronize, reopened ] | ||
workflow_dispatch: | ||
|
||
permissions: | ||
security-events: write | ||
contents: read | ||
actions: read | ||
|
||
jobs: | ||
security-scan: | ||
uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2 | ||
with: | ||
java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate. | ||
java-version: 11 # What version of Java to set up for the build. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Automates creation of Release Drafts using Release Drafter | ||
# More Info: https://github.com/jenkinsci/.github/blob/master/.github/release-drafter.adoc | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
|
||
jobs: | ||
update_release_draft: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# Drafts your next Release notes as Pull Requests are merged into the default branch | ||
- uses: release-drafter/release-drafter@v5 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
target | ||
|
||
# mvn hpi:run | ||
work | ||
|
||
# IntelliJ IDEA project files | ||
*.iml | ||
*.iws | ||
*.ipr | ||
.idea | ||
|
||
# Eclipse project files | ||
.settings | ||
.classpath | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd"> | ||
<extension> | ||
<groupId>io.jenkins.tools.incrementals</groupId> | ||
<artifactId>git-changelist-maven-extension</artifactId> | ||
<version>1.7</version> | ||
</extension> | ||
</extensions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-Pconsume-incrementals | ||
-Pmight-produce-incrementals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
See the documentation for more options: | ||
https://github.com/jenkins-infra/pipeline-library/ | ||
*/ | ||
buildPlugin( | ||
forkCount: '1C', // run this number of tests in parallel for faster feedback. If the number terminates with a 'C', the value will be multiplied by the number of available CPU cores | ||
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests | ||
configurations: [ | ||
[platform: 'linux', jdk: 21], | ||
[platform: 'windows', jdk: 17], | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License | ||
|
||
Copyright 2023 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# jobselector | ||
|
||
## Introduction | ||
|
||
TODO Describe what your plugin does here | ||
|
||
## Getting started | ||
|
||
TODO Tell users how to configure your plugin here, include screenshots, pipeline examples and | ||
configuration-as-code examples. | ||
|
||
## Issues | ||
|
||
TODO Decide where you're going to host your issues, the default is Jenkins JIRA, but you can also enable GitHub issues, | ||
If you use GitHub issues there's no need for this section; else add the following line: | ||
|
||
Report issues and enhancements in the [Jenkins issue tracker](https://issues.jenkins.io/). | ||
|
||
## Contributing | ||
|
||
TODO review the default [CONTRIBUTING](https://github.com/jenkinsci/.github/blob/master/CONTRIBUTING.md) file and make sure it is appropriate for your plugin, if not then add your own one adapted from the base file | ||
|
||
Refer to our [contribution guidelines](https://github.com/jenkinsci/.github/blob/master/CONTRIBUTING.md) | ||
|
||
## LICENSE | ||
|
||
Licensed under MIT, see [LICENSE](LICENSE.md) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.75</version> | ||
<relativePath /> | ||
</parent> | ||
<groupId>io.jenkins.plugins</groupId> | ||
<artifactId>jobchoice</artifactId> | ||
<version>${revision}${changelist}</version> | ||
<packaging>hpi</packaging> | ||
<name>Job Selector Plugin</name> | ||
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url> | ||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>https://opensource.org/license/mit/</url> | ||
</license> | ||
</licenses> | ||
<scm child.scm.connection.inherit.append.path="false" child.scm.developerConnection.inherit.append.path="false" child.scm.url.inherit.append.path="false"> | ||
<connection>scm:git:https://github.com/${gitHubRepo}</connection> | ||
<developerConnection>scm:git:https://github.com/${gitHubRepo}</developerConnection> | ||
<tag>${scmTag}</tag> | ||
<url>https://github.com/${gitHubRepo}</url> | ||
</scm> | ||
<properties> | ||
<revision>1.0</revision> | ||
<changelist>-SNAPSHOT</changelist> | ||
|
||
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ --> | ||
<jenkins.version>2.387.3</jenkins.version> | ||
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo> | ||
<spotless.check.skip>false</spotless.check.skip> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<!-- Pick up common dependencies for the selected LTS line: https://github.com/jenkinsci/bom#usage --> | ||
<groupId>io.jenkins.tools.bom</groupId> | ||
<artifactId>bom-2.387.x</artifactId> | ||
<version>2543.vfb_1a_5fb_9496d</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>structs</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins.workflow</groupId> | ||
<artifactId>workflow-basic-steps</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins.workflow</groupId> | ||
<artifactId>workflow-cps</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins.workflow</groupId> | ||
<artifactId>workflow-durable-task-step</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins.workflow</groupId> | ||
<artifactId>workflow-job</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<repositories> | ||
<repository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
</repository> | ||
</repositories> | ||
<pluginRepositories> | ||
<pluginRepository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
</pluginRepository> | ||
</pluginRepositories> | ||
</project> |
169 changes: 169 additions & 0 deletions
169
src/main/java/io/jenkins/plugins/jobchoice/JobChoiceParameterDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package io.jenkins.plugins.jobchoice; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.*; | ||
import hudson.model.*; | ||
import java.util.*; | ||
import jenkins.model.Jenkins; | ||
import net.sf.json.JSONObject; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.export.Exported; | ||
import org.springframework.security.access.AccessDeniedException; | ||
|
||
public class JobChoiceParameterDefinition extends SimpleParameterDefinition { | ||
|
||
private Map<String, String> jobs; | ||
private ArrayList<String> choices; | ||
|
||
private final String path; | ||
|
||
private long lastUpdate = 0; | ||
|
||
@DataBoundConstructor | ||
public JobChoiceParameterDefinition(@NonNull final String name, final String path) { | ||
super(name); | ||
this.path = path; | ||
this.updateData(); | ||
} | ||
|
||
@Override | ||
public ParameterValue createValue(String value) { | ||
return createParameterValue(value); | ||
} | ||
|
||
@Override | ||
public ParameterValue createValue(@NonNull StaplerRequest req, JSONObject jo) { | ||
final StringParameterValue value = req.bindJSON(StringParameterValue.class, jo); | ||
|
||
return createParameterValue(value.getValue()); | ||
} | ||
|
||
protected ParameterValue createParameterValue(final String value) throws IllegalArgumentException { | ||
final String outPutValue = jobs.get(value); | ||
if (outPutValue == null) { | ||
throw new IllegalArgumentException(Messages.JobChoiceParameterDefinition_IllegalChoice(value, getName())); | ||
} | ||
|
||
return new StringParameterValue(getName(), Util.fixNull(outPutValue), getDescription()); | ||
} | ||
|
||
private String defaultValue; | ||
|
||
public String getDefaultValue() { | ||
return this.defaultValue; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setDefaultValue(String defaultValue) { | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
private String description; | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
/** | ||
* @return choices | ||
*/ | ||
@Exported | ||
public List<String> getChoices() { | ||
// update data to make sure newest data is used | ||
this.updateData(); | ||
|
||
return this.choices; | ||
} | ||
|
||
private void updateData() throws IllegalArgumentException, AccessDeniedException { | ||
long now = System.currentTimeMillis(); | ||
// if last update is not older than 1min ago do no update. | ||
if (now - lastUpdate < 60000) { | ||
return; | ||
} | ||
lastUpdate = now; | ||
|
||
ItemGroup group = getItemGroupByPath(this.path); | ||
if (group == null) { | ||
throw new IllegalArgumentException(Messages.JobChoiceParameterDefinition_IllegalPath(this.path, getName())); | ||
} | ||
|
||
HashMap<String, String> jobMap = new HashMap<>(); | ||
HashMap<String, Calendar> timeMap = new HashMap<>(); | ||
ArrayList<String> choices = new ArrayList<>(); | ||
|
||
for (Object ob : group.getAllItems()) { | ||
if (!(ob instanceof Item)) { | ||
continue; | ||
} | ||
Item it = (Item) ob; | ||
jobMap.put(it.getName(), it.getFullName()); | ||
choices.add(it.getName()); | ||
if (ob instanceof Job) { | ||
Job j = (Job) ob; | ||
timeMap.put(it.getName(), j.getLastBuild().getTimestamp()); | ||
} | ||
} | ||
this.jobs = jobMap; | ||
|
||
choices.sort((d1, d2) -> { | ||
Calendar c1 = timeMap.get(d1); | ||
Calendar c2 = timeMap.get(d2); | ||
return c1.compareTo(c2); | ||
}); | ||
|
||
this.choices = choices; | ||
} | ||
|
||
private ItemGroup getItemGroupByPath(@NonNull String fullName) throws AccessDeniedException { | ||
StringTokenizer tokens = new StringTokenizer(fullName, "/"); | ||
ItemGroup parent = Jenkins.getInstanceOrNull(); | ||
if (!tokens.hasMoreTokens()) { | ||
return null; | ||
} else { | ||
while (parent != null) { | ||
Item item = parent.getItem(tokens.nextToken()); | ||
if (!tokens.hasMoreTokens()) { | ||
if (item instanceof ItemGroup) { | ||
return (ItemGroup) item; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
if (!(item instanceof ItemGroup)) { | ||
return null; | ||
} | ||
|
||
if (!item.hasPermission(Item.READ)) { | ||
return null; | ||
} | ||
|
||
parent = (ItemGroup) item; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Extension | ||
@Symbol("jobChoice") | ||
public static final class DescriptorImpl extends ParameterDescriptor { | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@NonNull | ||
@Override | ||
public String getDisplayName() { | ||
return Messages.JobChoiceParameterDefinition_DisplayName(); | ||
} | ||
} | ||
} |
Oops, something went wrong.