From 51932b75d8fcf75c7f3617a110fe60271cd72b57 Mon Sep 17 00:00:00 2001 From: Michal Konečný Date: Sep 19 2018 10:42:32 +0000 Subject: Use pagure api as dependency --- diff --git a/pom.xml b/pom.xml index fc79d92..50dc309 100644 --- a/pom.xml +++ b/pom.xml @@ -2,6 +2,11 @@ 4.0.0 + com.mycompany + pagure-api + 1.0-SNAPSHOT + + org.jenkins-ci.plugins structs 1.14 @@ -31,7 +36,7 @@ handy-uri-templates 2.1.6 - + com.mashape.unirest unirest-java 1.4.9 @@ -46,11 +51,6 @@ jackson-databind 2.6.2 - - com.damnhandy - handy-uri-templates - 2.1.6 - diff --git a/src/main/java/org/jenkinsci/plugins/pagure/BranchDiscoveryTrait.java b/src/main/java/org/jenkinsci/plugins/pagure/BranchDiscoveryTrait.java index 2039484..9f26bcc 100644 --- a/src/main/java/org/jenkinsci/plugins/pagure/BranchDiscoveryTrait.java +++ b/src/main/java/org/jenkinsci/plugins/pagure/BranchDiscoveryTrait.java @@ -7,7 +7,7 @@ import jenkins.scm.api.*; import jenkins.scm.api.trait.*; import jenkins.scm.impl.trait.Discovery; import org.apache.commons.lang.StringUtils; -import org.jenkinsci.plugins.pagure.api.PagurePullRequest; +import io.pagure.api.PagurePullRequest; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.DataBoundConstructor; diff --git a/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSource.java b/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSource.java index f5cf7e1..30932eb 100644 --- a/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSource.java +++ b/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSource.java @@ -15,7 +15,7 @@ import jenkins.scm.api.*; import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy; import jenkins.scm.api.trait.SCMSourceTrait; import org.apache.commons.lang.StringUtils; -import org.jenkinsci.plugins.pagure.api.PagureProject; +import io.pagure.api.PagureProject; import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; diff --git a/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSourceRequest.java b/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSourceRequest.java index 7e53bf0..d596740 100644 --- a/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSourceRequest.java +++ b/src/main/java/org/jenkinsci/plugins/pagure/PagureSCMSourceRequest.java @@ -11,8 +11,8 @@ import jenkins.scm.api.SCMSource; import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy; import jenkins.scm.api.mixin.TagSCMHead; import jenkins.scm.api.trait.SCMSourceRequest; -import org.jenkinsci.plugins.pagure.api.Pagure; -import org.jenkinsci.plugins.pagure.api.PagurePullRequest; +import io.pagure.api.Pagure; +import io.pagure.api.PagurePullRequest; import java.util.*; diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/Pagure.java b/src/main/java/org/jenkinsci/plugins/pagure/api/Pagure.java deleted file mode 100644 index b7ecdb0..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/Pagure.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.jenkinsci.plugins.pagure.api; - -import com.damnhandy.uri.template.UriTemplate; -import com.damnhandy.uri.template.UriTemplateBuilder; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.mashape.unirest.http.HttpResponse; -import com.mashape.unirest.http.Unirest; -import com.mashape.unirest.http.exceptions.UnirestException; - -import java.io.IOException; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - - -public class Pagure implements PagureInterface { - - private final String pagureServer; - private String apiPath = "api"; - private Integer apiVersion = 0; - private ObjectMapper mapper = new ObjectMapper(); - - - public Pagure(String pagureServer) { - - this.pagureServer = pagureServer; - - // All of our Enums set strings in the toString() method so we don't have to - // rely on uppercased values that come with the name() method. - mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); - } - - public Pagure withApiPath(String apiPath){ - this.apiPath = apiPath; - return this; - } - - public Pagure withApiVersion(Integer apiVersion){ - this.apiVersion = apiVersion; - return this; - } - - @Override - public PagureProject getProject(String projectName) throws IOException, UnirestException { - return anObject( - pagure() - .path("projectname") - .build() - .set("apiPath", apiPath) - .set("apiVersion", apiVersion) - .set("projectname", projectName), - PagureProject.class - ); - } - - @Override - public List getProjects(String username) throws IOException, UnirestException { - return listOfObjects( - pagure() - .literal("/projects") - .query("owner") - .build() - .set("apiPath", apiPath) - .set("apiVersion", apiVersion) - .set("owner", username), - PagureProject.class, - "projects" - ); - - } - - @Override - public List getProjects(PagureGroup pagureGroup) throws IOException, UnirestException { - return getProjects(pagureGroup.name); - } - - @Override - public PagurePullRequest getPullRequest(String projectName, long prNumber) throws IOException, UnirestException{ - return anObject( - pagure() - .path("projectpath") - .literal("/pull-request") - .path("prnumber") - .build() - .set("apiPath", apiPath) - .set("apiVersion", apiVersion) - .set("projectpath", projectName) - .set("prnumber", prNumber), - PagurePullRequest.class - ); - } - - @Override - public PagurePullRequest getPullRequest(PagureProject project, long prNumber) - throws IOException, UnirestException { - - return getPullRequest(project.fullname, prNumber); - } - - @Override - public List getPullRequests(String projectName) throws IOException, UnirestException { - return listOfObjects( - pagure() - .path("projectpath") - .literal("/pull-requests") - .build() - .set("apiPath", apiPath) - .set("apiVersion", apiVersion) - .set("projectpath", projectName), - PagurePullRequest.class - ); - } - - @Override - public List getPullRequests(PagureProject project) throws IOException, UnirestException { - return getPullRequests(project.fullname); - } - - @Override - public List getPullRequests(String projectName, PagurePullRequestState state) - throws IOException, UnirestException { - return listOfObjects( - pagure() - .path("projectpath") - .literal("/pull-requests") - .query("status") - .build() - .set("status", state.toString()) - .set("apiPath", apiPath) - .set("apiVersion", apiVersion) - .set("projectpath", projectName), - PagurePullRequest.class - ); - } - - @Override - public List getPullRequests(PagureProject project, PagurePullRequestState state) - throws IOException, UnirestException { - return getPullRequests(project.fullname, state); - } - - - @Override - public PagureUser getCurrentUser() { - return null; - } - - @Override - public PagureUser getUser(String username) { - return null; - } - - @Override - public PagureGroup getGroup(String groupName) throws IOException, UnirestException { - return anObject( - pagure() - .literal("/group") - .path("groupname") - .build() - .set("apiPath", apiPath) - .set("apiVersion", apiVersion) - .set("groupname", groupName), - PagureGroup.class - ); - } - - - - public static void main(String[] args){ - - Pagure client = new Pagure("https://stg.pagure.io"); - - try { - List r = client.getProjects("bstinson"); - System.out.println(r); - } catch (Exception ex) { - Logger.getLogger(Pagure.class.getName()).log(Level.SEVERE, null, ex); - } - - try { - PagurePullRequest pr = client.getPullRequest("test-branches", 5); - System.out.println(pr); - } catch (Exception ex) { - Logger.getLogger(Pagure.class.getName()).log(Level.SEVERE, null, ex); - } - - try { - PagureGroup group = client.getGroup("pagure-scm-plugin-test"); - System.out.println(group); - } catch (Exception ex) { - Logger.getLogger(Pagure.class.getName()).log(Level.SEVERE, null, ex); - } - } - - private UriTemplateBuilder pagure(){ - return UriTemplate.buildFromTemplate(pagureServer) - .path(UriTemplateBuilder.var("apiPath")) - .path(UriTemplateBuilder.var("apiVersion")); - } - - private T anObject(UriTemplate template, final Class pagureObjectClass) throws IOException { - HttpResponse response = null; - try { - System.out.println(template.expand()); - response = Unirest.get(template.expand()).asString(); - } catch (UnirestException e) { - e.printStackTrace(); - } - - JsonNode n = mapper.readTree(response.getBody()); - - return mapper - .readerFor(mapper.getTypeFactory() - .constructType(pagureObjectClass)) - .readValue(n); - } - - private List listOfObjects(UriTemplate template, final Class pagureObjectClass) throws IOException { - HttpResponse response = null; - try { - response = Unirest.get(template.expand()).asString(); - } catch (UnirestException e) { - e.printStackTrace(); - } - - JsonNode n = mapper.readTree(response.getBody()); - - return mapper - .readerFor(mapper.getTypeFactory() - .constructCollectionType(List.class, pagureObjectClass)) - .readValue(n); - - } - - private List listOfObjects(UriTemplate template, final Class pagureObjectClass, String jsonAttribute) throws IOException { - HttpResponse response = null; - try { - response = Unirest.get(template.expand()).asString(); - } catch (UnirestException e) { - e.printStackTrace(); - } - - JsonNode n = mapper.readTree(response.getBody()); - - return mapper - .readerFor(mapper.getTypeFactory() - .constructCollectionType(List.class, pagureObjectClass)) - .readValue(n.get(jsonAttribute)); - - } - - @Override - public void close() { - } -} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureGroup.java b/src/main/java/org/jenkinsci/plugins/pagure/api/PagureGroup.java deleted file mode 100644 index 5a456ff..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureGroup.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.jenkinsci.plugins.pagure.api; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.List; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class PagureGroup { - @JsonProperty("creator") - Object creator; - - @JsonProperty("date_created") - String date_created; - - @JsonProperty("description") - String description; - - @JsonProperty("display_name") - String display_name; - - @JsonProperty("group_type") - String group_type; - - @JsonProperty("members") - List members; - - @JsonProperty("name") - String name; - - @Override - public String toString() { - return "PagureGroup{" + - "creator=" + creator + - ", date_created='" + date_created + '\'' + - ", description='" + description + '\'' + - ", display_name='" + display_name + '\'' + - ", group_type='" + group_type + '\'' + - ", members=" + members + - ", name='" + name + '\'' + - '}'; - } - -} diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureInterface.java b/src/main/java/org/jenkinsci/plugins/pagure/api/PagureInterface.java deleted file mode 100644 index e193863..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureInterface.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.jenkinsci.plugins.pagure.api; - -import com.mashape.unirest.http.exceptions.UnirestException; - -import java.io.IOException; -import java.util.List; - -public interface PagureInterface extends AutoCloseable { - PagureUser getCurrentUser() throws IOException, UnirestException; - PagureUser getUser(String username) throws IOException, UnirestException; - - PagureGroup getGroup(String groupName) throws IOException, UnirestException; - PagureProject getProject(String projectName) throws IOException, UnirestException; - PagurePullRequest getPullRequest(String projectName, long prNumber) throws IOException, UnirestException; - PagurePullRequest getPullRequest(PagureProject projectName, long prNumber) throws IOException, UnirestException; - - List getProjects(String username) throws IOException, UnirestException; - List getProjects(PagureGroup pagureGroup) throws IOException, UnirestException; - - List getPullRequests(String projectName) throws IOException, UnirestException; - List getPullRequests(PagureProject project) throws IOException, UnirestException; - - List getPullRequests(String projectName, PagurePullRequestState state) throws IOException, UnirestException; - List getPullRequests(PagureProject project, PagurePullRequestState state) throws IOException, UnirestException; - -} diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureProject.java b/src/main/java/org/jenkinsci/plugins/pagure/api/PagureProject.java deleted file mode 100644 index ce063fc..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureProject.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.jenkinsci.plugins.pagure.api; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.List; -import java.util.Map; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class PagureProject{ - @JsonProperty("accesss_groups") - Map access_groups; - - @JsonProperty("access_users") - Map access_users; - - @JsonProperty("close_status") - List close_status; - - @JsonProperty("date_created") - String date_created; - - @JsonProperty("date_modified") - String date_modified; - - @JsonProperty("description") - String description; - - @JsonProperty("milestones") - Map milestones; - - @JsonProperty("namespace") - String namespace; - - @JsonProperty("id") - Integer id; - - @JsonProperty("name") - String name; - - @JsonProperty("fullname") - String fullname; - - //@JsonProperty("parent") - //Map parent; - - @JsonProperty("priorities") - Map priorities; - - @JsonProperty("tags") - List tags; - - @JsonProperty("user") - PagureUser user; - - @Override - public String toString() { - return "PagureProject{" + - "fullname='" + fullname + '\'' + - '}'; - } - - /*@JsonProperty("user") - private void unpackuser(Map userjson){ - this.user = new PagureUser(userjson.get("name")); - }*/ -} diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/PagurePullRequest.java b/src/main/java/org/jenkinsci/plugins/pagure/api/PagurePullRequest.java deleted file mode 100644 index 4054821..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/PagurePullRequest.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.jenkinsci.plugins.pagure.api; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.List; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class PagurePullRequest { - - @JsonProperty("assignee") - private PagureUser assignee; - - @JsonProperty("branch") - private String branch; - - @JsonProperty("branch_from") - private String branchFrom; - - @JsonProperty("closed_at") - private Object closedAt; - - @JsonProperty("closed_by") - private Object closedBy; - - @JsonProperty("comments") - private List comments = null; - - @JsonProperty("commit_start") - private Object commitStart; - - @JsonProperty("commit_stop") - private Object commitStop; - - @JsonProperty("date_created") - private String dateCreated; - - @JsonProperty("id") - private int id; - - @JsonProperty("project") - private PagureProject project; - - @JsonProperty("repo_from") - private PagureProject repoFrom; - - @JsonProperty("status") - private PagurePullRequestState status; - - @Override - public String toString() { - return "PagurePullRequest{" + - "assignee=" + assignee + - ", branch='" + branch + '\'' + - ", branchFrom='" + branchFrom + '\'' + - ", closedAt=" + closedAt + - ", closedBy=" + closedBy + - ", comments=" + comments + - ", commitStart=" + commitStart + - ", commitStop=" + commitStop + - ", dateCreated='" + dateCreated + '\'' + - ", id=" + id + - ", project=" + project + - ", repoFrom=" + repoFrom + - ", status=" + status + - '}'; - } -} - diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/PagurePullRequestState.java b/src/main/java/org/jenkinsci/plugins/pagure/api/PagurePullRequestState.java deleted file mode 100644 index 532388e..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/PagurePullRequestState.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.jenkinsci.plugins.pagure.api; - -public enum PagurePullRequestState { - OPEN("Open"), - CLOSED("Closed"), - MERGED("Merged"); - - private final String issueState; - - PagurePullRequestState(String issueState) { - this.issueState = issueState; - } - - @Override - public String toString() { - return issueState; - } -} diff --git a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureUser.java b/src/main/java/org/jenkinsci/plugins/pagure/api/PagureUser.java deleted file mode 100644 index 72492a8..0000000 --- a/src/main/java/org/jenkinsci/plugins/pagure/api/PagureUser.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.jenkinsci.plugins.pagure.api; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * - * @author bstinson - */ - -@JsonIgnoreProperties(ignoreUnknown = true) -class PagureUser{ - @JsonProperty("id") - int id; - - @JsonProperty("name") - String name; - - @JsonProperty("fullname") - String fullname; - -}