Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/Console/Synchronize.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static function (User $user) use ($self) {
}

$self->checkClosedTasks($api, $user->id);
$self->unassignRemovedTasks($api, $user->id, $gitlabTasks);
$self->syncTasks($gitlabTasks, $user->id);
}
);
Expand Down Expand Up @@ -139,6 +140,32 @@ private function checkClosedTasks(GitlabApi $api, int $userID): void
});
}

/**
* Detach the user from tasks whose GitLab issue is neither open nor closed
* for them anymore, i.e. the issue was unassigned or reassigned.
*/
private function unassignRemovedTasks(GitlabApi $api, int $userID, array $gitlabTasks): void
{
TaskRelation::whereHas(
'task',
static fn($query) => $query->whereRelation('users', 'id', $userID)
)
->whereNotIn('gitlab_id', Arr::pluck($gitlabTasks, 'id'))
->chunkById(30, static function ($relations) use ($api, $userID) {
try {
$closed = $api->getClosedUserTasks($relations->pluck('gitlab_issue_iid')->unique()->all());
} catch (Throwable $throwable) {
// Without the closed list we can't tell closed issues from unassigned ones
Log::error('Closed tasks cant be fetched for user: ' . $throwable->getMessage());
return;
}

$relations
->whereNotIn('gitlab_id', Arr::pluck($closed, 'id'))
->each(static fn(TaskRelation $relation) => $relation->task?->users()->detach($userID));
});
}

private function syncTasks(array $gitlabTasks, int $userID): void
{
$defaultPriorityId = Settings::scope('core')->get('default_priority_id') ?? Priority::firstOrFail()->id;
Expand Down
Loading