Make the gitea url required
This commit is contained in:
@@ -3,4 +3,4 @@
|
|||||||
# Engines
|
# Engines
|
||||||
Custom search engines
|
Custom search engines
|
||||||
## Gitea
|
## 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";
|
const API_PATH = "/api/v1/repos/search";
|
||||||
|
|
||||||
|
function isValidUrl(string) {
|
||||||
|
try {
|
||||||
|
new URL(string);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default class GiteaEngine {
|
export default class GiteaEngine {
|
||||||
name = "Gitea";
|
name = "Gitea";
|
||||||
type = "web"; // web | images | videos | news | custom
|
type = "web"; // web | images | videos | news | custom
|
||||||
@@ -11,17 +20,30 @@ export default class GiteaEngine {
|
|||||||
giteaToken = "";
|
giteaToken = "";
|
||||||
|
|
||||||
settingsSchema = [
|
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 },
|
{ 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) {
|
configure(settings) {
|
||||||
// called after settings save and on server restart
|
// 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;
|
this.giteaSearchUrl = this.giteaBase + API_PATH;
|
||||||
|
|
||||||
|
if(!isValidUrl(this.giteaSearchUrl)){
|
||||||
|
this.giteaSearchUrl = "";
|
||||||
|
}
|
||||||
|
|
||||||
this.giteaToken = settings.accessToken || "";
|
this.giteaToken = settings.accessToken || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async isConfigured(){
|
||||||
|
if(this.giteaSearchUrl == ""){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
async executeSearch (
|
async executeSearch (
|
||||||
query,
|
query,
|
||||||
page = 1,
|
page = 1,
|
||||||
@@ -29,6 +51,11 @@ export default class GiteaEngine {
|
|||||||
context
|
context
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
configured = await this.isConfigured();
|
||||||
|
if(!configured){
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
const doFetch = context?.fetch ?? fetch;
|
const doFetch = context?.fetch ?? fetch;
|
||||||
|
|
||||||
var headers = {};
|
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 TEST_GITEA_URL = "https://gitea.furb.it"; // Change to your local instance if testing real connections
|
||||||
const API_PATH = "/api/v1/repos/search";
|
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)
|
// Mock Settings (Replace with your config or local instance URL)
|
||||||
const settings = {
|
const settings = {
|
||||||
giteaUrl: TEST_GITEA_URL,
|
giteaUrl: TEST_GITEA_URL,
|
||||||
@@ -40,8 +37,65 @@ const context = {
|
|||||||
|
|
||||||
// --- TEST EXECUTION ---
|
// --- TEST EXECUTION ---
|
||||||
|
|
||||||
async function runTest() {
|
async function runConfigTest() {
|
||||||
console.log("Initializing Gitea Engine...");
|
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
|
// 1. Configure the engine
|
||||||
engine.configure(settings);
|
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",
|
"path": "engines/gitea-searchengine",
|
||||||
"name": "Gitea",
|
"name": "Gitea",
|
||||||
"description": "Search your private gitea instance",
|
"description": "Search your private gitea instance",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"screenshots": [
|
"screenshots": [
|
||||||
"screenshots/1.png"
|
"screenshots/1.png"
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user