Skip to content

Commit

Permalink
pre-commit warning for large sloc commits
Browse files Browse the repository at this point in the history
Summary: this diff adds the Split suggestion banner to the commit mode w/ some minor mods to the verbiage to encourage folks to select less files.

Reviewed By: evangrayk

Differential Revision: D58418870

fbshipit-source-id: 458dc9be3f06c7e58799c8acab47c91aa19508f2
  • Loading branch information
Ben Monro authored and facebook-github-bot committed Jun 11, 2024
1 parent 5fa46eb commit 1da854b
Showing 1 changed file with 70 additions and 17 deletions.
87 changes: 70 additions & 17 deletions addons/isl/src/CommitInfoView/SplitSuggestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,104 @@ import {Divider} from '../components/Divider';
import GatedComponent from '../components/GatedComponent';
import {T, t} from '../i18n';
import {localStorageBackedAtom} from '../jotaiUtils';
import {uncommittedChangesWithPreviews} from '../previews';
import {
MAX_FILES_ALLOWED_FOR_DIFF_STAT,
SLOC_THRESHOLD_FOR_SPLIT_SUGGESTIONS,
} from '../sloc/diffStatConstants';
import {useFetchSignificantLinesOfCode} from '../sloc/useFetchSignificantLinesOfCode';
import {
useFetchPendingSignificantLinesOfCode,
useFetchSignificantLinesOfCode,
} from '../sloc/useFetchSignificantLinesOfCode';
import {SplitButton} from '../stackEdit/ui/SplitButton';
import {type CommitInfo} from '../types';
import {commitMode} from './CommitInfoState';
import {useAtomValue} from 'jotai';
import {Suspense} from 'react';
import {Icon} from 'shared/Icon';

export const splitSuggestionEnabled = localStorageBackedAtom<boolean>(
'isl.split-suggestion-enabled',
true,
);

function SplitSuggestionImpl({commit}: {commit: CommitInfo}) {
const significantLinesOfCode = useFetchSignificantLinesOfCode(commit);
if (
significantLinesOfCode == null ||
significantLinesOfCode <= SLOC_THRESHOLD_FOR_SPLIT_SUGGESTIONS
) {
return null;
}
function SuggestionBanner({
tooltip,
buttons,
children,
}: {
tooltip: string;
buttons?: React.ReactNode;
children: React.ReactNode;
}) {
return (
<>
<Divider />
<Banner
tooltip={t('This commit has $sloc significant lines of code (top 10%)', {
replace: {$sloc: String(significantLinesOfCode)},
})}
tooltip={tooltip}
kind={BannerKind.default}
icon={<Icon size="M" icon="lightbulb" color="blue" />}
alwaysShowButtons
buttons={<SplitButton trackerEventName="SplitOpenFromSplitSuggestion" commit={commit} />}>
buttons={buttons}>
<Column alignStart style={{gap: 0}}>
<b>
<T>Consider splitting up this commit</T>
</b>
<T>Small Diffs lead to less SEVs & quicker review times</T>
{children}
</Column>
</Banner>
</>
);
}

function NewCommitSuggestion() {
const pendingSignificantLinesOfCode = useFetchPendingSignificantLinesOfCode();
if (pendingSignificantLinesOfCode == null) {
return null;
}

if (pendingSignificantLinesOfCode > SLOC_THRESHOLD_FOR_SPLIT_SUGGESTIONS) {
return (
<SuggestionBanner
tooltip={t('This commit would have $sloc significant lines of code (top 10%)', {
replace: {$sloc: String(pendingSignificantLinesOfCode)},
})}>
<b>
<T>Consider unselecting some of these changes</T>
</b>
<T>Small Diffs lead to less SEVs & quicker review times</T>
</SuggestionBanner>
);
}
}

function SplitSuggestionImpl({commit}: {commit: CommitInfo}) {
const mode = useAtomValue(commitMode);
const significantLinesOfCode = useFetchSignificantLinesOfCode(commit) ?? -1;
const uncommittedChanges = useAtomValue(uncommittedChangesWithPreviews);

// no matter what if the commit is over the threshold, we show the split suggestion
if (significantLinesOfCode > SLOC_THRESHOLD_FOR_SPLIT_SUGGESTIONS) {
return (
<SuggestionBanner
tooltip={t('This commit has $sloc significant lines of code (top 10%)', {
replace: {$sloc: String(significantLinesOfCode)},
})}
buttons={<SplitButton trackerEventName="SplitOpenFromSplitSuggestion" commit={commit} />}>
<b>
<T>Consider splitting up this commit</T>
</b>
<T>Small Diffs lead to less SEVs & quicker review times</T>
</SuggestionBanner>
);
}

// if there are uncommitted changes, let's (maybe) show the suggestion to make a new commit
if (uncommittedChanges.length > 0 && mode === 'commit') {
return <NewCommitSuggestion />;
}

// no need to show any suggestion
return null;
}

export default function SplitSuggestion({commit}: {commit: CommitInfo}) {
const enabled = useAtomValue(splitSuggestionEnabled);
if (!enabled || commit.totalFileCount > MAX_FILES_ALLOWED_FOR_DIFF_STAT) {
Expand Down

0 comments on commit 1da854b

Please sign in to comment.