From 5971983a1f420205877289ddff87d5598270f6db Mon Sep 17 00:00:00 2001 From: Dirk Wilden Date: Mon, 24 Jan 2022 09:10:58 +0100 Subject: [PATCH 1/4] add gitlab engine --- README.md | 3 ++- config.yaml | 7 ++++++ src/engines/gitlab.ts | 54 +++++++++++++++++++++++++++++++++++++++++++ src/engines/index.ts | 2 ++ src/util.ts | 36 +++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/engines/gitlab.ts diff --git a/README.md b/README.md index f35b969..989241a 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,12 @@ The full list of supported data sources: - [Dropbox](https://www.dropbox.com/) files and folders - [Figma](https://www.figma.com/) files, projects, and teams - [GitHub](https://github.com/) PRs, issues, and repo metadata +- [GitLab](https://gitlab.com/) merge-requests - [Google Drive](https://www.google.com/drive/) docs, spreadsheets, etc. - [Google Groups](https://groups.google.com/) groups - [Greenhouse](https://www.greenhouse.io/) job posts - [Guru](https://www.getguru.com/) cards -- [Hound](https://github.com/hound-search/hound)-indexed code +- [Hound](https://github.com/hound-search/hound) indexed code - [Jenkins](https://www.jenkins.io/) job names - [Jira](https://www.atlassian.com/software/jira) issues - [Lingo](https://www.lingoapp.com/) assets diff --git a/config.yaml b/config.yaml index 8311b68..7a9d299 100644 --- a/config.yaml +++ b/config.yaml @@ -93,6 +93,13 @@ engines: # GitHub personal access token token: abcdef0123456789abcdef0123456789abcdef01 + # GitLab Merge Requests + gitlab: + # GitLab API origin (optional, defaults to https://gitlab.com) + origin: https://gitlab.com + # GitLab personal access token + token: abcdef0123456789abcdef0123456789abcdef01 + # Greenhouse job posts greenhouse: # Board token diff --git a/src/engines/gitlab.ts b/src/engines/gitlab.ts new file mode 100644 index 0000000..ab03923 --- /dev/null +++ b/src/engines/gitlab.ts @@ -0,0 +1,54 @@ +import axios, { AxiosInstance } from "axios"; +import * as marked from "marked"; + +import { getUnixTime, trimLines } from "../util"; + +let client: AxiosInstance | undefined; + +const engine: Engine = { + id: "gitlab", + init: ({ + origin = "https://gitlab.com", + token, + }: { + origin: string; + token: string; + }) => { + const axiosClient = axios.create({ + baseURL: `${origin}/api/v4`, + headers: { Authorization: `bearer ${token}` }, + }); + client = axiosClient; + }, + name: "GitLab", + search: async q => { + if (!(client)) { + throw Error("Engine not initialized"); + } + + // https://docs.gitlab.com/ee/api/merge_requests.html#list-merge-requests + const data: { + title: string, + description: string, + web_url: string, + updated_at: string + }[] = ( + await client.get("/merge_requests", { + params: { + search: q, + scope: 'all' + } + }) + ).data; + + return data + .map(mr => ({ + modified: getUnixTime(mr.updated_at), + snippet: `
${marked(trimLines(mr.description, q))}
`, + title: mr.title, + url: mr.web_url, + })); + }, +}; + +export default engine; diff --git a/src/engines/index.ts b/src/engines/index.ts index 8df0fd9..ac195b7 100644 --- a/src/engines/index.ts +++ b/src/engines/index.ts @@ -4,6 +4,7 @@ import drive from "./drive"; import dropbox from "./dropbox"; import figma from "./figma"; import github from "./github"; +import gitlab from "./gitlab"; import greenhouse from "./greenhouse"; import groups from "./groups"; import guru from "./guru"; @@ -28,6 +29,7 @@ const engines: Engine[] = [ dropbox, figma, github, + gitlab, greenhouse, groups, guru, diff --git a/src/util.ts b/src/util.ts index 8b00737..3e87ea5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -87,6 +87,42 @@ export const fuzzyIncludes = (() => { fuzzify(haystack ?? "").includes(fuzzify(needle)); })(); +/** + * Trim the result to at most 'max_rows' lines. This method is used + * to restrict the length of a search result snippet, so that the ui stays clear. + * The method tries to trim the lines in a way such that at least the first match + * to the query stays visible. + */ +export const trimLines = (result: string, q: string) :string => { + + const max_rows = 8; + const half_rows = max_rows / 2; + + let lines = result.split('\n'); + + if (lines.length > max_rows) { + + let matchIdx = lines.findIndex(line => fuzzyIncludes(line, q)); + let startIdx = 0; + + if (matchIdx > half_rows) { + startIdx = matchIdx - half_rows; + } + + let endIdx = matchIdx + half_rows; + + if (endIdx > lines.length) { + endIdx = lines.length; + startIdx = Math.max(0, endIdx - max_rows); + } + + return lines.slice(startIdx, endIdx).join('\n'); + + } else { + return result; + } +} + /** * Converts a date string such as "2020-06-30T21:06:25.166Z" to a Unix * timestamp in seconds. From 5af08f9c053b38da6622f64581ba8fa9c0aa42a2 Mon Sep 17 00:00:00 2001 From: Art Chaidarun Date: Mon, 24 Jan 2022 11:50:56 -0500 Subject: [PATCH 2/4] Format code with pre-commit --- src/engines/gitlab.ts | 30 ++++++++++++++---------------- src/util.ts | 33 +++++++++++++++------------------ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/engines/gitlab.ts b/src/engines/gitlab.ts index ab03923..dc3e541 100644 --- a/src/engines/gitlab.ts +++ b/src/engines/gitlab.ts @@ -22,32 +22,30 @@ const engine: Engine = { }, name: "GitLab", search: async q => { - if (!(client)) { + if (!client) { throw Error("Engine not initialized"); } // https://docs.gitlab.com/ee/api/merge_requests.html#list-merge-requests const data: { - title: string, - description: string, - web_url: string, - updated_at: string + description: string; + title: string; + updated_at: string; + web_url: string; }[] = ( await client.get("/merge_requests", { - params: { - search: q, - scope: 'all' - } + params: { scope: "all", search: q }, }) ).data; - return data - .map(mr => ({ - modified: getUnixTime(mr.updated_at), - snippet: `
${marked(trimLines(mr.description, q))}
`, - title: mr.title, - url: mr.web_url, - })); + return data.map(mr => ({ + modified: getUnixTime(mr.updated_at), + snippet: `
${marked( + trimLines(mr.description, q), + )}
`, + title: mr.title, + url: mr.web_url, + })); }, }; diff --git a/src/util.ts b/src/util.ts index 3e87ea5..902f846 100644 --- a/src/util.ts +++ b/src/util.ts @@ -88,40 +88,37 @@ export const fuzzyIncludes = (() => { })(); /** - * Trim the result to at most 'max_rows' lines. This method is used - * to restrict the length of a search result snippet, so that the ui stays clear. - * The method tries to trim the lines in a way such that at least the first match - * to the query stays visible. + * Trim the result to at most 'max_rows' lines. This method is used to + * restrict the length of a search result snippet, so that the UI stays clear. + * The method tries to trim the lines in a way such that at least the first + * match to the query stays visible. */ -export const trimLines = (result: string, q: string) :string => { +export const trimLines = (result: string, q: string): string => { + const maxRows = 8; + const halfRows = maxRows / 2; - const max_rows = 8; - const half_rows = max_rows / 2; - - let lines = result.split('\n'); - - if (lines.length > max_rows) { + let lines = result.split("\n"); + if (lines.length > maxRows) { let matchIdx = lines.findIndex(line => fuzzyIncludes(line, q)); let startIdx = 0; - if (matchIdx > half_rows) { - startIdx = matchIdx - half_rows; + if (matchIdx > halfRows) { + startIdx = matchIdx - halfRows; } - let endIdx = matchIdx + half_rows; + let endIdx = matchIdx + halfRows; if (endIdx > lines.length) { endIdx = lines.length; - startIdx = Math.max(0, endIdx - max_rows); + startIdx = Math.max(0, endIdx - maxRows); } - return lines.slice(startIdx, endIdx).join('\n'); - + return lines.slice(startIdx, endIdx).join("\n"); } else { return result; } -} +}; /** * Converts a date string such as "2020-06-30T21:06:25.166Z" to a Unix From cb047f759e90c82cd9a41d57ff2e2c844e1e5317 Mon Sep 17 00:00:00 2001 From: Art Chaidarun Date: Mon, 24 Jan 2022 14:07:58 -0500 Subject: [PATCH 3/4] Always show MAX_ROWS lines when possible --- src/util.ts | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/util.ts b/src/util.ts index 902f846..17ed41a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -88,36 +88,32 @@ export const fuzzyIncludes = (() => { })(); /** - * Trim the result to at most 'max_rows' lines. This method is used to + * Trim the result to at most `MAX_ROWS` lines. This method is used to * restrict the length of a search result snippet, so that the UI stays clear. * The method tries to trim the lines in a way such that at least the first * match to the query stays visible. */ export const trimLines = (result: string, q: string): string => { - const maxRows = 8; - const halfRows = maxRows / 2; + const MAX_ROWS = 8; + const halfRows = MAX_ROWS / 2; - let lines = result.split("\n"); - - if (lines.length > maxRows) { - let matchIdx = lines.findIndex(line => fuzzyIncludes(line, q)); - let startIdx = 0; - - if (matchIdx > halfRows) { - startIdx = matchIdx - halfRows; - } - - let endIdx = matchIdx + halfRows; + result = result.trim(); + const lines = result.split("\n"); + if (lines.length <= MAX_ROWS) { + return result; + } - if (endIdx > lines.length) { - endIdx = lines.length; - startIdx = Math.max(0, endIdx - maxRows); - } + const matchIdx = lines.findIndex(line => fuzzyIncludes(line, q)); + let startIdx = Math.max(0, matchIdx - halfRows); + let endIdx = Math.min(lines.length, matchIdx + halfRows); - return lines.slice(startIdx, endIdx).join("\n"); - } else { - return result; + if (startIdx === 0) { + endIdx = MAX_ROWS; + } else if (endIdx === lines.length) { + startIdx = lines.length - MAX_ROWS; } + + return lines.slice(startIdx, endIdx).join("\n"); }; /** From 48bd0ec90d601a0deb082967e11c7c910e552bf1 Mon Sep 17 00:00:00 2001 From: Art Chaidarun Date: Mon, 24 Jan 2022 14:10:39 -0500 Subject: [PATCH 4/4] Match verbs --- src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index 17ed41a..ed81c9b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -88,7 +88,7 @@ export const fuzzyIncludes = (() => { })(); /** - * Trim the result to at most `MAX_ROWS` lines. This method is used to + * Trims the result to at most `MAX_ROWS` lines. This method is used to * restrict the length of a search result snippet, so that the UI stays clear. * The method tries to trim the lines in a way such that at least the first * match to the query stays visible.