| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {CommitInfo} from '../types'; |
| b69ab31 | | | 9 | import type {CommitInfoMode} from './CommitInfoState'; |
| b69ab31 | | | 10 | import type {CommitMessageFields, FieldConfig} from './types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 13 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 14 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 15 | import {LinkButton} from 'isl-components/LinkButton'; |
| b69ab31 | | | 16 | import {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 17 | import {useCallback} from 'react'; |
| b69ab31 | | | 18 | import {useContextMenu} from 'shared/ContextMenu'; |
| b69ab31 | | | 19 | import {font, spacing} from '../../../components/theme/tokens.stylex'; |
| b69ab31 | | | 20 | import {FlexSpacer} from '../ComponentUtils'; |
| b69ab31 | | | 21 | import {Internal} from '../Internal'; |
| b69ab31 | | | 22 | import {tracker} from '../analytics'; |
| b69ab31 | | | 23 | import {useFeatureFlagSync} from '../featureFlags'; |
| b69ab31 | | | 24 | import {T, t} from '../i18n'; |
| b69ab31 | | | 25 | import {readAtom, writeAtom} from '../jotaiUtils'; |
| b69ab31 | | | 26 | import platform from '../platform'; |
| b69ab31 | | | 27 | import {dagWithPreviews} from '../previews'; |
| b69ab31 | | | 28 | import {layout} from '../stylexUtils'; |
| b69ab31 | | | 29 | import {useModal} from '../useModal'; |
| b69ab31 | | | 30 | import { |
| b69ab31 | | | 31 | commitMessageTemplate, |
| b69ab31 | | | 32 | editedCommitMessages, |
| b69ab31 | | | 33 | getDefaultEditedCommitMessage, |
| b69ab31 | | | 34 | } from './CommitInfoState'; |
| b69ab31 | | | 35 | import { |
| b69ab31 | | | 36 | commitMessageFieldsSchema, |
| b69ab31 | | | 37 | findConflictingFieldsWhenMerging, |
| b69ab31 | | | 38 | mergeCommitMessageFields, |
| b69ab31 | | | 39 | mergeOnlyEmptyMessageFields, |
| b69ab31 | | | 40 | parseCommitMessageFields, |
| b69ab31 | | | 41 | } from './CommitMessageFields'; |
| b69ab31 | | | 42 | import {SmallCapsTitle} from './utils'; |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | /** |
| b69ab31 | | | 45 | * The last entry in a tokenized field value is used as the value being typed in the editor. |
| b69ab31 | | | 46 | * When filling, we want all the values to be tokens and not inserted to the editors. |
| b69ab31 | | | 47 | * Add empty entries at the end of all tokenized fields to force tokens. |
| b69ab31 | | | 48 | */ |
| b69ab31 | | | 49 | function forceTokenizeAllFields(fields: CommitMessageFields): CommitMessageFields { |
| b69ab31 | | | 50 | const result: CommitMessageFields = {}; |
| b69ab31 | | | 51 | for (const [key, value] of Object.entries(fields)) { |
| b69ab31 | | | 52 | if (Array.isArray(value)) { |
| b69ab31 | | | 53 | result[key] = value.length > 0 && value.at(-1) ? [...value, ''] : value; |
| b69ab31 | | | 54 | } else { |
| b69ab31 | | | 55 | result[key] = value; |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | } |
| b69ab31 | | | 58 | return result; |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | const fillCommitMessageMethods: Array<{ |
| b69ab31 | | | 62 | key: string; |
| b69ab31 | | | 63 | label: string; |
| b69ab31 | | | 64 | getMessage: ( |
| b69ab31 | | | 65 | commit: CommitInfo, |
| b69ab31 | | | 66 | mode: CommitInfoMode, |
| b69ab31 | | | 67 | ) => CommitMessageFields | undefined | Promise<CommitMessageFields | undefined>; |
| b69ab31 | | | 68 | tooltip: string; |
| b69ab31 | | | 69 | }> = [ |
| b69ab31 | | | 70 | { |
| b69ab31 | | | 71 | key: 'last-commit', |
| b69ab31 | | | 72 | label: t('last commit'), |
| b69ab31 | | | 73 | tooltip: t("Fill in the previous commit's message here."), |
| b69ab31 | | | 74 | getMessage: (commit: CommitInfo, mode: CommitInfoMode) => { |
| b69ab31 | | | 75 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 76 | const dag = readAtom(dagWithPreviews); |
| b69ab31 | | | 77 | if (!dag || !schema) { |
| b69ab31 | | | 78 | return undefined; |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | // If in commit mode, "last commit" is actually the head commit. |
| b69ab31 | | | 81 | // Otherwise, it's the parent. |
| b69ab31 | | | 82 | const parent = dag.get(mode === 'commit' ? commit.hash : commit.parents[0]); |
| b69ab31 | | | 83 | if (!parent) { |
| b69ab31 | | | 84 | return undefined; |
| b69ab31 | | | 85 | } |
| b69ab31 | | | 86 | const fields = parseCommitMessageFields(schema, parent.title, parent.description); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | if (Internal.diffFieldTag) { |
| b69ab31 | | | 89 | // don't fill in diff field, so we don't conflict with a previous diff |
| b69ab31 | | | 90 | delete fields[Internal.diffFieldTag]; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | return forceTokenizeAllFields(fields); |
| b69ab31 | | | 93 | }, |
| b69ab31 | | | 94 | }, |
| b69ab31 | | | 95 | { |
| b69ab31 | | | 96 | key: 'template-file', |
| b69ab31 | | | 97 | label: t('template file'), |
| b69ab31 | | | 98 | tooltip: t( |
| b69ab31 | | | 99 | 'Fill in your configured commit message template.\nSee `sl help config` for more information.', |
| b69ab31 | | | 100 | ), |
| b69ab31 | | | 101 | getMessage: (_commit: CommitInfo, _mode: CommitInfoMode) => { |
| b69ab31 | | | 102 | const template = readAtom(commitMessageTemplate); |
| b69ab31 | | | 103 | return template as CommitMessageFields | undefined; |
| b69ab31 | | | 104 | }, |
| b69ab31 | | | 105 | }, |
| b69ab31 | | | 106 | ...(Internal.fillCommitMessageMethods ?? []), |
| b69ab31 | | | 107 | ]; |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | export function FillCommitMessage({commit, mode}: {commit: CommitInfo; mode: CommitInfoMode}) { |
| b69ab31 | | | 110 | const showModal = useModal(); |
| b69ab31 | | | 111 | const menu = useContextMenu(() => [ |
| b69ab31 | | | 112 | { |
| b69ab31 | | | 113 | label: t('Clear commit message'), |
| b69ab31 | | | 114 | onClick: async () => { |
| b69ab31 | | | 115 | const confirmed = await platform.confirm( |
| b69ab31 | | | 116 | t('Are you sure you want to clear the currently edited commit message?'), |
| b69ab31 | | | 117 | ); |
| b69ab31 | | | 118 | if (confirmed) { |
| b69ab31 | | | 119 | writeAtom(editedCommitMessages('head'), {}); |
| b69ab31 | | | 120 | } |
| b69ab31 | | | 121 | }, |
| b69ab31 | | | 122 | }, |
| b69ab31 | | | 123 | ]); |
| b69ab31 | | | 124 | const fillMessage = useCallback( |
| b69ab31 | | | 125 | async (newMessage: CommitMessageFields) => { |
| b69ab31 | | | 126 | const hashOrHead = mode === 'commit' ? 'head' : commit.hash; |
| b69ab31 | | | 127 | // TODO: support amending a message |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 130 | const existing = readAtom(editedCommitMessages(hashOrHead)); |
| b69ab31 | | | 131 | if (schema == null) { |
| b69ab31 | | | 132 | return; |
| b69ab31 | | | 133 | } |
| b69ab31 | | | 134 | if (existing == null) { |
| b69ab31 | | | 135 | writeAtom(editedCommitMessages(hashOrHead), getDefaultEditedCommitMessage()); |
| b69ab31 | | | 136 | return; |
| b69ab31 | | | 137 | } |
| b69ab31 | | | 138 | const oldMessage = existing as CommitMessageFields; |
| b69ab31 | | | 139 | const buttons = [ |
| b69ab31 | | | 140 | {label: t('Cancel')}, |
| b69ab31 | | | 141 | {label: t('Overwrite')}, |
| b69ab31 | | | 142 | {label: t('Merge')}, |
| b69ab31 | | | 143 | {label: t('Only Fill Empty'), primary: true}, |
| b69ab31 | | | 144 | ] as const; |
| b69ab31 | | | 145 | let answer: (typeof buttons)[number] | undefined = buttons[2]; // merge if no conflicts |
| b69ab31 | | | 146 | const conflictingFields = findConflictingFieldsWhenMerging(schema, oldMessage, newMessage); |
| b69ab31 | | | 147 | if (conflictingFields.length > 0) { |
| b69ab31 | | | 148 | answer = await showModal({ |
| b69ab31 | | | 149 | type: 'confirm', |
| b69ab31 | | | 150 | title: t('Commit Messages Conflict'), |
| b69ab31 | | | 151 | icon: 'warning', |
| b69ab31 | | | 152 | message: ( |
| b69ab31 | | | 153 | <MessageConflictWarning |
| b69ab31 | | | 154 | conflictingFields={conflictingFields} |
| b69ab31 | | | 155 | oldMessage={oldMessage} |
| b69ab31 | | | 156 | newMessage={newMessage} |
| b69ab31 | | | 157 | /> |
| b69ab31 | | | 158 | ), |
| b69ab31 | | | 159 | buttons, |
| b69ab31 | | | 160 | }); |
| b69ab31 | | | 161 | } |
| b69ab31 | | | 162 | if (answer === buttons[3]) { |
| b69ab31 | | | 163 | const merged = mergeOnlyEmptyMessageFields(schema, oldMessage, newMessage); |
| b69ab31 | | | 164 | writeAtom(editedCommitMessages(hashOrHead), merged); |
| b69ab31 | | | 165 | return; |
| b69ab31 | | | 166 | } else if (answer === buttons[2]) { |
| b69ab31 | | | 167 | const merged = mergeCommitMessageFields(schema, oldMessage, newMessage); |
| b69ab31 | | | 168 | writeAtom(editedCommitMessages(hashOrHead), merged); |
| b69ab31 | | | 169 | return; |
| b69ab31 | | | 170 | } else if (answer === buttons[1]) { |
| b69ab31 | | | 171 | writeAtom(editedCommitMessages(hashOrHead), newMessage); |
| b69ab31 | | | 172 | return; |
| b69ab31 | | | 173 | } |
| b69ab31 | | | 174 | }, |
| b69ab31 | | | 175 | [commit, mode, showModal], |
| b69ab31 | | | 176 | ); |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | const showDevmate = |
| b69ab31 | | | 179 | useFeatureFlagSync(Internal.featureFlags?.AIGenerateCommitMessage) && |
| b69ab31 | | | 180 | platform.platformName === 'vscode'; |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | const methods = ( |
| b69ab31 | | | 183 | <> |
| b69ab31 | | | 184 | {fillCommitMessageMethods |
| b69ab31 | | | 185 | // Only show Devmate option if allowlisted in GK |
| b69ab31 | | | 186 | .filter(method => method.key !== 'devmate' || showDevmate) |
| b69ab31 | | | 187 | .map(method => ( |
| b69ab31 | | | 188 | <Tooltip |
| b69ab31 | | | 189 | title={method.tooltip} |
| b69ab31 | | | 190 | key={method.key} |
| b69ab31 | | | 191 | placement="bottom" |
| b69ab31 | | | 192 | delayMs={DOCUMENTATION_DELAY}> |
| b69ab31 | | | 193 | <LinkButton |
| b69ab31 | | | 194 | onClick={() => { |
| b69ab31 | | | 195 | tracker.operation( |
| b69ab31 | | | 196 | 'FillCommitMessage', |
| b69ab31 | | | 197 | 'FetchError', |
| b69ab31 | | | 198 | {extras: {method: method.label}}, |
| b69ab31 | | | 199 | async () => { |
| b69ab31 | | | 200 | const newMessage = await method.getMessage(commit, mode); |
| b69ab31 | | | 201 | if (newMessage == null) { |
| b69ab31 | | | 202 | return; |
| b69ab31 | | | 203 | } |
| b69ab31 | | | 204 | fillMessage(newMessage); |
| b69ab31 | | | 205 | }, |
| b69ab31 | | | 206 | ); |
| b69ab31 | | | 207 | }}> |
| b69ab31 | | | 208 | {method.label} |
| b69ab31 | | | 209 | </LinkButton> |
| b69ab31 | | | 210 | </Tooltip> |
| b69ab31 | | | 211 | ))} |
| b69ab31 | | | 212 | </> |
| b69ab31 | | | 213 | ); |
| b69ab31 | | | 214 | return ( |
| b69ab31 | | | 215 | <div {...stylex.props(layout.flexRow, styles.container)}> |
| b69ab31 | | | 216 | <T replace={{$methods: methods}}>Fill commit message from $methods</T> |
| b69ab31 | | | 217 | <FlexSpacer /> |
| b69ab31 | | | 218 | <Button icon onClick={menu} data-testid="fill-commit-message-more-options"> |
| b69ab31 | | | 219 | <Icon icon="ellipsis" /> |
| b69ab31 | | | 220 | </Button> |
| b69ab31 | | | 221 | </div> |
| b69ab31 | | | 222 | ); |
| b69ab31 | | | 223 | } |
| b69ab31 | | | 224 | |
| b69ab31 | | | 225 | function MessageConflictWarning({ |
| b69ab31 | | | 226 | conflictingFields, |
| b69ab31 | | | 227 | oldMessage, |
| b69ab31 | | | 228 | newMessage, |
| b69ab31 | | | 229 | }: { |
| b69ab31 | | | 230 | conflictingFields: Array<FieldConfig>; |
| b69ab31 | | | 231 | oldMessage: CommitMessageFields; |
| b69ab31 | | | 232 | newMessage: CommitMessageFields; |
| b69ab31 | | | 233 | }) { |
| b69ab31 | | | 234 | return ( |
| b69ab31 | | | 235 | <div data-testid="fill-message-conflict-warning"> |
| b69ab31 | | | 236 | <div> |
| b69ab31 | | | 237 | <T>The new commit message being loaded conflicts with your current message.</T> |
| b69ab31 | | | 238 | </div> |
| b69ab31 | | | 239 | <div style={{maxWidth: '500px'}}> |
| b69ab31 | | | 240 | <T> |
| b69ab31 | | | 241 | Would you like to overwrite your current message with the new one, merge them, or only |
| b69ab31 | | | 242 | fill fields that are empty in the current message? |
| b69ab31 | | | 243 | </T> |
| b69ab31 | | | 244 | </div> |
| b69ab31 | | | 245 | <div style={{marginBlock: spacing.pad}}> |
| b69ab31 | | | 246 | <T>These fields are conflicting:</T> |
| b69ab31 | | | 247 | </div> |
| b69ab31 | | | 248 | <div> |
| b69ab31 | | | 249 | {conflictingFields.map((field, i) => { |
| b69ab31 | | | 250 | const oldValue = oldMessage[field.key]; |
| b69ab31 | | | 251 | const newValue = newMessage[field.key]; |
| b69ab31 | | | 252 | return ( |
| b69ab31 | | | 253 | <div key={i} {...stylex.props(layout.paddingBlock)}> |
| b69ab31 | | | 254 | <SmallCapsTitle> |
| b69ab31 | | | 255 | <Icon icon={field.icon} /> |
| b69ab31 | | | 256 | {field.key} |
| b69ab31 | | | 257 | </SmallCapsTitle> |
| b69ab31 | | | 258 | <div {...stylex.props(layout.flexRow)}> |
| b69ab31 | | | 259 | <b> |
| b69ab31 | | | 260 | <T>Current:</T> |
| b69ab31 | | | 261 | </b> |
| b69ab31 | | | 262 | <Truncate>{oldValue}</Truncate> |
| b69ab31 | | | 263 | </div> |
| b69ab31 | | | 264 | <div {...stylex.props(layout.flexRow)}> |
| b69ab31 | | | 265 | <b> |
| b69ab31 | | | 266 | <T>New:</T> |
| b69ab31 | | | 267 | </b> |
| b69ab31 | | | 268 | <Truncate>{newValue}</Truncate> |
| b69ab31 | | | 269 | </div> |
| b69ab31 | | | 270 | </div> |
| b69ab31 | | | 271 | ); |
| b69ab31 | | | 272 | })} |
| b69ab31 | | | 273 | </div> |
| b69ab31 | | | 274 | </div> |
| b69ab31 | | | 275 | ); |
| b69ab31 | | | 276 | } |
| b69ab31 | | | 277 | |
| b69ab31 | | | 278 | function Truncate({children}: {children: string | Array<string>}) { |
| b69ab31 | | | 279 | const content = Array.isArray(children) |
| b69ab31 | | | 280 | ? children.filter(v => v.trim() !== '').join(', ') |
| b69ab31 | | | 281 | : children; |
| b69ab31 | | | 282 | return ( |
| b69ab31 | | | 283 | <span {...stylex.props(styles.truncate)} title={content}> |
| b69ab31 | | | 284 | {content} |
| b69ab31 | | | 285 | </span> |
| b69ab31 | | | 286 | ); |
| b69ab31 | | | 287 | } |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | const styles = stylex.create({ |
| b69ab31 | | | 290 | container: { |
| b69ab31 | | | 291 | padding: spacing.half, |
| b69ab31 | | | 292 | paddingInline: spacing.pad, |
| b69ab31 | | | 293 | paddingBottom: 0, |
| b69ab31 | | | 294 | gap: spacing.half, |
| b69ab31 | | | 295 | alignItems: 'center', |
| b69ab31 | | | 296 | fontSize: font.small, |
| b69ab31 | | | 297 | marginInline: spacing.pad, |
| b69ab31 | | | 298 | marginTop: spacing.half, |
| b69ab31 | | | 299 | }, |
| b69ab31 | | | 300 | truncate: { |
| b69ab31 | | | 301 | overflow: 'hidden', |
| b69ab31 | | | 302 | textOverflow: 'ellipsis', |
| b69ab31 | | | 303 | whiteSpace: 'nowrap', |
| b69ab31 | | | 304 | maxWidth: 500, |
| b69ab31 | | | 305 | }, |
| b69ab31 | | | 306 | }); |