Skip to content

Commit

Permalink
Add possibility to give issue fields to retrieve.
Browse files Browse the repository at this point in the history
- Add new getIssue methods to give the fields
- Use set instead of varargs in issues and comments
- Add utility methods to get all fields from a class
  • Loading branch information
Chavjoh committed Jan 26, 2024
1 parent e0ab95c commit 27e8be6
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 85 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ It requires the following dependency:
Everything for issues, including comments, links, transitions, attachments and work logs**
- Issues
- `addIssue(Issue issue)`
- `getIssue(String issueKey, IssueExpand... flags)`
- `getIssueOptional(String issueKey, IssueExpand... flags)`
- `getIssue(String issueKey)`
- `getIssue(String issueKey, Set<IssueExpand> flags)`
- `getIssue(String issueKey, Set<IssueExpand> flags, Set<String> fields)`
- `getIssueOptional(String issueKey)`
- `getIssueOptional(String issueKey, Set<IssueExpand> flags)`
- `getIssueOptional(String issueKey, Set<IssueExpand> flags, Set<String> fields)`
- `updateIssue(Issue issue)`
- `deleteIssue(String issueKey)`
- `assignIssue(String issueKey, User user)`
Expand All @@ -98,9 +102,12 @@ It requires the following dependency:
- `doTransition(String issueKey, IssueTransition transition)`
- Comments
- `getComments(String issueKey)`
- `getComments(String issueKey, Integer startAt, Integer maxResults, CommentExpand... flags)`
- `getComment(String issueKey, String id, CommentExpand... flags)`
- `getCommentOptional(String issueKey, String id, CommentExpand... flags)`
- `getComments(String issueKey, Integer startAt, Integer maxResults)`
- `getComments(String issueKey, Integer startAt, Integer maxResults, Set<CommentExpand> flags)`
- `getComment(String issueKey, String id)`
- `getComment(String issueKey, String id, Set<CommentExpand> flags)`
- `getCommentOptional(String issueKey, String id)`
- `getCommentOptional(String issueKey, String id, Set<CommentExpand> flags)`
- `addComment(String issueKey, Comment comment)`
- `updateComment(String issueKey, Comment comment)`
- `deleteComment(String issueKey, String id)`
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/chavaillaz/client/jira/JiraConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toSet;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.normalizeSpace;

import java.lang.reflect.Field;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.chavaillaz.client.jira.domain.ErrorMessages;
import com.chavaillaz.client.jira.jackson.OffsetDateTimeDeserializer;
import com.chavaillaz.client.jira.jackson.OffsetDateTimeSerializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand Down Expand Up @@ -96,4 +102,37 @@ public static String extractHtmlErrors(String html) {
.orElse(null);
}

/**
* Gets the names of all fields declared in a class type and its hierarchy.
* The names are retrieved either by {@link JsonProperty#value()} or by {@link Field#getName()}.
* Meant to be used to optimize calls to Jira in order to get only the attributes available in the given model.
*
* @param type The class type
* @return The names of all declared fields
*/
public static Set<String> getNameFields(Class<?> type) {
return getDeepFields(type).stream()
.map(field -> Optional.ofNullable(field.getAnnotation(JsonProperty.class))
.map(JsonProperty::value)
.orElse(field.getName()))
.collect(toSet());
}

/**
* Gathers all fields from the given type and all its hierarchy.
*
* @param type The class type
* @return The fields, including the inherited ones
*/
public static List<Field> getDeepFields(Class<?> type) {
if (type == null) {
return emptyList();
}

List<Field> deepFields = new ArrayList<>(getDeepFields(type.getSuperclass()));
List<Field> fields = List.of(type.getDeclaredFields());
deepFields.addAll(fields);
return deepFields;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chavaillaz.client.jira.apache;

import static com.chavaillaz.client.common.apache.ApacheHttpUtils.multipartWithFiles;
import static java.lang.String.join;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.delete;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.get;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.post;
Expand All @@ -9,6 +10,7 @@

import java.io.File;
import java.io.InputStream;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.common.security.Authentication;
Expand Down Expand Up @@ -57,8 +59,8 @@ public CompletableFuture<Identity> addIssue(T issue) {
}

@Override
public CompletableFuture<T> getIssue(String issueKey, IssueExpand... expandFlags) {
return sendAsync(requestBuilder(get(), URL_ISSUE_SELECTION, issueKey, IssueExpand.getParameters(expandFlags)), issueType);
public CompletableFuture<T> getIssue(String issueKey, Set<IssueExpand> expandFlags, Set<String> fields) {
return sendAsync(requestBuilder(get(), URL_ISSUE_SELECTION, issueKey, IssueExpand.asParameter(expandFlags), join(",", fields)), issueType);
}

@Override
Expand Down Expand Up @@ -90,13 +92,13 @@ public CompletableFuture<Void> doTransition(String issueKey, IssueTransition tra
}

@Override
public CompletableFuture<Comments> getComments(String issueKey, Integer startAt, Integer maxResults, CommentExpand... expandFlags) {
return sendAsync(requestBuilder(get(), URL_ISSUE_COMMENTS_SELECTION, issueKey, startAt, maxResults, CommentExpand.getParameters(expandFlags)), Comments.class);
public CompletableFuture<Comments> getComments(String issueKey, Integer startAt, Integer maxResults, Set<CommentExpand> expandFlags) {
return sendAsync(requestBuilder(get(), URL_ISSUE_COMMENTS_SELECTION, issueKey, startAt, maxResults, CommentExpand.asParameter(expandFlags)), Comments.class);
}

@Override
public CompletableFuture<Comment> getComment(String issueKey, String id, CommentExpand... expandFlags) {
return sendAsync(requestBuilder(get(), URL_ISSUE_COMMENT_SELECTION, issueKey, id, CommentExpand.getParameters(expandFlags)), Comment.class);
public CompletableFuture<Comment> getComment(String issueKey, String id, Set<CommentExpand> expandFlags) {
return sendAsync(requestBuilder(get(), URL_ISSUE_COMMENT_SELECTION, issueKey, id, CommentExpand.asParameter(expandFlags)), Comment.class);
}

@Override
Expand Down
Loading

0 comments on commit 27e8be6

Please sign in to comment.