Skip to content

Commit

Permalink
Merge branch 'dev-apilayer3' into upgrade_mud
Browse files Browse the repository at this point in the history
# Conflicts:
#	grade-management-new/GradeManagement.Bll/Services/CourseService.cs
  • Loading branch information
Gzozo committed Oct 8, 2024
2 parents 9c48561 + 22b7098 commit 3d3938c
Show file tree
Hide file tree
Showing 74 changed files with 978 additions and 794 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

namespace Ahk.GitHub.Monitor.EventHandlers
{
public class ActionWorkflowRunHandler : RepositoryEventBase<WorkflowRunEventPayload>
public class ActionWorkflowRunHandler(
Services.IGitHubClientFactory gitHubClientFactory,
Microsoft.Extensions.Caching.Memory.IMemoryCache cache,
Microsoft.Extensions.Logging.ILogger logger)
: RepositoryEventBase<WorkflowRunEventPayload>(gitHubClientFactory, cache, logger)
{
public const int WorkflowRunThreshold = 5;
public const string GitHubWebhookEventName = "workflow_run";
private const string WarningText = ":exclamation: **You triggered too many automated evaluations; extra evaluations are penalized. Túl sok automata értékelést futtattál; az extra futtatások pontlevonással járnak.** ";

public ActionWorkflowRunHandler(Services.IGitHubClientFactory gitHubClientFactory, Microsoft.Extensions.Caching.Memory.IMemoryCache cache, Microsoft.Extensions.Logging.ILogger logger)
: base(gitHubClientFactory, cache, logger)
{
}

protected override async Task<EventHandlerResult> executeCore(WorkflowRunEventPayload webhookPayload)
{
if (webhookPayload.Action.Equals("completed", StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
using System;
using System.Threading.Tasks;
using Ahk.GitHub.Monitor.Services;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Octokit;

namespace Ahk.GitHub.Monitor.EventHandlers
{
public class BranchProtectionRuleHandler : RepositoryEventBase<CreateEventPayload>
public class BranchProtectionRuleHandler(
IGitHubClientFactory gitHubClientFactory,
IMemoryCache cache,
ILogger logger)
: RepositoryEventBase<CreateEventPayload>(gitHubClientFactory, cache, logger)
{
public const string GitHubWebhookEventName = "create";

public BranchProtectionRuleHandler(Services.IGitHubClientFactory gitHubClientFactory, Microsoft.Extensions.Caching.Memory.IMemoryCache cache, Microsoft.Extensions.Logging.ILogger logger)
: base(gitHubClientFactory, cache, logger)
{
}

protected override async Task<EventHandlerResult> executeCore(CreateEventPayload webhookPayload)
{
if (!webhookPayload.RefType.StringValue.Equals("branch", StringComparison.OrdinalIgnoreCase))
return EventHandlerResult.NoActionNeeded($"create event for ref {webhookPayload.RefType} is not of interest");
{
return EventHandlerResult.NoActionNeeded(
$"create event for ref {webhookPayload.RefType} is not of interest");
}

await GitHubClient.Repository.Branch.UpdateBranchProtection(
webhookPayload.Repository.Id, webhookPayload.Ref, getBranchProtectionSettingsUpdate(webhookPayload.Ref, webhookPayload.Repository.DefaultBranch));
await this.GitHubClient.Repository.Branch.UpdateBranchProtection(
webhookPayload.Repository.Id, webhookPayload.Ref,
getBranchProtectionSettingsUpdate(webhookPayload.Ref, webhookPayload.Repository.DefaultBranch));
return EventHandlerResult.ActionPerformed("branch protection rule applied");
}

private static BranchProtectionSettingsUpdate getBranchProtectionSettingsUpdate(string branchName, string repositoryDefaultBranch)
private static BranchProtectionSettingsUpdate getBranchProtectionSettingsUpdate(string branchName,
string repositoryDefaultBranch)
{
// For default: prohibits the merge request into default to be merged
// For other branches: disables force push
return new BranchProtectionSettingsUpdate(
requiredStatusChecks: null, // Required. Require status checks to pass before merging. Set to null to disable.
requiredPullRequestReviews: getBranchProtectionRequiredReviewsUpdate(branchName, repositoryDefaultBranch),
restrictions: null, // Push access restrictions. Null to disable.
enforceAdmins: false); // Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
requiredStatusChecks: null, // Required. Require status checks to pass before merging. Set to null to disable.
requiredPullRequestReviews: getBranchProtectionRequiredReviewsUpdate(branchName,
repositoryDefaultBranch),
restrictions: null, // Push access restrictions. Null to disable.
enforceAdmins: false); // Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
}

private static BranchProtectionRequiredReviewsUpdate getBranchProtectionRequiredReviewsUpdate(string branchName, string repositoryDefaultBranch)
private static BranchProtectionRequiredReviewsUpdate getBranchProtectionRequiredReviewsUpdate(string branchName,
string repositoryDefaultBranch)
{
if (branchName.Equals(repositoryDefaultBranch, StringComparison.OrdinalIgnoreCase))
return new BranchProtectionRequiredReviewsUpdate(false, false, 1); // Prohibits the student from merging the pull request.
else
return null;
{
return
new BranchProtectionRequiredReviewsUpdate(false, false,
1); // Prohibits the student from merging the pull request.
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
namespace Ahk.GitHub.Monitor.EventHandlers
{
public class EventHandlerResult
public class EventHandlerResult(string result)
{
public EventHandlerResult(string result)
=> this.Result = result;

public string Result { get; }
public string Result { get; } = result;

public static EventHandlerResult PayloadError(string message) => new EventHandlerResult($"payload error: {message}");
public static EventHandlerResult NoActionNeeded(string message) => new EventHandlerResult($"no action needed: {message}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace Ahk.GitHub.Monitor.EventHandlers
{
public class IssueCommentEditDeleteHandler : RepositoryEventBase<IssueCommentPayload>
public class IssueCommentEditDeleteHandler(
Services.IGitHubClientFactory gitHubClientFactory,
Microsoft.Extensions.Caching.Memory.IMemoryCache cache,
Microsoft.Extensions.Logging.ILogger logger)
: RepositoryEventBase<IssueCommentPayload>(gitHubClientFactory, cache, logger)
{
public const string GitHubWebhookEventName = "issue_comment";
private const string WarningText = ":exclamation: **An issue comment was deleted / edited. Egy megjegyzes torolve vagy modositva lett.**";

public IssueCommentEditDeleteHandler(Services.IGitHubClientFactory gitHubClientFactory, Microsoft.Extensions.Caching.Memory.IMemoryCache cache, Microsoft.Extensions.Logging.ILogger logger)
: base(gitHubClientFactory, cache, logger)
{
}

protected override async Task<EventHandlerResult> executeCore(IssueCommentPayload webhookPayload)
{
if (webhookPayload.Issue == null)
Expand All @@ -25,11 +24,9 @@ protected override async Task<EventHandlerResult> executeCore(IssueCommentPayloa
{
return EventHandlerResult.NoActionNeeded($"comment action {webhookPayload.Action} by {webhookPayload.Sender.Login} allowed, referencing own comment");
}
else
{
await GitHubClient.Issue.Comment.Create(webhookPayload.Repository.Id, webhookPayload.Issue.Number, WarningText);
return EventHandlerResult.ActionPerformed("comment action resulting in warning");
}

await this.GitHubClient.Issue.Comment.Create(webhookPayload.Repository.Id, webhookPayload.Issue.Number, WarningText);
return EventHandlerResult.ActionPerformed("comment action resulting in warning");
}

return EventHandlerResult.EventNotOfInterest(webhookPayload.Action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

namespace Ahk.GitHub.Monitor.EventHandlers
{
public class PullRequestOpenDuplicateHandler : RepositoryEventBase<PullRequestEventPayload>
public class PullRequestOpenDuplicateHandler(
Services.IGitHubClientFactory gitHubClientFactory,
Microsoft.Extensions.Caching.Memory.IMemoryCache cache,
Microsoft.Extensions.Logging.ILogger logger)
: RepositoryEventBase<PullRequestEventPayload>(gitHubClientFactory, cache, logger)
{
public const string GitHubWebhookEventName = "pull_request";
private const string WarningText = ":exclamation: **You have multiple pull requests. Tobb pull request-et nyitottal.** {} \n\n";

public PullRequestOpenDuplicateHandler(Services.IGitHubClientFactory gitHubClientFactory, Microsoft.Extensions.Caching.Memory.IMemoryCache cache, Microsoft.Extensions.Logging.ILogger logger)
: base(gitHubClientFactory, cache, logger)
{
}

protected override async Task<EventHandlerResult> executeCore(PullRequestEventPayload webhookPayload)
{
if (webhookPayload.PullRequest == null)
Expand All @@ -28,16 +27,16 @@ protected override async Task<EventHandlerResult> executeCore(PullRequestEventPa
{
return EventHandlerResult.NoActionNeeded("pull request open is ok, there are no other PRs");
}
else
{
var (handledOpen, resultOpen) = await handleAnyOpenPrs(webhookPayload, repositoryPrs);
var (handledClosed, resultClosed) = await handleAnyClosedPrs(webhookPayload, repositoryPrs);

if (!handledOpen && !handledClosed)
return EventHandlerResult.NoActionNeeded($"{resultOpen}; {resultClosed}");
else
return EventHandlerResult.ActionPerformed($"{resultOpen}; {resultClosed}");
var (handledOpen, resultOpen) = await this.handleAnyOpenPrs(webhookPayload, repositoryPrs);
var (handledClosed, resultClosed) = await this.handleAnyClosedPrs(webhookPayload, repositoryPrs);

if (!handledOpen && !handledClosed)
{
return EventHandlerResult.NoActionNeeded($"{resultOpen}; {resultClosed}");
}

return EventHandlerResult.ActionPerformed($"{resultOpen}; {resultClosed}");
}

return EventHandlerResult.EventNotOfInterest(webhookPayload.Action);
Expand All @@ -63,16 +62,14 @@ private Task<IReadOnlyList<PullRequest>> getAllRepoPullRequests(PullRequestEvent

return (true, "pull request open handled with multiple open PRs");
}
else
{
return (false, "pull request open is ok, there are no other open PRs");
}

return (false, "pull request open is ok, there are no other open PRs");
}

private async Task<(bool HasProblem, string ResultText)> handleAnyClosedPrs(PullRequestEventPayload webhookPayload, IReadOnlyCollection<PullRequest> repositoryPrs)
{
var closedPrs = repositoryPrs.Where(otherPr => otherPr.State == ItemState.Closed).ToList();
if (closedPrs.Any())
if (closedPrs.Count != 0)
{
var prsClosedByNotStudent = new List<int>();
foreach (var otherClosedPr in closedPrs)
Expand All @@ -81,22 +78,18 @@ private Task<IReadOnlyList<PullRequest>> getAllRepoPullRequests(PullRequestEvent
prsClosedByNotStudent.Add(otherClosedPr.Number);
}

if (prsClosedByNotStudent.Any())
if (prsClosedByNotStudent.Count != 0)
{
var warningText = getWarningText(webhookPayload.PullRequest.Number, prsClosedByNotStudent);
await GitHubClient.Issue.Comment.Create(webhookPayload.Repository.Id, webhookPayload.Number, warningText);

return (true, "pull request open handled with already closed PRs");
}
else
{
return (false, "pull request open is ok, there are no other evaluated PRs");
}
}
else
{
return (false, "pull request open is ok, there are no other closed PRs");

return (false, "pull request open is ok, there are no other evaluated PRs");
}

return (false, "pull request open is ok, there are no other closed PRs");
}

private async Task<bool> isPrClosedByNotStudent(PullRequestEventPayload webhookPayload, PullRequest pr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

namespace Ahk.GitHub.Monitor.EventHandlers
{
public class PullRequestReviewToAssigneeHandler : RepositoryEventBase<PullRequestEventPayload>
public class PullRequestReviewToAssigneeHandler(
Services.IGitHubClientFactory gitHubClientFactory,
Microsoft.Extensions.Caching.Memory.IMemoryCache cache,
Microsoft.Extensions.Logging.ILogger logger)
: RepositoryEventBase<PullRequestEventPayload>(gitHubClientFactory, cache, logger)
{
public const string GitHubWebhookEventName = "pull_request";

public PullRequestReviewToAssigneeHandler(Services.IGitHubClientFactory gitHubClientFactory, Microsoft.Extensions.Caching.Memory.IMemoryCache cache, Microsoft.Extensions.Logging.ILogger logger)
: base(gitHubClientFactory, cache, logger)
{
}

protected override async Task<EventHandlerResult> executeCore(PullRequestEventPayload webhookPayload)
{
if (webhookPayload.PullRequest == null)
Expand All @@ -25,15 +24,14 @@ protected override async Task<EventHandlerResult> executeCore(PullRequestEventPa
{
return EventHandlerResult.PayloadError("no requested reviewer in webhook payload");
}
else if (!isPrAssignedToReviewer(webhookPayload))

if (!isPrAssignedToReviewer(webhookPayload))
{
await GitHubClient.Issue.Assignee.AddAssignees(webhookPayload.Repository.Owner.Login, webhookPayload.Repository.Name, webhookPayload.PullRequest.Number, getUsersToAssign(webhookPayload));
await this.GitHubClient.Issue.Assignee.AddAssignees(webhookPayload.Repository.Owner.Login, webhookPayload.Repository.Name, webhookPayload.PullRequest.Number, getUsersToAssign(webhookPayload));
return EventHandlerResult.ActionPerformed("pull request review_requested handled, assignee set");
}
else
{
return EventHandlerResult.NoActionNeeded("pull request review_requested is ok, assignee is present");
}

return EventHandlerResult.NoActionNeeded("pull request review_requested is ok, assignee is present");
}

return EventHandlerResult.EventNotOfInterest(webhookPayload.Action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@

namespace Ahk.GitHub.Monitor.EventHandlers
{
public abstract class RepositoryEventBase<TPayload> : IGitHubEventHandler
public abstract class RepositoryEventBase<TPayload>(
IGitHubClientFactory gitHubClientFactory,
IMemoryCache cache,
ILogger logger)
: IGitHubEventHandler
where TPayload : ActivityPayload
{
protected readonly ILogger Logger;

private readonly IGitHubClientFactory gitHubClientFactory;
private readonly IMemoryCache cache;

protected RepositoryEventBase(IGitHubClientFactory gitHubClientFactory, IMemoryCache cache, ILogger logger)
{
this.gitHubClientFactory = gitHubClientFactory;
this.cache = cache;
this.Logger = logger;
}
protected readonly ILogger Logger = logger;

protected IGitHubClient GitHubClient { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@

namespace Ahk.GitHub.Monitor.EventHandlers
{
public class PullRequestStatusTrackingHandler : RepositoryEventBase<PullRequestEventPayload>
public class PullRequestStatusTrackingHandler(
IGitHubClientFactory gitHubClientFactory,
IStatusTrackingStore statusTrackingStore,
IMemoryCache cache,
ILogger logger) : RepositoryEventBase<PullRequestEventPayload>(gitHubClientFactory, cache, logger)
{
public const string GitHubWebhookEventName = "pull_request";
private readonly IStatusTrackingStore statusTrackingStore;

public PullRequestStatusTrackingHandler(IGitHubClientFactory gitHubClientFactory, IStatusTrackingStore statusTrackingStore, IMemoryCache cache, ILogger logger)
: base(gitHubClientFactory, cache, logger)
{
this.statusTrackingStore = statusTrackingStore;
}

protected override async Task<EventHandlerResult> executeCore(PullRequestEventPayload webhookPayload)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public AutoMapperProfile()
{
CreateMap<Assignment, Shared.Dtos.Assignment>();
CreateMap<Course, Shared.Dtos.Course>();
CreateMap<Exercise, Shared.Dtos.Response.Exercise>();
CreateMap<Group, Shared.Dtos.Response.Group>();
CreateMap<Exercise, Shared.Dtos.Response.ExerciseResponse>();
CreateMap<Group, Shared.Dtos.Response.GroupResponse>();
CreateMap<Language, Shared.Dtos.Language>();
CreateMap<PullRequest, Shared.Dtos.PullRequest>();
CreateMap<PullRequest, Shared.Dtos.PullRequestForDashboard>();
CreateMap<Score, Shared.Dtos.Score>();
CreateMap<ScoreType, Shared.Dtos.ScoreType>();
CreateMap<ScoreTypeExercise, Shared.Dtos.ScoreTypeExercise>();
CreateMap<Semester, Shared.Dtos.Semester>();
CreateMap<Student, Shared.Dtos.Response.Student>();
CreateMap<Subject, Shared.Dtos.Response.Subject>();
CreateMap<Student, Shared.Dtos.Response.StudentResponse>();
CreateMap<Subject, Shared.Dtos.Response.SubjectResponse>();
CreateMap<User, Shared.Dtos.User>();
}
}
Loading

0 comments on commit 3d3938c

Please sign in to comment.