92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
const DEFAULT_URL = "";
|
|
const API_PATH = "/api/v1/repos/search";
|
|
|
|
function isValidUrl(string) {
|
|
try {
|
|
new URL(string);
|
|
return true;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export default class GiteaEngine {
|
|
name = "Gitea";
|
|
type = "web"; // web | images | videos | news | custom
|
|
bangShortcut = "gitea";
|
|
|
|
giteaSearchUrl = "";
|
|
giteaBase = "";
|
|
giteaToken = "";
|
|
|
|
settingsSchema = [
|
|
{ key: "giteaUrl", label: "Gitea Instance URL", description: "The base URL of your custom gitea instance.", type: "url", required: true },
|
|
{ key: "accessToken", label: "Access Token", description: "Access Token to the gitea instance, allows searching for private repositories. Requires repository read permission.", type: "password", required: false },
|
|
]
|
|
|
|
configure(settings) {
|
|
// called after settings save and on server restart
|
|
this.giteaBase = (settings.giteaUrl || DEFAULT_URL );
|
|
this.giteaSearchUrl = this.giteaBase + API_PATH;
|
|
|
|
if(!isValidUrl(this.giteaSearchUrl)){
|
|
this.giteaSearchUrl = "";
|
|
}
|
|
|
|
this.giteaToken = settings.accessToken || "";
|
|
}
|
|
|
|
async isConfigured(){
|
|
if(this.giteaSearchUrl == ""){
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
async executeSearch (
|
|
query,
|
|
page = 1,
|
|
timeFilter,
|
|
context
|
|
) {
|
|
try {
|
|
configured = await this.isConfigured();
|
|
if(!configured){
|
|
return [];
|
|
}
|
|
|
|
const doFetch = context?.fetch ?? fetch;
|
|
|
|
var headers = {};
|
|
|
|
if(this.giteaToken.length > 0){
|
|
headers = { "Authorization": `token ${this.giteaToken}` };
|
|
}
|
|
|
|
const response = await doFetch(`${this.giteaSearchUrl}?q=${encodeURIComponent(query)}&page=${encodeURIComponent(page)}`, {headers: headers})
|
|
context?.sentinel?.(response, this.name)
|
|
const response_json = await response.json()
|
|
|
|
if (response_json.ok != true) {
|
|
throw context.engineError(
|
|
"Gitea failure",
|
|
`${this.name} returned a nok response.`,
|
|
{ engine: this.name },
|
|
);
|
|
}
|
|
|
|
return response_json.data.map((r) => ({
|
|
title: r.full_name,
|
|
url: r.html_url,
|
|
snippet: r.description ?? "",
|
|
source: this.name,
|
|
...(r.avatar_url ? { thumbnail: r.avatar_url } : {}),
|
|
}));
|
|
} catch (e) {
|
|
if (e?.name === "SentinelBreach") throw e
|
|
return []
|
|
}
|
|
}
|
|
|
|
} |