| |
@@ -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
|
| |
I removed the pagure api from pagure-scm and added it as a maven dependency instead.
Right now to work with it you need to do following:
1. You need to clone pagure-java-client
git clone ssh://git@pagure.io/pagure-java-client.git
2. You need to build maven artifact
mvn package
3. Install file to local maven repository
mvn install:install-file -Dfile=target/pagure-api-1.0-SNAPSHOT.jar
The pagure-scm project will now successfully resolve the dependency.