-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueEventListener.cs
More file actions
100 lines (90 loc) · 3.55 KB
/
IssueEventListener.cs
File metadata and controls
100 lines (90 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Countersoft.Gemini.Extensibility.Events;
using Countersoft.Foundation.Commons.Extensions;
using Countersoft.Gemini.Extensibility.Apps;
using Countersoft.Gemini.Commons.Dto;
using Countersoft.Gemini.Contracts;
using Countersoft.Gemini.Extensibility;
namespace Webhooks
{
[AppGuid(AppConstants.AppId)]
[AppType(AppTypeEnum.Event)]
[AppName("Webhooks")]
[AppKey("Webhooks")]
[AppDescription("Post data to a webhook")]
public class IssueEventListener : AbstractIssueListener
{
private List<WebhooksData> GetData(GeminiContext context, bool create, bool update)
{
var data = context.GlobalConfigurationWidgetStore.Get<List<WebhooksData>>(AppConstants.AppId);
if (data == null || data.Value == null)
{
data = new Countersoft.Gemini.Commons.Entity.GlobalConfigurationWidgetData<List<WebhooksData>>();
data.Value = new List<WebhooksData>();
}
if (create && update)
{
data.Value.RemoveAll(a => !a.Create && !a.Update);
}
else if (create)
{
data.Value.RemoveAll(a => !a.Create);
}
else if (update)
{
data.Value.RemoveAll(a => !a.Update);
}
else
{
data.Value.RemoveAll(a => a.Create || a.Update);
}
return data.Value;
}
public override void AfterCreateFull(IssueDtoEventArgs args)
{
CallWebhook(GetData(args.Context, true, false), args.Issue, null);
}
public override void AfterUpdateFull(IssueDtoEventArgs args)
{
CallWebhook(GetData(args.Context, false, true), args.Issue, args.Previous);
}
private string GetIssueJson(object data)
{
Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter());
serializer.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
serializer.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
using (System.IO.TextWriter tw = new System.IO.StringWriter())
{
serializer.Serialize(tw, data);
return tw.ToString();
}
}
private void CallWebhook(List<WebhooksData> urls, IssueDto data, IssueDto previous)
{
WebClient client = new WebClient();
var payload = new { Issue = data, Previous = previous };
payload.Issue.Entity = new Countersoft.Gemini.Commons.Entity.Issue(payload.Issue.Entity); // Make sure the json doesn't do a REF to the base entity.
if (previous != null)
{
payload.Previous.Entity = new Countersoft.Gemini.Commons.Entity.Issue(payload.Previous.Entity);
}
var json = GetIssueJson(payload);
foreach (var url in urls)
{
try
{
client.UploadString(url.Url, "POST", json);
}
catch (Exception ex)
{
LogService.Logger.LogError(ex, string.Concat("Webhooks app - ", url.Url));
}
}
}
}
}