-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
765 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
|
||
using MessageCardModel; | ||
using MessageCardModel.Actions; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Aliencube.GitHubActions.Teams.ConsoleApp | ||
{ | ||
public class ActionConverter : JsonConverter | ||
{ | ||
/// <inheritdoc /> | ||
public override bool CanWrite => false; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanRead => true; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanConvert(Type objectType) => objectType == typeof(BaseAction); | ||
|
||
/// <inheritdoc /> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException(); | ||
|
||
/// <inheritdoc /> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var jsonObject = JObject.Load(reader); | ||
|
||
var actionType = jsonObject.GetValue("@type").Value<string>(); | ||
|
||
BaseAction target = actionType switch | ||
{ | ||
"ActionCard" => new ActionCardAction(), | ||
"HttpPOST" => new HttpPostAction(), | ||
"OpenUri" => new OpenUriAction(), | ||
|
||
string type => throw new NotSupportedException($"Cannot deserialize action type: {type}") | ||
}; | ||
|
||
serializer.Populate(jsonObject.CreateReader(), target); | ||
|
||
return target; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace Aliencube.GitHubActions.Teams.ConsoleApp | ||
{ | ||
/// <summary> | ||
/// This provides interfaces to the <see cref="MessageHandler" /> class. | ||
/// </summary> | ||
public interface IMessageHandler | ||
{ | ||
/// <summary> | ||
/// Gets the JSON serialised message. | ||
/// </summary> | ||
string Converted { get; } | ||
|
||
/// <summary> | ||
/// Gets the incoming webhook URI to Microsoft Teams. | ||
/// </summary> | ||
string RequestUri { get; } | ||
|
||
/// <summary> | ||
/// Builds a message in Actionable Message Format. | ||
/// </summary> | ||
/// <param name="options"><see cref="Options" /> instance.</param> | ||
/// <param name="settings"><see cref="JsonSerializerSettings" /> instance.</param> | ||
IMessageHandler BuildMessage(Options options, JsonSerializerSettings settings); | ||
|
||
/// <summary> | ||
/// Sends a message to a Microsoft Teams channel. | ||
/// </summary> | ||
/// <param name="client"><see cref="HttpClient" /> instance.</param> | ||
Task<int> SendMessageAsync(HttpClient client); | ||
} | ||
} |
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,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using MessageCardModel; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace Aliencube.GitHubActions.Teams.ConsoleApp | ||
{ | ||
/// <summary> | ||
/// This represents the console app entity. | ||
/// </summary> | ||
public class MessageHandler : IMessageHandler | ||
{ | ||
/// <inheritdoc /> | ||
public virtual string Converted { get; private set; } | ||
|
||
/// <inheritdoc /> | ||
public virtual string RequestUri { get; private set; } | ||
|
||
/// <inheritdoc /> | ||
public IMessageHandler BuildMessage(Options options, JsonSerializerSettings settings) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
if (settings == null) | ||
{ | ||
throw new ArgumentNullException(nameof(settings)); | ||
} | ||
|
||
var card = new MessageCard() | ||
{ | ||
Title = ParseString(options.Title), | ||
Summary = ParseString(options.Summary), | ||
Text = ParseString(options.Text), | ||
ThemeColor = ParseString(options.ThemeColor), | ||
Sections = ParseCollection<Section>(options.Sections, settings), | ||
Actions = ParseCollection<BaseAction>(options.Actions, settings) | ||
}; | ||
|
||
this.Converted = JsonConvert.SerializeObject(card, settings); | ||
this.RequestUri = options.WebhookUri; | ||
|
||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<int> SendMessageAsync(HttpClient client) | ||
{ | ||
if (client == null) | ||
{ | ||
throw new ArgumentNullException(nameof(client)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(this.RequestUri)) | ||
{ | ||
throw new InvalidOperationException("Webhook URI not ready"); | ||
} | ||
|
||
var message = default(string); | ||
var exitCode = default(int); | ||
|
||
using (var content = new StringContent(this.Converted, Encoding.UTF8, "application/json")) | ||
using (var response = await client.PostAsync(this.RequestUri, content).ConfigureAwait(false)) | ||
{ | ||
try | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
|
||
message = $"Message sent: {this.Converted}"; | ||
exitCode = 0; | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
message = $"Error: {ex.Message}"; | ||
exitCode = (int) response.StatusCode; | ||
} | ||
} | ||
|
||
Console.WriteLine(message); | ||
|
||
return exitCode; | ||
} | ||
|
||
private static string ParseString(string value) | ||
{ | ||
var parsed = string.IsNullOrWhiteSpace(value) | ||
? null | ||
: value; | ||
|
||
return parsed; | ||
} | ||
|
||
private static List<T> ParseCollection<T>(string value, JsonSerializerSettings settings) | ||
{ | ||
var parsed = string.IsNullOrWhiteSpace(value) | ||
? null | ||
: JsonConvert.DeserializeObject<List<T>>(value, settings); | ||
|
||
return parsed; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.