Extended code.

This commit is contained in:
Christian P. MOMON 2016-07-18 04:25:56 +02:00
parent 90dee57a2e
commit e0efd2b812
10 changed files with 814 additions and 211 deletions

View file

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/apache-httpclient-4.5.2.jar"/> <classpathentry kind="lib" path="lib/apache-httpclient-4.5.2.jar" sourcepath="lib/apache-httpclient-4.5.2-sources.zip"/>
<classpathentry kind="lib" path="lib/apache-httpcore-4.4.5.jar"/> <classpathentry kind="lib" path="lib/apache-httpcore-4.4.5.jar"/>
<classpathentry kind="lib" path="lib/commons-io-2.4.jar"/> <classpathentry kind="lib" path="lib/commons-io-2.4.jar" sourcepath="lib/commons-io-2.4-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.1.jar"/> <classpathentry kind="lib" path="lib/commons-lang3-3.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/> <classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.3.1.jar"/> <classpathentry kind="lib" path="lib/devinsy-utils-0.3.1.jar"/>
<classpathentry kind="lib" path="lib/javax.json-1.0.4.jar"/> <classpathentry kind="lib" path="lib/javax.json-1.0.4.jar" sourcepath="lib/javax.json-1.0.4-sources.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/> <classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar"/> <classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/> <classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/>

View file

@ -13,7 +13,7 @@ cleanup.always_use_this_for_non_static_field_access=true
cleanup.always_use_this_for_non_static_method_access=false cleanup.always_use_this_for_non_static_method_access=false
cleanup.convert_functional_interfaces=false cleanup.convert_functional_interfaces=false
cleanup.convert_to_enhanced_for_loop=false cleanup.convert_to_enhanced_for_loop=false
cleanup.correct_indentation=false cleanup.correct_indentation=true
cleanup.format_source_code=true cleanup.format_source_code=true
cleanup.format_source_code_changes_only=false cleanup.format_source_code_changes_only=false
cleanup.insert_inferred_type_arguments=false cleanup.insert_inferred_type_arguments=false
@ -57,5 +57,63 @@ cleanup.use_this_for_non_static_method_access_only_if_necessary=true
cleanup_profile=_GitLabAPI cleanup_profile=_GitLabAPI
cleanup_settings_version=2 cleanup_settings_version=2
eclipse.preferences.version=1 eclipse.preferences.version=1
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
formatter_profile=_GitLabAPI formatter_profile=_GitLabAPI
formatter_settings_version=12 formatter_settings_version=12
sp_cleanup.add_default_serial_version_id=true
sp_cleanup.add_generated_serial_version_id=false
sp_cleanup.add_missing_annotations=true
sp_cleanup.add_missing_deprecated_annotations=true
sp_cleanup.add_missing_methods=false
sp_cleanup.add_missing_nls_tags=false
sp_cleanup.add_missing_override_annotations=true
sp_cleanup.add_missing_override_annotations_interface_methods=true
sp_cleanup.add_serial_version_id=false
sp_cleanup.always_use_blocks=true
sp_cleanup.always_use_parentheses_in_expressions=true
sp_cleanup.always_use_this_for_non_static_field_access=true
sp_cleanup.always_use_this_for_non_static_method_access=false
sp_cleanup.convert_functional_interfaces=false
sp_cleanup.convert_to_enhanced_for_loop=false
sp_cleanup.correct_indentation=true
sp_cleanup.format_source_code=true
sp_cleanup.format_source_code_changes_only=false
sp_cleanup.insert_inferred_type_arguments=false
sp_cleanup.make_local_variable_final=false
sp_cleanup.make_parameters_final=true
sp_cleanup.make_private_fields_final=false
sp_cleanup.make_type_abstract_if_missing_method=false
sp_cleanup.make_variable_declarations_final=true
sp_cleanup.never_use_blocks=false
sp_cleanup.never_use_parentheses_in_expressions=false
sp_cleanup.on_save_use_additional_actions=true
sp_cleanup.organize_imports=true
sp_cleanup.qualify_static_field_accesses_with_declaring_class=false
sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true
sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true
sp_cleanup.qualify_static_member_accesses_with_declaring_class=false
sp_cleanup.qualify_static_method_accesses_with_declaring_class=false
sp_cleanup.remove_private_constructors=true
sp_cleanup.remove_redundant_type_arguments=false
sp_cleanup.remove_trailing_whitespaces=true
sp_cleanup.remove_trailing_whitespaces_all=true
sp_cleanup.remove_trailing_whitespaces_ignore_empty=false
sp_cleanup.remove_unnecessary_casts=true
sp_cleanup.remove_unnecessary_nls_tags=true
sp_cleanup.remove_unused_imports=true
sp_cleanup.remove_unused_local_variables=false
sp_cleanup.remove_unused_private_fields=true
sp_cleanup.remove_unused_private_members=false
sp_cleanup.remove_unused_private_methods=true
sp_cleanup.remove_unused_private_types=true
sp_cleanup.sort_members=true
sp_cleanup.sort_members_all=false
sp_cleanup.use_anonymous_class_creation=false
sp_cleanup.use_blocks=true
sp_cleanup.use_blocks_only_for_return_and_throw=false
sp_cleanup.use_lambda=true
sp_cleanup.use_parentheses_in_expressions=true
sp_cleanup.use_this_for_non_static_field_access=true
sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=false
sp_cleanup.use_this_for_non_static_method_access=true
sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true

3
README.MD Normal file
View file

@ -0,0 +1,3 @@
GitLabAPIC is a GitLab API Client.
CAUTION: the GitLab API is huge bugged and limited. Lucky you are if it is enough.

View file

@ -2,7 +2,7 @@
# ################ # ################
log4j.rootLogger = INFO, CONSOLE log4j.rootLogger = INFO, CONSOLE
log4j.logger.fr.devinsy.gitlabapi = DEBUG log4j.logger.fr.devinsy.gitlabapic = DEBUG
log4j.logger.org.apache.http = INFO log4j.logger.org.apache.http = INFO
org.apache.commons.beanutils = INFO org.apache.commons.beanutils = INFO
@ -10,4 +10,4 @@ org.apache.commons.beanutils = INFO
#-- #--
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d{ISO8601} - MIGPUCK [%-5p] %34.34c.%25M - %m%n log4j.appender.CONSOLE.layout.ConversionPattern = %d{ISO8601} - GITLABAPIC [%-5p] %34.34c.%25M - %m%n

View file

@ -1,5 +1,6 @@
package fr.devinsy.gitlabapic; package fr.devinsy.gitlabapic;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
@ -9,20 +10,23 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpException;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author Christian Pierre MOMON (christian.momon@devinsy.fr) * @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/ */
public class Utils public class Utils
@ -32,7 +36,7 @@ public class Utils
public static final String DEFAULT_CHARSET_NAME = "UTF-8"; public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/** /**
* *
* @param source * @param source
* @return * @return
*/ */
@ -47,7 +51,7 @@ public class Utils
} }
/** /**
* *
* @param source * @param source
* @return * @return
*/ */
@ -98,14 +102,15 @@ public class Utils
} }
/** /**
* *
* @param uri * @param uri
* @return * @return
* @throws IOException * @throws IOException
* @throws HttpException
*/ */
public static int httpPost(final URI uri, final String privateToken, final String... parameters) throws IOException public static String httpPost(final URI uri, final String privateToken, final String... parameters) throws IOException, HttpException
{ {
int result; String result;
logger.debug("[uri={}][privateToken={}][parameters={}]", uri, privateToken, parameters); logger.debug("[uri={}][privateToken={}][parameters={}]", uri, privateToken, parameters);
@ -115,7 +120,7 @@ public class Utils
} }
else else
{ {
CloseableHttpResponse response2 = null; CloseableHttpResponse response = null;
try try
{ {
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
@ -134,22 +139,97 @@ public class Utils
nvps.add(new BasicNameValuePair(parameters[parameterIndex], parameters[parameterIndex + 1])); nvps.add(new BasicNameValuePair(parameters[parameterIndex], parameters[parameterIndex + 1]));
} }
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(100000);
new UrlEncodedFormEntity(nvps).writeTo(out);
logger.debug("urlencoded=[{}]", out.toString());
httpPost.setEntity(new UrlEncodedFormEntity(nvps)); httpPost.setEntity(new UrlEncodedFormEntity(nvps));
logger.debug("httpPost={}", httpPost); logger.debug("httpPost={}", httpPost);
response2 = httpClient.execute(httpPost); response = httpClient.execute(httpPost);
logger.debug("status line={} [{}]", response2.getStatusLine(), uri); logger.debug("status line={} [{}]", response.getStatusLine(), uri);
result = response2.getStatusLine().getStatusCode(); if ((response.getStatusLine().getStatusCode() != 200) && (response.getStatusLine().getStatusCode() != 201))
HttpEntity entity2 = response2.getEntity(); {
throw new HttpException("Status code returned: " + response.getStatusLine());
// do something useful with the response body }
// and ensure it is fully consumed else
EntityUtils.consume(entity2); {
result = new StringList(IOUtils.readLines(response.getEntity().getContent())).toString();
}
} }
finally finally
{ {
response2.close(); response.close();
}
}
//
return result;
}
/**
*
* @param uri
* @param privateToken
* @param parameters
* @return
* @throws IOException
* @throws HttpException
*/
public static String httpPut(final URI uri, final String privateToken, final String... parameters) throws IOException, HttpException
{
String result;
logger.debug("[uri={}][privateToken={}][parameters={}]", uri, privateToken, parameters);
if (uri == null)
{
throw new IllegalArgumentException("Null parameter.");
}
else
{
CloseableHttpResponse response = null;
try
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(uri);
if (privateToken != null)
{
httpPut.setHeader("PRIVATE-TOKEN", privateToken);
}
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex += 2)
{
if (StringUtils.isNotBlank(parameters[parameterIndex + 1]))
{
nvps.add(new BasicNameValuePair(parameters[parameterIndex], parameters[parameterIndex + 1]));
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream(100000);
new UrlEncodedFormEntity(nvps).writeTo(out);
logger.debug("urlencoded=[{}]", out.toString());
httpPut.setEntity(new UrlEncodedFormEntity(nvps));
logger.debug("httpPut={}", httpPut);
response = httpClient.execute(httpPut);
logger.debug("status line={} [{}]", response.getStatusLine(), uri);
if ((response.getStatusLine().getStatusCode() != 200) && (response.getStatusLine().getStatusCode() != 201))
{
throw new HttpException("Status code returned: " + response.getStatusLine());
}
else
{
result = new StringList(IOUtils.readLines(response.getEntity().getContent())).toString();
}
}
finally
{
response.close();
} }
} }

View file

@ -2,6 +2,7 @@ package fr.devinsy.gitlabapic.v3;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
@ -10,17 +11,20 @@ import javax.json.Json;
import javax.json.JsonArray; import javax.json.JsonArray;
import javax.json.JsonObject; import javax.json.JsonObject;
import javax.json.JsonReader; import javax.json.JsonReader;
import javax.json.JsonString;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.gitlabapic.GitLabAPIException;
import fr.devinsy.gitlabapic.Utils; import fr.devinsy.gitlabapic.Utils;
import fr.devinsy.util.strings.StringList; import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author Christian Pierre MOMON (christian.momon@devinsy.fr) * @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/ */
public class GitLab public class GitLab
@ -28,15 +32,21 @@ public class GitLab
private static final Logger logger = LoggerFactory.getLogger(GitLab.class); private static final Logger logger = LoggerFactory.getLogger(GitLab.class);
public static final String DEFAULT_CHARSET_NAME = "UTF-8"; public static final String DEFAULT_CHARSET_NAME = "UTF-8";
public static final int NOID = -1;
public static void closeIssue(final ProjectRef source, final int issueId, final String updatedAt) throws GitLabAPIException
{
editIssue(source, issueId, null, null, null, null, null, "close", updatedAt);
}
/** /**
* *
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static void copyLabels(final ProjectRef source, final ProjectRef target) throws IOException, URISyntaxException public static void copyLabels(final ProjectRef source, final ProjectRef target) throws GitLabAPIException
{ {
//
Labels labels = GitLab.searchLabels(source); Labels labels = GitLab.searchLabels(source);
for (Label label : labels) for (Label label : labels)
{ {
@ -45,12 +55,13 @@ public class GitLab
} }
/** /**
* *
* @param args * @param args
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static int createIssue(final ProjectRef project, final Issue issue) throws IOException, URISyntaxException public static int createIssue(final ProjectRef project, final Issue issue) throws GitLabAPIException
{ {
int result; int result;
@ -61,46 +72,64 @@ public class GitLab
} }
/** /**
* *
* CAUTION: created_at parameter is ignored in case of non project owner or * CAUTION: created_at parameter is ignored in case of non project owner or
* admin. * admin.
* *
* @param args * @param args
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static int createIssue(final ProjectRef project, final String title, final String description, final String assigneeId, final String milestoneId, final StringList labels, public static int createIssue(final ProjectRef project, final String title, final String description, final String assigneeId, final String milestoneId, final StringList labels,
final String createdAt) throws IOException, URISyntaxException final String createdAt) throws GitLabAPIException
{ {
int result; int result;
String url = String.format("%sapi/v3/projects/%d/issues", project.getWebsite(), project.getId()); try
StringList fixedLabels;
if (labels == null)
{ {
fixedLabels = new StringList(); String url = String.format("%sapi/v3/projects/%d/issues", project.getWebsite(), project.getId());
}
else
{
fixedLabels = labels;
}
result = Utils.httpPost(new URI(url), project.getPrivateToken(), "title", title, "description", description, "labels", fixedLabels.toStringWithCommas(), "created_at", createdAt); StringList fixedLabels;
if (labels == null)
{
fixedLabels = new StringList();
}
else
{
fixedLabels = labels;
}
//
String response = Utils.httpPost(new URI(url), project.getPrivateToken(), "title", fixBug19902(title), "description", fixBug19902(description), "labels", fixedLabels.toStringWithCommas(),
"created_at", createdAt);
//
JsonReader source = Json.createReader(new StringReader(response));
JsonObject item = source.readObject();
result = item.getInt("id");
}
catch (IOException | URISyntaxException | HttpException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error creating issue: " + exception.getMessage(), exception);
}
// //
return result; return result;
} }
/** /**
* *
* @param args * @param args
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
* @throws GitLabAPIException
*/ */
public static int createLabel(final ProjectRef project, final Label label) throws IOException, URISyntaxException public static String createLabel(final ProjectRef project, final Label label) throws GitLabAPIException
{ {
int result; String result;
result = createLabel(project, label.getName(), label.getColor(), label.getDescription()); result = createLabel(project, label.getName(), label.getColor(), label.getDescription());
@ -109,32 +138,153 @@ public class GitLab
} }
/** /**
* *
* @param args * @param args
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static int createLabel(final ProjectRef project, final String name, final String color, final String description) throws IOException, URISyntaxException public static String createLabel(final ProjectRef project, final String name, final String color, final String description) throws GitLabAPIException
{ {
int result; String result;
String url = String.format("%sapi/v3/projects/%d/labels", project.getWebsite(), project.getId()); String url = String.format("%sapi/v3/projects/%d/labels", project.getWebsite(), project.getId());
result = Utils.httpPost(new URI(url), project.getPrivateToken(), "name", name, "color", color, "description", description); try
{
String response = Utils.httpPost(new URI(url), project.getPrivateToken(), "name", name, "color", color, "description", description);
JsonReader source = Json.createReader(new StringReader(response));
JsonObject item = source.readObject();
result = item.getString("name");
}
catch (IOException | URISyntaxException | HttpException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error creating label: " + exception.getMessage(), exception);
}
// //
return result; return result;
} }
/** /**
* *
* @param sourceWebsite * CAUTION: created_at parameter is ignored in case of non project owner or
* @param projectId * admin.
* @param privateToken *
* @param args
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static void importLabels(final ProjectRef source, final ProjectRef target) throws IOException, URISyntaxException public static int createNote(final ProjectRef project, final int parentId, final String body, final String createdAt) throws GitLabAPIException
{
int result;
try
{
String url = String.format("%sapi/v3/projects/%d/issues/%d/notes", project.getWebsite(), project.getId(), parentId);
String response = Utils.httpPost(new URI(url), project.getPrivateToken(), "body", fixBug19902(body), "created_at", createdAt, "system", "true");
logger.debug("response=[{}]", response);
result = 0;
}
catch (IOException | URISyntaxException | HttpException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error creating note: " + exception.getMessage(), exception);
}
//
return result;
}
/**
*
* CAUTION: created_at parameter is ignored in case of non project owner or
* admin.
*
* @param args
* @throws GitLabAPIException
* @throws IOException
* @throws URISyntaxException
*/
public static int editIssue(final ProjectRef project, final int issueId, final String title, final String description, final String assigneeId, final String milestoneId, final StringList labels,
final String stateEvent, final String updatedAt) throws GitLabAPIException
{
int result;
try
{
String url = String.format("%sapi/v3/projects/%d/issues/%d", project.getWebsite(), project.getId(), issueId);
StringList fixedLabels;
if (labels == null)
{
fixedLabels = new StringList();
}
else
{
fixedLabels = labels;
}
//
String response = Utils.httpPut(new URI(url), project.getPrivateToken(), "title", fixBug19902(title), "description", fixBug19902(description), "labels", fixedLabels.toStringWithCommas(),
"state_event", stateEvent, "updated_at", updatedAt);
//
JsonReader source = Json.createReader(new StringReader(response));
JsonObject item = source.readObject();
result = item.getInt("id");
}
catch (IOException | URISyntaxException | HttpException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error creating issue: " + exception.getMessage(), exception);
}
//
return result;
}
/**
*
* https://gitlab.com/gitlab-org/gitlab-ce/issues/19902
*
* @param source
* @return
*/
private static String fixBug19902(final String string)
{
String result;
if (string == null)
{
result = null;
}
else
{
result = string.replace("é", "e").replace("ç", "c").replace("è", "e");
}
//
return result;
}
/**
*
* @param sourceWebsite
* @param projectId
* @param privateToken
* @throws GitLabAPIException
* @throws IOException
* @throws URISyntaxException
*/
public static void importLabels(final ProjectRef source, final ProjectRef target) throws GitLabAPIException
{ {
Labels labels = GitLab.searchLabels(source); Labels labels = GitLab.searchLabels(source);
@ -145,100 +295,59 @@ public class GitLab
} }
/** /**
* *
* @param args * @param args
* @throws IOException * @throws IOException
* @throws GitLabAPIException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static Issue searchIssue(final ProjectRef project, final int issueId) throws IOException public static Issue searchIssue(final ProjectRef project, final int issueId) throws GitLabAPIException
{ {
Issue result; Issue result;
logger.debug("[project={}][issueId={}]", project, issueId); try
String url = String.format("%sapi/v3/projects/%d/issues/%d", project.getWebsite(), project.getId(), issueId);
logger.debug("url=[{}]", url);
//
byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Issue();
/*
result.setId(item.getInt("id"));
result.setIid(item.getInt("iid"));
result.setProjectId(item.getInt("project_id"));
result.setTitle(item.getString("title"));
result.setDescription(item.getString("description"));
result.setState(item.getString("state"));
result.setCreatedAt(item.getString("created_at"));
result.setUpdatedAt(item.getString("updated_at"));
// private StringList labels;
// issue.setMilestone(item.getString("milestone"));
// private String assignee;
result.setAuthorId(item.getJsonObject("author").getInt("id"));
result.setAuthorUserName(item.getJsonObject("author").getString("username"));
result.setSubscribed(item.getBoolean("subscribed"));
*/
//
return result;
}
/**
*
* @param args
* @throws IOException
* @throws URISyntaxException
*/
public static Issues searchIssues(final ProjectRef project) throws IOException
{
Issues result;
String url = String.format("%sapi/v3/projects/%d/issues", project.getWebsite(), project.getId());
//
byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Issues();
for (JsonObject item : items.getValuesAs(JsonObject.class))
{ {
Issue issue = new Issue(); logger.debug("[project={}][issueId={}]", project, issueId);
issue.setId(item.getInt("id")); String url = String.format("%sapi/v3/projects/%d/issues/%d", project.getWebsite(), project.getId(), issueId);
issue.setIid(item.getInt("iid"));
issue.setProjectId(item.getInt("project_id")); logger.debug("url=[{}]", url);
issue.setTitle(item.getString("title"));
issue.setDescription(item.getString("description")); //
issue.setState(item.getString("state")); byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
issue.setCreatedAt(item.getString("created_at"));
issue.setUpdatedAt(item.getString("updated_at")); //
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Issue();
/*
result.setId(item.getInt("id"));
result.setIid(item.getInt("iid"));
result.setProjectId(item.getInt("project_id"));
result.setTitle(item.getString("title"));
result.setDescription(item.getString("description"));
result.setState(item.getString("state"));
result.setCreatedAt(item.getString("created_at"));
result.setUpdatedAt(item.getString("updated_at"));
// private StringList labels; // private StringList labels;
// issue.setMilestone(item.getString("milestone")); // issue.setMilestone(item.getString("milestone"));
// private String assignee; // private String assignee;
issue.setAuthorId(item.getJsonObject("author").getInt("id")); result.setAuthorId(item.getJsonObject("author").getInt("id"));
issue.setAuthorUserName(item.getJsonObject("author").getString("username")); result.setAuthorUserName(item.getJsonObject("author").getString("username"));
issue.setSubscribed(item.getBoolean("subscribed")); result.setSubscribed(item.getBoolean("subscribed"));
*/
result.add(issue); }
catch (IOException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error searching issue: " + exception.getMessage(), exception);
} }
// //
@ -246,38 +355,51 @@ public class GitLab
} }
/** /**
* *
* @param args * @param args
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static Labels searchLabels(final ProjectRef project) throws IOException public static Notes searchIssueNotes(final ProjectRef project, final int issueId) throws GitLabAPIException
{ {
Labels result; Notes result;
String url = String.format("%sapi/v3/projects/%d/labels", project.getWebsite(), project.getId()); try
//
byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Labels();
for (JsonObject item : items.getValuesAs(JsonObject.class))
{ {
Label label = new Label(); String url = String.format("%sapi/v3/projects/%d/issues/%d/notes", project.getWebsite(), project.getId(), issueId);
label.setName(item.getString("name")); //
label.setColor(item.getString("color")); byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
result.add(label); //
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Notes();
for (JsonObject item : items.getValuesAs(JsonObject.class))
{
Note note = new Note();
note.setId(item.getInt("id"));
note.setBody(item.getString("body"));
note.setCreatedAt(item.getString("created_at"));
note.setAuthorId(item.getJsonObject("author").getInt("id"));
note.setAuthorName(item.getJsonObject("author").getString("name"));
note.setAuthorUserName(item.getJsonObject("author").getString("username"));
result.add(note);
}
}
catch (IOException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error searching labels: " + exception.getMessage(), exception);
} }
// //
@ -285,67 +407,189 @@ public class GitLab
} }
/** /**
* *
* @param args
* @throws GitLabAPIException
* @throws IOException
* @throws URISyntaxException
*/
public static Issues searchIssues(final ProjectRef project) throws GitLabAPIException
{
Issues result;
try
{
String url = String.format("%sapi/v3/projects/%d/issues", project.getWebsite(), project.getId());
//
byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Issues();
for (JsonObject item : items.getValuesAs(JsonObject.class))
{
Issue issue = new Issue();
issue.setId(item.getInt("id"));
issue.setIid(item.getInt("iid"));
issue.setProjectId(item.getInt("project_id"));
issue.setTitle(item.getString("title"));
issue.setDescription(item.getString("description"));
issue.setState(item.getString("state"));
issue.setCreatedAt(item.getString("created_at"));
issue.setUpdatedAt(item.getString("updated_at"));
for (JsonString subItem : item.getJsonArray("labels").getValuesAs(JsonString.class))
{
issue.labels().add(subItem.getString());
}
// issue.setMilestone(item.getString("milestone"));
// private String assignee;
issue.setAuthorId(item.getJsonObject("author").getInt("id"));
issue.setAuthorName(item.getJsonObject("author").getString("name"));
issue.setAuthorUserName(item.getJsonObject("author").getString("username"));
issue.setSubscribed(item.getBoolean("subscribed"));
result.add(issue);
}
}
catch (IOException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error searching labels: " + exception.getMessage(), exception);
}
//
return result;
}
/**
*
* @param args
* @throws GitLabAPIException
* @throws IOException
* @throws URISyntaxException
*/
public static Labels searchLabels(final ProjectRef project) throws GitLabAPIException
{
Labels result;
try
{
String url = String.format("%sapi/v3/projects/%d/labels", project.getWebsite(), project.getId());
//
byte[] data = Utils.httpGet(new URL(url), project.getPrivateToken());
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
result = new Labels();
for (JsonObject item : items.getValuesAs(JsonObject.class))
{
Label label = new Label();
label.setName(item.getString("name"));
label.setColor(item.getString("color"));
result.add(label);
}
}
catch (IOException exception)
{
logger.error(exception.getMessage(), exception);
throw new GitLabAPIException("Error searching labels: " + exception.getMessage(), exception);
}
//
return result;
}
/**
*
* @param website * @param website
* @param projectName * @param projectName
* name of project with namespace * name of project with namespace
* @param privateToken * @param privateToken
* @return * @return
* @throws GitLabAPIException
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static ProjectRef searchProject(final String website, final String token, final String privateToken) throws IOException public static ProjectRef searchProject(final String website, final String token, final String privateToken) throws GitLabAPIException
{ {
ProjectRef result; ProjectRef result;
String url = String.format("%sapi/v3/projects", website); try
//
byte[] data = Utils.httpGet(new URL(url), privateToken);
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
//
boolean ended = false;
int itemIndex = 0;
JsonObject searchedItem = null;
while (!ended)
{ {
if (itemIndex < items.size()) String url = String.format("%sapi/v3/projects", website);
{
JsonObject item = items.getJsonObject(itemIndex);
if (StringUtils.equals(item.getString("name_with_namespace"), token)) //
byte[] data = Utils.httpGet(new URL(url), privateToken);
//
StringList json = new StringList(IOUtils.readLines(new ByteArrayInputStream(data), DEFAULT_CHARSET_NAME));
logger.debug("json={}", json);
//
JsonReader source = Json.createReader(new ByteArrayInputStream(data));
JsonArray items = source.readArray();
//
boolean ended = false;
int itemIndex = 0;
JsonObject searchedItem = null;
while (!ended)
{
if (itemIndex < items.size())
{ {
ended = true; JsonObject item = items.getJsonObject(itemIndex);
searchedItem = item;
if (StringUtils.equals(item.getString("name_with_namespace"), token))
{
ended = true;
searchedItem = item;
}
else
{
itemIndex += 1;
}
} }
else else
{ {
itemIndex += 1; ended = true;
searchedItem = null;
} }
} }
//
if (searchedItem == null)
{
result = null;
}
else else
{ {
ended = true; result = new ProjectRef(website, searchedItem.getInt("id"), privateToken);
searchedItem = null;
} }
} }
catch (IOException exception)
//
if (searchedItem == null)
{ {
result = null; logger.error(exception.getMessage(), exception);
} throw new GitLabAPIException("Error searching labels: " + exception.getMessage(), exception);
else
{
result = new ProjectRef(website, searchedItem.getInt("id"), privateToken);
} }
// //

View file

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory;
import fr.devinsy.util.strings.StringList; import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author Christian Pierre MOMON (christian.momon@devinsy.fr) * @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/ */
public class Issue public class Issue
@ -25,12 +25,13 @@ public class Issue
private StringList labels; private StringList labels;
private String milestone; private String milestone;
private String assignee; private String assignee;
private String authorUserName;
private int authorId; private int authorId;
private String authorName;
private String authorUserName;
private boolean subscribed; private boolean subscribed;
/** /**
* *
*/ */
public Issue() public Issue()
{ {
@ -47,6 +48,11 @@ public class Issue
return this.authorId; return this.authorId;
} }
public String getAuthorName()
{
return this.authorName;
}
public String getAuthorUserName() public String getAuthorUserName()
{ {
return this.authorUserName; return this.authorUserName;
@ -72,11 +78,6 @@ public class Issue
return this.iid; return this.iid;
} }
public StringList labels()
{
return this.labels;
}
public String getMilestone() public String getMilestone()
{ {
return this.milestone; return this.milestone;
@ -102,11 +103,21 @@ public class Issue
return this.updatedAt; return this.updatedAt;
} }
public boolean isSubscribed()
{
return this.subscribed;
}
public boolean isSuscribed() public boolean isSuscribed()
{ {
return this.subscribed; return this.subscribed;
} }
public StringList labels()
{
return this.labels;
}
public void setAssignee(final String assignee) public void setAssignee(final String assignee)
{ {
this.assignee = assignee; this.assignee = assignee;
@ -117,6 +128,11 @@ public class Issue
this.authorId = authorId; this.authorId = authorId;
} }
public void setAuthorName(final String authorName)
{
this.authorName = authorName;
}
public void setAuthorUserName(final String authorUserName) public void setAuthorUserName(final String authorUserName)
{ {
this.authorUserName = authorUserName; this.authorUserName = authorUserName;
@ -173,7 +189,7 @@ public class Issue
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
* @author Christian Pierre MOMON (christian.momon@devinsy.fr) * @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/ */
public class Label public class Label
@ -16,7 +16,7 @@ public class Label
private String description; private String description;
/** /**
* *
* @param name * @param name
* @param color * @param color
*/ */
@ -26,7 +26,7 @@ public class Label
} }
/** /**
* *
* @param name * @param name
* @param color * @param color
*/ */
@ -36,7 +36,7 @@ public class Label
} }
/** /**
* *
* @param name * @param name
* @param color * @param color
* @param description * @param description
@ -79,7 +79,7 @@ public class Label
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()

View file

@ -0,0 +1,170 @@
package fr.devinsy.gitlabapic.v3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class Note
{
private static final Logger logger = LoggerFactory.getLogger(Note.class);
private int id;
private String body;
// "attachment": null,
private int authorId;
private String authorName;
private String authorUserName;
// "author": {"id": 1, "username": "pipin", "email": "admin@example.com",
// "name": "Pip", "state": "active", "created_at": "2013-09-30T13:46:01Z" },
private String createdAt;
private String updatedAt;
private boolean system;
private boolean upvote;
private boolean downvote;
private int noteableId;
private String noteableType;
/**
*
*/
public Note()
{
}
public int getAuthorId()
{
return this.authorId;
}
public String getAuthorName()
{
return this.authorName;
}
public String getAuthorUserName()
{
return this.authorUserName;
}
public String getBody()
{
return this.body;
}
public String getCreatedAt()
{
return this.createdAt;
}
public int getId()
{
return this.id;
}
public int getNoteableId()
{
return this.noteableId;
}
public String getNoteableType()
{
return this.noteableType;
}
public String getUpdatedAt()
{
return this.updatedAt;
}
public boolean isDownvote()
{
return this.downvote;
}
public boolean isSystem()
{
return this.system;
}
public boolean isUpvote()
{
return this.upvote;
}
public void setAuthorId(final int authorId)
{
this.authorId = authorId;
}
public void setAuthorName(final String authorName)
{
this.authorName = authorName;
}
public void setAuthorUserName(final String authorUserName)
{
this.authorUserName = authorUserName;
}
public void setBody(final String body)
{
this.body = body;
}
public void setCreatedAt(final String createdAt)
{
this.createdAt = createdAt;
}
public void setDownvote(final boolean downvote)
{
this.downvote = downvote;
}
public void setId(final int id)
{
this.id = id;
}
public void setNoteableId(final int noteableId)
{
this.noteableId = noteableId;
}
public void setNoteableType(final String noteableType)
{
this.noteableType = noteableType;
}
public void setSystem(final boolean system)
{
this.system = system;
}
public void setUpdatedAt(final String updatedAt)
{
this.updatedAt = updatedAt;
}
public void setUpvote(final boolean upvote)
{
this.upvote = upvote;
}
/**
*
*/
@Override
public String toString()
{
String result;
result = String.format("[id=%d][body=%s]", this.id, this.body);
//
return result;
}
}

View file

@ -0,0 +1,32 @@
package fr.devinsy.gitlabapic.v3;
import java.util.ArrayList;
import java.util.Collections;
import fr.devinsy.gitlabapic.v3.IssueComparator.Sorting;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class Notes extends ArrayList<Note>
{
private static final long serialVersionUID = 5126712173655206727L;
/**
*
*/
public Notes()
{
super();
}
/**
*
* @param initialCapacity
*/
public Notes(final int initialCapacity)
{
super(initialCapacity);
}
}