Skip to content

Commit 87f942b

Browse files
refactor: address comments.
1 parent f53e515 commit 87f942b

3 files changed

Lines changed: 71 additions & 21 deletions

File tree

playwright/app.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ test('Open PR drawer confirms and submits component/styles filepaths', async ({
649649
await connectByotWithSingleRepo(page)
650650
await ensureOpenPrDrawerOpen(page)
651651

652-
await page.locator('#github-pr-head-branch').fill('develop/open-pr-test')
652+
await page.locator('#github-pr-head-branch').fill('Develop/Open-Pr-Test')
653653
await page.locator('#github-pr-component-path').fill('examples/component/App.tsx')
654654
await page.locator('#github-pr-styles-path').fill('examples/styles/app.css')
655655
await page.locator('#github-pr-title').fill('Apply editor updates from develop')
@@ -680,14 +680,41 @@ test('Open PR drawer confirms and submits component/styles filepaths', async ({
680680
const createdRefPayload = createdRefBody as CreateRefRequestBody | null
681681
const pullRequestPayload = pullRequestBody as PullRequestCreateBody | null
682682

683-
expect(createdRefPayload?.ref).toBe('refs/heads/develop/open-pr-test')
683+
expect(createdRefPayload?.ref).toBe('refs/heads/Develop/Open-Pr-Test')
684684
expect(createdRefPayload?.sha).toBe('abc123mainsha')
685685

686686
expect(upsertRequests).toHaveLength(2)
687687
expect(upsertRequests[0]?.path).toBe('examples/component/App.tsx')
688688
expect(upsertRequests[1]?.path).toBe('examples/styles/app.css')
689-
expect(pullRequestPayload?.head).toBe('develop/open-pr-test')
689+
expect(pullRequestPayload?.head).toBe('Develop/Open-Pr-Test')
690690
expect(pullRequestPayload?.base).toBe('main')
691+
692+
await ensureOpenPrDrawerOpen(page)
693+
await expect(page.locator('#github-pr-component-path')).toHaveValue(
694+
'examples/component/App.tsx',
695+
)
696+
await expect(page.locator('#github-pr-styles-path')).toHaveValue(
697+
'examples/styles/app.css',
698+
)
699+
await expect(page.locator('#github-pr-base-branch')).toHaveValue('main')
700+
701+
await expect(page.locator('#github-pr-head-branch')).toHaveValue(
702+
/^develop\/develop\/editor-sync-/,
703+
)
704+
await expect(page.locator('#github-pr-head-branch')).not.toHaveValue(
705+
'Develop/Open-Pr-Test',
706+
)
707+
await expect(page.locator('#github-pr-title')).toHaveValue(
708+
'Apply component and styles edits to knightedcodemonkey/develop',
709+
)
710+
await expect(page.locator('#github-pr-body')).toHaveValue(
711+
[
712+
'This PR was created from @knighted/develop editor content.',
713+
'',
714+
'- Component source -> examples/component/App.tsx',
715+
'- Styles source -> examples/styles/app.css',
716+
].join('\n'),
717+
)
691718
})
692719

693720
test('Open PR drawer validates unsafe filepaths', async ({ page }) => {

src/modules/github-byot-controls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export const createGitHubByotControls = ({
214214

215215
const getSelectedRepositoryObject = () => {
216216
if (!lastSelectedRepository) {
217-
return
217+
return null
218218
}
219219

220220
return writableRepos.find(repo => repo.fullName === lastSelectedRepository) ?? null

src/modules/github-pr-drawer.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,27 @@ const saveRepositoryPrConfig = ({ repositoryFullName, config }) => {
4040
}
4141
}
4242

43-
const clearRepositoryPrConfig = repositoryFullName => {
43+
const clearRepositoryPrDraftFields = ({
44+
repositoryFullName,
45+
componentFilePath,
46+
stylesFilePath,
47+
baseBranch,
48+
}) => {
4449
if (typeof repositoryFullName !== 'string' || !repositoryFullName.trim()) {
4550
return
4651
}
4752

48-
try {
49-
localStorage.removeItem(`${prConfigStoragePrefix}${repositoryFullName}`)
50-
} catch {
51-
/* noop */
52-
}
53+
saveRepositoryPrConfig({
54+
repositoryFullName,
55+
config: {
56+
componentFilePath,
57+
stylesFilePath,
58+
baseBranch,
59+
headBranch: '',
60+
prTitle: '',
61+
prBody: '',
62+
},
63+
})
5364
}
5465

5566
const toSafeText = value => (typeof value === 'string' ? value.trim() : '')
@@ -97,18 +108,20 @@ const validateFilePath = value => {
97108
}
98109

99110
const sanitizeBranchPart = value => {
100-
const trimmed = toSafeText(value).toLowerCase()
111+
const trimmed = toSafeText(value)
101112
if (!trimmed) {
102113
return ''
103114
}
104115

105116
return trimmed
106-
.replace(/[^a-z0-9._/-]/g, '-')
117+
.replace(/[^A-Za-z0-9._/-]/g, '-')
107118
.replace(/\/+/g, '/')
108119
.replace(/-{2,}/g, '-')
109120
.replace(/^[-/.]+|[-/.]+$/g, '')
110121
}
111122

123+
const sanitizeAutoBranchPart = value => sanitizeBranchPart(value).toLowerCase()
124+
112125
const toUtcBranchStamp = () => {
113126
const now = new Date()
114127
const parts = [
@@ -137,7 +150,7 @@ const isAutoGeneratedHeadBranch = value => {
137150
}
138151

139152
const createDefaultBranchName = repository => {
140-
const repoName = sanitizeBranchPart(repository?.name ?? '') || 'repo'
153+
const repoName = sanitizeAutoBranchPart(repository?.name ?? '') || 'repo'
141154
const stamp = toUtcBranchStamp()
142155
const entropy = createBranchEntropySuffix()
143156
return `develop/${repoName}/editor-sync-${stamp}-${entropy}`
@@ -322,7 +335,8 @@ export const createGitHubPrDrawer = ({
322335
const syncFormForRepository = ({ resetBranch = false, resetAll = false } = {}) => {
323336
const repository = getSelectedRepositoryObject()
324337
const repositoryFullName = getRepositoryFullName(repository)
325-
const savedConfig = resetAll ? {} : readRepositoryPrConfig(repositoryFullName)
338+
const savedConfig = readRepositoryPrConfig(repositoryFullName)
339+
const savedDraftConfig = resetAll ? {} : savedConfig
326340

327341
const componentFilePath =
328342
typeof savedConfig.componentFilePath === 'string' && savedConfig.componentFilePath
@@ -352,7 +366,7 @@ export const createGitHubPrDrawer = ({
352366

353367
if (headBranchInput instanceof HTMLInputElement) {
354368
if (resetAll || resetBranch || !toSafeText(headBranchInput.value)) {
355-
const savedHeadBranch = sanitizeBranchPart(savedConfig.headBranch)
369+
const savedHeadBranch = sanitizeBranchPart(savedDraftConfig.headBranch)
356370
headBranchInput.value =
357371
savedHeadBranch && !isAutoGeneratedHeadBranch(savedHeadBranch)
358372
? savedHeadBranch
@@ -363,15 +377,15 @@ export const createGitHubPrDrawer = ({
363377
if (prTitleInput instanceof HTMLInputElement) {
364378
if (resetAll || !toSafeText(prTitleInput.value)) {
365379
prTitleInput.value =
366-
toSafeText(savedConfig.prTitle) || toDefaultPrTitle(repository)
380+
toSafeText(savedDraftConfig.prTitle) || toDefaultPrTitle(repository)
367381
}
368382
}
369383

370384
if (prBodyInput instanceof HTMLTextAreaElement) {
371385
if (resetAll || !toSafeText(prBodyInput.value)) {
372386
prBodyInput.value =
373-
typeof savedConfig.prBody === 'string' && savedConfig.prBody
374-
? savedConfig.prBody
387+
typeof savedDraftConfig.prBody === 'string' && savedDraftConfig.prBody
388+
? savedDraftConfig.prBody
375389
: toDefaultPrBody({ componentFilePath, stylesFilePath })
376390
}
377391
}
@@ -512,7 +526,12 @@ export const createGitHubPrDrawer = ({
512526
signal: abortController.signal,
513527
})
514528
.then(result => {
515-
persistCurrentPaths()
529+
clearRepositoryPrDraftFields({
530+
repositoryFullName: repositoryLabel,
531+
componentFilePath: componentPathValidation.value,
532+
stylesFilePath: stylesPathValidation.value,
533+
baseBranch: values.baseBranch,
534+
})
516535
const url = result.pullRequest.htmlUrl
517536
setStatus(
518537
url ? `Pull request opened: ${url}` : 'Pull request opened successfully.',
@@ -522,7 +541,6 @@ export const createGitHubPrDrawer = ({
522541
url,
523542
pullRequestNumber: result.pullRequest.number,
524543
})
525-
clearRepositoryPrConfig(repositoryLabel)
526544
resetOnNextOpen = true
527545
setOpen(false)
528546
})
@@ -534,7 +552,12 @@ export const createGitHubPrDrawer = ({
534552
const message =
535553
error instanceof Error ? error.message : 'Failed to open pull request.'
536554
setStatus(`Open PR failed: ${message}`, 'error')
537-
clearRepositoryPrConfig(repositoryLabel)
555+
clearRepositoryPrDraftFields({
556+
repositoryFullName: repositoryLabel,
557+
componentFilePath: componentPathValidation.value,
558+
stylesFilePath: stylesPathValidation.value,
559+
baseBranch: values.baseBranch,
560+
})
538561
resetOnNextOpen = true
539562
})
540563
.finally(() => {

0 commit comments

Comments
 (0)