-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WiP] - Non-tested draft for JENKINS-25726
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
- Loading branch information
1 parent
47b292f
commit 36e0003
Showing
6 changed files
with
302 additions
and
1 deletion.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...psys/arc/jenkinsci/plugins/jobrestrictions/restrictions/job/StartedByUserRestriction.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,124 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2014 Oleg Nenashev <o.v.nenashev@gmail.com>. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.synopsys.arc.jenkinsci.plugins.jobrestrictions.restrictions.job; | ||
|
||
import com.synopsys.arc.jenkinsci.plugins.jobrestrictions.Messages; | ||
import com.synopsys.arc.jenkinsci.plugins.jobrestrictions.restrictions.JobRestriction; | ||
import com.synopsys.arc.jenkinsci.plugins.jobrestrictions.restrictions.JobRestrictionDescriptor; | ||
import com.synopsys.arc.jenkinsci.plugins.jobrestrictions.util.UserSelector; | ||
import hudson.Extension; | ||
import hudson.model.Cause; | ||
import hudson.model.Queue; | ||
import hudson.model.Run; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.annotation.Nonnull; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* | ||
* @author Oleg Nenashev <o.v.nenashev@gmail.com> | ||
* @since 0.4 | ||
*/ | ||
public class StartedByUserRestriction extends JobRestriction { | ||
|
||
private final List<UserSelector> usersList; | ||
private final boolean checkUsersFromUpstremProjects; | ||
private final boolean acceptAutomaticRuns; | ||
private transient Set<String> acceptedUsers = null; | ||
|
||
@DataBoundConstructor | ||
public StartedByUserRestriction(List<UserSelector> usersList, boolean checkUsersFromUpstremProjects, boolean acceptAutomaticRuns) { | ||
this.usersList = usersList; | ||
this.checkUsersFromUpstremProjects = checkUsersFromUpstremProjects; | ||
this.acceptAutomaticRuns = acceptAutomaticRuns; | ||
} | ||
|
||
public List<UserSelector> getUsersList() { | ||
return usersList; | ||
} | ||
|
||
public boolean isAcceptAutomaticRuns() { | ||
return acceptAutomaticRuns; | ||
} | ||
|
||
public boolean isCheckUsersFromUpstremProjects() { | ||
return checkUsersFromUpstremProjects; | ||
} | ||
|
||
private synchronized @Nonnull Set<String> getAcceptedUsers() { | ||
if (acceptedUsers == null) { | ||
final List<UserSelector> selectors = getUsersList(); | ||
acceptedUsers = new HashSet<String>(selectors.size()); | ||
for (UserSelector selector : selectors) { | ||
acceptedUsers.add(selector.getSelectedUserId()); // merge equal entries | ||
} | ||
} | ||
return acceptedUsers; | ||
} | ||
|
||
private boolean acceptsUser(String userId) { | ||
return getAcceptedUsers().contains(userId); | ||
} | ||
|
||
/**package*/ boolean canTake(@Nonnull List<Cause> causes) { | ||
for (Cause cause : causes) { | ||
if (cause instanceof Cause.UserIdCause) { | ||
String startedBy = ((Cause.UserIdCause)cause).getUserId(); | ||
if (acceptsUser(startedBy)) { | ||
return true; | ||
} | ||
} | ||
if (checkUsersFromUpstremProjects && cause instanceof Cause.UpstreamCause) { | ||
final List<Cause> upstreamCauses = ((Cause.UpstreamCause)cause).getUpstreamCauses(); | ||
if (canTake(upstreamCauses)) { // Recursive call to iterate through all causes | ||
return true; | ||
} | ||
} | ||
//TODO: check acceptAutomaticRuns | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean canTake(Queue.BuildableItem item) { | ||
return canTake(item.getCauses()); | ||
} | ||
|
||
@Override | ||
public boolean canTake(Run run) { | ||
return canTake(run.getCauses()); | ||
} | ||
|
||
@Extension | ||
public static class DescriptorImpl extends JobRestrictionDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return Messages.restrictions_Job_RegexName(); | ||
} | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
src/main/java/com/synopsys/arc/jenkinsci/plugins/jobrestrictions/util/UserSelector.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,101 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2014 Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.synopsys.arc.jenkinsci.plugins.jobrestrictions.util; | ||
|
||
import hudson.Extension; | ||
import hudson.Util; | ||
import hudson.model.Describable; | ||
import hudson.model.Descriptor; | ||
import hudson.model.User; | ||
import hudson.util.FormValidation; | ||
import java.io.Serializable; | ||
import javax.annotation.CheckForNull; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.QueryParameter; | ||
|
||
/** | ||
* Describable Item, which allows to configure a user. | ||
* @since 0.4 | ||
*/ | ||
//TODO: Autocompletion | ||
public class UserSelector implements Describable<UserSelector>, Serializable { | ||
|
||
/**ID of the user*/ | ||
@CheckForNull String selectedUserId; | ||
|
||
@DataBoundConstructor | ||
public UserSelector(@CheckForNull String selectedUserId) { | ||
this.selectedUserId = hudson.Util.fixEmptyAndTrim(selectedUserId); | ||
} | ||
|
||
@CheckForNull | ||
public String getSelectedUserId() { | ||
return selectedUserId; | ||
} | ||
|
||
@Override | ||
public Descriptor<UserSelector> getDescriptor() { | ||
return DESCRIPTOR; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof UserSelector) { | ||
UserSelector cmp = (UserSelector)obj; | ||
return selectedUserId != null ? selectedUserId.equals(cmp.selectedUserId) : cmp.selectedUserId == null; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int hash = 7; | ||
hash = 17 * hash + (selectedUserId != null ? selectedUserId.hashCode() : 0); | ||
return hash; | ||
} | ||
|
||
@Extension | ||
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); | ||
public static class DescriptorImpl extends Descriptor<UserSelector> { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "N/A"; | ||
} | ||
|
||
public FormValidation doCheckSelectedUserId(@QueryParameter String selectedUserId) { | ||
selectedUserId = Util.fixEmptyAndTrim(selectedUserId); | ||
if (selectedUserId == null) { | ||
return FormValidation.error("Field is empty"); | ||
} | ||
|
||
User user = User.get(selectedUserId, false, null); | ||
if (user == null) { | ||
return FormValidation.warning("User " + selectedUserId + " is not registered in Jenkins"); | ||
} | ||
|
||
return FormValidation.ok(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...c/jenkinsci/plugins/jobrestrictions/restrictions/job/StartdByUserRestriction/config.jelly
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,38 @@ | ||
<!-- | ||
* The MIT License | ||
* | ||
* Copyright 2013 Synopsys Inc., Oleg Nenashev <nenashev@synopsys.com> | ||
* | ||
* 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. | ||
--> | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" | ||
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" | ||
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project"> | ||
|
||
<f:entry title="${%Users list}"> | ||
<f:repeatableProperty field="usersList" add="${%Add user}"/> | ||
</f:entry> | ||
<f:entry field="checkUsersFromUpstremProjects"> | ||
<f:checkbox title="${%Accept upstream runs started by users}" checked="${it.checkUsersFromUpstremProjects}"/> | ||
</f:entry> | ||
<f:entry field="acceptAutomaticRuns"> | ||
<f:checkbox title="${%Accept runs started by other causes}" checked="${it.acceptAutomaticRuns}"/> | ||
</f:entry> | ||
</j:jelly> |
36 changes: 36 additions & 0 deletions
36
...sources/com/synopsys/arc/jenkinsci/plugins/jobrestrictions/util/UserSelector/config.jelly
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,36 @@ | ||
<!-- | ||
* The MIT License | ||
* | ||
* Copyright 2013 Synopsys Inc., Oleg Nenashev <nenashev@synopsys.com> | ||
* | ||
* 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. | ||
--> | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" | ||
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" | ||
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project"> | ||
<f:entry title="${%User ID}" field="selectedUserId"> | ||
<f:textbox/> | ||
</f:entry> | ||
<f:entry title=""> | ||
<div align="right"> | ||
<f:repeatableDeleteButton /> | ||
</div> | ||
</f:entry> | ||
</j:jelly> |
3 changes: 3 additions & 0 deletions
3
...synopsys/arc/jenkinsci/plugins/jobrestrictions/util/UserSelector/help-selectedUserId.html
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,3 @@ | ||
<div> | ||
User ID. A user should be registered in Jenkins. | ||
</div> |