]> source.dussan.org Git - gitea.git/commitdiff
Fix context popup error (#17398)
authorwxiaoguang <wxiaoguang@gmail.com>
Fri, 22 Oct 2021 14:34:01 +0000 (22:34 +0800)
committerGitHub <noreply@github.com>
Fri, 22 Oct 2021 14:34:01 +0000 (22:34 +0800)
* Fix context popup error

web_src/js/features/contextpopup.js
web_src/js/utils.js
web_src/js/utils.test.js

index b97f77275aeb7eab82ce3630640a00e970f1021e..aa73aad2410dc2192d1c9d0530bb64ca0c30fbfc 100644 (file)
@@ -1,6 +1,7 @@
 import Vue from 'vue';
 
 import ContextPopup from '../components/ContextPopup.vue';
+import {parseIssueHref} from '../utils.js';
 
 export default function initContextPopups() {
   const refIssues = $('.ref-issue');
@@ -10,7 +11,9 @@ export default function initContextPopups() {
     if ($(this).hasClass('ref-external-issue')) {
       return;
     }
-    const [index, _issues, repo, owner] = $(this).attr('href').replace(/[#?].*$/, '').split('/').reverse();
+
+    const {owner, repo, index} = parseIssueHref($(this).attr('href'));
+    if (!owner) return;
 
     const el = document.createElement('div');
     el.className = 'ui custom popup hidden';
index b555650bc535f3063e8223a9b8ba47f476615ef9..6310b2cb9984c06da5a59aae46f140ee2f382d4e 100644 (file)
@@ -57,3 +57,9 @@ export function mqBinarySearch(feature, minValue, maxValue, step, unit) {
   }
   return mqBinarySearch(feature, minValue, mid - step, step, unit); // feature is < mid
 }
+
+export function parseIssueHref(href) {
+  const path = (href || '').replace(/[#?].*$/, '');
+  const [_, owner, repo, type, index] = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path) || [];
+  return {owner, repo, type, index};
+}
index 859046c87c9763bc928e10c9117743710a77c2fa..3f6f92107924972b875718f5075a7a903f6b957a 100644 (file)
@@ -1,5 +1,5 @@
 import {
-  basename, extname, isObject, uniq, stripTags, joinPaths,
+  basename, extname, isObject, uniq, stripTags, joinPaths, parseIssueHref,
 } from './utils.js';
 
 test('basename', () => {
@@ -66,3 +66,21 @@ test('uniq', () => {
 test('stripTags', () => {
   expect(stripTags('<a>test</a>')).toEqual('test');
 });
+
+test('parseIssueHref', () => {
+  expect(parseIssueHref('/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
+  expect(parseIssueHref('/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
+  expect(parseIssueHref('/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('https://example.com/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('https://example.com/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
+  expect(parseIssueHref('https://example.com/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('https://example.com/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
+  expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
+  expect(parseIssueHref('')).toEqual({owner: undefined, repo: undefined, type: undefined, index: undefined});
+});