Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from PromoFaux/development
Browse files Browse the repository at this point in the history
1.3.2
  • Loading branch information
PromoFaux committed Dec 23, 2017
2 parents 4a4e545 + da21f4c commit b0a0226
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 299 deletions.
10 changes: 6 additions & 4 deletions Matterhook.NET/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ public class Config
public DiscourseConfig DiscourseConfig { get; set; }
public GithubConfig GithubConfig { get; set; }
public DockerHubConfig DockerHubConfig { get; set; }

}

public class DiscourseConfig
{
public bool LogOnlyErrors { get; set; } = true;
public string Secret { get; set; }
public string[] IgnoredTopicTitles { get; set; }
public bool IgnorePrivateMessages { get; set; }
public MattermostConfig MattermostConfig { get; set; }

}

public class MattermostConfig
Expand All @@ -29,22 +30,23 @@ public class MattermostConfig
//TODO: Look at configuring subscribed events
public class GithubConfig
{
public bool LogOnlyErrors { get; set; } = true;
public string Secret { get; set; }
public MattermostConfig DefaultMattermostConfig { get; set; }
public bool VerboseCommitMessages { get; set; }
public List<RepoConfig> RepoList { get; set; }
public bool DebugSavePayloads { get; set; } = false;
}

public class RepoConfig
{
public string RepoName { get; set; }
public MattermostConfig MattermostConfig { get; set; }

}

public class DockerHubConfig
{
public bool LogOnlyErrors { get; set; } = true;
public MattermostConfig DefaultMattermostConfig { get; set; }
public List<RepoConfig> RepoList { get; set; }
}
}
}
67 changes: 46 additions & 21 deletions Matterhook.NET/Controllers/DiscourseHookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -16,6 +17,7 @@
using Newtonsoft.Json.Linq;
using ReverseMarkdown;
using Matterhook.NET.MatterhookClient;
using Microsoft.IdentityModel.Tokens;


namespace Matterhook.NET.Controllers
Expand Down Expand Up @@ -54,10 +56,21 @@ public async Task<IActionResult> Receive()
Request.Headers.TryGetValue("X-Discourse-Event", out StringValues eventName);
Request.Headers.TryGetValue("X-Discourse-Event-Signature", out StringValues signature);
Request.Headers.TryGetValue("X-Discourse-Instance", out StringValues discourseUrl);
Request.Headers.TryGetValue("Content-type", out var content);

_discourseUrl = discourseUrl;

stuffToLog.Add($"Hook Id: {eventId}");

if (content != "application/json")
{
const string error = "Invalid content type. Expected application/json";
stuffToLog.Add(error);
Util.LogList(stuffToLog);
return StatusCode(400, error);

}

using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
payloadText = await reader.ReadToEndAsync().ConfigureAwait(false);
Expand All @@ -68,13 +81,13 @@ public async Task<IActionResult> Receive()

if (signature == calcSig)
{
var discourseHook = new DiscourseHook(eventId,eventType,eventName,signature,payloadText);
var discourseHook = new DiscourseHook(eventId, eventType, eventName, signature, payloadText);
var matterHook = new MatterhookClient.MatterhookClient(_config.MattermostConfig.WebhookUrl);
HttpResponseMessage response = null;
MattermostMessage message = null;
if (discourseHook.EventName == "post_created")
{
message = PostCreated((PostPayload) discourseHook.Payload);
message = PostCreated((PostPayload)discourseHook.Payload);
response = await matterHook.PostAsync(message);
}

Expand All @@ -84,30 +97,37 @@ public async Task<IActionResult> Receive()
? $"Unable to post to Mattermost {response.StatusCode}"
: "Unable to post to Mattermost");

return Content(response != null ? $"Problem posting to Mattermost: {response.StatusCode}" : "Problem Posting to Mattermost");
return StatusCode(500, response != null
? $"Unable to post to Mattermost: {response.StatusCode}"
: "Unable to post to Mattermost");
}
if (message != null) stuffToLog.Add(message.Text);
stuffToLog.Add("Succesfully posted to Mattermost");
Util.LogList(stuffToLog);
return Ok();
}
else
{
stuffToLog.Add("Invalid Signature!");
stuffToLog.Add($"Expected: {signature}");
stuffToLog.Add($"Calculated: {calcSig}");
Util.LogList(stuffToLog);
return Unauthorized();

if (!_config.LogOnlyErrors)
{
if (message != null) stuffToLog.Add(message.Text);
stuffToLog.Add("Succesfully posted to Mattermost");
Util.LogList(stuffToLog);
}

return StatusCode(200, "Succesfully posted to Mattermost");
}


stuffToLog.Add("Invalid Signature!");
stuffToLog.Add($"Expected: {signature}");
stuffToLog.Add($"Calculated: {calcSig}");
Util.LogList(stuffToLog);
return StatusCode(401,"Invalid signature. Please check your secret values in the config and Discourse");

}
catch (Exception e)
{
stuffToLog.Add(e.Message);
Util.LogList(stuffToLog);
return Content(e.Message);
return StatusCode(e is NotImplementedException ? 501 : e is WarningException? 202: 500, e.Message);
}




}

private static string ExpandDiscourseUrls(string input, string discourseUrl)
Expand All @@ -123,7 +143,8 @@ private MattermostMessage PostCreated(PostPayload payload)

if (_config.IgnoredTopicTitles.Contains(p.topic_title))
{
throw new Exception("Post title matches ignored titles");
throw new WarningException("Post title matches an ignored title");

}

if (_config.IgnorePrivateMessages)
Expand All @@ -137,11 +158,15 @@ private MattermostMessage PostCreated(PostPayload payload)
}
catch
{
throw new Exception("Unable to retrieve topic, possibly a PM so we should ignore this.");
throw new WarningException("Unable to retrieve topic, possibly a PM so we should ignore this.");
}
}


if (p.cooked == "")
{
throw new WarningException("Body empty, nothing to post.");
}


var retVal = new MattermostMessage
{
Expand Down
21 changes: 16 additions & 5 deletions Matterhook.NET/Controllers/DockerHubHookController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -73,19 +72,31 @@ public async Task<IActionResult> Receive()

var response = await matterHook.PostAsync(msg);

if (response.StatusCode == HttpStatusCode.OK)
if (response == null || response.StatusCode != HttpStatusCode.OK)
{
stuffToLog.Add(response != null
? $"Unable to post to Mattermost {response.StatusCode}"
: "Unable to post to Mattermost");

return StatusCode(500, response != null
? $"Unable to post to Mattermost: {response.StatusCode}"
: "Unable to post to Mattermost");
}

if (!_config.LogOnlyErrors)
{
stuffToLog.Add(msg.Text);
stuffToLog.Add("Succesfully posted to Mattermost");
Util.LogList(stuffToLog);
return Ok();
}
return Content("Unable to post to Mattermost");

return StatusCode(200, "Succesfully posted to Mattermost");
}
catch (Exception e)
{
stuffToLog.Add(e.Message);
Util.LogList(stuffToLog);
return Content(e.Message);
return StatusCode(500,e.Message);
}
}
}
Expand Down
Loading

0 comments on commit b0a0226

Please sign in to comment.