-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
230 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IGiteaResponse { | ||
data: any[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { workspace, window } from 'vscode'; | ||
|
||
interface ConfigStorage { | ||
token: string; | ||
instanceURL: string; | ||
owner: string; | ||
repo: string; | ||
sslVerify: boolean; | ||
} | ||
|
||
export interface ConfigTypes extends ConfigStorage { | ||
readonly repoApiUrl: string; | ||
} | ||
|
||
export class Config implements ConfigTypes { | ||
private get storage() { | ||
return workspace.getConfiguration('gitea', null); | ||
} | ||
|
||
private loadConfigValue<T extends keyof ConfigStorage>(configKey: T, type: 'string' | 'boolean' | 'number', acceptDetault = false): ConfigStorage[T] { | ||
if (!acceptDetault && !this.storage.has(configKey)) { | ||
window.showErrorMessage("Gitea-VSCode didn't find a required configuration value: " + configKey); | ||
throw new Error(`Failed to load configuration: "${configKey}"`); | ||
} | ||
|
||
const value = this.storage.has(configKey) | ||
? (this.storage.get(configKey) as ConfigStorage[T]) | ||
: (this.storage.inspect(configKey) as { defaultValue: ConfigStorage[T]; key: string }).defaultValue; | ||
|
||
if (typeof value === type && (type !== 'string' || (value as string).length > 0)) { | ||
return value as ConfigStorage[T]; | ||
} | ||
|
||
window.showErrorMessage('Gitea-VSCode failed to load a configuration value that is needed: ' + configKey); | ||
throw new Error(`Failed to load configuration: "gitea.${configKey}"`); | ||
} | ||
|
||
public get token() { | ||
return this.loadConfigValue('token', 'string'); | ||
} | ||
|
||
public set token(value) { | ||
this.storage.update('token', value); | ||
} | ||
|
||
public set instanceUrl(value: string) { | ||
this.storage.update('instanceURL', value); | ||
} | ||
|
||
public get instanceURL(): any { | ||
return this.loadConfigValue('instanceURL', 'string'); | ||
} | ||
|
||
public get owner() { | ||
return this.loadConfigValue('owner', 'string'); | ||
} | ||
|
||
public set owner(value) { | ||
this.storage.update('owner', value); | ||
} | ||
|
||
public get repo() { | ||
return this.loadConfigValue('repo', 'string'); | ||
} | ||
|
||
public set repo(value) { | ||
this.storage.update('repo', value); | ||
} | ||
|
||
public get repoApiUrl() { | ||
return this.instanceURL.replace(/\/$/, "") + '/api/v1/repos/' + this.owner + '/' + this.repo + '/issues'; | ||
} | ||
|
||
public set sslVerify(value) { | ||
this.storage.update('sslVerify', value); | ||
} | ||
|
||
public get sslVerify() { | ||
return this.loadConfigValue('sslVerify', 'boolean') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as vscode from 'vscode'; | ||
import * as https from 'https'; | ||
import axios from 'axios'; | ||
|
||
import { IGiteaResponse } from './IGiteaResponse'; | ||
|
||
export class GiteaConnector { | ||
private authToken: string; | ||
private ssl: boolean; | ||
|
||
public constructor(authToken: string, ssl: boolean = false) { | ||
this.authToken = authToken; | ||
this.ssl = ssl; | ||
} | ||
|
||
public async getIssues(repoUri: string, state: string, page: number = 0): Promise<IGiteaResponse> { | ||
return this.getEndpoint(`${repoUri}?state=${state}&page=${page}`); | ||
} | ||
|
||
private async getEndpoint(url: string): Promise<IGiteaResponse> { | ||
return new Promise<IGiteaResponse>((resolve, reject) => { | ||
return axios.get(url, this.requestOptions).then((data) => { | ||
resolve(data); | ||
}).catch((err) => { | ||
this.displayErrorMessage(err); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
private async postEndpoint(url: string): Promise<IGiteaResponse> { | ||
return new Promise<IGiteaResponse>((resolve, reject) => { | ||
return axios.post(url, this.requestOptions); | ||
}); | ||
} | ||
|
||
private get requestOptions(): object { | ||
const agent = new https.Agent({ | ||
rejectUnauthorized: this.ssl, | ||
}); | ||
return { | ||
headers: {Authorization: 'token ' + this.authToken}, | ||
httpsAgent: agent, | ||
}; | ||
} | ||
|
||
private displayErrorMessage(err: string) { | ||
vscode.window.showErrorMessage("Error occoured. " + err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.