Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data tables #375

Merged
merged 23 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ English text's are mandatory for new entries.
-->

- [ ] English
- […]
- [ ] Franc
mPokornyETM marked this conversation as resolved.
Show resolved Hide resolved
- [ ] Slovak
- [ ] Czech
- [ ] ...

### Submitter checklist

Expand All @@ -61,11 +64,12 @@ English text's are mandatory for new entries.
- Fill in the **Proposed upgrade guidelines** section only if there are breaking changes or changes that may require extra steps from users during the upgrade.
- [ ] There is automated testing or an explanation that explains why this change has no tests.
- [ ] New public classes, fields, and methods are annotated with `@Restricted` or have `@since TODO` Javadocs, as appropriate.
- [ ] New public functions for internal use only are annotated with `@NoExternalUse`. In case it is used by non java code the `Used by {@code <panel>.jelly}` Javadocs are annotated.
- [ ] New deprecations are annotated with `@Deprecated(since = "TODO")` or `@Deprecated(forRemoval = true, since = "TODO")`, if applicable.
- [ ] New or substantially changed JavaScript is not defined inline and does not call `eval` to ease the future introduction of Content Security Policy (CSP) directives (see [documentation](https://www.jenkins.io/doc/developer/security/csp/)).
- [ ] For dependency updates, there are links to external changelogs and, if possible, full differentials.
- [ ] For new APIs and extension points, there is a link to at least one consumer.
- [ ] Any localizations are transfered to *.properties files.
- [ ] Any localizations are transferred to *.properties files.

### Maintainer checklist

Expand Down
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>data-tables-api</artifactId>
</dependency>

<!-- Testing scope -->
<dependency>
Expand Down Expand Up @@ -155,6 +159,11 @@
<artifactId>test-harness</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.jenkins.plugins.lockableresources.LockableResource;
import org.jenkins.plugins.lockableresources.LockableResourcesManager;
import org.jenkins.plugins.lockableresources.Messages;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
Expand Down Expand Up @@ -106,18 +108,60 @@ public LockableResource getResource(final String resourceName) {
return LockableResourcesManager.get().fromName(resourceName);
}

/**
* Get amount of free resources assigned to given *label*
* @param label Label to search.
* @return Amount of free labels.
*/
public int getFreeResourceAmount(String label) {
return LockableResourcesManager.get().getFreeResourceAmount(label);
}

/**
* Get percentage (0-100) usage of resources assigned to given *label*
*
* Used by {@code actions/LockableResourcesRootAction/index.jelly}
* @since TODO
* @param label Label to search.
* @return Percentage usages of *label* around all resources
*/
@Restricted(NoExternalUse.class)
public int getFreeResourcePercentage(String label) {
final int allCount = this.getAssignedResourceAmount(label);
if (allCount == 0) {
return allCount;
}
return (int)((double)this.getFreeResourceAmount(label) / (double)allCount * 100);
}

/**
* Get all existing labels as list.
* @return All possible labels.
*/
public Set<String> getAllLabels() {
return LockableResourcesManager.get().getAllLabels();
}

/**
* Get amount of all labels.
* @return Amount of all labels.
*/
public int getNumberOfAllLabels() {
return LockableResourcesManager.get().getAllLabels().size();
}

/**
* Get amount of resources assigned to given *label*
*
* Used by {@code actions/LockableResourcesRootAction/index.jelly}
* @param label Label to search.
* @return Amount of assigned resources.
*/
@Restricted(NoExternalUse.class)
public int getAssignedResourceAmount(String label) {
return LockableResourcesManager.get().getResourcesWithLabel(label, null).size();
}

@RequirePOST
public void doUnlock(StaplerRequest req, StaplerResponse rsp)
throws IOException, ServletException
Expand Down Expand Up @@ -190,7 +234,12 @@ public void doSteal(StaplerRequest req, StaplerResponse rsp)
public void doReassign(StaplerRequest req, StaplerResponse rsp)
throws IOException, ServletException
{
Jenkins.get().checkPermission(STEAL);
String userName = getUserName();
if (userName == null) {
throw new AccessDeniedException3(Jenkins.getAuthentication2(), STEAL);
}

Jenkins.get().checkAnyPermission(STEAL, Jenkins.ADMINISTER);

String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
Expand All @@ -199,14 +248,6 @@ public void doReassign(StaplerRequest req, StaplerResponse rsp)
return;
}

String userName = getUserName();
if (userName == null
|| (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)
&& !Jenkins.get().hasPermission(STEAL))
) {
throw new AccessDeniedException3(Jenkins.getAuthentication2(), STEAL);
}

if (userName.equals(r.getReservedBy())) {
// Can not achieve much by re-assigning the
// resource I already hold to myself again,
Expand Down
Loading