Repository navigation
feat: let users recommit commits #1506
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
Open
sheraz4196
wants to merge
8
commits into
dev-null-sh:production
Choose a base branch
from
sheraz4196:feat/recommit
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+431
−7
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
42a1e4f
feat: add commit recommit endpoint
sheraz4196 6dc6577
feat: add recommit action to commit cards
sheraz4196 48b1301
fix: show and persist recommit state
sheraz4196 4d5a66a
ui: distinguish quotes from recommits
sheraz4196 74f0261
test: cover recommit response paths
sheraz4196 e303fcd
test: stabilize vector search coverage
sheraz4196 4dd7d29
fix: distinguish recommits in thread rows
sheraz4196 b8bb5e2
fix: serialize recommit creation
sheraz4196 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
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,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Enums\CommitStatus; | ||
| use App\Models\Commit; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| final readonly class CreateCommitRecommit | ||
| { | ||
| public function __construct(private CreateCommit $createCommit) | ||
| { | ||
| // | ||
| } | ||
|
|
||
| public function handle(User $user, Commit $commit): Commit | ||
| { | ||
| return DB::transaction(function () use ($user, $commit): Commit { | ||
| // Every recommit of this source locks the same row through the duplicate check and insert. | ||
| $commit = Commit::query()->lockForUpdate()->findOrFail($commit->id); | ||
|
|
||
| if ($commit->status !== CommitStatus::Approved || $commit->isOwnedBy($user)) { | ||
| throw new AuthorizationException; | ||
| } | ||
|
|
||
| $alreadyRecommitted = Commit::query() | ||
| ->where('user_id', $user->id) | ||
| ->where('quoted_commit_id', $commit->id) | ||
| ->where('body', '') | ||
| ->exists(); | ||
|
|
||
| if ($alreadyRecommitted) { | ||
| throw new AuthorizationException; | ||
| } | ||
|
|
||
| return $this->createCommit->handle($user, '', quotedCommit: $commit, linkPreview: false); | ||
| }, attempts: 3); | ||
| } | ||
| } | ||
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,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Actions\CreateCommitRecommit; | ||
| use App\Enums\CommitStatus; | ||
| use App\Http\Requests\CreateCommitRecommitRequest; | ||
| use App\Models\Commit; | ||
| use App\Models\User; | ||
| use App\Queries\UserAchievementsQuery; | ||
| use Illuminate\Container\Attributes\CurrentUser; | ||
| use Illuminate\Http\RedirectResponse; | ||
| use Inertia\Inertia; | ||
|
|
||
| final readonly class CommitRecommitController | ||
| { | ||
| public function store(CreateCommitRecommitRequest $request, #[CurrentUser] User $user, Commit $commit, CreateCommitRecommit $action, UserAchievementsQuery $achievements): RedirectResponse | ||
| { | ||
| $before = $achievements->unlockedBadgeNames($user); | ||
| $recommit = $action->handle($user, $commit); | ||
| $unlocked = $achievements->unlockedBadgeNames($user)->diff($before)->values(); | ||
|
|
||
| if ($unlocked->isNotEmpty()) { | ||
| Inertia::flash('achievementsUnlocked', $unlocked->all()); | ||
| } | ||
|
|
||
| if ($recommit->status === CommitStatus::Pending) { | ||
| Inertia::flash('toast', [ | ||
| 'type' => 'info', | ||
| 'message' => 'Your recommit is pending review. Everyone will see it once it is approved.', | ||
| ]); | ||
| } else { | ||
| Inertia::flash('toast', [ | ||
| 'type' => 'success', | ||
| 'message' => 'Recommitted.', | ||
| ]); | ||
| } | ||
|
|
||
| return back(); | ||
| } | ||
| } |
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,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Enums\CommitStatus; | ||
| use App\Models\Commit; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class CreateCommitRecommitRequest extends FormRequest | ||
| { | ||
| public function authorize(): Response | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $commit = $this->route('commit'); | ||
| assert($commit instanceof Commit); | ||
|
|
||
| if (! $commit->isVisibleTo($user)) { | ||
| return Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| if ($commit->status !== CommitStatus::Approved || $commit->isOwnedBy($user)) { | ||
| return Response::deny(); | ||
| } | ||
|
|
||
| return Response::allow(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
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
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
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
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
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,102 @@ | ||
| import { Form, usePage } from '@inertiajs/react'; | ||
| import { Check, Repeat2 } from 'lucide-react'; | ||
| import { useRef } from 'react'; | ||
| import { store } from '@/actions/App/Http/Controllers/CommitRecommitController'; | ||
| import { ActionTooltip } from '@/components/action-tooltip'; | ||
| import { CommitRulesDialog, useCommitRules } from '@/components/commit-rules'; | ||
| import { useOwnsCommit } from '@/hooks/use-owns-commit'; | ||
| import { signIn } from '@/lib/auth'; | ||
| import { cn } from '@/lib/utils'; | ||
| import type { Commit } from '@/types'; | ||
|
|
||
| export function CommitRecommitButton({ | ||
| commit, | ||
| className, | ||
| }: { | ||
| commit: Commit; | ||
| className?: string; | ||
| }) { | ||
| const { auth } = usePage().props; | ||
| const ownsCommit = useOwnsCommit(); | ||
| const commitRules = useCommitRules(); | ||
| const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
|
|
||
| if (commit.status !== 'approved' || ownsCommit(commit)) { | ||
| return null; | ||
| } | ||
|
|
||
| const recommitted = commit.recommitted === true; | ||
|
|
||
| return auth.user === null ? ( | ||
| <ActionTooltip label="Recommit"> | ||
| <button | ||
| type="button" | ||
| onClick={signIn} | ||
| aria-label="Recommit" | ||
| className={cn( | ||
| 'flex items-center gap-1.5 rounded-md px-2 py-0.5 text-[13px] text-muted-foreground transition-colors hover:bg-accent hover:text-foreground', | ||
| className, | ||
| )} | ||
| data-test="recommit-button" | ||
| > | ||
| <Repeat2 className="size-4" /> | ||
| <span className="sr-only sm:not-sr-only">Recommit</span> | ||
| </button> | ||
| </ActionTooltip> | ||
| ) : ( | ||
| <Form | ||
| {...store.form(commit.id)} | ||
| onBefore={() => commitRules.beforeSubmit()} | ||
| transform={(data) => ({ | ||
| ...data, | ||
| commit_rules_accepted: commitRules.hasAccepted(), | ||
| })} | ||
| options={{ preserveScroll: true }} | ||
| > | ||
| {({ processing }) => ( | ||
| <> | ||
| <ActionTooltip | ||
| label={recommitted ? 'Recommitted' : 'Recommit'} | ||
| > | ||
| <button | ||
| type="submit" | ||
| disabled={recommitted || processing} | ||
| aria-label={ | ||
| recommitted ? 'Recommitted' : 'Recommit' | ||
| } | ||
| aria-pressed={recommitted} | ||
| className={cn( | ||
| 'flex items-center gap-1.5 rounded-md px-2 py-0.5 text-[13px] text-muted-foreground transition-colors hover:bg-accent hover:text-foreground disabled:pointer-events-none disabled:opacity-70', | ||
| recommitted && 'text-primary', | ||
| className, | ||
| )} | ||
| data-test="recommit-button" | ||
| > | ||
| {recommitted ? ( | ||
| <Check className="size-4" /> | ||
| ) : ( | ||
| <Repeat2 className="size-4" /> | ||
| )} | ||
| <span className="sr-only sm:not-sr-only"> | ||
| {recommitted ? 'Recommitted' : 'Recommit'} | ||
| </span> | ||
| </button> | ||
| </ActionTooltip> | ||
| <textarea | ||
| ref={textareaRef} | ||
| name="body" | ||
| tabIndex={-1} | ||
| aria-hidden="true" | ||
| className="sr-only" | ||
| readOnly | ||
| value="" | ||
| /> | ||
| <CommitRulesDialog | ||
| rules={commitRules} | ||
| textarea={textareaRef} | ||
| /> | ||
| </> | ||
| )} | ||
| </Form> | ||
| ); | ||
| } |
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
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
Oops, something went wrong.
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.
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 6705
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 23300
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 41064
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 6794
Enforce the duplicate rule during restoration
If a user creates a replacement within the undo window, restoring the first recommit can leave two active recommits for the same source. Check for a replacement at the restoration boundary and coordinate that check with creation. Preserve normal undo when no replacement exists.