Question

I have 15 usernames with me, I need to pull worklog entries of these users and manipulate it from JAVA client

Below are the jar files am using to connect JIRA api and fetch values

enter image description here

The code is pasted below

public class JiraConnector {

JiraRestClient jira;

public JiraConnector() throws URISyntaxException {
    String url = prop().getUrl();
    String userName = prop().getUser() ;
    String password = prop().getpwd() ;
    JerseyJiraRestClientFactory clientFactory = new JerseyJiraRestClientFactory();
    jira = clientFactory.createWithBasicHttpAuthentication(new URI(url),
            userName, password);
    System.out.println("Connection established to >> " + url);
}

public void printIssueDetails(String jiraNumber) {
    System.out.println("JiraNumber is " + jiraNumber);
    Issue issue = jira.getIssueClient().getIssue(jiraNumber, null);

    System.out.println(issue.getSummary());
    System.out.println(issue.getDescription());
}

public void printUserWorkLog(String userName) {
    System.out.println("user details invoked ... ");
    User user = jira.getUserClient().getUser(userName, null);
    System.out.println(user.getDisplayName());
    System.out.println(user.getEmailAddress());

}

For any given username, am able to print his displayName and emailAdress (all those basic infos).

But I need to get the list of worklogs for the given user. Not sure how to proceed

Was it helpful?

Solution

You can find all worklog records for selected issue:

List<Worklog> worklogByIssue = ComponentAccessor.getWorklogManager().getByIssue(issue);

After that you can parse all worklog records to determine for what user this record created:

for (Worklog worklogByIssueItem : worklogByIssue)
{
   int timeSpent = worklogByIssueItem.getTimeSpent().intValue();
   String worklogAuthorName = worklogByIssueItem.getAuthorObject().getName();
   ...
}

And last task is search of issues by some params:

public static List<Issue> searchIssues(SearchParametersAggregator searchParams)
{
    String jqlQuery = searchParams.getJqlQuery();
    String projectId = searchParams.getProjectId();
    String condition = createCondition(jqlQuery, projectId);

    JqlQueryBuilder jqlQueryBuilder = prepareJqlQueryBuilder(condition);

    return searchIssues(jqlQueryBuilder);
}

static List<Issue> searchIssues(JqlQueryBuilder jqlQueryBuilder)
{
    Query query = jqlQueryBuilder.buildQuery();
    SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
    try
    {
        ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
        User user = applicationUser.getDirectoryUser();
        SearchResults searchResults = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
        List<Issue> issues = searchResults.getIssues();

        return issues;
    }
    catch (SearchException e)
    {
        LOGGER.error("Error occurs during search of issues");
        e.printStackTrace();
    }

    return new ArrayList<Issue>();
}

static JqlQueryBuilder prepareJqlQueryBuilder(String condition)
{
    try
    {
        Query query = jqlQueryParser.parseQuery(condition);
        JqlQueryBuilder builder = JqlQueryBuilder.newBuilder(query);

        return builder;
    }
    catch (JqlParseException e)
    {
        throw new RuntimeException("JqlParseException during parsing jqlQuery!");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top