Skip to content
Permalink
Browse files

[FIXED JENKINS-1489] Fix for project roles

  • Loading branch information...
kreyssel authored and kohsuke committed Apr 21, 2011
1 parent 494c3b6 commit a022864fb2e5a1dbc6b183177440a3fb383d8d54
@@ -30,7 +30,7 @@
/**
* Associates {@link AbstractProject} with {@link JiraSite}.
*
* @author Kohsuke Kawaguchi
* @author Kohsuke Kawaguchi
*/
public class JiraProjectProperty extends JobProperty<AbstractProject<?, ?>> {

@@ -168,7 +168,7 @@ public FormValidation doLoginCheck(StaplerRequest request)
}
JiraSite site = new JiraSite(new URL(url), request
.getParameter("user"), request.getParameter("pass"), false,
false, null, false, request.getParameter("groupVisibility"));
false, null, false, request.getParameter("groupVisibility"), request.getParameter("roleVisibility"));
try {
site.createSession();
return FormValidation.ok();
@@ -5,6 +5,7 @@
import hudson.plugins.jira.soap.RemoteGroup;
import hudson.plugins.jira.soap.RemoteIssue;
import hudson.plugins.jira.soap.RemoteProject;
import hudson.plugins.jira.soap.RemoteProjectRole;
import hudson.plugins.jira.soap.RemoteValidationException;

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

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) {
rc.setGroupLevel(groupVisibility);
}
} catch (RemoteValidationException rve) {
LOGGER.throwing(this.getClass().toString(), "addComment", rve);
LOGGER.throwing(this.getClass().toString(), "setGroupLevel", rve);
}

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

@@ -117,6 +128,35 @@ public RemoteGroup getGroup(String groupId) throws RemoteException {
LOGGER.fine("Fetching groupInfo from " + 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 {
return site.existsIssue(id);
@@ -57,6 +57,11 @@
*/
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.
*/
@@ -96,7 +101,7 @@
*/
@DataBoundConstructor
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("/"))
try {
url = new URL(url.toExternalForm()+"/");
@@ -117,6 +122,7 @@ public JiraSite(URL url, String userName, String password, boolean supportsWikiS

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

public String getName() {
@@ -89,7 +89,7 @@ static boolean perform(AbstractBuild<?, ?> build, BuildListener listener) throws

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

session.addComment(issue.id,
createComment(build, useWikiStyleComments,
jenkinsRootUrl, aggregateComment.toString(), recordScmChanges, issue), groupVisibility);
jenkinsRootUrl, aggregateComment.toString(), recordScmChanges, issue), groupVisibility, roleVisibility);
} catch (RemotePermissionException e) {
// Seems like RemotePermissionException can mean 'no permission' as well as
// 'issue doesn't exist'.
// To prevent carrying forward invalid issues forever, we have to drop them
// 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);
}
}
@@ -36,6 +36,9 @@
<f:entry title="${%Visible for Group}" help="/plugin/jira/help-group-visibility.html">
<f:textbox name="jira.groupVisibility" value="${site.groupVisibility}"/>
</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="">
<div align="right">
<f:repeatableDeleteButton />
@@ -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>
@@ -8,7 +8,7 @@
*/
public class MockJiraSite extends JiraSite {
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
@@ -191,7 +191,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
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:
FreeStyleBuild build = mock(FreeStyleBuild.class);
@@ -207,7 +207,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
// test:
List<JiraIssue> ids = Lists.newArrayList(new JiraIssue("FOOBAR-4711", "Title"));
Updater.submitComments(build,
System.out, "http://jenkins" , ids, session, false, false, "");
System.out, "http://jenkins" , ids, session, false, false, "", "");

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

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

0 comments on commit a022864

Please sign in to comment.
You can’t perform that action at this time.