diff options
7 files changed, 172 insertions, 88 deletions
@@ -1,82 +1,87 @@ <?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <groupId>uk.co.blatech</groupId> - <artifactId>blaears</artifactId> - <version>0.0.1-SNAPSHOT</version> - <packaging>jar</packaging> + <groupId>uk.co.blatech</groupId> + <artifactId>blaears</artifactId> + <version>0.0.1-SNAPSHOT</version> + <packaging>jar</packaging> - <name>blaears</name> - <description>Listens for commands and does things</description> + <name>blaears</name> + <description>Listens for commands and does things</description> - <parent> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-parent</artifactId> - <version>1.3.0.RELEASE</version> - <relativePath/> <!-- lookup parent from repository --> - </parent> + <parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>1.3.0.RELEASE</version> + <relativePath/> <!-- lookup parent from repository --> + </parent> - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <java.version>1.7</java.version> - <jackson.version>2.6.4</jackson.version> - </properties> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <java.version>1.7</java.version> + <jackson.version>2.6.4</jackson.version> + </properties> - <dependencies> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-velocity</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-web</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter</artifactId> - <exclusions> - <exclusion> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <scope>test</scope> - <exclusions> - <exclusion> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-log4j</artifactId> - </dependency> + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-velocity</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-log4j</artifactId> + </dependency> - <!-- Jackson JSON Mapper --> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>${jackson.version}</version> - </dependency> + <!-- Jackson JSON Mapper --> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>${jackson.version}</version> + </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> - </plugin> - </plugins> - </build> - + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.4</version> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + </project> -€
\ No newline at end of file diff --git a/src/main/java/uk/co/blatech/blaears/controllers/BlauploadController.java b/src/main/java/uk/co/blatech/blaears/controllers/BlauploadController.java new file mode 100644 index 0000000..4d11d79 --- /dev/null +++ b/src/main/java/uk/co/blatech/blaears/controllers/BlauploadController.java @@ -0,0 +1,44 @@ +package uk.co.blatech.blaears.controllers; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +import java.io.IOException; + +/** + * Takes notifications of uploads from Blaupload and sends them to IRC + */ +@Controller +public class BlauploadController { + + private static final Logger log = Logger.getLogger(BlauploadController.class); + + @RequestMapping(value = "/blaupload", method = RequestMethod.POST) + String notify(@RequestParam("url") String url, @RequestParam("channel") String channel, Model model) { + + if (StringUtils.isBlank(url) || StringUtils.isBlank(channel)) { + log.error("Invalid notification from blaupload: url=" + url + ", channel=" + channel); + model.addAttribute("result", "failed"); + return "blaupload"; + } + String command = "echo " + channel + " New file uploaded: " + url + "| bash /home/smsd/msgrelay.bash"; + log.info("Running command: [" + command + "]"); + try { + Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", command}); + } catch (IOException e) { + log.error("Failed to send message to " + channel + " for " + url); + e.printStackTrace(); + model.addAttribute("result", "failed"); + return "blaupload"; + } + + log.info("blaupload notification for " + url); + model.addAttribute("result", "OK"); + return "blaupload"; + } +} diff --git a/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java b/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java index 179caee..7c6fecb 100644 --- a/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java +++ b/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java @@ -1,12 +1,11 @@ package uk.co.blatech.blaears.controllers; -import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import uk.co.blatech.blaears.models.gitlab.Commit; import uk.co.blatech.blaears.models.gitlab.Merge; import java.io.IOException; @@ -18,32 +17,42 @@ import java.io.IOException; @Controller public class GitMergeController { + private static final Logger log = Logger.getLogger(GitMergeController.class); + @RequestMapping(value = "/git/merge", method = RequestMethod.POST) String merge(@RequestBody Merge mergeMsg, Model model){ try { - String action = ""; - - if (mergeMsg.getObjectAttributes().getAction().equals("open")) { - action = " created a merge request from "; - } else if (mergeMsg.getObjectAttributes().getAction().equals("merge")) { - action = " merged from branch "; - } else { - action = " did a " + mergeMsg.getObjectAttributes().getAction() + " from "; -// return "gitlab"; - } + String action = mergeMsg.getObjectAttributes().getAction(); String user = mergeMsg.getUser().getUsername(); String source = mergeMsg.getObjectAttributes().getSourceBranch(); String target = mergeMsg.getObjectAttributes().getTargetBranch(); String url = mergeMsg.getObjectAttributes().getUrl(); String title = mergeMsg.getObjectAttributes().getTitle(); String repo = mergeMsg.getRepository().getName(); + + log.info ("Received a merge " + action + " by " + user + " from " + source + " to " + target + ", " + title + " " + url); + String actionTxt = ""; + + if (action.equals("open")) { + actionTxt = " created a merge request from ["; + } else if (action.equals("merge")) { + actionTxt = " merged from branch ["; + } else if (!action.equals("update")){ + actionTxt = " did a " + mergeMsg.getObjectAttributes().getAction() + " from ["; + } else { + log.info ("Dropping update message"); + return "gitlab"; + } + + String command = "echo theblueroom " + user + actionTxt + source + "] to [" + target + "] on [" + repo + "]: [" + title + "] "+ url + " | bash /home/smsd/msgrelay.bash "; + log.info("Running command: " + command); //DANGER - Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "echo theblueroom " + user + action + source + " to " + target + " on " + repo + ": [" + title + "] "+ url + " | bash /home/smsd/msgrelay.bash "}); + Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", command}); model.addAttribute("result", "OK"); } catch (IOException e) { - System.out.println("Failed to run command"); + log.error("Failed to run command"); e.printStackTrace(); model.addAttribute("result", "failed"); } diff --git a/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java b/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java index 9d461c9..e0f684b 100644 --- a/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java +++ b/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java @@ -23,6 +23,10 @@ public class GitPushController { @RequestMapping(value = "/git/push", method = RequestMethod.POST) String push(@RequestBody Push pushMsg, Model model){ + if (!pushMsg.getRef().endsWith("master")) { + log.info("Push is not to master, dropping message"); + return "gitlab"; + } try { String commitMsg = ""; for (Commit commit: pushMsg.getCommits()) { @@ -35,10 +39,13 @@ public class GitPushController { log.info("Received commits: " + commitMsg); //DANGER - Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "echo theblueroom " + pushMsg.getUserName() + " pushed to " + pushMsg.getRepository().getName() + ": "+ commitMsg + " | bash /home/smsd/msgrelay.bash "}); + String command = "echo theblueroom " + pushMsg.getUserName() + " pushed to [" + pushMsg.getRepository().getName() + "]: "+ commitMsg + " | bash /home/smsd/msgrelay.bash"; + log.info("Running command: " + command); + + Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", command}); model.addAttribute("result", "OK"); } catch (IOException e) { - System.out.println("Failed to run command"); + log.error("Failed to run command"); e.printStackTrace(); model.addAttribute("result", "failed"); } diff --git a/src/main/java/uk/co/blatech/blaears/models/gitlab/Push.java b/src/main/java/uk/co/blatech/blaears/models/gitlab/Push.java index bc20045..8d81a01 100644 --- a/src/main/java/uk/co/blatech/blaears/models/gitlab/Push.java +++ b/src/main/java/uk/co/blatech/blaears/models/gitlab/Push.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class Push { String objectKind; + String ref; @JsonProperty("user_name") String userName; Repository repository; @@ -24,6 +25,14 @@ public class Push { this.objectKind = objectKind; } + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + public String getUserName() { return userName; } diff --git a/src/main/resources/templates/blaupload.vm b/src/main/resources/templates/blaupload.vm new file mode 100644 index 0000000..a39cf9a --- /dev/null +++ b/src/main/resources/templates/blaupload.vm @@ -0,0 +1,10 @@ +<!DOCTYPE html> +<html> +<head lang="en"> + <meta charset="UTF-8"> + <title></title> +</head> +<body> + $result +</body> +</html>
\ No newline at end of file diff --git a/src/main/resources/templates/gitlab.vm b/src/main/resources/templates/gitlab.vm index e3529fc..a39cf9a 100644 --- a/src/main/resources/templates/gitlab.vm +++ b/src/main/resources/templates/gitlab.vm @@ -5,6 +5,6 @@ <title></title> </head> <body> - $body + $result </body> </html>
\ No newline at end of file |