Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix for project roles for JENKINS-1489
  • Loading branch information
kreyssel committed Apr 21, 2011
1 parent 494c3b6 commit b340a52
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
Expand Up @@ -30,7 +30,7 @@
/**
* Associates {@link AbstractProject} with {@link JiraSite}.
*
* @author Kohsuke Kawaguchi
* @author Kohsuke Kawaguchi
*/
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
.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();
Expand Down
46 changes: 43 additions & 3 deletions src/main/java/hudson/plugins/jira/JiraSession.java
Expand Up @@ -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;
Expand Down Expand Up @@ -76,18 +77,28 @@ public Set<String> getProjectKeys() throws RemoteException {
* @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);
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/hudson/plugins/jira/JiraSite.java
Expand Up @@ -57,6 +57,11 @@ public class JiraSite {
*/
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.
*/
Expand Down Expand Up @@ -96,7 +101,7 @@ public class JiraSite {
*/
@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()+"/");
Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/jira/Updater.java
Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand Down
Expand Up @@ -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 />
Expand Down
6 changes: 6 additions & 0 deletions src/main/webapp/help-role-visibility.html
@@ -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
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/hudson/plugins/jira/UpdaterTest.java
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b340a52

Please sign in to comment.