Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code structure adjustments #400

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10.5</version>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down Expand Up @@ -226,6 +231,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<!-- <configuration>-->
<!-- <skip>true</skip>-->
<!-- </configuration>-->
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/gitlab/api/GitlabAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.*;

import static org.gitlab.api.http.Method.*;

Expand Down Expand Up @@ -1812,8 +1808,9 @@ public GitlabMergeRequest acceptMergeRequest(Serializable projectId, Integer mer
GitlabHTTPRequestor requestor = retrieve().method(PUT);
requestor.with("id", projectId);
requestor.with("merge_request_iid", mergeRequestIid);
if (mergeCommitMessage != null)
if (mergeCommitMessage != null) {
requestor.with("merge_commit_message", mergeCommitMessage);
}
return requestor.to(tailUrl, GitlabMergeRequest.class);
}

Expand Down Expand Up @@ -2612,7 +2609,10 @@ public void deleteProjectHook(GitlabProject project, String hookId) throws IOExc
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId;
retrieve().method(DELETE).to(tailUrl, Void.class);
}

public List<GitlabIssue> getIssues(String assigneeId){
String tailUrl = GitlabIssue.URL+"?assigneeId="+assigneeId+"&scope=all";
return this.retrieve().getAll(tailUrl, GitlabIssue[].class);
}
public List<GitlabIssue> getIssues(GitlabProject project) {
return getIssues(project.getId());
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/gitlab/api/TokenType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package org.gitlab.api;

/**
* User Token Type
* @author Chi Vinh Le
*/
public enum TokenType {
/**
* User private token
*/
PRIVATE_TOKEN("private_token", "PRIVATE-TOKEN", "%s"),
/**
* User access token
*/
ACCESS_TOKEN("access_token", "Authorization", "Bearer %s");

private final String tokenParamName;
Expand Down
59 changes: 26 additions & 33 deletions src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
package org.gitlab.api.http;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UncheckedIOException;
import org.apache.commons.io.IOUtils;
import org.gitlab.api.AuthMethod;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.GitlabAPIException;
import org.gitlab.api.TokenType;

import javax.net.ssl.*;
import java.io.*;
import java.lang.reflect.Field;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.*;

import org.apache.commons.io.IOUtils;
import org.gitlab.api.AuthMethod;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.GitlabAPIException;
import org.gitlab.api.TokenType;

import static org.gitlab.api.http.Method.GET;
import static org.gitlab.api.http.Method.POST;
import static org.gitlab.api.http.Method.PUT;
import static org.gitlab.api.http.Method.*;

/**
* Gitlab HTTP Requestor
Expand Down Expand Up @@ -86,8 +74,8 @@ public GitlabHTTPRequestor method(Method method) {
* Sets the HTTP Form Post parameters for the request
* Has a fluent api for method chaining
*
* @param key Form parameter Key
* @param value Form parameter Value
* @param key Form parameter Key
* @param value Form parameter Value
* @return this
*/
public GitlabHTTPRequestor with(String key, Object value) {
Expand All @@ -96,13 +84,13 @@ public GitlabHTTPRequestor with(String key, Object value) {
}
return this;
}

/**
* Sets the HTTP Form Post parameters for the request
* Has a fluent api for method chaining
*
* @param key Form parameter Key
* @param file File data
* @param key Form parameter Key
* @param file File data
* @return this
*/
public GitlabHTTPRequestor withAttachment(String key, File file) {
Expand Down Expand Up @@ -138,7 +126,7 @@ public <T> T to(String tailAPIUrl, Class<T> type, T instance) throws IOException
if (hasAttachments()) {
submitAttachments(connection);
} else if (hasOutput()) {
submitData(connection);
submitData(connection);
} else if (PUT.equals(method)) {
// PUT requires Content-Length: 0 even when there is no body (eg: API for protecting a branch)
connection.setDoOutput(true);
Expand Down Expand Up @@ -301,7 +289,7 @@ private void submitAttachments(HttpURLConnection connection) throws IOException
writer.append("--").append(boundary).append("--").append(CRLF).flush();
}
}

private void submitData(HttpURLConnection connection) throws IOException {
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
Expand All @@ -311,7 +299,7 @@ private void submitData(HttpURLConnection connection) throws IOException {
private boolean hasAttachments() {
return !attachments.isEmpty();
}

private boolean hasOutput() {
return method.equals(POST) || method.equals(PUT) && !data.isEmpty();
}
Expand Down Expand Up @@ -361,13 +349,17 @@ private <T> T parse(HttpURLConnection connection, Class<T> type, T instance) thr
if (byte[].class == type) {
return type.cast(IOUtils.toByteArray(wrapStream(connection, connection.getInputStream())));
}
reader = new InputStreamReader(wrapStream(connection, connection.getInputStream()), "UTF-8");
reader = new InputStreamReader(wrapStream(connection, connection.getInputStream()), StandardCharsets.UTF_8);
String json = IOUtils.toString(reader);
if (type != null && type == String.class) {
return type.cast(json);
}
if (type != null && type != Void.class) {
return GitlabAPI.MAPPER.readValue(json, type);
try {
return GitlabAPI.MAPPER.readValue(json, type);
} catch (Exception e) {
throw new IOException("Json Parse Failed,JSON String:" + json);
}
} else if (instance != null) {
return GitlabAPI.MAPPER.readerForUpdating(instance).readValue(json);
} else {
Expand Down Expand Up @@ -440,6 +432,7 @@ public void checkServerTrusted(
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Added per https://github.com/timols/java-gitlab-api/issues/44
HttpsURLConnection.setDefaultHostnameVerifier(nullVerifier);
} catch (Exception ignore) {}
} catch (Exception ignore) {
}
}
}
36 changes: 33 additions & 3 deletions src/main/java/org/gitlab/api/http/Method.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
package org.gitlab.api.http;

/**
* @author Shaburov Oleg
* Created by Oleg Shaburov on 03.05.2018
* [email protected]
*/
public enum Method {

GET, PUT, POST, PATCH, DELETE, HEAD, OPTIONS, TRACE;

/**
* HTTP GET method
*/
GET,
/**
* HTTP PUT method
*/
PUT,
/**
* HTTP POST method
*/
POST,
/**
* HTTP PATCH method
*/
PATCH,
/**
* HTTP DELETE method
*/
DELETE,
/**
* HTTP HEAD method
*/
HEAD,
/**
* HTTP OPTIONS
*/
OPTIONS,
/**
* HTTP TRACE
*/
TRACE;
}
35 changes: 31 additions & 4 deletions src/main/java/org/gitlab/api/models/GitlabIssue.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.gitlab.api.models;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class GitlabIssue {

Expand Down Expand Up @@ -71,6 +71,9 @@ public enum Action {
@JsonProperty("web_url")
private String webUrl;

@JsonProperty("moved_to_id")
private String movedToId;

public int getId() {
return id;
}
Expand Down Expand Up @@ -134,6 +137,7 @@ public List<GitlabUser> getAssignees() {
public void setAssignees(List<GitlabUser> assignees) {
this.assignees = assignees;
}

public GitlabUser getAssignee() {
return assignee;
}
Expand Down Expand Up @@ -230,4 +234,27 @@ public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getClosedAt() {
return closedAt;
}

public void setClosedAt(Date closedAt) {
this.closedAt = closedAt;
}

public String getWebUrl() {
return webUrl;
}

public void setWebUrl(String webUrl) {
this.webUrl = webUrl;
}

public String getMovedToId() {
return movedToId;
}

public void setMovedToId(String movedToId) {
this.movedToId = movedToId;
}
}