Make the gitea url required
This commit is contained in:
@@ -3,4 +3,4 @@
|
||||
# Engines
|
||||
Custom search engines
|
||||
## Gitea
|
||||
Search through the hosted gitea or your own selfhosted instance. Supports a Access Token to also search private repositories.
|
||||
Search through your own selfhosted instance. Supports a Access Token to also search private repositories.
|
||||
@@ -1,6 +1,15 @@
|
||||
const HOSTED_URL = "https://gitea.com";
|
||||
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
|
||||
@@ -11,17 +20,30 @@ export default class GiteaEngine {
|
||||
giteaToken = "";
|
||||
|
||||
settingsSchema = [
|
||||
{ key: "giteaUrl", label: "Gitea Instance URL", description: "The base URL of your custom gitea instance, if empty uses the public hosted version.", type: "url", required: false },
|
||||
{ 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 || HOSTED_URL );
|
||||
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,
|
||||
@@ -29,6 +51,11 @@ export default class GiteaEngine {
|
||||
context
|
||||
) {
|
||||
try {
|
||||
configured = await this.isConfigured();
|
||||
if(!configured){
|
||||
return [];
|
||||
}
|
||||
|
||||
const doFetch = context?.fetch ?? fetch;
|
||||
|
||||
var headers = {};
|
||||
|
||||
@@ -8,9 +8,6 @@ const GiteaEngine = require('../index.js').default;
|
||||
const TEST_GITEA_URL = "https://gitea.furb.it"; // Change to your local instance if testing real connections
|
||||
const API_PATH = "/api/v1/repos/search";
|
||||
|
||||
// Create the Engine Instance
|
||||
const engine = new GiteaEngine();
|
||||
|
||||
// Mock Settings (Replace with your config or local instance URL)
|
||||
const settings = {
|
||||
giteaUrl: TEST_GITEA_URL,
|
||||
@@ -40,8 +37,65 @@ const context = {
|
||||
|
||||
// --- TEST EXECUTION ---
|
||||
|
||||
async function runTest() {
|
||||
async function runConfigTest() {
|
||||
console.log("Initializing Gitea Engine...");
|
||||
engine = new GiteaEngine();
|
||||
|
||||
//Mock settings
|
||||
empty_settings = {
|
||||
giteaUrl: "",
|
||||
accessToken: ""
|
||||
};
|
||||
|
||||
incorrect_settings = {
|
||||
giteaUrl: "this_is_not_a_url",
|
||||
accessToken: ""
|
||||
};
|
||||
|
||||
correct_settings = {
|
||||
giteaUrl: TEST_GITEA_URL,
|
||||
accessToken: ""
|
||||
// Set accessToken to a real token if you want to test private repo search
|
||||
};
|
||||
|
||||
// No config
|
||||
configured = await engine.isConfigured();
|
||||
if(configured){
|
||||
console.error("Engine is not yet configured, but claims to be");
|
||||
}
|
||||
|
||||
// Empty config
|
||||
console.log("Configuring Gitea Engine with empty url...");
|
||||
engine.configure(empty_settings);
|
||||
|
||||
configured = await engine.isConfigured();
|
||||
if(configured){
|
||||
console.error("Engine is incorrectly configured, but claims to be correct");
|
||||
}
|
||||
|
||||
// Incorrect config
|
||||
console.log("Configuring Gitea Engine with incorrect url...");
|
||||
engine.configure(incorrect_settings);
|
||||
|
||||
configured = await engine.isConfigured();
|
||||
if(configured){
|
||||
console.error("Engine is incorrectly configured, but claims to be correct");
|
||||
}
|
||||
|
||||
// Correct config
|
||||
console.log("Configuring Gitea Engine with correct settings...");
|
||||
engine.configure(correct_settings);
|
||||
|
||||
configured = await engine.isConfigured();
|
||||
if(!configured){
|
||||
console.error("Engine is correctly configured, but claims to be incorrect");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function runSearchTest() {
|
||||
console.log("Initializing Gitea Engine...");
|
||||
engine = new GiteaEngine();
|
||||
|
||||
// 1. Configure the engine
|
||||
engine.configure(settings);
|
||||
@@ -77,4 +131,5 @@ async function runTest() {
|
||||
}
|
||||
}
|
||||
|
||||
runTest().catch(console.error);
|
||||
runConfigTest().catch(console.error);
|
||||
runSearchTest().catch(console.error);
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
"path": "engines/gitea-searchengine",
|
||||
"name": "Gitea",
|
||||
"description": "Search your private gitea instance",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"screenshots": [
|
||||
"screenshots/1.png"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user