Permalink
Browse files

add files which apparently I should have added several commits ago...

  • Loading branch information...
1 parent a9d84b8 commit a150a3e84d17cfe6f6d336f3244cfb0bf67c57d3 @philandstuff committed Feb 13, 2011
View
60 src/main/com/thoughtworks/go/monitor/BlameDisplay.java
@@ -0,0 +1,60 @@
+package com.thoughtworks.go.monitor;
+
+import javax.swing.*;
+import java.awt.*;
+
+public class BlameDisplay implements BuildMonitorListener {
+ public BlameDisplay(String title) {
+ panel = new JPanel();
+ JPanel titleNamePanel = new JPanel();
+ titleNamePanel.setLayout(new FlowLayout(FlowLayout.LEADING));
+ userLabel = new JLabel("Blame Monitor");
+ infoLabel = new JLabel("Blame Monitor");
+ JLabel titleLabel = new JLabel(title + ": ");
+ titleNamePanel.add(titleLabel);
+ titleNamePanel.add(userLabel);
+ titleNamePanel.setOpaque(false);
+ panel.setLayout(new GridLayout(2,1));
+ panel.add(titleNamePanel);
+ panel.add(infoLabel);
+
+ userLabel.setFont(new Font("Dialog", Font.PLAIN, 72));
+ userLabel.setOpaque(false);
+ titleLabel.setFont(new Font("Dialog", Font.PLAIN, 72));
+ titleLabel.setOpaque(false);
+ infoLabel.setFont(new Font("Dialog", Font.PLAIN, 40));
+ infoLabel.setOpaque(false);
+ infoLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
+ panel.setVisible(true);
+ }
+
+ private JPanel panel;
+ private JLabel userLabel;
+ private JLabel infoLabel;
+
+ public JPanel getPanel() {
+ return panel;
+ }
+
+ private void showWrappedInfo(String info) {
+ infoLabel.setText("<html>"+info+"</html>");
+ }
+
+ public void brokeTheBuild(String user, String info) {
+ panel.setBackground(Color.RED);
+ userLabel.setText(user);
+ showWrappedInfo(info);
+ }
+
+ public void fixedTheBuild(String user, String info) {
+ panel.setBackground(Color.GREEN);
+ userLabel.setText(user);
+ showWrappedInfo(info);
+ }
+
+ public void pushedWorkingBuild(String user, String info) {
+ panel.setBackground(Color.GREEN);
+ userLabel.setText(user);
+ showWrappedInfo(info);
+ }
+}
View
91 src/main/com/thoughtworks/go/monitor/PipelineReader.java
@@ -0,0 +1,91 @@
+package com.thoughtworks.go.monitor;
+
+import com.thoughtworks.go.TalkToGo;
+import com.thoughtworks.go.TalkToGo2Dot1;
+import com.thoughtworks.go.domain.FeedEntry;
+import com.thoughtworks.go.domain.Material;
+import com.thoughtworks.go.domain.Pipeline;
+import com.thoughtworks.go.domain.Stage;
+import com.thoughtworks.go.http.HttpClientWrapper;
+import com.thoughtworks.go.visitor.StageVisitor;
+import com.thoughtworks.go.visitor.criteria.MatchingStageVisitor;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class PipelineReader {
+ final String pipelineName;
+ final String stageName;
+ final String GO_SERVER;
+
+ public PipelineReader(String pipelineName, String stageName, String go_server) {
+ this.pipelineName = pipelineName;
+ this.stageName = stageName;
+ GO_SERVER = go_server;
+ }
+
+ Set<String> checkinUsersForThisStage(Stage stage) {
+ Set<String> users = new HashSet<String>();
+ for (Material material : stage.getPipeline().materials()) {
+ for (Material.Change change : material.getChanges()) {
+ users.add(change.getUser());
+ }
+ }
+ return users;
+ }
+
+ public String commitsForThisStage(Stage stage) {
+ StringBuilder commits = new StringBuilder();
+ for (Material material : stage.getPipeline().materials()) {
+ for (Material.Change change : material.getChanges()) {
+ try {
+ Field commitMsg = Material.Change.class.getDeclaredField("message");
+ commitMsg.setAccessible(true);
+ commits.append(commitMsg.get(change) + "<br>");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ return commits.toString();
+ }
+
+ Stage latestStage() {
+ final List<Stage> stageListHack = new ArrayList<Stage>();
+ TalkToGo talkToGo = new TalkToGo2Dot1(pipelineName, new HttpClientWrapper(GO_SERVER, 8153), false);
+
+ talkToGo.visitStages(new StageVisitor() {
+ public void visitStage(Stage stage) {
+ if (!stage.getResult().equals("Cancelled")) {
+ stageListHack.add(stage);
+ }
+ }
+
+ public void visitPipeline(Pipeline pipeline) {
+ }
+ }, new StopAfterFirstMatchCriteria(stageName));
+ return stageListHack.isEmpty() ? null : stageListHack.get(0);
+ }
+
+ private class StopAfterFirstMatchCriteria extends MatchingStageVisitor {
+
+ private boolean visitedOnce;
+
+ public StopAfterFirstMatchCriteria(String stageName) {
+ super(stageName);
+ }
+
+ @Override
+ public boolean shouldVisit(FeedEntry feedEntry) {
+ return visitedOnce = super.shouldVisit(feedEntry);
+ }
+
+ @Override
+ public boolean shouldContinue() {
+ return !visitedOnce;
+ }
+ }
+}
View
67 src/main/com/thoughtworks/go/monitor/StageMonitor.java
@@ -0,0 +1,67 @@
+package com.thoughtworks.go.monitor;
+
+import com.thoughtworks.go.domain.Stage;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class StageMonitor {
+ Set<String> visitedStages = new HashSet<String>();
+ List<BuildMonitorListener> listeners = new ArrayList<BuildMonitorListener>();
+ boolean borked = false;
+ final PipelineReader pipelineReader;
+
+ public StageMonitor(String pipelineName, String stageName, String goServer, BuildMonitorListener... listeners) {
+ pipelineReader = new PipelineReader(pipelineName, stageName, goServer);
+
+ for (BuildMonitorListener listener : listeners) {
+ this.listeners.add(listener);
+ }
+ }
+
+ public void pollForNewCompletion() {
+ Stage stage = pipelineReader.latestStage();
+ if (stage != null && !alreadySeen(stage)) {
+ System.out.println("Found new stage with result " + stage.getResult());
+ reportNewStage(stage);
+ }
+ }
+
+ void reportNewStage(Stage stage) {
+ visitedStages.add(stage.getStageLocator());
+
+ Set<String> users = pipelineReader.checkinUsersForThisStage(stage);
+ String commit = pipelineReader.commitsForThisStage(stage);
+ String user = trim(users.iterator().next());
+
+ for (BuildMonitorListener listener : listeners) {
+ if (isBroken(stage)) {
+ listener.brokeTheBuild(user, commit);
+ } else if (wasFixed(stage)) {
+ listener.fixedTheBuild(user, commit);
+ } else {
+ listener.pushedWorkingBuild(user, commit);
+ }
+ }
+
+ borked = isBroken(stage);
+ }
+
+ private String trim(String userAndEmail) {
+ return userAndEmail.substring(0,userAndEmail.indexOf("<"));
+ }
+
+ boolean alreadySeen(Stage stage) {
+ return visitedStages.contains(stage.getStageLocator());
+ }
+
+ boolean wasFixed(Stage stage) {
+ return borked && stage.getResult().equals("Passed");
+ }
+
+ boolean isBroken(Stage stage) {
+ return stage.getResult().equals("Failed");
+ }
+}

0 comments on commit a150a3e

Please sign in to comment.