#2 Use pagure api as dependency
Merged 5 years ago by bstinson. Opened 5 years ago by zlopez.
cico-tools/ zlopez/pagure-scm pagure_api  into  master

file modified
+6 -6
@@ -2,6 +2,11 @@ 

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>

      <dependencies>

          <dependency>

+             <groupId>com.mycompany</groupId>

+             <artifactId>pagure-api</artifactId>

+             <version>1.0-SNAPSHOT</version>

+         </dependency>

+         <dependency>

              <groupId>org.jenkins-ci.plugins</groupId>

              <artifactId>structs</artifactId>

              <version>1.14</version>
@@ -31,7 +36,7 @@ 

              <artifactId>handy-uri-templates</artifactId>

              <version>2.1.6</version>

          </dependency>

-                 <dependency>

+         <dependency>

              <groupId>com.mashape.unirest</groupId>

              <artifactId>unirest-java</artifactId>

              <version>1.4.9</version>
@@ -46,11 +51,6 @@ 

              <artifactId>jackson-databind</artifactId>

              <version>2.6.2</version>

          </dependency>

-         <dependency>

-             <groupId>com.damnhandy</groupId>

-             <artifactId>handy-uri-templates</artifactId>

-             <version>2.1.6</version>

-         </dependency>

      </dependencies>

      <build>

          <plugins>

@@ -7,7 +7,7 @@ 

  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;

@@ -15,7 +15,7 @@ 

  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;

@@ -11,8 +11,8 @@ 

  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.*;

  

@@ -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<PagureProject> 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<PagureProject> 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<PagurePullRequest> 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<PagurePullRequest> getPullRequests(PagureProject project) throws IOException, UnirestException {

-         return getPullRequests(project.fullname);

-     }

- 

-     @Override

-     public List<PagurePullRequest> 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<PagurePullRequest> 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<PagureProject> 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> T anObject(UriTemplate template, final Class<T> pagureObjectClass) throws IOException {

-         HttpResponse<String> 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 <T> List<T> listOfObjects(UriTemplate template, final Class<T> pagureObjectClass) throws IOException {

-         HttpResponse<String> 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 <T> List<T> listOfObjects(UriTemplate template, final Class<T> pagureObjectClass, String jsonAttribute) throws IOException {

-         HttpResponse<String> 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

@@ -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<String> 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 + '\'' +

-               '}';

-    }

- 

- }

@@ -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<PagureProject> getProjects(String username) throws IOException, UnirestException;

-     List<PagureProject> getProjects(PagureGroup pagureGroup) throws IOException, UnirestException;

- 

-     List<PagurePullRequest> getPullRequests(String projectName) throws IOException, UnirestException;

-     List<PagurePullRequest> getPullRequests(PagureProject project) throws IOException, UnirestException;

- 

-     List<PagurePullRequest> getPullRequests(String projectName, PagurePullRequestState state) throws IOException, UnirestException;

-     List<PagurePullRequest> getPullRequests(PagureProject project, PagurePullRequestState state) throws IOException, UnirestException;

- 

- }

@@ -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<String,List> access_groups;

- 

-     @JsonProperty("access_users")

-     Map<String,List> 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<String, String> milestones;

- 

-     @JsonProperty("namespace")

-     String namespace;

- 

-     @JsonProperty("id")

-     Integer id;

- 

-     @JsonProperty("name")

-     String name;

- 

-     @JsonProperty("fullname")

-     String fullname;

- 

-     //@JsonProperty("parent")

-     //Map parent;

- 

-     @JsonProperty("priorities")

-     Map<String,String> priorities;

- 

-     @JsonProperty("tags")

-     List<String> tags;

- 

-     @JsonProperty("user")

-     PagureUser user;

- 

-     @Override

-     public String toString() {

-         return "PagureProject{" +

-                 "fullname='" + fullname + '\'' +

-                 '}';

-     }

- 

-     /*@JsonProperty("user")

-     private void unpackuser(Map<String,String> userjson){

-         this.user = new PagureUser(userjson.get("name"));

-     }*/

- }

@@ -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<Object> 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 +

-                 '}';

-     }

- }

- 

@@ -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;

-     }

- }

@@ -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;

- 

- }