Skip to content

Commit

Permalink
added GithubUtils and controller method to retrieve recent events for…
Browse files Browse the repository at this point in the history
… a repository
  • Loading branch information
Zomis committed Jan 21, 2015
1 parent ef46383 commit 0dec0b9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
package com.skiwi.githubhooksechatservice.events;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.skiwi.githubhooksechatservice.jackson.MessageTypeIdResolver;

/**
*
* @author Frank van Heeswijk
*/
@JsonTypeInfo(use = Id.CUSTOM, property = "type", include = As.PROPERTY)
@JsonTypeIdResolver(MessageTypeIdResolver.class)
public abstract class AnySetterJSONObject {
@JsonAnySetter
protected void anySetter(final String name, final Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.skiwi.githubhooksechatservice.mvc.beans.GithubUtils;
import com.skiwi.githubhooksechatservice.mvc.beans.StartupBean;
import com.skiwi.githubhooksechatservice.mvc.beans.Statistics;

Expand Down Expand Up @@ -52,6 +53,11 @@ public StartupBean startup() {
return new StartupBean();
}

@Bean
public GithubUtils githubUtils() {
return new GithubUtils();
}

@Bean
public com.skiwi.githubhooksechatservice.mvc.configuration.BotConfiguration config() {
com.skiwi.githubhooksechatservice.mvc.configuration.BotConfiguration config = new com.skiwi.githubhooksechatservice.mvc.configuration.BotConfiguration();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.skiwi.githubhooksechatservice.jackson;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class MessageTypeIdResolver implements TypeIdResolver {

private JavaType mBaseType;

@Override
public void init(JavaType baseType) {
mBaseType = baseType;
}

@Override
public Id getMechanism() {
return Id.CUSTOM;
}

@Override
public String idFromValue(Object obj) {
return idFromValueAndType(obj, obj.getClass());
}

@Override
public String idFromBaseType() {
throw new AssertionError("this should never happen");
}

@Override
public String idFromValueAndType(Object obj, Class<?> clazz) {
return clazz.getSimpleName();// AnySetterJSONObject mess = (AnySetterJSONObject) obj;
// return mess.getCommand();
}

@Override
public JavaType typeFromId(String type) {
Class<?> clazz;
try {
clazz = Class.forName("com.skiwi.githubhooksechatservice.events.github." + type);
} catch (ClassNotFoundException e) {
throw new UnsupportedOperationException("No such defined type: " + type);
}
return TypeFactory.defaultInstance().constructSpecializedType(mBaseType, clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.skiwi.githubhooksechatservice.mvc.beans;

import java.io.IOException;
import java.net.URL;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.skiwi.githubhooksechatservice.events.AnySetterJSONObject;

public class GithubUtils {



public AnySetterJSONObject[] data(String name, String repository) {
ObjectMapper mapper = new ObjectMapper(); // just need one
try {
URL url = new URL("https://api.github.com/repos/" + name + "/" + repository + "/events");
AnySetterJSONObject[] data = mapper.readValue(url, AnySetterJSONObject[].class);
return data;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}



}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.skiwi.githubhooksechatservice.mvc.controllers;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skiwi.githubhooksechatservice.chatbot.ChatBot;
import com.skiwi.githubhooksechatservice.events.AnySetterJSONObject;
import com.skiwi.githubhooksechatservice.mvc.beans.GithubUtils;

/**
*
Expand All @@ -19,13 +24,23 @@ public class BotController {
@Autowired
private ChatBot chatBot;

@Autowired
private GithubUtils githubUtils;

@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String hello() {
System.out.println("test");
return "Hello World!";
}

@RequestMapping(value = "/gittest", method = RequestMethod.GET)
@ResponseBody
public String gitScan(@RequestParam("name") String name, @RequestParam("repository") String repository) {
AnySetterJSONObject[] blocks = githubUtils.data(name, repository);
return Arrays.toString(blocks);
}

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public void test(WebhookParameters params) {
Expand Down

0 comments on commit 0dec0b9

Please sign in to comment.