From 364bdf417ee00d0f87cf526b0156efa1b4839949 Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Mon, 18 Sep 2023 23:51:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20functionality=20to=20?= =?UTF-8?q?generate=20branch=20name=20based=20on=20file=20diffs=20and=20up?= =?UTF-8?q?date=20branch=20name=20if=20it=20doesn't=20match=20the=20genera?= =?UTF-8?q?ted=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/src/lib/api/cloud/api.ts | 9 ++++++ .../routes/repo/[projectId]/BranchLane.svelte | 31 +++++++++++++++++++ .../[projectId]/BranchLanePopupMenu.svelte | 6 +++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/lib/api/cloud/api.ts b/packages/ui/src/lib/api/cloud/api.ts index 9f389be2e..7d7e64b40 100644 --- a/packages/ui/src/lib/api/cloud/api.ts +++ b/packages/ui/src/lib/api/cloud/api.ts @@ -191,6 +191,15 @@ export function getCloudApiClient( // 'X-Auth-Token': token }, body: JSON.stringify(params) + }).then(parseResponseJSON), + branch: (token: string, params: { diff: string }): Promise<{ message: string }> => + fetch(getUrl('summarize_branch_name/branch.json'), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Auth-Token': token + }, + body: JSON.stringify(params) }).then(parseResponseJSON) }, chat: { diff --git a/packages/ui/src/routes/repo/[projectId]/BranchLane.svelte b/packages/ui/src/routes/repo/[projectId]/BranchLane.svelte index 51f94e11c..2e2b3c379 100644 --- a/packages/ui/src/routes/repo/[projectId]/BranchLane.svelte +++ b/packages/ui/src/routes/repo/[projectId]/BranchLane.svelte @@ -30,6 +30,7 @@ import { sortLikeFileTree } from '$lib/vbranches/filetree'; import CommitDialog from './CommitDialog.svelte'; import { writable } from 'svelte/store'; + import { computedAddedRemoved } from '$lib/vbranches/fileStatus'; const [send, receive] = crossfade({ duration: (d) => Math.sqrt(d * 200), @@ -134,6 +135,34 @@ commitDialogShown = false; } + function generateBranchName() { + const diff = branch.files + .map((f) => f.hunks) + .flat() + .map((h) => h.diff) + .flat() + .join('\n') + .slice(0, 5000); + + if ($user) { + cloud.summarize.branch($user.access_token, { diff }).then((result) => { + console.log(result); + if (result.message && result.message !== branch.name) { + branch.name = result.message; + handleBranchNameChange(); + } + }); + } + } + + $: linesTouched = computedAddedRemoved(...branch.files); + $: if ( + branch.name.toLowerCase().includes('virtual branch') && + linesTouched.added + linesTouched.removed > 0 + ) { + generateBranchName(); + } + // We have to create this manually for now. // TODO: Use document.body.addEventListener to avoid having to use backdrop let popupMenu = new BranchLanePopupMenu({ @@ -157,6 +186,8 @@ handleExpandAll(); } else if (e.detail == 'collapse') { handleCollapseAll(); + } else if (e.detail == 'generate-branch-name') { + generateBranchName(); } }); }); diff --git a/packages/ui/src/routes/repo/[projectId]/BranchLanePopupMenu.svelte b/packages/ui/src/routes/repo/[projectId]/BranchLanePopupMenu.svelte index 7e2abe4b0..718beeb39 100644 --- a/packages/ui/src/routes/repo/[projectId]/BranchLanePopupMenu.svelte +++ b/packages/ui/src/routes/repo/[projectId]/BranchLanePopupMenu.svelte @@ -11,7 +11,7 @@ let popupMenu: PopupMenu; const dispatch = createEventDispatcher<{ - action: 'expand' | 'collapse'; + action: 'expand' | 'collapse' | 'generate-branch-name'; }>(); export function openByMouse(e: MouseEvent, item: any) { @@ -37,6 +37,10 @@ >Collapse all + dispatch('action', 'generate-branch-name')} + >Generate branch name +