-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from lukelast/teams-incoming-webhook
Add support for MS Teams
- Loading branch information
Showing
15 changed files
with
423 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/in/ashwanthkumar/gocd/teams/CardHttpContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package in.ashwanthkumar.gocd.teams; | ||
|
||
import com.google.api.client.http.AbstractHttpContent; | ||
import com.google.api.client.json.Json; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
|
||
/** | ||
* Serialize a {@link TeamsCard} for the Google HTTP Client. | ||
*/ | ||
public class CardHttpContent extends AbstractHttpContent { | ||
private final TeamsCard card; | ||
|
||
protected CardHttpContent(TeamsCard card) { | ||
super(Json.MEDIA_TYPE); | ||
this.card = card; | ||
} | ||
|
||
@Override | ||
public void writeTo(OutputStream out) throws IOException { | ||
try (var osw = new OutputStreamWriter(out)) { | ||
osw.write(card.toString()); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/in/ashwanthkumar/gocd/teams/MessageCardSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package in.ashwanthkumar.gocd.teams; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import in.ashwanthkumar.gocd.slack.ruleset.PipelineStatus; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* These objects create the MessageCard JSON sent to Teams using {@link com.google.gson.Gson}. | ||
* More details: | ||
* https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference | ||
*/ | ||
public class MessageCardSchema { | ||
@SerializedName("@type") | ||
String type = "MessageCard"; | ||
String themeColor = Color.NONE.getHexCode(); | ||
String title = ""; | ||
/** | ||
* Not sure what this does, but a summary or text field is required. | ||
*/ | ||
String summary = "GoCD build update"; | ||
List<FactSection> sections = new ArrayList<>(); | ||
List<Object> potentialAction = new ArrayList<>(); | ||
|
||
public enum Color { | ||
NONE(""), | ||
RED("990000"), | ||
GREEN("009900"); | ||
|
||
private final String hexCode; | ||
|
||
Color(String hexCode) { | ||
this.hexCode = hexCode; | ||
} | ||
|
||
public static Color findColor(PipelineStatus status) { | ||
switch (status) { | ||
case PASSED: | ||
case FIXED: | ||
return Color.GREEN; | ||
case FAILED: | ||
case BROKEN: | ||
return Color.RED; | ||
default: | ||
return Color.NONE; | ||
} | ||
} | ||
|
||
public String getHexCode() { | ||
return this.hexCode; | ||
} | ||
} | ||
|
||
public static class Fact { | ||
String name = ""; | ||
String value = ""; | ||
|
||
public Fact(String name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static class FactSection { | ||
List<Fact> facts = new ArrayList<>(); | ||
} | ||
|
||
public static class OpenUriAction { | ||
@SerializedName("@type") | ||
String type = "OpenUri"; | ||
String name = ""; | ||
List<Target> targets = new ArrayList<>(); | ||
|
||
public OpenUriAction(String name, String uri) { | ||
this.name = name; | ||
this.targets.add(new MessageCardSchema.Target(uri)); | ||
} | ||
} | ||
|
||
public static class Target { | ||
String os = "default"; | ||
String uri = ""; | ||
|
||
public Target(String uri) { | ||
this.uri = uri; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package in.ashwanthkumar.gocd.teams; | ||
|
||
import com.google.gson.Gson; | ||
|
||
/** | ||
* Populate the values of a Message Card for Teams. | ||
*/ | ||
public class TeamsCard { | ||
private final MessageCardSchema.FactSection factSection = new MessageCardSchema.FactSection(); | ||
private final MessageCardSchema schema = new MessageCardSchema(); | ||
|
||
public TeamsCard() { | ||
this.schema.sections.add(this.factSection); | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.schema.title = title; | ||
} | ||
|
||
public void addFact(String name, String value) { | ||
this.factSection.facts.add(new MessageCardSchema.Fact(name, value)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new Gson().toJson(schema); | ||
} | ||
|
||
public void setColor(MessageCardSchema.Color color) { | ||
this.schema.themeColor = color.getHexCode(); | ||
} | ||
|
||
public void addLinkAction(String name, String uri) { | ||
this.schema.potentialAction.add(new MessageCardSchema.OpenUriAction(name, uri)); | ||
} | ||
} |
Oops, something went wrong.