Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.thucydides.plugins.jira.requirements;
  
 
 import java.util.*;
 
 import static net.thucydides.plugins.jira.requirements.JIRARequirementsConfiguration.JIRA_CUSTOM_FIELD;
 import static net.thucydides.plugins.jira.requirements.JIRARequirementsConfiguration.JIRA_CUSTOM_NARRATIVE_FIELD;
 import static net.thucydides.plugins.jira.requirements.JIRARequirementsConfiguration.JIRA_MAX_THREADS;


Integrate Thucydides reports with requirements, epics and stories in a JIRA server.
 
 public class JIRARequirementsProvider implements RequirementsTagProvider {
 
     private List<Requirementrequirements = null;
     private final JerseyJiraClient jiraClient;
     private final String projectKey;
     private final EnvironmentVariables environmentVariables;
 
     private final String EPIC_LINK = "Epic Link";
     //private List<String> requirementsLinks = ImmutableList.of("Epic Link");
 
     // issue in linkedIssues(TRAD-27,"relates to")
     private final org.slf4j.Logger logger = LoggerFactory.getLogger(JIRARequirementsProvider.class);
 
     private final  ListeningExecutorService executorService;
 
     private final AtomicInteger queueSize = new AtomicInteger(0);
     static int DEFAULT_MAX_THREADS = 4;
 
     public JIRARequirementsProvider() {
         this(new SystemPropertiesJIRAConfiguration(Injectors.getInjector().getProvider(EnvironmentVariables.class).get() ),
                 Injectors.getInjector().getProvider(EnvironmentVariables.class).get() );
     }
 
     public JIRARequirementsProvider(JIRAConfiguration jiraConfiguration) {
         this(jiraConfiguration, Injectors.getInjector().getProvider(EnvironmentVariables.class).get() );
     }
 
     private int getMaxJobs() {
     }
     public JIRARequirementsProvider(JIRAConfiguration jiraConfigurationEnvironmentVariables environmentVariables) {
         logConnectionDetailsFor(jiraConfiguration);
          = jiraConfiguration.getProject();
         this. = environmentVariables;
          = new ConfigurableJiraClient(jiraConfiguration.getJiraUrl(),
                 jiraConfiguration.getJiraUser(),
                 jiraConfiguration.getJiraPassword(),
                 ).usingCustomFields(customFieldsDefinedIn(environmentVariables));
 
          = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(getMaxJobs()));
     }
 
     private List<StringdefinedCustomFields() {
         List<StringcustomFields = Lists.newArrayList();
         int customFieldIndex = 1;
         while (addCustomFieldIfDefined(customFieldscustomFieldNumber(customFieldIndex++))) ;
         return customFields;
     }
 
     private List<StringcustomFieldsDefinedIn(EnvironmentVariables environmentVariables) {
         List<StringcustomFields = Lists.newArrayList();
         addCustomFieldIfDefined(environmentVariablescustomFields,
         customFields.addAll(definedCustomFields());
         return customFields;
     }
 
     private String customFieldNumber(int customFieldIndex) {
         return .getName() + "." + customFieldIndex;
    }
    private boolean addCustomFieldIfDefined(EnvironmentVariables environmentVariables,
                                            List<StringcustomFields,
                                            String customField) {
        String customFieldName = environmentVariables.getProperty(customField);
        if (StringUtils.isNotEmpty(customFieldName)) {
            customFields.add(customFieldName);
            return true;
        }
        return false;
    }
    private void logConnectionDetailsFor(JIRAConfiguration jiraConfiguration) {
        .debug("JIRA URL: {0}"jiraConfiguration.getJiraUrl());
        .debug("JIRA project: {0}"jiraConfiguration.getProject());
        .debug("JIRA user: {0}"jiraConfiguration.getJiraUser());
    }
    private String getProjectKey() {
        return ;
    }
    @Override
    public List<RequirementgetRequirements() {
        if ( == null) {
            List<IssueSummaryrootRequirementIssues;
            .info("Loading root requirements: " + rootRequirementsJQL());
            try {
                rootRequirementIssues = .findByJQL(rootRequirementsJQL());
            } catch (JSONException e) {
                .info("No root requirements found (JQL = " + rootRequirementsJQL(), e);
                rootRequirementIssues = Lists.newArrayList();
            }
            .debug("Loading root requirements done: " + rootRequirementIssues.size());
             = Collections.synchronizedList(new ArrayList<Requirement>());
            for (final IssueSummary issueSummary : rootRequirementIssues) {
                final ListenableFuture<IssueSummaryfuture = .submit(new Callable<IssueSummary>() {
                    @Override
                    public IssueSummary call() throws Exception {
                        return issueSummary;
                    }
                });
                future.addListener(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            .incrementAndGet();
                            Requirement requirement = requirementFrom(future.get());
                            List<RequirementchildRequirements = findChildrenFor(requirement, 0);
                            .add(requirement.withChildren(childRequirements));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                }, MoreExecutors.sameThreadExecutor());
                future.addListener(new Runnable() {
                    @Override
                    public void run() {
                        .decrementAndGet();
                    }
                }, );
            }
            waitTillEmpty();
             = addParentsTo();
        }
        return ;
    }
    private List<RequirementaddParentsTo(List<Requirementrequirements) {
        return addParentsTo(requirementsnull);
    }
    private final List<RequirementNO_REQUIREMENTS = ImmutableList.of();
    private List<RequirementaddParentsTo(List<RequirementrequirementsString parent) {
        List<RequirementaugmentedRequirements = Lists.newArrayList();
        for(Requirement requirement : requirements) {
            List<Requirementchildren = requirement.hasChildren()
                    ? addParentsTo(requirement.getChildren(),requirement.getName()) : ;
            augmentedRequirements.add(requirement.withParent(parent).withChildren(children));
        }
        return augmentedRequirements;
    }
    private Requirement requirementFrom(IssueSummary issue) {
        Requirement baseRequirement = Requirement.named(issue.getSummary())
                .withOptionalCardNumber(issue.getKey())
                .withType(issue.getType())
                .withNarrative(narativeTextFrom(issue))
                .withReleaseVersions(issue.getFixVersions());
        for (String fieldName : definedCustomFields()) {
            if (issue.customField(fieldName).isPresent()) {
                String value = issue.customField(fieldName).get().asString();
                String renderedValue = issue.getRendered().customField(fieldName).get();
                baseRequirement = baseRequirement.withCustomField(fieldName).setTo(valuerenderedValue);
            }
        }
        return baseRequirement;
    }
    private String narativeTextFrom(IssueSummary issue) {
        if (customFieldName.isPresent()) {
            return customFieldNameFor(issuecustomFieldName.get()).or(issue.getRendered().getDescription());
        } else {
            return issue.getRendered().getDescription();
        }
    }
    private Optional<StringcustomFieldNameFor(IssueSummary issueString customFieldName) {
        if (issue.customField(customFieldName).isPresent()) {
            return Optional.of(issue.customField(customFieldName).get().asString());
        } else {
            return Optional.absent();
        }
    }
    private List<RequirementfindChildrenFor(Requirement parentfinal int level) {
        List<IssueSummarychildren = null;
        try {
            .info("Loading child requirements for: " + parent.getName());
            children = .findByJQL(childIssuesJQL(parentlevel));
            .info("Loading child requirements for " + parent.getName() + " done: " + children.size());
        } catch (JSONException e) {
            .warn("No children found for requirement " + parente);
            return ;
        }
        final List<RequirementchildRequirements = Collections.synchronizedList(new ArrayList<Requirement>());
        for(IssueSummary childIssue : children) {
            Requirement childRequirement = requirementFrom(childIssue);
            if (moreRequirements(level)) {
                List<RequirementgrandChildren = findChildrenFor(childRequirementlevel + 1);
                childRequirement = childRequirement.withChildren(grandChildren);
            }
            childRequirements.add(childRequirement);
        }
        return childRequirements;
    }
    private void waitTillEmpty(AtomicInteger counter) {
        while (counter.get() > 0) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
            }
        }
    }
    private String childIssuesJQL(Requirement parentint level) {
        String linkType = getRequirementsLinks().get(level);
        if (linkType.equals()) {
            return "'" + getRequirementsLinks().get(level) + "' = " + parent.getCardNumber();
        } else {
            return "issue in linkedIssues(" + parent.getCardNumber() + ",\"" + linkType + "\")";
        }
    }
    private boolean moreRequirements(int level) {
        return level < getRequirementsLinks().size() - 1;
    }
    //////////////////////////////////////
    private String rootRequirementsJQL() {
        return "issuetype = " + getRootIssueType() + " and project=" + getProjectKey();
    }
    private String getRootIssueType() {
    }
    @Override
    public Optional<RequirementgetParentRequirementOf(TestOutcome testOutcome) {
        .debug("Find parent requirement in JIRA for " + testOutcome.getTitle());
        List<StringissueKeys = testOutcome.getIssueKeys();
        if (!issueKeys.isEmpty()) {
            try {
                Optional<IssueSummaryparentIssue = .findByKey(issueKeys.get(0));
                if (parentIssue.isPresent()) {
                    .debug("Parent found: " + parentIssue.get());
                    return Optional.of(requirementFrom(parentIssue.get()));
                } else {
                    return Optional.absent();
                }
            } catch (JSONException e) {
                if (noSuchIssue(e)) {
                    return Optional.absent();
                } else {
                    throw new IllegalArgumentException(e);
                }
            }
        } else {
            return Optional.absent();
        }
    }
    private boolean noSuchIssue(JSONException e) {
        return e.getMessage().contains("error 400");
    }
    @Override
    public Optional<RequirementgetRequirementFor(TestTag testTag) {
        for (Requirement requirement : getFlattenedRequirements()) {
            if (requirement.getType().equals(testTag.getType()) && requirement.getName().equals(testTag.getName())) {
                return Optional.of(requirement);
            }
        }
        return Optional.absent();
    }
    @Override
    public Set<TestTaggetTagsFor(TestOutcome testOutcome) {
        List<Stringissues = testOutcome.getIssueKeys();
        Set<TestTagtags = Sets.newHashSet();
        for (String issue : issues) {
            tags.addAll(tagsFromIssue(issue));
        }
        return ImmutableSet.copyOf(tags);
    }
    private Collection<? extends TestTagtagsFromIssue(String issueKey) {
        return tagReader.addIssueTags(issueKey)
                .addRequirementTags(issueKey)
                .addVersionTags(issueKey).getTags();
    }
        return getFlattenedRequirements(getRequirements());
    }
    private List<RequirementgetFlattenedRequirements(List<RequirementsomeRequirements) {
        List<RequirementflattenedRequirements = Lists.newArrayList();
        for (Requirement requirement : someRequirements) {
            flattenedRequirements.add(requirement);
            flattenedRequirements.addAll(getFlattenedRequirements(requirement.getChildren()));
        }
        return flattenedRequirements;
    }
    public List<StringgetRequirementsLinks() {
                                                                   "Epic Link");
        return Splitter.on(",").trimResults().omitEmptyStrings().splitToList(requirementLinks);
    }
New to GrepCode? Check out our FAQ X