From f4995b8cc12dd36de695f6218d8f4bd5b3828311 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 18:36:59 +0000 Subject: [PATCH 01/12] due-date update and comment deletion Signed-off-by: Yarden Shoham --- web_src/js/features/repo-issue.js | 45 ++++++++++++++++--------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 6fb13b0ddae28..42a93b2ffc1ab 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -6,6 +6,7 @@ import {setFileFolding} from './file-fold.js'; import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js'; import {toAbsoluteUrl} from '../utils.js'; import {initDropzone} from './common-global.js'; +import {POST} from '../modules/fetch.js'; const {appSubUrl, csrfToken} = window.config; @@ -40,7 +41,7 @@ export function initRepoIssueTimeTracking() { }); } -function updateDeadline(deadlineString) { +async function updateDeadline(deadlineString) { hideElem($('#deadline-err-invalid-date')); $('#deadline-loader').addClass('loading'); @@ -56,23 +57,21 @@ function updateDeadline(deadlineString) { realDeadline = new Date(newDate); } - $.ajax(`${$('#update-issue-deadline-form').attr('action')}`, { - data: JSON.stringify({ - due_date: realDeadline, - }), - headers: { - 'X-Csrf-Token': csrfToken, - }, - contentType: 'application/json', - type: 'POST', - success() { + try { + const response = await POST($('#update-issue-deadline-form').attr('action'), { + data: {due_date: realDeadline} + }); + + if (response.ok) { window.location.reload(); - }, - error() { - $('#deadline-loader').removeClass('loading'); - showElem($('#deadline-err-invalid-date')); - }, - }); + } else { + throw new Error('Invalid response'); + } + } catch (error) { + console.error('Error:', error); + $('#deadline-loader').removeClass('loading'); + showElem($('#deadline-err-invalid-date')); + } } export function initRepoIssueDue() { @@ -156,12 +155,12 @@ export function initRepoIssueSidebarList() { export function initRepoIssueCommentDelete() { // Delete comment - $(document).on('click', '.delete-comment', function () { + $(document).on('click', '.delete-comment', async function () { const $this = $(this); if (window.confirm($this.data('locale'))) { - $.post($this.data('url'), { - _csrf: csrfToken, - }).done(() => { + try { + const response = await POST($this.data('url')); + if (!response.ok) throw new Error('Failed to delete comment'); const $conversationHolder = $this.closest('.conversation-holder'); // Check if this was a pending comment. @@ -186,7 +185,9 @@ export function initRepoIssueCommentDelete() { } $conversationHolder.remove(); } - }); + } catch (error) { + console.error('Error:', error); + } } return false; }); From b92f854b7bf54eff41099d6f5405d5f0a6b2ba7c Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 18:46:41 +0000 Subject: [PATCH 02/12] update branch --- web_src/js/features/repo-issue.js | 34 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 42a93b2ffc1ab..059e0dd0d6267 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -227,22 +227,32 @@ export function initRepoIssueCodeCommentCancel() { export function initRepoPullRequestUpdate() { // Pull Request update button const $pullUpdateButton = $('.update-button > button'); - $pullUpdateButton.on('click', function (e) { + $pullUpdateButton.on('click', async function (e) { e.preventDefault(); const $this = $(this); const redirect = $this.data('redirect'); $this.addClass('loading'); - $.post($this.data('do'), { - _csrf: csrfToken - }).done((data) => { - if (data.redirect) { - window.location.href = data.redirect; - } else if (redirect) { - window.location.href = redirect; - } else { - window.location.reload(); - } - }); + let response; + try { + response = await POST($this.data('do')); + } catch (error) { + console.error('Error:', error); + } finally { + $this.removeClass('loading'); + } + let data; + try { + data = await response?.json(); // the response is probably not a JSON + } catch (error) { + console.error('Error:', error); + } + if (data?.redirect) { + window.location.href = data.redirect; + } else if (redirect) { + window.location.href = redirect; + } else { + window.location.reload(); + } }); $('.update-button > .dropdown').dropdown({ From 02f107c07aba09b9670efa0f698499f31f0b2c41 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 18:56:43 +0000 Subject: [PATCH 03/12] allow edits from maintainers button --- web_src/js/features/repo-issue.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 059e0dd0d6267..5966df11e14cd 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -278,20 +278,24 @@ export function initRepoPullRequestAllowMaintainerEdit() { const promptError = $checkbox.attr('data-prompt-error'); $checkbox.checkbox({ - 'onChange': () => { + 'onChange': async () => { const checked = $checkbox.checkbox('is checked'); let url = $checkbox.attr('data-url'); url += '/set_allow_maintainer_edit'; $checkbox.checkbox('set disabled'); - $.ajax({url, type: 'POST', - data: {_csrf: csrfToken, allow_maintainer_edit: checked}, - error: () => { - showTemporaryTooltip($checkbox[0], promptError); - }, - complete: () => { - $checkbox.checkbox('set enabled'); - }, - }); + try { + const response = await POST(url, { + data: {allow_maintainer_edit: checked}, + }); + if (!response.ok) { + throw new Error('Failed to update maintainer edit permission'); + } + } catch (error) { + console.error('Error:', error); + showTemporaryTooltip($checkbox[0], promptError); + } finally { + $checkbox.checkbox('set enabled'); + } }, }); } From 0c03e33a6b48e855a0174b9a147b9ed81398221d Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 19:10:33 +0000 Subject: [PATCH 04/12] reviewer addition or deletion --- web_src/js/features/repo-issue.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 5966df11e14cd..ffed12e95c9d7 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -345,16 +345,18 @@ export function initRepoIssueWipTitle() { } export async function updateIssuesMeta(url, action, issueIds, elementId) { - return $.ajax({ - type: 'POST', - url, - data: { - _csrf: csrfToken, - action, - issue_ids: issueIds, - id: elementId, - }, - }); + try { + const params = new URLSearchParams(); + params.append('action', action); + params.append('issue_ids', issueIds); + params.append('id', elementId); + const response = await POST(url, {data: params}); + if (!response.ok) { + throw new Error('Failed to update issues meta'); + } + } catch (error) { + console.error('Error:', error); + } } export function initRepoIssueComments() { From 140dbda33277f81f93c7d1ad74eb8f41702828e5 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 19:24:28 +0000 Subject: [PATCH 05/12] WIP removal button --- web_src/js/features/repo-issue.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index ffed12e95c9d7..d91121758043f 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -564,11 +564,19 @@ export function initRepoIssueWipToggle() { const title = toggleWip.getAttribute('data-title'); const wipPrefix = toggleWip.getAttribute('data-wip-prefix'); const updateUrl = toggleWip.getAttribute('data-update-url'); - await $.post(updateUrl, { - _csrf: csrfToken, - title: title?.startsWith(wipPrefix) ? title.slice(wipPrefix.length).trim() : `${wipPrefix.trim()} ${title}`, - }); - window.location.reload(); + + try { + const params = new URLSearchParams(); + params.append('title', title?.startsWith(wipPrefix) ? title.slice(wipPrefix.length).trim() : `${wipPrefix.trim()} ${title}`); + + const response = await POST(updateUrl, {data: params}); + if (!response.ok) { + throw new Error('Failed to toggle WIP status'); + } + window.location.reload(); + } catch (error) { + console.error('Error:', error); + } }); } From 42be633d466f96f631a6d9954cca2406ede23136 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 19:39:40 +0000 Subject: [PATCH 06/12] new diff code comment button --- web_src/js/features/repo-issue.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index d91121758043f..a2e1509c831b0 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -6,7 +6,7 @@ import {setFileFolding} from './file-fold.js'; import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js'; import {toAbsoluteUrl} from '../utils.js'; import {initDropzone} from './common-global.js'; -import {POST} from '../modules/fetch.js'; +import {POST, GET} from '../modules/fetch.js'; const {appSubUrl, csrfToken} = window.config; @@ -528,15 +528,20 @@ export function initRepoPullRequestReview() { const td = ntr.find(`.add-comment-${side}`); const commentCloud = td.find('.comment-code-cloud'); if (commentCloud.length === 0 && !ntr.find('button[name="pending_review"]').length) { - const html = await $.get($(this).closest('[data-new-comment-url]').attr('data-new-comment-url')); - td.html(html); - td.find("input[name='line']").val(idx); - td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed'); - td.find("input[name='path']").val(path); - - initDropzone(td.find('.dropzone')[0]); - const editor = await initComboMarkdownEditor(td.find('.combo-markdown-editor')); - editor.focus(); + try { + const response = await GET($(this).closest('[data-new-comment-url]').attr('data-new-comment-url')); + const html = await response.text(); + td.html(html); + td.find("input[name='line']").val(idx); + td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed'); + td.find("input[name='path']").val(path); + + initDropzone(td.find('.dropzone')[0]); + const editor = await initComboMarkdownEditor(td.find('.combo-markdown-editor')); + editor.focus(); + } catch (error) { + console.error('Error:', error); + } } }); } From 3a3c8fafbe1b436d84740e33a948ff12df25e17e Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 19:47:10 +0000 Subject: [PATCH 07/12] issue title edit button --- web_src/js/features/repo-issue.js | 34 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index a2e1509c831b0..265132bd8ba1d 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -606,39 +606,45 @@ export function initRepoIssueTitleEdit() { $('#edit-title').on('click', editTitleToggle); $('#cancel-edit-title').on('click', editTitleToggle); - $('#save-edit-title').on('click', editTitleToggle).on('click', function () { - const pullrequest_targetbranch_change = function (update_url) { + $('#save-edit-title').on('click', editTitleToggle).on('click', async function () { + const pullrequest_targetbranch_change = async function (update_url) { const targetBranch = $('#pull-target-branch').data('branch'); const $branchTarget = $('#branch_target'); if (targetBranch === $branchTarget.text()) { window.location.reload(); return false; } - $.post(update_url, { - _csrf: csrfToken, - target_branch: targetBranch - }).always(() => { + try { + const params = new URLSearchParams(); + params.append('target_branch', targetBranch); + await POST(update_url, {data: params}); + } catch (error) { + console.error('Error:', error); + } finally { window.location.reload(); - }); + } }; const pullrequest_target_update_url = $(this).attr('data-target-update-url'); if ($editInput.val().length === 0 || $editInput.val() === $issueTitle.text()) { $editInput.val($issueTitle.text()); - pullrequest_targetbranch_change(pullrequest_target_update_url); + await pullrequest_targetbranch_change(pullrequest_target_update_url); } else { - $.post($(this).attr('data-update-url'), { - _csrf: csrfToken, - title: $editInput.val() - }, (data) => { + try { + const params = new URLSearchParams(); + params.append('title', $editInput.val()); + const response = await POST($(this).attr('data-update-url'), {data: params}); + const data = await response.json(); $editInput.val(data.title); $issueTitle.text(data.title); if (pullrequest_target_update_url) { - pullrequest_targetbranch_change(pullrequest_target_update_url); // it will reload the window + await pullrequest_targetbranch_change(pullrequest_target_update_url); // it will reload the window } else { window.location.reload(); } - }); + } catch (error) { + console.error('Error:', error); + } } return false; }); From f67b9d4b7327bffc8a740c9ba1a527b723d6ba47 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 19:47:59 +0000 Subject: [PATCH 08/12] Remove CSRF token --- web_src/js/features/repo-issue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 265132bd8ba1d..59b16a3a78b40 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -8,7 +8,7 @@ import {toAbsoluteUrl} from '../utils.js'; import {initDropzone} from './common-global.js'; import {POST, GET} from '../modules/fetch.js'; -const {appSubUrl, csrfToken} = window.config; +const {appSubUrl} = window.config; export function initRepoIssueTimeTracking() { $(document).on('click', '.issue-add-time', () => { From c49a87b92455c1587df8517934226ceef73f0592 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 22:51:41 +0200 Subject: [PATCH 09/12] Update web_src/js/features/repo-issue.js Co-authored-by: silverwind --- web_src/js/features/repo-issue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 59b16a3a78b40..236d6b57f97b6 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -68,7 +68,7 @@ async function updateDeadline(deadlineString) { throw new Error('Invalid response'); } } catch (error) { - console.error('Error:', error); + console.error(error); $('#deadline-loader').removeClass('loading'); showElem($('#deadline-err-invalid-date')); } From 9bdc5e34494cf2cd72734eb082e759eadf119ba0 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 20:53:16 +0000 Subject: [PATCH 10/12] remove double error --- web_src/js/features/repo-issue.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 236d6b57f97b6..539e5f650d8d6 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -186,7 +186,7 @@ export function initRepoIssueCommentDelete() { $conversationHolder.remove(); } } catch (error) { - console.error('Error:', error); + console.error(error); } } return false; @@ -236,7 +236,7 @@ export function initRepoPullRequestUpdate() { try { response = await POST($this.data('do')); } catch (error) { - console.error('Error:', error); + console.error(error); } finally { $this.removeClass('loading'); } @@ -244,7 +244,7 @@ export function initRepoPullRequestUpdate() { try { data = await response?.json(); // the response is probably not a JSON } catch (error) { - console.error('Error:', error); + console.error(error); } if (data?.redirect) { window.location.href = data.redirect; @@ -291,7 +291,7 @@ export function initRepoPullRequestAllowMaintainerEdit() { throw new Error('Failed to update maintainer edit permission'); } } catch (error) { - console.error('Error:', error); + console.error(error); showTemporaryTooltip($checkbox[0], promptError); } finally { $checkbox.checkbox('set enabled'); @@ -355,7 +355,7 @@ export async function updateIssuesMeta(url, action, issueIds, elementId) { throw new Error('Failed to update issues meta'); } } catch (error) { - console.error('Error:', error); + console.error(error); } } @@ -540,7 +540,7 @@ export function initRepoPullRequestReview() { const editor = await initComboMarkdownEditor(td.find('.combo-markdown-editor')); editor.focus(); } catch (error) { - console.error('Error:', error); + console.error(error); } } }); @@ -580,7 +580,7 @@ export function initRepoIssueWipToggle() { } window.location.reload(); } catch (error) { - console.error('Error:', error); + console.error(error); } }); } @@ -619,7 +619,7 @@ export function initRepoIssueTitleEdit() { params.append('target_branch', targetBranch); await POST(update_url, {data: params}); } catch (error) { - console.error('Error:', error); + console.error(error); } finally { window.location.reload(); } @@ -643,7 +643,7 @@ export function initRepoIssueTitleEdit() { window.location.reload(); } } catch (error) { - console.error('Error:', error); + console.error(error); } } return false; From f36afb74d942102fcff7ab04289a245c336e5081 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 20:55:10 +0000 Subject: [PATCH 11/12] Change call and definition --- web_src/js/features/repo-issue.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 539e5f650d8d6..43d37663ba94d 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -344,13 +344,9 @@ export function initRepoIssueWipTitle() { }); } -export async function updateIssuesMeta(url, action, issueIds, elementId) { +export async function updateIssuesMeta(url, action, issue_ids, id) { try { - const params = new URLSearchParams(); - params.append('action', action); - params.append('issue_ids', issueIds); - params.append('id', elementId); - const response = await POST(url, {data: params}); + const response = await POST(url, {data: new URLSearchParams({action, issue_ids, id})}); if (!response.ok) { throw new Error('Failed to update issues meta'); } From 72d14d72f22f4c043f23e7b3b79ea58eb7cf1d5f Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 13 Mar 2024 22:56:19 +0200 Subject: [PATCH 12/12] Update web_src/js/features/repo-issue.js Co-authored-by: silverwind --- web_src/js/features/repo-issue.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 43d37663ba94d..01c30a9e53e57 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -611,9 +611,7 @@ export function initRepoIssueTitleEdit() { return false; } try { - const params = new URLSearchParams(); - params.append('target_branch', targetBranch); - await POST(update_url, {data: params}); + await POST(update_url, {data: new URLSearchParams({target_branch: targetBranch})}); } catch (error) { console.error(error); } finally {