-
Notifications
You must be signed in to change notification settings - Fork 49
feat(onboarding): listener conclui git_challenge na aprovação do PR (#353) #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danielhe4rt
merged 1 commit into
feat/squads
from
story/353-git-challenge-pr-approval-listener
Sep 28, 2026
+262
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
app-modules/onboarding/src/Listeners/CompleteGitChallengeOnPullRequestApproval.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Onboarding\Listeners; | ||
|
|
||
| use He4rt\Identity\ExternalIdentity\Enums\IdentityProvider; | ||
| use He4rt\Identity\ExternalIdentity\Models\ExternalIdentity; | ||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\IntegrationGithub\Enums\PurposeType; | ||
| use He4rt\IntegrationGithub\Events\GithubPullRequestApproved; | ||
| use He4rt\IntegrationGithub\Models\GithubRepository; | ||
| use He4rt\Onboarding\Actions\AdvanceStep; | ||
| use He4rt\Onboarding\Enums\OnboardingStatus; | ||
| use He4rt\Onboarding\Enums\OnboardingStepStatus; | ||
| use He4rt\Onboarding\Enums\OnboardingType; | ||
| use He4rt\Onboarding\Models\Onboarding; | ||
| use Illuminate\Contracts\Database\Query\Builder; | ||
|
|
||
| /** | ||
| * O vínculo do GitHub é pré-requisito do step git_challenge (#350): quando o | ||
| * evento chega, o autor sempre casa um usuário via ExternalIdentity — não há | ||
| * reconciliação/buffer para aprovações "órfãs". | ||
| */ | ||
| final readonly class CompleteGitChallengeOnPullRequestApproval | ||
| { | ||
| public function __construct( | ||
| private AdvanceStep $advanceStep, | ||
| ) {} | ||
|
|
||
| public function handle(GithubPullRequestApproved $event): void | ||
| { | ||
| if (!$this->isChallengeRepo($event->repo)) { | ||
| return; | ||
| } | ||
|
|
||
| $onboarding = $this->pendingGitChallengeOnboarding($event->author_login); | ||
|
|
||
| if (!$onboarding instanceof Onboarding) { | ||
| return; | ||
| } | ||
|
|
||
| $this->advanceStep->handle($onboarding, [ | ||
| 'repo' => $event->repo, | ||
| 'pr_number' => $event->pr_number, | ||
| 'approved_at' => $event->approved_at, | ||
| ]); | ||
| } | ||
|
|
||
| private function isChallengeRepo(string $repo): bool | ||
| { | ||
| return GithubRepository::query() | ||
| ->enabled() | ||
| ->where('full_name', $repo) | ||
| ->where('purpose', PurposeType::Challenge) | ||
| ->exists(); | ||
| } | ||
|
|
||
| private function pendingGitChallengeOnboarding(string $authorLogin): ?Onboarding | ||
| { | ||
| $identity = ExternalIdentity::query() | ||
| ->where('provider', IdentityProvider::GitHub) | ||
| ->whereNotNull('connected_at') | ||
| ->whereNull('disconnected_at') | ||
| ->where('metadata->username', $authorLogin) | ||
| ->first(); | ||
|
|
||
| if (!$identity instanceof ExternalIdentity || !$identity->user instanceof User) { | ||
| return null; | ||
| } | ||
|
|
||
| return Onboarding::query() | ||
| ->whereBelongsTo($identity->user) | ||
| ->where('type', OnboardingType::Squads) | ||
| ->where('status', OnboardingStatus::InProgress) | ||
| ->whereHas('steps', function (Builder $query): void { | ||
| $query->where('step_key', 'git_challenge') | ||
| ->where('status', OnboardingStepStatus::Pending); | ||
| }) | ||
| ->first(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
app-modules/onboarding/tests/Feature/CompleteGitChallengeOnPullRequestApprovalTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use He4rt\Identity\ExternalIdentity\Enums\IdentityProvider; | ||
| use He4rt\Identity\ExternalIdentity\Models\ExternalIdentity; | ||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\IntegrationGithub\Enums\PurposeType; | ||
| use He4rt\IntegrationGithub\Events\GithubPullRequestApproved; | ||
| use He4rt\IntegrationGithub\Models\GithubRepository; | ||
| use He4rt\Onboarding\Actions\AdvanceStep; | ||
| use He4rt\Onboarding\Actions\StartOnboarding; | ||
| use He4rt\Onboarding\Contracts\OnboardingCompletionGate; | ||
| use He4rt\Onboarding\Enums\OnboardingStatus; | ||
| use He4rt\Onboarding\Enums\OnboardingStepStatus; | ||
| use He4rt\Onboarding\Enums\OnboardingType; | ||
| use He4rt\Onboarding\Models\Onboarding; | ||
|
|
||
| /** | ||
| * @return array{user: User, onboarding: Onboarding} | ||
| */ | ||
| function linkedUserAtGitChallenge(string $githubLogin = 'maria'): array | ||
| { | ||
| $user = User::factory()->create(); | ||
| Onboarding::factory()->for($user)->completed()->create(); | ||
|
|
||
| ExternalIdentity::factory()->create([ | ||
| 'model_type' => $user->getMorphClass(), | ||
| 'model_id' => $user->id, | ||
| 'provider' => IdentityProvider::GitHub, | ||
| 'connected_at' => now(), | ||
| 'disconnected_at' => null, | ||
| 'metadata' => ['username' => $githubLogin], | ||
| ]); | ||
|
|
||
| $onboarding = resolve(StartOnboarding::class)->handle($user, OnboardingType::Squads); | ||
|
|
||
| resolve(AdvanceStep::class)->handle($onboarding, ['data' => ['terms' => true]]); | ||
|
|
||
| $onboarding->refresh(); | ||
|
|
||
| return ['user' => $user, 'onboarding' => $onboarding]; | ||
| } | ||
|
|
||
| test('aprovação do PR em repo de challenge conclui o git_challenge e libera APTO', function (): void { | ||
| ['user' => $user, 'onboarding' => $onboarding] = linkedUserAtGitChallenge(); | ||
|
|
||
| GithubRepository::factory()->create([ | ||
| 'full_name' => 'he4rt/git-challenge', | ||
| 'purpose' => PurposeType::Challenge, | ||
| ]); | ||
|
|
||
| event(new GithubPullRequestApproved( | ||
| author_login: 'maria', | ||
| repo: 'he4rt/git-challenge', | ||
| pr_number: 42, | ||
| approved_at: '2026-06-01T12:00:00Z', | ||
| )); | ||
|
|
||
| $onboarding->refresh(); | ||
| $step = $onboarding->steps()->where('step_key', 'git_challenge')->sole(); | ||
|
|
||
| expect($step->status)->toBe(OnboardingStepStatus::Done) | ||
| ->and($step->data)->toBe([ | ||
| 'repo' => 'he4rt/git-challenge', | ||
| 'pr_number' => 42, | ||
| 'approved_at' => '2026-06-01T12:00:00Z', | ||
| ]) | ||
| ->and($onboarding->status)->toBe(OnboardingStatus::Completed) | ||
| ->and(resolve(OnboardingCompletionGate::class)->isCompleted($user, OnboardingType::Squads)) | ||
| ->toBeTrue(); | ||
| }); | ||
|
|
||
| test('aprovação do PR em repo que não é de challenge é ignorada', function (): void { | ||
| ['onboarding' => $onboarding] = linkedUserAtGitChallenge(); | ||
|
|
||
| GithubRepository::factory()->create([ | ||
| 'full_name' => 'he4rt/heartdevs.com', | ||
| 'purpose' => PurposeType::Contributions, | ||
| ]); | ||
|
|
||
| event(new GithubPullRequestApproved( | ||
| author_login: 'maria', | ||
| repo: 'he4rt/heartdevs.com', | ||
| pr_number: 42, | ||
| approved_at: '2026-06-01T12:00:00Z', | ||
| )); | ||
|
|
||
| $onboarding->refresh(); | ||
| $step = $onboarding->steps()->where('step_key', 'git_challenge')->sole(); | ||
|
|
||
| expect($step->status)->toBe(OnboardingStepStatus::Pending) | ||
| ->and($onboarding->status)->toBe(OnboardingStatus::InProgress); | ||
| }); | ||
|
|
||
| test('aprovação de autor sem GitHub vinculado é ignorada sem lançar exceção', function (): void { | ||
| ['onboarding' => $onboarding] = linkedUserAtGitChallenge(githubLogin: 'maria'); | ||
|
|
||
| GithubRepository::factory()->create([ | ||
| 'full_name' => 'he4rt/git-challenge', | ||
| 'purpose' => PurposeType::Challenge, | ||
| ]); | ||
|
|
||
| event(new GithubPullRequestApproved( | ||
| author_login: 'alguem-sem-conta', | ||
| repo: 'he4rt/git-challenge', | ||
| pr_number: 42, | ||
| approved_at: '2026-06-01T12:00:00Z', | ||
| )); | ||
|
|
||
| $onboarding->refresh(); | ||
| $step = $onboarding->steps()->where('step_key', 'git_challenge')->sole(); | ||
|
|
||
| expect($step->status)->toBe(OnboardingStepStatus::Pending) | ||
| ->and($onboarding->status)->toBe(OnboardingStatus::InProgress); | ||
| }); | ||
|
|
||
| test('aprovação chegando antes do onboarding alcançar o step git_challenge não avança o step form', function (): void { | ||
| $user = User::factory()->create(); | ||
| Onboarding::factory()->for($user)->completed()->create(); | ||
|
|
||
| ExternalIdentity::factory()->create([ | ||
| 'model_type' => $user->getMorphClass(), | ||
| 'model_id' => $user->id, | ||
| 'provider' => IdentityProvider::GitHub, | ||
| 'connected_at' => now(), | ||
| 'disconnected_at' => null, | ||
| 'metadata' => ['username' => 'maria'], | ||
| ]); | ||
|
|
||
| // Onboarding ainda no step "form" — git_challenge nem foi criado. | ||
| $onboarding = resolve(StartOnboarding::class)->handle($user, OnboardingType::Squads); | ||
|
|
||
| GithubRepository::factory()->create([ | ||
| 'full_name' => 'he4rt/git-challenge', | ||
| 'purpose' => PurposeType::Challenge, | ||
| ]); | ||
|
|
||
| event(new GithubPullRequestApproved( | ||
| author_login: 'maria', | ||
| repo: 'he4rt/git-challenge', | ||
| pr_number: 42, | ||
| approved_at: '2026-06-01T12:00:00Z', | ||
| )); | ||
|
|
||
| $onboarding->refresh(); | ||
| $step = $onboarding->steps()->sole(); | ||
|
|
||
| expect($step->step_key)->toBe('form') | ||
| ->and($step->status)->toBe(OnboardingStepStatus::Pending); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Se não me engano o InterNACHI consegue encontrar o evento.