Skip to content

Commit

Permalink
[FIXED JENKINS-1489] Fix for project roles
Browse files Browse the repository at this point in the history
  • Loading branch information
kreyssel authored and kohsuke committed Jun 15, 2011
1 parent 494c3b6 commit a022864
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/jira/JiraProjectProperty.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/** /**
* Associates {@link AbstractProject} with {@link JiraSite}. * Associates {@link AbstractProject} with {@link JiraSite}.
* *
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
public class JiraProjectProperty extends JobProperty<AbstractProject<?, ?>> { public class JiraProjectProperty extends JobProperty<AbstractProject<?, ?>> {


Expand Down Expand Up @@ -168,7 +168,7 @@ public FormValidation doLoginCheck(StaplerRequest request)
} }
JiraSite site = new JiraSite(new URL(url), request JiraSite site = new JiraSite(new URL(url), request
.getParameter("user"), request.getParameter("pass"), false, .getParameter("user"), request.getParameter("pass"), false,
false, null, false, request.getParameter("groupVisibility")); false, null, false, request.getParameter("groupVisibility"), request.getParameter("roleVisibility"));
try { try {
site.createSession(); site.createSession();
return FormValidation.ok(); return FormValidation.ok();
Expand Down
46 changes: 43 additions & 3 deletions src/main/java/hudson/plugins/jira/JiraSession.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hudson.plugins.jira.soap.RemoteGroup; import hudson.plugins.jira.soap.RemoteGroup;
import hudson.plugins.jira.soap.RemoteIssue; import hudson.plugins.jira.soap.RemoteIssue;
import hudson.plugins.jira.soap.RemoteProject; import hudson.plugins.jira.soap.RemoteProject;
import hudson.plugins.jira.soap.RemoteProjectRole;
import hudson.plugins.jira.soap.RemoteValidationException; import hudson.plugins.jira.soap.RemoteValidationException;


import java.rmi.RemoteException; import java.rmi.RemoteException;
Expand Down Expand Up @@ -76,18 +77,28 @@ public Set<String> getProjectKeys() throws RemoteException {
* @param groupVisibility * @param groupVisibility
*/ */
public void addComment(String issueId, String comment, public void addComment(String issueId, String comment,
String groupVisibility) throws RemoteException { String groupVisibility, String roleVisibility) throws RemoteException {
RemoteComment rc = new RemoteComment(); RemoteComment rc = new RemoteComment();
rc.setBody(comment); rc.setBody(comment);


try { try {
if (groupVisibility != null && groupVisibility != "" if (roleVisibility != null && roleVisibility.equals("") == false
&& getRole(roleVisibility) != null) {
rc.setRoleLevel(roleVisibility);
}
} catch (RemoteValidationException rve) {
LOGGER.throwing(this.getClass().toString(), "setRoleLevel", rve);
}

try {
if (groupVisibility != null && groupVisibility.equals("") == false
&& getGroup(groupVisibility) != null) { && getGroup(groupVisibility) != null) {
rc.setGroupLevel(groupVisibility); rc.setGroupLevel(groupVisibility);
} }
} catch (RemoteValidationException rve) { } catch (RemoteValidationException rve) {
LOGGER.throwing(this.getClass().toString(), "addComment", rve); LOGGER.throwing(this.getClass().toString(), "setGroupLevel", rve);
} }

service.addComment(token, issueId, rc); service.addComment(token, issueId, rc);
} }


Expand Down Expand Up @@ -117,6 +128,35 @@ public RemoteGroup getGroup(String groupId) throws RemoteException {
LOGGER.fine("Fetching groupInfo from " + groupId); LOGGER.fine("Fetching groupInfo from " + groupId);
return service.getGroup(token, groupId); return service.getGroup(token, groupId);
} }

/**
* Gets the details of a role, given a roleId. Used for validating role
* visibility.
*
* TODO: Cannot validate against the real project role the user have in the project,
* jira soap api has no such function!
*
* @param Role
* ID like "Software Development"
* @return null if no such role exists
*/
public RemoteProjectRole getRole(String roleId) throws RemoteException {
LOGGER.fine("Fetching roleInfo from " + roleId);

RemoteProjectRole[] roles= service.getProjectRoles(token);

if(roles != null && roles.length > 0) {
for(RemoteProjectRole role : roles) {
if(role != null && role.getName() != null && role.getName().equals(roleId)) {
return role;
}
}
}

LOGGER.info("Did not find role named " + roleId + ".");

return null;
}


public boolean existsIssue(String id) throws RemoteException { public boolean existsIssue(String id) throws RemoteException {
return site.existsIssue(id); return site.existsIssue(id);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/hudson/plugins/jira/JiraSite.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class JiraSite {
*/ */
public final String groupVisibility; public final String groupVisibility;


/**
* Role visibility to constrain the visibility of the added comment. Optional.
*/
public final String roleVisibility;

/** /**
* True if this JIRA is configured to allow Confluence-style Wiki comment. * True if this JIRA is configured to allow Confluence-style Wiki comment.
*/ */
Expand Down Expand Up @@ -96,7 +101,7 @@ public class JiraSite {
*/ */
@DataBoundConstructor @DataBoundConstructor
public JiraSite(URL url, String userName, String password, boolean supportsWikiStyleComment, boolean recordScmChanges, String userPattern, public JiraSite(URL url, String userName, String password, boolean supportsWikiStyleComment, boolean recordScmChanges, String userPattern,
boolean updateJiraIssueForAllStatus, String groupVisibility) { boolean updateJiraIssueForAllStatus, String groupVisibility, String roleVisibility) {
if(!url.toExternalForm().endsWith("/")) if(!url.toExternalForm().endsWith("/"))
try { try {
url = new URL(url.toExternalForm()+"/"); url = new URL(url.toExternalForm()+"/");
Expand All @@ -117,6 +122,7 @@ public JiraSite(URL url, String userName, String password, boolean supportsWikiS


this.updateJiraIssueForAllStatus = updateJiraIssueForAllStatus; this.updateJiraIssueForAllStatus = updateJiraIssueForAllStatus;
this.groupVisibility = Util.fixEmpty(groupVisibility); this.groupVisibility = Util.fixEmpty(groupVisibility);
this.roleVisibility = Util.fixEmpty(roleVisibility);
} }


public String getName() { public String getName() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/jira/Updater.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static boolean perform(AbstractBuild<?, ?> build, BuildListener listener) throws


if (doUpdate) { if (doUpdate) {
submitComments(build, logger, rootUrl, issues, submitComments(build, logger, rootUrl, issues,
session, useWikiStyleComments, site.recordScmChanges, site.groupVisibility); session, useWikiStyleComments, site.recordScmChanges, site.groupVisibility, site.roleVisibility);
} else { } else {
// this build didn't work, so carry forward the issues to the next build // this build didn't work, so carry forward the issues to the next build
build.addAction(new JiraCarryOverAction(issues)); build.addAction(new JiraCarryOverAction(issues));
Expand Down Expand Up @@ -122,7 +122,7 @@ static boolean perform(AbstractBuild<?, ?> build, BuildListener listener) throws
static void submitComments( static void submitComments(
AbstractBuild<?, ?> build, PrintStream logger, String jenkinsRootUrl, AbstractBuild<?, ?> build, PrintStream logger, String jenkinsRootUrl,
List<JiraIssue> issues, JiraSession session, List<JiraIssue> issues, JiraSession session,
boolean useWikiStyleComments, boolean recordScmChanges, String groupVisibility) throws RemoteException { boolean useWikiStyleComments, boolean recordScmChanges, String groupVisibility, String roleVisibility) throws RemoteException {
// copy to prevent ConcurrentModificationException // copy to prevent ConcurrentModificationException
List<JiraIssue> copy = new ArrayList<JiraIssue>(issues); List<JiraIssue> copy = new ArrayList<JiraIssue>(issues);
for (JiraIssue issue : copy) { for (JiraIssue issue : copy) {
Expand All @@ -140,13 +140,13 @@ static void submitComments(


session.addComment(issue.id, session.addComment(issue.id,
createComment(build, useWikiStyleComments, createComment(build, useWikiStyleComments,
jenkinsRootUrl, aggregateComment.toString(), recordScmChanges, issue), groupVisibility); jenkinsRootUrl, aggregateComment.toString(), recordScmChanges, issue), groupVisibility, roleVisibility);
} catch (RemotePermissionException e) { } catch (RemotePermissionException e) {
// Seems like RemotePermissionException can mean 'no permission' as well as // Seems like RemotePermissionException can mean 'no permission' as well as
// 'issue doesn't exist'. // 'issue doesn't exist'.
// To prevent carrying forward invalid issues forever, we have to drop them // To prevent carrying forward invalid issues forever, we have to drop them
// even if the cause of the exception was different. // even if the cause of the exception was different.
logger.println("Looks like " + issue.id + " is no valid JIRA issue. Issue will not be updated.\n" + e); logger.println("Looks like " + issue.id + " is no valid JIRA issue. Issue will not be updated or you dont have valid rights.\n" + e);
issues.remove(issue); issues.remove(issue);
} }
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<f:entry title="${%Visible for Group}" help="/plugin/jira/help-group-visibility.html"> <f:entry title="${%Visible for Group}" help="/plugin/jira/help-group-visibility.html">
<f:textbox name="jira.groupVisibility" value="${site.groupVisibility}"/> <f:textbox name="jira.groupVisibility" value="${site.groupVisibility}"/>
</f:entry> </f:entry>
<f:entry title="${%Visible for Project Role}" help="/plugin/jira/help-role-visibility.html">
<f:textbox name="jira.roleVisibility" value="${site.roleVisibility}"/>
</f:entry>
<f:entry title=""> <f:entry title="">
<div align="right"> <div align="right">
<f:repeatableDeleteButton /> <f:repeatableDeleteButton />
Expand Down
6 changes: 6 additions & 0 deletions src/main/webapp/help-role-visibility.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
Enter the name of the JIRA project role that has permission to view
the comment, leave the field empty to make the comment
available to all JIRA users.
<br>
</div>
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/jira/MockJiraSite.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/ */
public class MockJiraSite extends JiraSite { public class MockJiraSite extends JiraSite {
public MockJiraSite() throws MalformedURLException { public MockJiraSite() throws MalformedURLException {
super(new URL("http://www.sun.com/"),null,null,false, false, null, false,""); super(new URL("http://www.sun.com/"),null,null,false, false, null, false,"","");
} }


@Override @Override
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/hudson/plugins/jira/UpdaterTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
return null; return null;
} }
}; };
doAnswer(answer).when(session).addComment(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); doAnswer(answer).when(session).addComment(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),Mockito.anyString());


// mock build: // mock build:
FreeStyleBuild build = mock(FreeStyleBuild.class); FreeStyleBuild build = mock(FreeStyleBuild.class);
Expand All @@ -207,7 +207,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
// test: // test:
List<JiraIssue> ids = Lists.newArrayList(new JiraIssue("FOOBAR-4711", "Title")); List<JiraIssue> ids = Lists.newArrayList(new JiraIssue("FOOBAR-4711", "Title"));
Updater.submitComments(build, Updater.submitComments(build,
System.out, "http://jenkins" , ids, session, false, false, ""); System.out, "http://jenkins" , ids, session, false, false, "", "");


Assert.assertEquals(1, comments.size()); Assert.assertEquals(1, comments.size());
RemoteComment comment = comments.get(0); RemoteComment comment = comments.get(0);
Expand All @@ -222,7 +222,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
when(changeLogSet.iterator()).thenReturn(entries.iterator()); when(changeLogSet.iterator()).thenReturn(entries.iterator());
ids = Lists.newArrayList(new JiraIssue("FOOBAR-4711", "Title")); ids = Lists.newArrayList(new JiraIssue("FOOBAR-4711", "Title"));
Updater.submitComments(build, Updater.submitComments(build,
System.out, "http://jenkins" , ids, session, false, false,""); System.out, "http://jenkins" , ids, session, false, false,"", "");


Assert.assertEquals(1, comments.size()); Assert.assertEquals(1, comments.size());
comment = comments.get(0); comment = comments.get(0);
Expand Down

0 comments on commit a022864

Please sign in to comment.