From 9a4e80d371a46219ecb32a827a48161387c87f95 Mon Sep 17 00:00:00 2001 From: Izon Thomaz Mielke Date: Fri, 7 Jun 2024 12:08:41 +0930 Subject: [PATCH] feat(custom): Add Jira and Gitlab custom scripts (#2306) --- src/content/_custom_nf_gitlab.js | 142 ++++++++++++++++++++++++ src/content/_custom_nf_jira.js | 183 +++++++++++++++++++++++++++++++ src/custom-scripts.js | 8 ++ 3 files changed, 333 insertions(+) create mode 100644 src/content/_custom_nf_gitlab.js create mode 100644 src/content/_custom_nf_jira.js diff --git a/src/content/_custom_nf_gitlab.js b/src/content/_custom_nf_gitlab.js new file mode 100644 index 000000000..1b88f61e4 --- /dev/null +++ b/src/content/_custom_nf_gitlab.js @@ -0,0 +1,142 @@ +//// START GITLAB +togglbutton.render( + '.issue-details .detail-page-description:not(.toggl)', + { observe: !0 }, + function (t) { + const n = [ + [a()] + .filter(Boolean) + .map(function (r) { + return '#' + r; + }) + .join(''), + i(t), + ] + .filter(Boolean) + .join(' '); + o($('.detail-page-header-actions'), n, !0), o($('.time-tracker'), n); + } +); +togglbutton.render( + '.merge-request > .detail-page-header:not(.toggl)', + { observe: !0 }, + function (t) { + const n = [ + [a()] + .filter(Boolean) + .map(function (r) { + return 'MR' + r + '::'; + }) + .join(''), + i(t), + ] + .filter(Boolean) + .join(' '); + o($('.detail-page-header-actions'), n, !0), o($('.time-tracker'), n); + } +); +function o(t, e, n = !1) { + const r = togglbutton.createTimerLink({ + className: 'gitlab', + description: e, + tags: l, + taskId: (o, i) => { + return extractTaskId(o, i); + }, + projectName: (o, i) => { + return extractProjectName(o, i); + }, + }); + n ? t.parentElement.insertBefore(r, t) : t.parentElement.appendChild(r, t); +} +function i(t) { + const e = t.querySelector('.title'); + return e ? e.textContent.trim() : ''; +} +function a() { + const t = window.location.pathname, + e = /-\/(issues|merge_requests)\/(?\d+)/; + return (e.test(t) ? t.match(e) : { groups: { id: '' } }).groups.id; +} +function s() { + const t = + $('.title .project-item-select-holder') || + $('.breadcrumbs-list li:nth-last-child(3) .breadcrumb-item-text'); + return t ? t.textContent.trim() : ''; +} +function l() { + const t = document.querySelectorAll('div.labels span[data-qa-label-name]'); + if (!t) return []; + const e = []; + for (const n of Object.values(t)) { + const r = n.getAttribute('data-qa-label-name'); + e.push(r); + } + return e; +} + +////END GITLAB + +////START HELPER +const ExtractNFGitlabTogglTaskId = () => { + debugger; + let togglTaskId = null; + let tags = l(); + const GITLAB_LABEL_REGEX = /togg(e|)l(::|:)/gim; + const REMOVE_TRALING_LABEL_DESCRIPTION = / .*/; + + debugger; + if (Array.isArray(tags)) { + tags.forEach((element) => { + if (element.match(GITLAB_LABEL_REGEX)) { + togglTaskId = element + .replace(GITLAB_LABEL_REGEX, '') + .toUpperCase() + .replace(REMOVE_TRALING_LABEL_DESCRIPTION, ''); + return; + } + }); + } + return togglTaskId; +}; + +function extractTaskId(projects, tasks) { + debugger; + let nfTogglTask = ExtractNFGitlabTogglTaskId(); + if (nfTogglTask === null) { + return {}; + } + + const n = Object.keys(tasks).filter((o) => + tasks[o].name.startsWith(nfTogglTask) + ), + s = n.length > 0 ? tasks[n[0]].id : {}; + return s; +} + +function extractTaskProjectId(nfTogglTask, projects, tasks) { + if (nfTogglTask === null) { + return null; + } + + const n = Object.keys(tasks).filter((o) => + tasks[o].name.startsWith(nfTogglTask) + ), + s = n.length > 0 ? tasks[n[0]].project_id : null; + console.log(s); + return s; +} + +function extractProjectName(projects, tasks) { + debugger; + let nfTogglTask = ExtractNFGitlabTogglTaskId(); + if (nfTogglTask === null) { + return null; + } + + let projectID = extractTaskProjectId(nfTogglTask, projects, tasks); + const n = Object.keys(projects).filter((o) => projects[o].id === projectID), + s = n.length > 0 ? projects[n[0]].name : null; + console.log(s); + return s; +} diff --git a/src/content/_custom_nf_jira.js b/src/content/_custom_nf_jira.js new file mode 100644 index 000000000..2ae38b023 --- /dev/null +++ b/src/content/_custom_nf_jira.js @@ -0,0 +1,183 @@ +/// START JIRA + +togglbutton.render( + '#ghx-detail-view [spacing] h1:not(.toggl)', + { observe: !0 }, + function () { + debugger; + ({}).DEBUG && console.info('\u{1F3C3} "Jira 2017 sidebar" rendering'); + const n = $('#ghx-detail-view'), + t = createTag('div', 'jira-ghx-toggl-button'), + i = $('[spacing] h1', n), + e = $('[spacing] a', n), + o = $('.bgdPDV'); + let r = i.textContent; + e !== null && (r = e.textContent + ' ' + r); + const a = togglbutton.createTimerLink({ + className: 'jira2017', + description: r, + buttonType: 'minimal', + taskId: (o, i) => { + return extractTaskId(ExtractNFJiraTogglTaskId(), o, i); + }, + projectName: (o, i) => { + return extractProjectName(ExtractNFJiraTogglTaskId(), o, i); + }, + }); + t.appendChild(a), e.parentNode.appendChild(t); + } +); + +togglbutton.render('#title-heading:not(.toggl)', { observe: !0 }, function (n) { + const i = $('[id="title-text"]', n).textContent.trim(), + e = togglbutton.createTimerLink({ + className: 'confluence', + description: i, + taskId: (o, i) => { + return extractTaskId(ExtractNFJiraTogglTaskId(), o, i); + }, + projectName: (o, i) => { + return extractProjectName(ExtractNFJiraTogglTaskId(), o, i); + }, + }); + $('#title-text').appendChild(e); +}); + +//JIRA Issue View +togglbutton.render( + '.issue-header-content:not(.toggl)', + { observe: !0 }, + function (n) { + debugger; + ({}).DEBUG && console.info('\u{1F3C3} "Jira 2017 issue page" rendering'); + const t = $('#key-val', n), + i = $('#summary-val', n) || ''; + let e = $('.bgdPDV'), + o; + i && (o = i.textContent.trim()), + t !== null && (o && (o = ' ' + o), (o = t.textContent + o)), + e === null && + (e = $( + '[data-test-id="navigation-apps.project-switcher-v2"] button > div:nth-child(2) > div' + )), + e === null && (e = $('#project-name-val')); + + const r = togglbutton.createTimerLink({ + className: 'jira2017', + description: o, + taskId: (o, i) => { + return extractTaskId(ExtractNFJiraTogglTaskId(), o, i); + }, + projectName: (o, i) => { + return extractProjectName(ExtractNFJiraTogglTaskId(), o, i); + }, + }); + r.style.marginLeft = '8px'; + const a = + ($('.issue-link') || {}).parentElement || + ($('.aui-nav li') || {}).lastElementChild; + a && a.appendChild(r); + } +); + +//JIRA Board Detail +togglbutton.render( + '#ghx-detail-issue:not(.toggl)', + { observe: !0 }, + function (n) { + debugger; + const t = createTag('div', 'ghx-toggl-button'), + i = $('[data-field-id="summary"]', n), + e = $('.ghx-fieldname-issuekey a'), + o = $('.ghx-project', n); + let r = i.textContent; + e !== null && (r = e.textContent + ' ' + r); + const a = togglbutton.createTimerLink({ + className: 'jira', + description: r, + taskId: (o, i) => { + return extractTaskId(ExtractNFJiraTogglTaskId(), o, i); + }, + projectName: (o, i) => { + return extractProjectName(ExtractNFJiraTogglTaskId(), o, i); + }, + }); + t.appendChild(a), $('#ghx-detail-head').appendChild(t); + } +); + +// JIRA Issue List Issue View +togglbutton.render( + '.issue-header-content:not(.toggl)', + { observe: !0 }, + function (n) { + debugger; + let t; + const i = $('#key-val', n), + e = $('#summary-val', n) || '', + o = $('#project-name-val', n); + e && (t = e.textContent), + i !== null && (t && (t = ' ' + t), (t = i.textContent + t)); + const r = togglbutton.createTimerLink({ + className: 'jira', + description: t, + taskId: (o, i) => { + return extractTaskId(ExtractNFJiraTogglTaskId(), o, i); + }, + projectName: (o, i) => { + return extractProjectName(ExtractNFJiraTogglTaskId(), o, i); + }, + }), + a = createTag('ul', 'toolbar-group'), + s = createTag('li', 'toolbar-item'); + s.appendChild(r), a.appendChild(s), $('.toolbar-split-left').appendChild(a); + } +); +//// END JIRA + +////START HELPER +const ExtractNFJiraTogglTaskId = () => { + let e = ''; + const t = document.querySelector('#customfield_10206-val'); + let ret = + (t && (e && (e += ' '), (e += t.textContent.trim())), + e.split('-')[0].trim()); + return ret.length > 0 ? ret : null; +}; + +function extractTaskId(nfTogglTask, projects, tasks) { + if (nfTogglTask === null) { + return {}; + } + const n = Object.keys(tasks).filter((o) => + tasks[o].name.startsWith(nfTogglTask) + ), + s = n.length > 0 ? tasks[n[0]].id : {}; + return s; +} + +function extractTaskProjectId(nfTogglTask, projects, tasks) { + debugger; + if (nfTogglTask === null) { + return null; + } + const n = Object.keys(tasks).filter((o) => + tasks[o].name.startsWith(nfTogglTask) + ), + s = n.length > 0 ? tasks[n[0]].project_id : null; + console.log(s); + return s; +} + +function extractProjectName(nfTogglTask, projects, tasks) { + if (nfTogglTask === null) { + return null; + } + let projectID = extractTaskProjectId(nfTogglTask, projects, tasks); + const n = Object.keys(projects).filter((o) => projects[o].id === projectID), + s = n.length > 0 ? projects[n[0]].name : null; + console.log(s); + return s; +} + +////END HELPER diff --git a/src/custom-scripts.js b/src/custom-scripts.js index add8bcbec..1ed21a6bc 100644 --- a/src/custom-scripts.js +++ b/src/custom-scripts.js @@ -3,4 +3,12 @@ export const CustomScripts = { name: 'Custom Script For XYZ', file: '_custom_SampleScript.js', }, + 'nfJiraScript': { + name: 'Custom Jira Script', + file: '_custom_nf_jira.js', + }, + 'nfGitlabScript': { + name: 'Custom Gitlab Script', + file: '_custom_nf_gitlab.js', + }, } \ No newline at end of file