Skip to content

Commit

Permalink
Pull request can now be created with assignee already assigned
Browse files Browse the repository at this point in the history
  • Loading branch information
mackmarton committed Oct 24, 2024
1 parent e88649c commit 934af7b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,29 @@ public async Task ConsumePullRequestOpenedEventAsync(PullRequestOpened pullReque
{
var repositoryName = GetRepositoryNameFromUrl(pullRequestOpened.GitHubRepositoryUrl);
var assignment = await assignmentService.GetAssignmentModelByGitHubRepoNameWithoutQfAsync(repositoryName);
var pullRequest = new PullRequest()
var pullRequest = await pullRequestService.GetModelByUrlWithoutQfAsync(pullRequestOpened.PullRequestUrl);

if (pullRequest != null)
{
Url = pullRequestOpened.PullRequestUrl,
OpeningDate = pullRequestOpened.OpeningDate,
Status = PullRequestStatus.Open,
BranchName = pullRequestOpened.BranchName,
AssignmentId = assignment.Id
};
pullRequest = await pullRequestService.CreateAsync(pullRequest, assignment.SubjectId);
pullRequest.OpeningDate = pullRequestOpened.OpeningDate;
pullRequest.BranchName = pullRequestOpened.BranchName;
await gradeManagementDbContext.SaveChangesAsync();
}
else
{
var pullRequestToCreate = new PullRequest()
{
Url = pullRequestOpened.PullRequestUrl,
OpeningDate = pullRequestOpened.OpeningDate,
Status = PullRequestStatus.Open,
BranchName = pullRequestOpened.BranchName,
AssignmentId = assignment.Id
};
pullRequestToCreate = await pullRequestService.CreateWithoutQfAsync(pullRequestToCreate, assignment.SubjectId);
pullRequest = await pullRequestService.GetModelByUrlWithoutQfAsync(pullRequestOpened.PullRequestUrl);
}



var assignmentLog = new AssignmentLog()
{
Expand Down Expand Up @@ -122,7 +136,7 @@ public async Task ConsumeCiEvaluationCompletedEventAsync(CiEvaluationCompleted c
throw new SecurityTokenException("Invalid API key");
}

if (student.NeptunCode.IsNullOrEmpty())
if (string.IsNullOrEmpty(student.NeptunCode))
{
var newStudent =
await studentService.GetStudentModelByNeptunAsync(ciEvaluationCompleted.StudentNeptun);
Expand Down Expand Up @@ -162,6 +176,32 @@ public async Task ConsumeTeacherAssignedEventAsync(TeacherAssigned teacherAssign
await using var transaction = await gradeManagementDbContext.Database.BeginTransactionAsync();
try
{
var repositoryName = GetRepositoryNameFromUrl(teacherAssigned.GitHubRepositoryUrl);
var assignment = await assignmentService.GetAssignmentModelByGitHubRepoNameWithoutQfAsync(repositoryName);
var pullRequest = await pullRequestService.GetModelByUrlWithoutQfAsync(teacherAssigned.PullRequestUrl);

if (pullRequest == null)
{
pullRequest = new Data.Models.PullRequest()
{
Url = teacherAssigned.PullRequestUrl,
OpeningDate = DateTime.UtcNow,
Status = PullRequestStatus.Open,
BranchName = "",
AssignmentId = assignment.Id,
SubjectId = assignment.SubjectId
};
}

pullRequest.TeacherId = null; // Unassign previous teacher
await gradeManagementDbContext.SaveChangesAsync();

if(teacherAssigned.TeacherGitHubIds == null || teacherAssigned.TeacherGitHubIds.Count == 0)
{
await transaction.CommitAsync();
return;
}

foreach (var teacherGithubId in teacherAssigned.TeacherGitHubIds)
{
User teacherModel;
Expand All @@ -174,9 +214,6 @@ public async Task ConsumeTeacherAssignedEventAsync(TeacherAssigned teacherAssign
continue; // Skip if teacher not found, maybe faulty assignment
}

var repositoryName = GetRepositoryNameFromUrl(teacherAssigned.GitHubRepositoryUrl);
var assignment = await assignmentService.GetAssignmentModelByGitHubRepoNameWithoutQfAsync(repositoryName);
var pullRequest = await pullRequestService.GetModelByUrlWithoutQfAsync(teacherAssigned.PullRequestUrl);
pullRequest.TeacherId = teacherModel.Id;
await gradeManagementDbContext.SaveChangesAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ namespace GradeManagement.Bll.Services;

public class PullRequestService(GradeManagementDbContext gradeManagementDbContext, IMapper mapper)
{
public async Task<PullRequest> GetByIdAsync(long id)
public async Task<PullRequest> GetByIdWithoutQfAsync(long id)
{
return await gradeManagementDbContext.PullRequest
.IgnoreQueryFiltersButNotIsDeleted()
.ProjectTo<PullRequest>(mapper.ConfigurationProvider)
.SingleEntityAsync(p => p.Id == id, id);
}

public async Task<Data.Models.PullRequest> GetModelByUrlWithoutQfAsync(string pullRequestUrl)
public async Task<Data.Models.PullRequest?> GetModelByUrlWithoutQfAsync(string pullRequestUrl)
{
return await gradeManagementDbContext.PullRequest
.IgnoreQueryFiltersButNotIsDeleted()
.SingleEntityAsync(p => p.Url == pullRequestUrl, 0);
.SingleOrDefaultAsync(p => p.Url == pullRequestUrl);
}

public async Task<PullRequest> CreateAsync(PullRequest pullRequest, long subjectId)
public async Task<PullRequest> CreateWithoutQfAsync(PullRequest pullRequest, long subjectId)
{
var pullRequestEntity = new Data.Models.PullRequest()
{
Expand All @@ -43,7 +44,7 @@ public async Task<PullRequest> CreateAsync(PullRequest pullRequest, long subject
gradeManagementDbContext.PullRequest.Add(pullRequestEntity);
await gradeManagementDbContext.SaveChangesAsync();

return await GetByIdAsync(pullRequestEntity.Id);
return await GetByIdWithoutQfAsync(pullRequestEntity.Id);
}

public async Task<IEnumerable<Score>> GetAllScoresByIdSortedByDateDescendingAsync(long id)
Expand Down

0 comments on commit 934af7b

Please sign in to comment.