From a1c66778daab3307cf4a635fd08d6544b55d16c9 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Thu, 10 Dec 2015 23:13:47 +0000 Subject: Add controller for merge requests --- .../blaears/controllers/GitMergeController.java | 44 +++++++++++++++++++ .../blaears/controllers/GitPushController.java | 41 ++++++++++++++++++ .../blaears/controllers/GitRelayController.java | 41 ------------------ .../uk/co/blatech/blaears/models/gitlab/Merge.java | 50 ++++++++++++++++++++++ .../blaears/models/gitlab/ObjectAttributes.java | 45 +++++++++++++++++++ .../uk/co/blatech/blaears/models/gitlab/User.java | 29 +++++++++++++ 6 files changed, 209 insertions(+), 41 deletions(-) create mode 100644 src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java create mode 100644 src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java delete mode 100644 src/main/java/uk/co/blatech/blaears/controllers/GitRelayController.java create mode 100644 src/main/java/uk/co/blatech/blaears/models/gitlab/Merge.java create mode 100644 src/main/java/uk/co/blatech/blaears/models/gitlab/ObjectAttributes.java create mode 100644 src/main/java/uk/co/blatech/blaears/models/gitlab/User.java diff --git a/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java b/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java new file mode 100644 index 0000000..5622aea --- /dev/null +++ b/src/main/java/uk/co/blatech/blaears/controllers/GitMergeController.java @@ -0,0 +1,44 @@ +package uk.co.blatech.blaears.controllers; + +import org.apache.commons.lang.StringUtils; +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; + +/** + * Takes messages sent by GitLab and sends them to IRC + * Currently just testing to see what we get + */ +@Controller +public class GitMergeController { + + @RequestMapping(value = "/git/merge", method = RequestMethod.POST) + String relay(@RequestBody Merge mergeMsg, Model model){ + try { + String action = ""; + + if (mergeMsg.getObjectKind().equals("open")) { + action = " created a merge request from "; + } else if (mergeMsg.getObjectKind().equals("merge")) { + action = " merged from branch "; + } else { + return "gitlab"; + } + //DANGER + Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "echo theblueroom " + mergeMsg.getUser().getUserName()+ " created a merge request from " + mergeMsg.getObjectAttributes().getSourceBranch() + " to " + mergeMsg.getObjectAttributes().getTargetBranch() + ": " + mergeMsg.getUrl() + " | bash /home/smsd/msgrelay.bash "}); + model.addAttribute("result", "OK"); + } catch (IOException e) { + System.out.println("Failed to run command"); + e.printStackTrace(); + model.addAttribute("result", "failed"); + } + return "gitlab"; + } + +} diff --git a/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java b/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java new file mode 100644 index 0000000..85592c8 --- /dev/null +++ b/src/main/java/uk/co/blatech/blaears/controllers/GitPushController.java @@ -0,0 +1,41 @@ +package uk.co.blatech.blaears.controllers; + +import org.apache.commons.lang.StringUtils; +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.Push; + +import java.io.IOException; + +/** + * Takes messages sent by GitLab and sends them to IRC + * Currently just testing to see what we get + */ +@Controller +public class GitPushController { + + @RequestMapping(value = "/git/push", method = RequestMethod.POST) + String relay(@RequestBody Push pushMsg, Model model){ + try { + String commitMsg = ""; + for (Commit commit: pushMsg.getCommits()) { + if (StringUtils.isNotEmpty(commitMsg)) { + 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 "}); + model.addAttribute("result", "OK"); + } catch (IOException e) { + System.out.println("Failed to run command"); + e.printStackTrace(); + model.addAttribute("result", "failed"); + } + return "gitlab"; + } + +} diff --git a/src/main/java/uk/co/blatech/blaears/controllers/GitRelayController.java b/src/main/java/uk/co/blatech/blaears/controllers/GitRelayController.java deleted file mode 100644 index 256d5fa..0000000 --- a/src/main/java/uk/co/blatech/blaears/controllers/GitRelayController.java +++ /dev/null @@ -1,41 +0,0 @@ -package uk.co.blatech.blaears.controllers; - -import org.apache.commons.lang.StringUtils; -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.Push; - -import java.io.IOException; - -/** - * Takes messages sent by GitLab and sends them to IRC - * Currently just testing to see what we get - */ -@Controller -public class GitRelayController { - - @RequestMapping(value = "/gitlab", method = RequestMethod.POST) - String relay(@RequestBody Push pushMsg, Model model){ - try { - String commitMsg = ""; - for (Commit commit: pushMsg.getCommits()) { - if (StringUtils.isNotEmpty(commitMsg)) { - 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 "}); - model.addAttribute("result", "OK"); - } catch (IOException e) { - System.out.println("Failed to run command"); - e.printStackTrace(); - model.addAttribute("result", "failed"); - } - return "gitlab"; - } - -} diff --git a/src/main/java/uk/co/blatech/blaears/models/gitlab/Merge.java b/src/main/java/uk/co/blatech/blaears/models/gitlab/Merge.java new file mode 100644 index 0000000..d8eaa99 --- /dev/null +++ b/src/main/java/uk/co/blatech/blaears/models/gitlab/Merge.java @@ -0,0 +1,50 @@ +package uk.co.blatech.blaears.models.gitlab; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Model for a GitLab Push message, to be converted from JSON + * See http://www.blatech.co.uk/help/web_hooks/web_hooks + */ +public class Merge { + + @JsonProperty("object_kind") + String objectKind; + User user; + ObjectAttributes objectAttributes; + String url; + + public String getObjectKind() { + return objectKind; + } + + public void setObjectKind(String objectKind) { + this.objectKind = objectKind; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public ObjectAttributes getObjectAttributes() { + return objectAttributes; + } + + public void setObjectAttributes(ObjectAttributes objectAttributes) { + this.objectAttributes = objectAttributes; + } + + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + +} diff --git a/src/main/java/uk/co/blatech/blaears/models/gitlab/ObjectAttributes.java b/src/main/java/uk/co/blatech/blaears/models/gitlab/ObjectAttributes.java new file mode 100644 index 0000000..8c679d7 --- /dev/null +++ b/src/main/java/uk/co/blatech/blaears/models/gitlab/ObjectAttributes.java @@ -0,0 +1,45 @@ +package uk.co.blatech.blaears.models.gitlab; + +/** + * Model for a GitLab object_attribute which is used in various hook messages, to be converted from JSON + * See http://www.blatech.co.uk/help/web_hooks/web_hooks + */ +public class ObjectAttributes { + + int id; + String targetBranch; + String sourceBranch; + String title; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTargetBranch() { + return targetBranch; + } + + public void setTargetBranch(String targetBranch) { + this.targetBranch = targetBranch; + } + + public String getSourceBranch() { + return sourceBranch; + } + + public void setSourceBranch(String sourceBranch) { + this.sourceBranch = sourceBranch; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/src/main/java/uk/co/blatech/blaears/models/gitlab/User.java b/src/main/java/uk/co/blatech/blaears/models/gitlab/User.java new file mode 100644 index 0000000..8403d83 --- /dev/null +++ b/src/main/java/uk/co/blatech/blaears/models/gitlab/User.java @@ -0,0 +1,29 @@ +package uk.co.blatech.blaears.models.gitlab; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Model for a GitLab User as sent by hooks, to be converted from JSON + * See http://www.blatech.co.uk/help/web_hooks/web_hooks + */ +public class User { + String name; + @JsonProperty("user_name") + String userName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } +} -- cgit v1.2.3