diff --git a/README.md b/README.md index e1573ae..db06fa4 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,15 @@ Otherwise do not push the .vscode folder to your repository and doublecheck this , "gitea.owner": "%OWNER%" , "gitea.repo": "%REPO_NAME%" ``` +- If your Gitea instance is on local network server then there are chances of Error 401 or 400 due to ssl/tls certificate issue is not being able to handled by the default Axios library method. In that case use following instead of above snippet. +``` + , "gitea.havingCertificateIssueOnLocalServer": true + , "gitea.host": %YOUR_GITEA_HOST% // Example: "192.168.0.99" (with quotes, without http or https) + , "gitea.port": %YOUR_GITEA_EXPOSED_PORT% // Gitea Remote Exposed Port. Example: 3100 (without quotes) + , "gitea.owner": "%OWNER%" + , "gitea.repo": "%REPO_NAME%" + , "gitea.sslVerify": false +``` ### The following details are needed diff --git a/package.json b/package.json index a7092dd..ea61f1c 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,28 @@ "description": "The remote gitea instance's url. Append base url to this string eg. http://localhost:8080 or http://localhost/gitea", "pattern": "^(https|http)://" }, + "gitea.havingCertificateIssueOnLocalServer": { + "scope": "resource", + "type": "boolean", + "default": false, + "description": "true=Try alternative connection method using https library which bypass tls certification issue on local server, false=Use default AXIOS method'." + }, + "gitea.host": { + "scope": "resource", + "type": "string", + "default": "", + "examples": [ + "example.com", + "192.168.0.99" + ], + "description": "The remote gitea instance's url without https or http" + }, + "gitea.port": { + "scope": "resource", + "type": "number", + "default": 3100, + "description": "The remote gitea instance's exposing port" + }, "gitea.owner": { "scope": "resource", "type": "string", diff --git a/src/config.ts b/src/config.ts index 72c0552..f11dcbb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,6 +3,9 @@ import { workspace, window } from 'vscode'; interface ConfigStorage { token: string; instanceURL: string; + havingCertificateIssueOnLocalServer: boolean; + host: string; + port: number; owner: string; repo: string; sslVerify: boolean; @@ -13,6 +16,7 @@ interface ConfigStorage { export interface ConfigTypes extends ConfigStorage { readonly repoApiUrl: string; + readonly endPointPath: string; } export class Config implements ConfigTypes { @@ -50,10 +54,26 @@ export class Config implements ConfigTypes { this.storage.update('instanceURL', value); } + public set host(value: string) { + this.storage.update('host', value); + } + + public set port(value: number) { + this.storage.update('port', value); + } + public get instanceURL(): any { return this.loadConfigValue('instanceURL', 'string'); } + public get host(): any { + return this.loadConfigValue('host', 'string'); + } + + public get port(): any { + return this.loadConfigValue('port', 'number'); + } + public get baseURL(): string { return this.loadConfigValue('baseURL', 'string'); } @@ -85,6 +105,20 @@ export class Config implements ConfigTypes { '/' + this.repo + '/issues'; } + public get endPointPath(): string { + return '/api/v1/repos/' + + this.owner + + '/' + this.repo + '/issues'; + } + + public set havingCertificateIssueOnLocalServer(value) { + this.storage.update('havingCertificateIssueOnLocalServer', value); + } + + public get havingCertificateIssueOnLocalServer() { + return this.loadConfigValue('havingCertificateIssueOnLocalServer', 'boolean'); + } + public set sslVerify(value) { this.storage.update('sslVerify', value); } diff --git a/src/giteaConnector.ts b/src/giteaConnector.ts index 9f44441..4474ea9 100644 --- a/src/giteaConnector.ts +++ b/src/giteaConnector.ts @@ -4,6 +4,7 @@ import axios from 'axios'; import { IGiteaResponse } from './IGiteaResponse'; import { Logger } from './logger'; +import { Config } from './config'; export class GiteaConnector { private authToken: string; @@ -15,9 +16,13 @@ export class GiteaConnector { } public async getIssues(repoUri: string, state: string, page: number = 0): Promise { - return this.getEndpoint(`${repoUri}?state=${state}&page=${page}`); + const config = new Config(); + return config.havingCertificateIssueOnLocalServer ? + this.getEndpoint2(`${repoUri}?state=${state}&page=${page}`): + this.getEndpoint(`${repoUri}?state=${state}&page=${page}`); } + /// Using AXIOS private async getEndpoint(url: string): Promise { Logger.debug('getEndpoint', 'request', {'url': url}) return new Promise((resolve, reject) => { @@ -32,6 +37,35 @@ export class GiteaConnector { }); } + /// Using https library because of self-signed certificates issue in axios, this issue was occurring when using axios with local gitea server running on docker + private async getEndpoint2(endPointPath: string): Promise { + const config = new Config(); + Logger.debug('getEndpoint', 'request', { 'url': endPointPath }); + + return new Promise(async (resolve, reject) => { + const responseData: Buffer[] = []; // Initialize as an empty array + + const req = https.request(this.requestOptions2(endPointPath), (res) => { + res.on('data', (chunk) => { + responseData.push(chunk); // Push each chunk to the array + }); + + res.on('end', () => { + const responseDataString = Buffer.concat(responseData).toString(); // Join chunks into a single string + const response: IGiteaResponse = { data: JSON.parse(responseDataString) }; // Parse the JSON data + resolve(response); + Logger.debug('getEndpoint', 'response', { 'url': config.host+ ':' + config.port + endPointPath, 'status': res.statusCode,'size': response.data.length }); + }); + }).on('error', (err) => { + this.displayErrorMessage(err.message); + Logger.log(err.message); + reject(err); + }); + + req.end(); + }); + } + private async postEndpoint(url: string): Promise { return new Promise((resolve, reject) => { return axios.post(url, this.requestOptions); @@ -48,6 +82,22 @@ export class GiteaConnector { }; } + /// Returns alternative request options for [getEndpoint2] method written above + private requestOptions2(endPointPath:string): object { + const config = new Config(); + return { + method: 'GET', + hostname: config.host, + port: config.port, + path: endPointPath, + + rejectUnauthorized: this.ssl, + headers: { + Authorization: 'token ' + this.authToken, + Accept: 'application/json;charset=utf-8' + }, + }; + } private displayErrorMessage(err: string) { vscode.window.showErrorMessage("Error occoured. " + err); } diff --git a/src/issueProvider.ts b/src/issueProvider.ts index 262b885..4542edb 100644 --- a/src/issueProvider.ts +++ b/src/issueProvider.ts @@ -30,7 +30,7 @@ export class IssueProvider implements vscode.TreeDataProvider { let page = 1; while (page < 11) { Logger.log( `Retrieve issues. State: ${this.state} - page ${page}`); - const issuesOfPage = (await giteaConnector.getIssues(config.repoApiUrl, this.state, page)).data; + const issuesOfPage = (await giteaConnector.getIssues(config.havingCertificateIssueOnLocalServer ? config.endPointPath : config.repoApiUrl, this.state, page)).data; Logger.log( `${issuesOfPage.length} issues retrieved (state: ${this.state} - page: ${page})`); issues.push(...issuesOfPage); issuesOfPage.forEach((c) => {