(ui, draft)
| 12 | * @return {Promise<void>} |
| 13 | */ |
| 14 | export async function createPR(ui, draft) { |
| 15 | const { ghOwnerInput: owner, ghRepoInput: repo, ghTokenInput: token } = ui; |
| 16 | if (!owner.value || !repo.value || !token.value) { |
| 17 | customAlert(ui, 'Please fill in GitHub settings first.'); |
| 18 | ui.settingsDetails.open = true; |
| 19 | if (!token.value) { |
| 20 | token.focus(); |
| 21 | } else if (!owner.value) { |
| 22 | owner.focus(); |
| 23 | } else if (!repo.value) { |
| 24 | repo.focus(); |
| 25 | } |
| 26 | return; |
| 27 | } |
| 28 | ui.githubPrBtn.disabled = true; |
| 29 | ui.githubPrBtn.textContent = '⏳ Creating...'; |
| 30 | try { |
| 31 | const { sync } = await import('../editor/create-post.js'); |
| 32 | await ensureAllTranslationsReady(ui, sync); |
| 33 | |
| 34 | const slug = ui.getSlug(ui.titleInput.value); |
| 35 | const branchName = `post-${slug}-${Date.now()}`; |
| 36 | const classifierResults = window.getSelectedClassifierResults |
| 37 | ? window.getSelectedClassifierResults() |
| 38 | : []; |
| 39 | const md = generateMarkdown( |
| 40 | draft, |
| 41 | ui.titleInput.value, |
| 42 | ui.descInput.value, |
| 43 | ui.dateInput.value, |
| 44 | ui.tagsInput.value, |
| 45 | ui.contentInput.value, |
| 46 | classifierResults, |
| 47 | ); |
| 48 | |
| 49 | const repoInfo = await ghFetch(ui, ''); |
| 50 | const defaultBranch = repoInfo.default_branch; |
| 51 | const branchInfo = await ghFetch(ui, `/branches/${defaultBranch}`); |
| 52 | const baseSha = branchInfo.commit.sha; |
| 53 | |
| 54 | await ghFetch(ui, '/git/refs', { |
| 55 | method: 'POST', |
| 56 | body: JSON.stringify({ ref: `refs/heads/${branchName}`, sha: baseSha }), |
| 57 | }); |
| 58 | |
| 59 | const getSha = async (path) => { |
| 60 | try { |
| 61 | const file = await ghFetch(ui, `/contents/${path}?ref=${branchName}`); |
| 62 | return file.sha; |
| 63 | } catch (e) { |
| 64 | return undefined; |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | // Upload images |
| 69 | const defaultLocale = window.DEFAULT_LOCALE || 'en'; |
| 70 | await uploadDraftImages(ui, draft, slug, branchName, defaultLocale, getSha); |
| 71 |
no test coverage detected