Skip to content

Commit

Permalink
first working version of Jira RequestIntegration for PPM 9.50
Browse files Browse the repository at this point in the history
  • Loading branch information
Canaud committed May 10, 2018
1 parent dd2e739 commit 784b0dc
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

public class JIRAConstants {

public static final String JIRA_NAME_PREFIX = "#@#name#@#";

public static final String NULL_VALUE = "<null>";

public static final String REPLACE_PROJECT_KEY = "%PROJECT_KEY%";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.util.*;

import static com.ppm.integration.agilesdk.connector.jira.JIRAConstants.JIRA_NAME_PREFIX;

public class JIRARequestIntegration extends RequestIntegration {

private JIRAServiceProvider service = new JIRAServiceProvider().useAdminAccount();
Expand All @@ -33,40 +35,45 @@ public class JIRARequestIntegration extends RequestIntegration {
{
List<AgileEntityFieldInfo> fieldsInfo = new ArrayList<>();

List<JIRAFieldInfo> fields = service.get(instanceConfigurationParameters).getFields(agileProjectValue, entityType);
List<JIRAFieldInfo> fields = new ArrayList(service.get(instanceConfigurationParameters).getFields(agileProjectValue, entityType).values());

for (JIRAFieldInfo field: fields) {

// We support following JIRA fields types to map:
// - string
// - number
// - array type with non-empty allowedValues
// - array type with non-empty allowedValues (i.e. drop down list)
// - User or array of User
// - priority (provided that allowedValues is non-empty)

if ("string".equals(field.getType())
|| "number".equals(field.getType())
|| "user".equals(field.getType())
|| "priority".equals(field.getType())
|| ("array".equals(field.getType()))) {

if (field.isList() && (field.getAllowedValues()== null || field.getAllowedValues().isEmpty())) {
// We only allow to select lists that have some value options.
if (field.isList() && !"user".equals(field.getType()) && (field.getAllowedValues()== null || field.getAllowedValues().isEmpty())) {
// We only allow to select lists that have some static value options or are users lists.
continue;
}

AgileEntityFieldInfo fieldInfo = new AgileEntityFieldInfo();
fieldInfo.setId(field.getKey());
fieldInfo.setLabel(field.getName());
fieldInfo.setListType(field.isList());
fieldInfo.setFieldType(field.getType());
fieldsInfo.add(fieldInfo);
}
}

return fieldsInfo;
}

@Override public List<AgileEntityField> getAgileEntityFieldValueList(String agileProjectValue, String entityType, String listIdentifier,
/*@Override public List<AgileEntityField> getAgileEntityFieldValueList(String agileProjectValue, String entityType, String listIdentifier,
ValueSet instanceConfigurationParameters)
{
// Not used.
List<JIRAFieldInfo> fields = service.get(instanceConfigurationParameters).getFields(agileProjectValue, entityType);
for (JIRAFieldInfo field : fields) {
Expand All @@ -77,35 +84,61 @@ public class JIRARequestIntegration extends RequestIntegration {
// Field not found.
return new ArrayList<AgileEntityField>();
}
}*/

@Override public AgileEntity updateEntity(String agileProjectValue, String entityType, AgileEntity entity,
ValueSet instanceConfigurationParameters)
{
Map<String, String> fields = getFieldsFromAgileEntity(entity);

JIRAServiceProvider.JIRAService jiraService = service.get(instanceConfigurationParameters);

Map<String, String> fields = getFieldsFromAgileEntity(entity, jiraService);


String issueKey = jiraService.updateIssue(agileProjectValue, entity.getId(), fields);

return jiraService.getSingleAgileEntityIssue(agileProjectValue, issueKey);
}

private Map<String,String> getFieldsFromAgileEntity(AgileEntity entity) {
private Map<String,String> getFieldsFromAgileEntity(AgileEntity entity, JIRAServiceProvider.JIRAService service) {
Map<String, String> fields = new HashMap<>();

Iterator<Map.Entry<String, List<AgileEntityFieldValue>>> fieldsIterator = entity.getAllFields();
Iterator<Map.Entry<String, DataField>> fieldsIterator = entity.getAllFields();

while (fieldsIterator.hasNext()) {
Map.Entry<String, List<AgileEntityFieldValue>> field = fieldsIterator.next();
Map.Entry<String, DataField> field = fieldsIterator.next();

DataField dataField = field.getValue();

// As of PPM 9.50, data fields coming from PPM can be either simple string, simple User, or list of Users.
if (dataField == null) {
fields.put(field.getKey(), null);
} else if (DataField.DATA_TYPE.USER.equals(dataField.getType())) {
// This is one or more user; however, in JIRA the user fields usually only take one user - or at least that's how we'll synch them.
User user = null;
if (dataField.isList()) {
// PPM Multi user field
List<User> users = (List<User>)dataField.get();
if (users != null && !users.isEmpty()) {
user = users.get(0);
}
} else {
// Single user
user = (User)dataField.get();
}

StringBuilder value = new StringBuilder("");
if (user == null) {
fields.put(field.getKey(), null);
} else {
// We need to retrieve the right Jira user matching the PPM user's email or username.
String jiraUsername = service.getJiraUsernameFromPpmUser(user);
fields.put(field.getKey(), jiraUsername == null ? null : JIRA_NAME_PREFIX + jiraUsername);
}

for (AgileEntityFieldValue v : field.getValue()) {
value.append(v.getValue());
} else {
// we consider it a single String value.
fields.put(field.getKey(), dataField.get() == null ? null : dataField.get().toString());
}

fields.put(field.getKey(), value.toString());
}

return fields;
Expand All @@ -114,10 +147,10 @@ private Map<String,String> getFieldsFromAgileEntity(AgileEntity entity) {
@Override public AgileEntity createEntity(String agileProjectValue, String entityType, AgileEntity entity,
ValueSet instanceConfigurationParameters)
{
Map<String, String> fields = getFieldsFromAgileEntity(entity);

JIRAServiceProvider.JIRAService jiraService = service.get(instanceConfigurationParameters);

Map<String, String> fields = getFieldsFromAgileEntity(entity, jiraService);

String issueKey = jiraService.createIssue(agileProjectValue, fields, null, entityType);

return jiraService.getSingleAgileEntityIssue(agileProjectValue, issueKey);
Expand Down
Loading

0 comments on commit 784b0dc

Please sign in to comment.