Skip to content

Commit f5b18ec

Browse files
author
Calin Lupas
authored
Merge pull request #31 from DevExcelerate/feature/new-team-request
Feature/new team request
2 parents cb79f94 + 615d711 commit f5b18ec

8 files changed

Lines changed: 50 additions & 43 deletions

src/DevExcelerateApi/DevExcelerateApi.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.4.0" />
1111
<PackageReference Include="Azure.Identity" Version="1.13.2" />
1212
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.7.0" />
13-
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.48.0" />
14-
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.8.0" />
13+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.49.0" />
14+
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.9.0" />
1515
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1616
<PackageReference Include="Octokit" Version="14.0.0" />
1717
<PackageReference Include="Octokit.Webhooks.AspNetCore" Version="2.4.1" />
18-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.8.0" />
18+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.9.0" />
1919
<PackageReference Include="YamlDotNet" Version="16.3.0" />
20-
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
20+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" />
2121
<PackageReference Include="Dapper" Version="2.1.66" />
2222
</ItemGroup>
2323

src/DevExcelerateApi/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public static async Task Main(string[] args)
7878
.RequireAuthorization();
7979

8080
// Start the service
81-
Task runTask = app.RunAsync();
82-
83-
// Wait for the service to complete.
84-
await runTask;
81+
await app.RunAsync();
8582
}
8683
}
8784
}

src/DevExcelerateApi/Services/DevExIssuesEventProcessorService.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Octokit.Webhooks.Events;
99
using Octokit.Webhooks.Events.IssueComment;
1010
using Octokit.Webhooks.Events.Issues;
11+
using Octokit.Webhooks.Models;
1112
using static DevExcelerateApi.Parsers.GitHubIssueFormParser;
1213

1314
namespace DevExcelerateApi.Services
@@ -53,6 +54,13 @@ public async Task SaveAndProcessIssueEventAsync(WebhookHeaders headers, IssuesEv
5354

5455
public async Task SaveAndProcessIssueCommentEventAsync(WebhookHeaders headers, IssueCommentEvent issueCommentEvent, IssueCommentAction action)
5556
{
57+
// Validate if the issue comment is from a user vs bot
58+
if (issueCommentEvent?.Sender?.Type == UserType.Bot)
59+
{
60+
_logger.LogInformation("Bypassing GitHub Webhook event for Issue# {number} with action - {action}...", issueCommentEvent?.Issue.Number, issueCommentEvent?.Action);
61+
return;
62+
}
63+
5664
_logger.LogInformation("Starting the process of the Issue Comment event for Issue# {number} with action - {action}...", (issueCommentEvent?.Issue.Number), issueCommentEvent?.Action);
5765

5866
await _gitHubWebhookRepository.SaveWebhookAsync(headers, issueCommentEvent!).ConfigureAwait(false);
@@ -114,13 +122,15 @@ private async Task ValidateIssue(IssuesEvent issuesEvent)
114122
// Check ticket status => From Approved to Validated or Failed
115123
if ((issuesEvent?.Issue?.Labels?.Any(i => i.Name.IsIssueApproved())).GetValueOrDefault())
116124
{
117-
await _repositoryService.ValidateCreateRepository(issuesEvent!, _gitHubOptions.UseInventoryWithPullRequest);
125+
// Check if the issue form is valid
126+
await _repositoryService.ValidateCreateRepository(issuesEvent!, createPullRequest: false);
118127
}
119128
else if ((issuesEvent?.Issue?.Labels?.Any(i => i.Name.IsIssueValidated())).GetValueOrDefault())
120129
{
121130
if (_gitHubOptions.UseInventoryWithPullRequest)
122131
{
123-
await _repositoryService.ValidateCreateRepository(issuesEvent!);
132+
// IF validated, create a pull request
133+
await _repositoryService.ValidateCreateRepository(issuesEvent!, createPullRequest: true);
124134
}
125135
else
126136
{

src/DevExcelerateApi/Services/DevExPullRequestEventProcessorService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private async Task ValidatePullRequestApproval(PullRequestEvent pullRequestEvent
4444
{
4545
var prNumber = (pullRequestEvent?.PullRequest.Number).GetValueOrDefault();
4646

47-
_logger.LogInformation("Pull request #{prNumber} is {State} with merge commit SHA {MergeCommitSha}.", prNumber, pullRequestEvent?.PullRequest.State, pullRequestEvent?.PullRequest.MergeCommitSha);
47+
_logger.LogInformation("Pull request #{prNumber} is {State} with merge commit SHA {MergeCommitSha}.", prNumber, pullRequestEvent?.PullRequest.State.Value, pullRequestEvent?.PullRequest.MergeCommitSha);
4848

4949
// Closed & Merged
5050
if (pullRequestEvent?.PullRequest.State == PullRequestState.Closed

src/DevExcelerateApi/Services/DevExWebhookEventProcessorService.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Octokit.Webhooks.Events.IssueComment;
77
using Octokit.Webhooks.Events.Issues;
88
using Octokit.Webhooks.Events.PullRequest;
9-
using Octokit.Webhooks.Models;
109

1110
namespace DevExcelerateApi.Services
1211
{
@@ -36,20 +35,26 @@ public class DevExWebhookEventProcessorService(
3635

3736
private readonly IList<IssuesAction> _allowedIssuesActions =
3837
[
39-
IssuesAction.Opened,
38+
//IssuesAction.Opened, // When we open an issue, it will always be labeled with the OPENED label
4039
IssuesAction.Edited,
41-
IssuesAction.Closed,
40+
//IssuesAction.Closed,
4241
IssuesAction.Reopened,
4342
IssuesAction.Labeled
4443
];
4544

45+
private readonly IList<IssueCommentAction> _allowedIssueCommentActions =
46+
[
47+
IssueCommentAction.Created,
48+
IssueCommentAction.Edited
49+
];
50+
4651
private readonly IList<PullRequestAction> _allowedPullRequestActions =
4752
[
48-
PullRequestAction.Opened,
49-
PullRequestAction.Edited,
50-
PullRequestAction.Labeled,
53+
//PullRequestAction.Opened,
54+
//PullRequestAction.Edited,
55+
//PullRequestAction.Labeled,
5156
PullRequestAction.Closed,
52-
PullRequestAction.Reopened
57+
//PullRequestAction.Reopened
5358
];
5459

5560
public override Task ProcessWebhookAsync(IDictionary<string, StringValues> headers, string body)
@@ -58,18 +63,21 @@ public override Task ProcessWebhookAsync(IDictionary<string, StringValues> heade
5863
{
5964
ArgumentNullException.ThrowIfNull(headers);
6065
ArgumentNullException.ThrowIfNull(body);
61-
66+
6267
var webhookHeaders = WebhookHeaders.Parse(headers);
6368

6469
// Bypass if not an implemented event
6570
if (_allowedWebhookEvents.Contains(webhookHeaders?.Event!))
6671
{
67-
return base.ProcessWebhookAsync(headers, body);
72+
// Not supported by Octokit.Webhooks
73+
if (!body.Contains("\"action\":\"typed\""))
74+
{
75+
return base.ProcessWebhookAsync(headers, body);
76+
}
6877
}
6978

7079
_logger.LogDebug("Bypassing GitHub Webhook event {Event}", webhookHeaders?.Event);
7180
return Task.CompletedTask;
72-
7381
}
7482
catch (Exception ex)
7583
{
@@ -80,48 +88,40 @@ public override Task ProcessWebhookAsync(IDictionary<string, StringValues> heade
8088

8189
protected override Task ProcessIssuesWebhookAsync(WebhookHeaders headers, IssuesEvent issuesEvent, IssuesAction action)
8290
{
83-
// Only process issues events in the onboarding repository
84-
// Skip if the Issues action is not supported
85-
if (_gitHubOptions?.RepoOnboarding != issuesEvent?.Repository?.Name || !_allowedIssuesActions.Contains(action))
86-
{
87-
_logger.LogDebug("Bypassing GitHub Webhook event for Issue# {Number} with action: {Action} in repository {RepoName}", issuesEvent?.Issue?.Number, issuesEvent?.Action, issuesEvent?.Repository?.Name);
88-
return Task.CompletedTask;
89-
}
90-
91-
if (issuesEvent?.Issue?.State?.Value == IssueState.Closed)
91+
// Only process issues events in the onboarding repository & for the allowed actions
92+
if (_gitHubOptions?.RepoOnboarding == issuesEvent?.Repository?.Name && _allowedIssuesActions.Contains(action))
9293
{
93-
_logger.LogDebug("Bypassing closed Issue# {Number} with action: {Action}.", issuesEvent?.Issue?.Number, issuesEvent?.Action);
94-
return Task.CompletedTask;
94+
return _devExIssuesEventProcessorService.SaveAndProcessIssueEventAsync(headers, issuesEvent!, action);
9595
}
9696

97-
return _devExIssuesEventProcessorService.SaveAndProcessIssueEventAsync(headers, issuesEvent!, action);
97+
// Skip if the Issues action is not supported
98+
_logger.LogDebug("Bypassing GitHub Webhook event for Issue# {Number} with action: {Action} in repository {RepoName}", issuesEvent?.Issue?.Number, issuesEvent?.Action, issuesEvent?.Repository?.Name);
99+
return Task.CompletedTask;
98100
}
99101

100102
protected override Task ProcessIssueCommentWebhookAsync(WebhookHeaders headers, IssueCommentEvent issueCommentEvent, IssueCommentAction action)
101103
{
102-
// Only process issue comment events in the onboarding repository
103-
if (_gitHubOptions?.RepoOnboarding == issueCommentEvent?.Repository?.Name)
104+
// Only process issue comment events in the onboarding repository & for the allowed actions
105+
if (_gitHubOptions?.RepoOnboarding == issueCommentEvent?.Repository?.Name && _allowedIssueCommentActions.Contains(action))
104106
{
105107
return _devExIssuesEventProcessorService.SaveAndProcessIssueCommentEventAsync(headers, issueCommentEvent!, action);
106108
}
107109

108110
_logger.LogDebug("Bypassing GitHub Webhook event for Issue# {Number} with action: {Action} in repository {RepoName}", issueCommentEvent?.Issue?.Number, issueCommentEvent?.Action, issueCommentEvent?.Repository?.Name);
109111
return Task.CompletedTask;
110-
111112
}
112113

113114
protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action)
114115
{
115-
// Only process issues events in the onboarding repository
116-
// Skip if the PR action is not supported
116+
// Only process issues events in the onboarding repository & for the allowed actions
117117
if (_gitHubOptions?.RepoInventory == pullRequestEvent?.Repository?.Name && _allowedPullRequestActions.Contains(action))
118118
{
119119
return _devExPullRequestEventProcessorService.SaveAndProcessPullRequestEventAsync(headers, pullRequestEvent!, action);
120120
}
121121

122+
// Skip if the PR action is not supported
122123
_logger.LogDebug("Bypassing GitHub Webhook event for PR# {Number} with action: {Action} in repository {RepoName}", pullRequestEvent?.PullRequest?.Number, pullRequestEvent?.Action, pullRequestEvent?.Repository?.Name);
123124
return Task.CompletedTask;
124-
125125
}
126126
}
127127
}

src/DevExcelerateApi/Services/IRepositoryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace DevExcelerateApi.Services
44
{
55
public interface IRepositoryService
66
{
7-
Task ValidateCreateRepository(IssuesEvent issuesEvent, bool createPullRequest = true);
7+
Task ValidateCreateRepository(IssuesEvent issuesEvent, bool createPullRequest);
88

99
Task SaveRepository(IssuesEvent issuesEvent);
1010

src/DevExcelerateApi/Services/RepositoryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class RepositoryService(
2424
private readonly GitHubOptions _gitHubOptions = gitHubOptions.Value;
2525
private readonly IStringLocalizer<RepositoryService> _stringLocalizer = stringLocalizer;
2626

27-
public async Task ValidateCreateRepository(IssuesEvent issuesEvent, bool createPullRequest = true)
27+
public async Task ValidateCreateRepository(IssuesEvent issuesEvent, bool createPullRequest)
2828
{
2929
var gitHubClient = await _gitHubClientFactory.GetInstallationAsync(issuesEvent?.Installation?.Id);
3030
var issueNumber = (issuesEvent?.Issue.Number).GetValueOrDefault();

src/DevExcelerateApp/DevExcelerateApp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.4.0" />
1111
<PackageReference Include="Azure.Identity" Version="1.13.2" />
1212
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.7.0" />
13-
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.8.0" />
13+
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.9.0" />
1414
<PackageReference Include="Octokit" Version="14.0.0" />
1515
<PackageReference Include="Octokit.Webhooks.AspNetCore" Version="2.4.1" />
16-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.8.0" />
16+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.9.0" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

0 commit comments

Comments
 (0)