-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIWebhookEventProcessor.cs
117 lines (110 loc) · 4.52 KB
/
CIWebhookEventProcessor.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
namespace CI;
using Octokit;
using Octokit.Webhooks;
using Octokit.Webhooks.Events;
using Octokit.Webhooks.Events.PullRequest;
using PullRequest = Octokit.Webhooks.Models.PullRequestEvent.PullRequest;
public class CIWebhookEventProcessor : WebhookEventProcessor
{
private readonly GitHubClient _client;
private readonly ILogger<CIWebhookEventProcessor> _logger;
public CIWebhookEventProcessor(GitHubClient client, ILogger<CIWebhookEventProcessor> logger)
{
this._client = client;
this._logger = logger;
}
protected override async Task ProcessPullRequestWebhookAsync(
WebhookHeaders headers,
PullRequestEvent pullRequestEvent, PullRequestAction action)
{
switch (action)
{
case PullRequestActionValue.Opened:
case PullRequestActionValue.Reopened:
case PullRequestActionValue.Labeled:
case PullRequestActionValue.Unlabeled:
case PullRequestActionValue.Synchronize:
{
await this.Process_pull_requestAsync(pullRequestEvent.PullRequest).ConfigureAwait(false);
break;
}
default:
{
break;
}
}
}
private async Task Process_pull_requestAsync(PullRequest pullRequest)
{
// todo: read comments from repo /org admins though for things like
// "I approve this" and then approve the changes and then wait for
// "merge squashed/rebased/commit" (if enabled on repository).
// seems I cannot create a status yet???
this._logger.LogDebug("Creating a new status check.");
var newCheckRun = new NewCheckRun("Misc/NEWS", pullRequest.Head.Sha)
{
Status = CheckStatus.InProgress
};
var check = await this._client.Check.Run.Create(
pullRequest.Base.Repo.Owner.Name,
pullRequest.Base.Repo.Name,
newCheckRun).ConfigureAwait(false);
var issue = await this._client.Issue.Get(
pullRequest.Base.Repo.Owner.Name,
pullRequest.Base.Repo.Name,
Convert.ToInt32(pullRequest.Number)).ConfigureAwait(false);
NewCheckRunOutput newCheckRunOutput;
CheckRunUpdate checkRunUpdate;
if (issue.Labels.Any(label => label.Name.Equals("skip news")))
{
newCheckRunOutput = new NewCheckRunOutput("Misc/NEWS", "'skip news' label found!");
checkRunUpdate = new CheckRunUpdate
{
Status = CheckStatus.Completed,
Output = newCheckRunOutput,
Conclusion = CheckConclusion.Success
};
this._logger.LogDebug("Check success.");
_ = await this._client.Check.Run.Update(
pullRequest.Base.Repo.Owner.Name,
pullRequest.Base.Repo.Name,
check.Id,
checkRunUpdate).ConfigureAwait(false);
return;
}
var files = await this._client.PullRequest.Files(
pullRequest.Base.Repo.Owner.Name,
pullRequest.Base.Repo.Name,
Convert.ToInt32(pullRequest.Number)).ConfigureAwait(false);
if (files.Any(file => file.FileName.StartsWith("Misc/NEWS")))
{
newCheckRunOutput = new NewCheckRunOutput("Misc/NEWS", "Misc/NEWS entry found!");
checkRunUpdate = new CheckRunUpdate
{
Status = CheckStatus.Completed,
Output = newCheckRunOutput,
Conclusion = CheckConclusion.Success
};
this._logger.LogDebug("Check success.");
_ = await this._client.Check.Run.Update(
pullRequest.Base.Repo.Owner.Name,
pullRequest.Base.Repo.Name,
check.Id,
checkRunUpdate).ConfigureAwait(false);
return;
}
newCheckRunOutput = new NewCheckRunOutput("Misc/NEWS", "Misc/NEWS entry not found and 'skip news' is not added!");
checkRunUpdate = new CheckRunUpdate
{
Status = CheckStatus.Completed,
Output = newCheckRunOutput,
Conclusion = CheckConclusion.Failure
};
this._logger.LogDebug("Check Failure.");
_ = await this._client.Check.Run.Update(
pullRequest.Base.Repo.Owner.Name,
pullRequest.Base.Repo.Name,
check.Id,
checkRunUpdate).ConfigureAwait(false);
}
}