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

Add new API GET methods #69

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open

Conversation

blanked
Copy link
Contributor

@blanked blanked commented Oct 24, 2020

I thought it'll be good to have GET methods as well. This will allow admins to write automation (eg. reports for publishing user access info) on the plugin without having to resort to script console.

@blanked
Copy link
Contributor Author

blanked commented Oct 25, 2020

ah it looks like the PR-merge build failed due to an issue on the agent? i'll close and reopen the PR to induce a rebuild

@blanked blanked closed this Oct 25, 2020
@blanked blanked reopened this Oct 25, 2020
@blanked blanked closed this Oct 25, 2020
@blanked blanked reopened this Oct 25, 2020
@blanked blanked closed this Oct 25, 2020
@blanked blanked reopened this Oct 25, 2020
@AbhyudayaSharma AbhyudayaSharma self-requested a review October 26, 2020 04:52
@AbhyudayaSharma AbhyudayaSharma added enhancement New feature or request hacktoberfest labels Oct 26, 2020
@AbhyudayaSharma
Copy link
Member

Thanks a lot @blanked ! I will try to review this PR as soon as possible.

@kalyantag
Copy link

@AbhyudayaSharma , Any update on this feature?

@AbhyudayaSharma
Copy link
Member

@kalyantag Sorry, I got busy with other things and forgot about this PR. I will review it this weekend.

@kalyantag
Copy link

Thanks @AbhyudayaSharma

Copy link
Member

@AbhyudayaSharma AbhyudayaSharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @blanked! Thanks a lot for the PR. Sorry for the late review; I completely forgot about it.

The PR looks good. I just have a few questions/suggestions. Please let me know what you think about them.

@Restricted(NoExternalUse.class)
public void doGetAgentRole(@QueryParameter(required = true) String name) throws IOException {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
JSONObject responseJson = new JSONObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can pass a true to the JSONObject constructor so that we return null instead of an empty object ({}). Or maybe we can return a descriptive error message saying that role does not exist.

http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html#JSONObject()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm..I would prefer not to return an error message and instead return either an empty object or null as I find it more inline with other services. I can change it to return null

JSONObject responseJson = new JSONObject();
AgentRole role = FolderAuthorizationStrategyAPI.getAgentRole(name);
if (role != null) {
responseJson.put("name", role.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to let json-lib handle converting the AbstractRole objects to JSONObjects using JSONObject#fromObject?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use JSONObject#fromObject, the role class must be of type string (json formatted), map, etc. The current class doesn't so it'll require a refactor for the class to extend either of these classes it to work. I'll keep it as is? Or we can

responseJson.put("sids", role.getSids());
responseJson.put("permissions", getPermissionsFromRole(role));
}
Stapler.getCurrentResponse().setContentType("application/json;charset=UTF-8");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible to directly return a JSONObject from a doFoo function like done here:

Copy link
Contributor Author

@blanked blanked Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I wasn't aware of that. Is that due to the @GET annotation from stapler? I can try refactoring it and testing if it works

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Verifying that the method returns all 3 roles
String response = page.getWebResponse().getContentAsString();
JSONObject json = JSONObject.fromObject(response);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to convert the JSONObject back to a FolderRole or AbstractRole object using http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html#toBean(net.sf.json.JSONObject,%20java.lang.Class) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my comment before, the roles need to extend map/bean to work. It'll require a refactor of the existing class

src/main/webapp/js/addrole.js Show resolved Hide resolved
@AbhyudayaSharma
Copy link
Member

@blanked Thanks a lot! Looks like the build is failing because of missing @throws in JavaDocs.

Exit code: 1 - /home/jenkins/workspace/Plugins_folder-auth-plugin_PR-69/src/main/java/io/jenkins/plugins/folderauth/FolderAuthorizationStrategyManagementLink.java:459: warning: no @throws for java.io.IOException

@blanked
Copy link
Contributor Author

blanked commented Mar 6, 2021

@blanked Thanks a lot! Looks like the build is failing because of missing @throws in JavaDocs.

Exit code: 1 - /home/jenkins/workspace/Plugins_folder-auth-plugin_PR-69/src/main/java/io/jenkins/plugins/folderauth/FolderAuthorizationStrategyManagementLink.java:459: warning: no @throws for java.io.IOException

Oh the throws IOException was meant to be removed due to the removal of stapler writer but I missed it.

@blanked
Copy link
Contributor Author

blanked commented Mar 6, 2021

Ok I've implemented the suggestions. Can you take another look and see if the PR is good to go? Thanks @AbhyudayaSharma

@blanked blanked requested a review from AbhyudayaSharma March 6, 2021 12:08
@blanked blanked closed this Mar 6, 2021
@blanked blanked reopened this Mar 6, 2021
@blanked blanked closed this Mar 6, 2021
@blanked blanked reopened this Mar 6, 2021
@AbhyudayaSharma
Copy link
Member

AbhyudayaSharma commented Mar 7, 2021 via email

@blanked blanked closed this Mar 7, 2021
@blanked blanked reopened this Mar 7, 2021
@blanked
Copy link
Contributor Author

blanked commented Mar 7, 2021

Ok I finally got a build to run successfully. I believe I've addressed all of the comments/suggestion. Let me know if there are any other comments/suggestions? Thanks!

@AbhyudayaSharma
Copy link
Member

Hi @oleg-nenashev ! If you have some time, could you please take a quick look at this PR?

@AbhyudayaSharma
Copy link
Member

Hi @blanked. Sorry for the delay. What would you think about exporting the entire plugin configuration as JSON as done in https://github.com/jenkinsci/folder-auth-plugin/pull/58/files#diff-237f6cdc333b75a3ea591296a855f117edf78bb4ec1f9cbbc2391811e672304d?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants