Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +16,7 @@ interface ConfigStorage {

export interface ConfigTypes extends ConfigStorage {
readonly repoApiUrl: string;
readonly endPointPath: string;
}

export class Config implements ConfigTypes {
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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);
}
Expand Down
52 changes: 51 additions & 1 deletion src/giteaConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,9 +16,13 @@ export class GiteaConnector {
}

public async getIssues(repoUri: string, state: string, page: number = 0): Promise<IGiteaResponse> {
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<IGiteaResponse> {
Logger.debug('getEndpoint', 'request', {'url': url})
return new Promise<IGiteaResponse>((resolve, reject) => {
Expand All @@ -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<IGiteaResponse> {
const config = new Config();
Logger.debug('getEndpoint', 'request', { 'url': endPointPath });

return new Promise<IGiteaResponse>(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<IGiteaResponse> {
return new Promise<IGiteaResponse>((resolve, reject) => {
return axios.post(url, this.requestOptions);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/issueProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class IssueProvider implements vscode.TreeDataProvider<Issue> {
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) => {
Expand Down