diff options
Diffstat (limited to 'src/main/java/uk/co')
4 files changed, 85 insertions, 16 deletions
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; } |