Skip to content

Commit 8cdd92b

Browse files
calin-lupas_dsamcapscalin-lupas_dsamcaps
authored andcommitted
#25 #39 Refactor pull request handling in services
Updated `RepositoryService.cs` and `TeamService.cs` to streamline the logic for processing pull requests. The `completeMessage` is now initialized with a localized error message when no changes are detected. The code directly processes files, and the completion message is constructed based on the success of saving a repository or team. Final steps for adding comments and closing issues have been simplified by removing unnecessary completeness checks.
1 parent 04671b3 commit 8cdd92b

2 files changed

Lines changed: 26 additions & 50 deletions

File tree

src/DevExcelerateApi/Services/RepositoryService.cs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -218,42 +218,30 @@ public async Task SaveRepository(PullRequestEvent pullRequestEvent)
218218
try
219219
{
220220
var fileContents = await gitHubClient.GetCommitFilesWithContentAsync(owner!, repo!, mergeCommitSha!);
221-
var isComplete = true;
222-
var completeMessage = string.Empty;
221+
var completeMessage = $"{_localizationService.GetLocalizedString(Constants.ERROR_PR_NO_CHANGES)}";
223222

224-
if (fileContents.Count == 0)
223+
foreach (var file in fileContents)
225224
{
226-
isComplete = false;
227-
completeMessage = $"{_localizationService.GetLocalizedString(Constants.ERROR_PR_NO_CHANGES)}";
228-
}
229-
else
230-
{
231-
foreach (var file in fileContents)
225+
if (file.Key.EndsWith(DevExPolicies.Repository_Metadata.GetPolicyYamlFileName(), StringComparison.OrdinalIgnoreCase))
232226
{
233-
if (file.Key.EndsWith(DevExPolicies.Repository_Metadata.GetPolicyYamlFileName(), StringComparison.OrdinalIgnoreCase))
234-
{
235-
var repoCreationMetadata = RepositoryRequestModel.DeserializeFromYaml(file.Value);
227+
var repoCreationMetadata = RepositoryRequestModel.DeserializeFromYaml(file.Value);
236228

237-
if (repoCreationMetadata.RequestType == RequestType.CREATE_REPOSITORY)
238-
{
239-
// Save the repo
240-
Repository? savedRepo = await SaveRepositoryInternal(pullRequestEvent?.Installation?.Id, repoCreationMetadata);
229+
if (repoCreationMetadata.RequestType == RequestType.CREATE_REPOSITORY)
230+
{
231+
// Save the repo
232+
Repository? savedRepo = await SaveRepositoryInternal(pullRequestEvent?.Installation?.Id, repoCreationMetadata);
241233

242-
completeMessage = $"{_localizationService.GetLocalizedString(Constants.ISSUE_COMPLETED)}\n- Repository: {savedRepo?.HtmlUrl}";
243-
}
234+
completeMessage = $"{_localizationService.GetLocalizedString(Constants.ISSUE_COMPLETED)}\n- Repository: {savedRepo?.HtmlUrl}";
244235
}
245236
}
246237
}
247238

248-
if (isComplete)
249-
{
250-
// Last step: Add a comment to the issue and close the issue
251-
await _issueService.ChangeIssueLabel(_gitHubOptions.RepoOnboarding, issueNumber, [LocalizationExtensions.GetLocalizedString(TicketStatus.COMPLETED.ToString())], pullRequestEvent!);
239+
// Last step: Add a comment to the issue and close the issue
240+
await _issueService.ChangeIssueLabel(_gitHubOptions.RepoOnboarding, issueNumber, [LocalizationExtensions.GetLocalizedString(TicketStatus.COMPLETED.ToString())], pullRequestEvent!);
252241

253-
await AddCommentToIssueAndPullRequest(issueNumber, completeMessage, pullRequestEvent!);
242+
await AddCommentToIssueAndPullRequest(issueNumber, completeMessage, pullRequestEvent!);
254243

255-
await gitHubClient.Issue.Update(owner, _gitHubOptions.RepoOnboarding, issueNumber, new IssueUpdate { State = ItemState.Closed });
256-
}
244+
await gitHubClient.Issue.Update(owner, _gitHubOptions.RepoOnboarding, issueNumber, new IssueUpdate { State = ItemState.Closed });
257245
}
258246
catch (Exception ex)
259247
{

src/DevExcelerateApi/Services/TeamService.cs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -256,42 +256,30 @@ public async Task SaveTeam(PullRequestEvent pullRequestEvent)
256256
try
257257
{
258258
var fileContents = await gitHubClient.GetCommitFilesWithContentAsync(owner!, repo!, mergeCommitSha!);
259-
var isComplete = true;
260-
var completeMessage = string.Empty;
259+
var completeMessage = $"{_localizationService.GetLocalizedString(Constants.ERROR_PR_NO_CHANGES)}";
261260

262-
if (fileContents.Count == 0)
261+
foreach (var file in fileContents)
263262
{
264-
isComplete = false;
265-
completeMessage = $"{_localizationService.GetLocalizedString(Constants.ERROR_PR_NO_CHANGES)}";
266-
}
267-
else
268-
{
269-
foreach (var file in fileContents)
263+
if (file.Key.EndsWith(DevExPolicies.Team_Metadata.GetPolicyYamlFileName(), StringComparison.OrdinalIgnoreCase))
270264
{
271-
if (file.Key.EndsWith(DevExPolicies.Team_Metadata.GetPolicyYamlFileName(), StringComparison.OrdinalIgnoreCase))
272-
{
273-
var teamMetadata = TeamRequestModel.DeserializeFromYaml(file.Value);
265+
var teamMetadata = TeamRequestModel.DeserializeFromYaml(file.Value);
274266

275-
if (teamMetadata.RequestType == RequestType.CREATE_TEAM || teamMetadata.RequestType == RequestType.UPDATE_TEAM)
276-
{
277-
// Save the team
278-
Team? savedTeam = await SaveTeamInternal(pullRequestEvent?.Installation?.Id, teamMetadata);
267+
if (teamMetadata.RequestType == RequestType.CREATE_TEAM || teamMetadata.RequestType == RequestType.UPDATE_TEAM)
268+
{
269+
// Save the team
270+
Team? savedTeam = await SaveTeamInternal(pullRequestEvent?.Installation?.Id, teamMetadata);
279271

280-
completeMessage = $"{_localizationService.GetLocalizedString(Constants.ISSUE_COMPLETED)}\n- Team: {savedTeam?.HtmlUrl}";
281-
}
272+
completeMessage = $"{_localizationService.GetLocalizedString(Constants.ISSUE_COMPLETED)}\n- Team: {savedTeam?.HtmlUrl}";
282273
}
283274
}
284275
}
285276

286-
if (isComplete)
287-
{
288-
// Last step: Add a comment to the issue and close the issue
289-
await _issueService.ChangeIssueLabel(_gitHubOptions.RepoOnboarding, issueNumber, [LocalizationExtensions.GetLocalizedString(TicketStatus.COMPLETED.ToString())], pullRequestEvent!);
277+
// Last step: Add a comment to the issue and close the issue
278+
await _issueService.ChangeIssueLabel(_gitHubOptions.RepoOnboarding, issueNumber, [LocalizationExtensions.GetLocalizedString(TicketStatus.COMPLETED.ToString())], pullRequestEvent!);
290279

291-
await AddCommentToIssueAndPullRequest(issueNumber, completeMessage, pullRequestEvent!);
280+
await AddCommentToIssueAndPullRequest(issueNumber, completeMessage, pullRequestEvent!);
292281

293-
await gitHubClient.Issue.Update(owner, _gitHubOptions.RepoOnboarding, issueNumber, new IssueUpdate { State = ItemState.Closed });
294-
}
282+
await gitHubClient.Issue.Update(owner, _gitHubOptions.RepoOnboarding, issueNumber, new IssueUpdate { State = ItemState.Closed });
295283
}
296284
catch (Exception ex)
297285
{

0 commit comments

Comments
 (0)