Skip to content

Suggestions for issues #32327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 29, 2024
Prev Previous commit
Next Next commit
get issues from api
  • Loading branch information
anbraten committed Oct 23, 2024
commit 41778d5ab9e3fe9cf80cfba10ac7bd75146b101b
14 changes: 7 additions & 7 deletions web_src/js/features/comp/TextExpander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {matchEmoji, matchMention, matchIssue} from '../../utils/match.ts';
import {emojiString} from '../emoji.ts';
import {svg} from '../../svg.ts';

type Issue = {state: 'open' | 'closed'; pull_request: {draft: boolean; merged: boolean} | null};
type Issue = {id: string; title: string; state: 'open' | 'closed'; pull_request?: {draft: boolean; merged: boolean}};
function getIssueIcon(issue: Issue) {
if (issue.pull_request !== null) {
if (issue.pull_request) {
if (issue.state === 'open') {
if (issue.pull_request.draft === true) {
return 'octicon-git-pull-request-draft'; // WIP PR
Expand All @@ -21,7 +21,7 @@ function getIssueIcon(issue: Issue) {
}

function getIssueColor(issue: Issue) {
if (issue.pull_request !== null) {
if (issue.pull_request) {
if (issue.pull_request.draft === true) {
return 'grey'; // WIP PR
} else if (issue.pull_request.merged === true) {
Expand Down Expand Up @@ -90,23 +90,23 @@ export function initTextExpander(expander) {

const ul = document.createElement('ul');
ul.classList.add('suggestions');
for (const {value, name, issue} of matches) {
for (const issue of matches) {
const li = document.createElement('li');
li.classList.add('tw-flex', 'tw-gap-2');
li.setAttribute('role', 'option');
li.setAttribute('data-value', `${key}${value}`);
li.setAttribute('data-value', `${key}${issue.id}`);

const icon = document.createElement('div');
icon.innerHTML = svg(getIssueIcon(issue), 16, ['text', getIssueColor(issue)].join(' ')).trim();
li.append(icon.firstChild);

const id = document.createElement('span');
id.classList.add('id');
id.textContent = value;
id.textContent = issue.id;
li.append(id);

const nameSpan = document.createElement('span');
nameSpan.textContent = name;
nameSpan.textContent = issue.title;
li.append(nameSpan);

ul.append(li);
Expand Down
117 changes: 12 additions & 105 deletions web_src/js/utils/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export function matchEmoji(queryText: string): string[] {
return sortAndReduce(results);
}

type Mention = {value: string; name: string; fullname: string; avatar: string};
export function matchMention(queryText: string): Mention[] {
type MentionSuggestion = {value: string; name: string; fullname: string; avatar: string};
export function matchMention(queryText: string): MentionSuggestion[] {
const query = queryText.toLowerCase();

// results is a map of weights, lower is better
const results = new Map<Mention, number>();
const results = new Map<MentionSuggestion, number>();
for (const obj of window.config.mentionValues ?? []) {
const index = obj.key.toLowerCase().indexOf(query);
if (index === -1) continue;
Expand All @@ -44,113 +44,20 @@ export function matchMention(queryText: string): Mention[] {
return sortAndReduce(results);
}

type Issue = {state: 'open' | 'closed'; pull_request: {draft: boolean; merged: boolean} | null};
type IssueMention = {value: string; name: string; issue: Issue};
export async function matchIssue(url: string, queryText: string): Promise<IssueMention[]> {
type Issue = {id: number; title: string; state: 'open' | 'closed'; pull_request?: {draft: boolean; merged: boolean}};
export async function matchIssue(url: string, queryText: string): Promise<Issue[]> {
const query = queryText.toLowerCase();

// http://localhost:3000/anbraten/test/issues/1
// http://localhost:3000/anbraten/test/compare/main...anbraten-patch-1
// TODO: support sub-path
const repository = (new URL(url)).pathname.split('/').slice(1, 3).join('/');
const issuePullRequestId = url.split('/').slice(-1)[0];
const issuePullRequestId = parseInt(url.split('/').slice(-1)[0]);

console.log('suggestions for 1', {
repository,
query,
const res = await request(`/api/v1/repos/${repository}/issues?q=${query}`, {
method: 'GET',
});

// TODO: fetch data from api
// const res = await request('/-/suggestions', {
// method: 'GET',
// data: {
// repository,
// query,
// },
// });
// console.log(await res.json());
const issues: Issue[] = await res.json();

// results is a map of weights, lower is better
const results = new Map<IssueMention, number>();
// for (const obj of window.config.mentionValues ?? []) {
// const index = obj.key.toLowerCase().indexOf(query);
// if (index === -1) continue;
// const existing = results.get(obj);
// results.set(obj, existing ? existing - index : index);
// }

results.set({
value: '28958',
name: 'Live removal of issue comments using htmx websocket',
issue: {
state: 'open',
pull_request: {
merged: false,
draft: false,
},
},
}, 0);

results.set({
value: '32234',
name: 'Calculate `PublicOnly` for org membership only once',
issue: {
state: 'closed',
pull_request: {
merged: true,
draft: false,
},
},
}, 1);

results.set({
value: '32280',
name: 'Optimize branch protection rule loading',
issue: {
state: 'open',
pull_request: {
merged: false,
draft: false,
},
},
}, 2);

results.set({
value: '32326',
name: 'Shallow Mirroring',
issue: {
state: 'open',
pull_request: null,
},
}, 3);

results.set({
value: '32248',
name: 'Make admins adhere to branch protection rules',
issue: {
state: 'closed',
pull_request: {
merged: true,
draft: false,
},
},
}, 4);

results.set({
value: '32249',
name: 'Add a way to disable branch protection rules for admins',
issue: {
state: 'closed',
pull_request: null,
},
}, 5);

// filter out current issue/pull request
for (const [key] of results.entries()) {
if (key.value === issuePullRequestId) {
results.delete(key);
break;
}
}

return sortAndReduce(results);
// filter issue with same id
return issues.filter((i) => i.id !== issuePullRequestId);
}
Loading