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

Commit b0a0226

Browse files
authored
Merge pull request #12 from PromoFaux/development
1.3.2
2 parents 4a4e545 + da21f4c commit b0a0226

18 files changed

+311
-299
lines changed

Matterhook.NET/Config.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ public class Config
77
public DiscourseConfig DiscourseConfig { get; set; }
88
public GithubConfig GithubConfig { get; set; }
99
public DockerHubConfig DockerHubConfig { get; set; }
10-
1110
}
1211

1312
public class DiscourseConfig
1413
{
14+
public bool LogOnlyErrors { get; set; } = true;
1515
public string Secret { get; set; }
1616
public string[] IgnoredTopicTitles { get; set; }
1717
public bool IgnorePrivateMessages { get; set; }
1818
public MattermostConfig MattermostConfig { get; set; }
19+
1920
}
2021

2122
public class MattermostConfig
@@ -29,22 +30,23 @@ public class MattermostConfig
2930
//TODO: Look at configuring subscribed events
3031
public class GithubConfig
3132
{
33+
public bool LogOnlyErrors { get; set; } = true;
3234
public string Secret { get; set; }
3335
public MattermostConfig DefaultMattermostConfig { get; set; }
34-
public bool VerboseCommitMessages { get; set; }
3536
public List<RepoConfig> RepoList { get; set; }
37+
public bool DebugSavePayloads { get; set; } = false;
3638
}
3739

3840
public class RepoConfig
3941
{
4042
public string RepoName { get; set; }
4143
public MattermostConfig MattermostConfig { get; set; }
42-
4344
}
4445

4546
public class DockerHubConfig
4647
{
48+
public bool LogOnlyErrors { get; set; } = true;
4749
public MattermostConfig DefaultMattermostConfig { get; set; }
4850
public List<RepoConfig> RepoList { get; set; }
4951
}
50-
}
52+
}

Matterhook.NET/Controllers/DiscourseHookController.cs

+46-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.ComponentModel;
56
using System.IO;
67
using System.Linq;
78
using System.Net;
@@ -16,6 +17,7 @@
1617
using Newtonsoft.Json.Linq;
1718
using ReverseMarkdown;
1819
using Matterhook.NET.MatterhookClient;
20+
using Microsoft.IdentityModel.Tokens;
1921

2022

2123
namespace Matterhook.NET.Controllers
@@ -54,10 +56,21 @@ public async Task<IActionResult> Receive()
5456
Request.Headers.TryGetValue("X-Discourse-Event", out StringValues eventName);
5557
Request.Headers.TryGetValue("X-Discourse-Event-Signature", out StringValues signature);
5658
Request.Headers.TryGetValue("X-Discourse-Instance", out StringValues discourseUrl);
59+
Request.Headers.TryGetValue("Content-type", out var content);
60+
5761
_discourseUrl = discourseUrl;
5862

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

65+
if (content != "application/json")
66+
{
67+
const string error = "Invalid content type. Expected application/json";
68+
stuffToLog.Add(error);
69+
Util.LogList(stuffToLog);
70+
return StatusCode(400, error);
71+
72+
}
73+
6174
using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
6275
{
6376
payloadText = await reader.ReadToEndAsync().ConfigureAwait(false);
@@ -68,13 +81,13 @@ public async Task<IActionResult> Receive()
6881

6982
if (signature == calcSig)
7083
{
71-
var discourseHook = new DiscourseHook(eventId,eventType,eventName,signature,payloadText);
84+
var discourseHook = new DiscourseHook(eventId, eventType, eventName, signature, payloadText);
7285
var matterHook = new MatterhookClient.MatterhookClient(_config.MattermostConfig.WebhookUrl);
7386
HttpResponseMessage response = null;
7487
MattermostMessage message = null;
7588
if (discourseHook.EventName == "post_created")
7689
{
77-
message = PostCreated((PostPayload) discourseHook.Payload);
90+
message = PostCreated((PostPayload)discourseHook.Payload);
7891
response = await matterHook.PostAsync(message);
7992
}
8093

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

87-
return Content(response != null ? $"Problem posting to Mattermost: {response.StatusCode}" : "Problem Posting to Mattermost");
100+
return StatusCode(500, response != null
101+
? $"Unable to post to Mattermost: {response.StatusCode}"
102+
: "Unable to post to Mattermost");
88103
}
89-
if (message != null) stuffToLog.Add(message.Text);
90-
stuffToLog.Add("Succesfully posted to Mattermost");
91-
Util.LogList(stuffToLog);
92-
return Ok();
93-
}
94-
else
95-
{
96-
stuffToLog.Add("Invalid Signature!");
97-
stuffToLog.Add($"Expected: {signature}");
98-
stuffToLog.Add($"Calculated: {calcSig}");
99-
Util.LogList(stuffToLog);
100-
return Unauthorized();
104+
105+
if (!_config.LogOnlyErrors)
106+
{
107+
if (message != null) stuffToLog.Add(message.Text);
108+
stuffToLog.Add("Succesfully posted to Mattermost");
109+
Util.LogList(stuffToLog);
110+
}
111+
112+
return StatusCode(200, "Succesfully posted to Mattermost");
101113
}
102-
114+
115+
stuffToLog.Add("Invalid Signature!");
116+
stuffToLog.Add($"Expected: {signature}");
117+
stuffToLog.Add($"Calculated: {calcSig}");
118+
Util.LogList(stuffToLog);
119+
return StatusCode(401,"Invalid signature. Please check your secret values in the config and Discourse");
120+
103121
}
104122
catch (Exception e)
105123
{
106124
stuffToLog.Add(e.Message);
107125
Util.LogList(stuffToLog);
108-
return Content(e.Message);
126+
return StatusCode(e is NotImplementedException ? 501 : e is WarningException? 202: 500, e.Message);
109127
}
110-
128+
129+
130+
111131
}
112132

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

124144
if (_config.IgnoredTopicTitles.Contains(p.topic_title))
125145
{
126-
throw new Exception("Post title matches ignored titles");
146+
throw new WarningException("Post title matches an ignored title");
147+
127148
}
128149

129150
if (_config.IgnorePrivateMessages)
@@ -137,11 +158,15 @@ private MattermostMessage PostCreated(PostPayload payload)
137158
}
138159
catch
139160
{
140-
throw new Exception("Unable to retrieve topic, possibly a PM so we should ignore this.");
161+
throw new WarningException("Unable to retrieve topic, possibly a PM so we should ignore this.");
141162
}
142163
}
143164

144-
165+
if (p.cooked == "")
166+
{
167+
throw new WarningException("Body empty, nothing to post.");
168+
}
169+
145170

146171
var retVal = new MattermostMessage
147172
{

Matterhook.NET/Controllers/DockerHubHookController.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Net;
65
using System.Text;
76
using System.Threading.Tasks;
@@ -73,19 +72,31 @@ public async Task<IActionResult> Receive()
7372

7473
var response = await matterHook.PostAsync(msg);
7574

76-
if (response.StatusCode == HttpStatusCode.OK)
75+
if (response == null || response.StatusCode != HttpStatusCode.OK)
7776
{
77+
stuffToLog.Add(response != null
78+
? $"Unable to post to Mattermost {response.StatusCode}"
79+
: "Unable to post to Mattermost");
80+
81+
return StatusCode(500, response != null
82+
? $"Unable to post to Mattermost: {response.StatusCode}"
83+
: "Unable to post to Mattermost");
84+
}
85+
86+
if (!_config.LogOnlyErrors)
87+
{
88+
stuffToLog.Add(msg.Text);
7889
stuffToLog.Add("Succesfully posted to Mattermost");
7990
Util.LogList(stuffToLog);
80-
return Ok();
8191
}
82-
return Content("Unable to post to Mattermost");
92+
93+
return StatusCode(200, "Succesfully posted to Mattermost");
8394
}
8495
catch (Exception e)
8596
{
8697
stuffToLog.Add(e.Message);
8798
Util.LogList(stuffToLog);
88-
return Content(e.Message);
99+
return StatusCode(500,e.Message);
89100
}
90101
}
91102
}

0 commit comments

Comments
 (0)