Skip to content

Commit

Permalink
Rework code base; #17
Browse files Browse the repository at this point in the history
  • Loading branch information
IJustDev committed Aug 16, 2020
1 parent cb4a963 commit 858b8b7
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 286 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Emojis in treeview

## [Unreleased]

- Initial release
- Rework code base


[1]: https://github.com/IJustDev/Gitea-VSCode/issues/1
[2]: https://github.com/IJustDev/Gitea-VSCode/issues/2
[2]: https://github.com/IJustDev/Gitea-VSCode/issues/2
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Gitea-VSCode",
"description": "Gitea Issue Tracker for vs-code",
"publisher": "IJustDev",
"version": "1.0.1",
"version": "1.1.0",
"engines": {
"vscode": "^1.32.0"
},
Expand Down Expand Up @@ -119,8 +119,7 @@
"vscode": "^1.1.28"
},
"dependencies": {
"axios": "^0.18.1",
"marked": "^0.6.2"
"axios": "^0.18.1"
},
"repository": {
"type": "github",
Expand Down
80 changes: 0 additions & 80 deletions src/Config.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/IGiteaResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IGiteaResponse {
data: any[];
}
81 changes: 81 additions & 0 deletions src/config.ts
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')
}
}
62 changes: 28 additions & 34 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,40 @@ import * as vscode from 'vscode';

import { showIssueHTML } from './template.html';
import { Issue } from './issue';
import { OpenIssuesProvider, ClosedIssuesProvider } from './issueProvider';
import { IssueProvider } from './issueProvider';

export function activate(context: vscode.ExtensionContext) {
let openIssues: Array<Issue> = [];
const openIssuesProvider = new OpenIssuesProvider();
const closedIssuesProvider = new ClosedIssuesProvider();

vscode.window.registerTreeDataProvider('open-issues', openIssuesProvider);
vscode.window.registerTreeDataProvider('closed-issues', closedIssuesProvider);

// TODO: Implement in next version.
// vscode.commands.registerCommand('giteaIssues.createIssue', async () => {
// const panel = vscode.window.createWebviewPanel('createIssue', 'Create an new Issue', vscode.ViewColumn.Active, {});
// panel.webview.html = "";
// });

vscode.commands.registerCommand('giteaIssues.openIssue', (issue: Issue) => {
for (let i = 0; i !== openIssues.length; i++) {
let openIssue = openIssues[i];
if (openIssue.issueId === issue.issueId) {
return;
}
}
export function showIssueInWebPanel(issue: Issue) {
const panel = vscode.window.createWebviewPanel('issue', issue.label, vscode.ViewColumn.Active, {});
panel.webview.html = showIssueHTML(issue);
openIssues.push(issue);
panel.onDidDispose((event) => {
for (let i = 0; i !== openIssues.length; i++) {
let openIssue = openIssues[i];
if (openIssue.issueId === issue.issueId) {
openIssues.splice(openIssues.indexOf(issue), 1);
return panel;
}

export function activate(context: vscode.ExtensionContext) {
// Array of issues; This is used to determine whether a issue is already open
// in a tab or not.
let openIssues: Array<Issue> = [];
const openIssuesProvider = new IssueProvider("open");
const closedIssuesProvider = new IssueProvider("closed");

vscode.window.registerTreeDataProvider('open-issues', openIssuesProvider);
vscode.window.registerTreeDataProvider('closed-issues', closedIssuesProvider);

vscode.commands.registerCommand('giteaIssues.openIssue', (issue: Issue) => {
const issueOpenable = openIssues.find((c) => c.issueId === issue.issueId) === undefined;

if (issueOpenable) {
const panel = showIssueInWebPanel(issue);
openIssues.push(issue);
panel.onDidDispose((event) => {
openIssues.splice(openIssues.indexOf(issue), 1);
});
}
}
});
});

vscode.commands.registerCommand('giteaIssues.refreshIssues', () => {
openIssuesProvider.refresh();
closedIssuesProvider.refresh();
});
vscode.commands.registerCommand('giteaIssues.refreshIssues', () => {
openIssuesProvider.refresh();
closedIssuesProvider.refresh();
});
}

export function deactivate() {}
50 changes: 50 additions & 0 deletions src/giteaConnector.ts
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);
}
}
4 changes: 2 additions & 2 deletions src/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export class Issue extends TreeItem {
public readonly label: string,
public issueId: number,
public body: string,
public issueState: string,
public state: string,
public assignee: string,
public creator: string,
public labels: Label[],
public collapsibleState: TreeItemCollapsibleState,
public readonly command?: Command
public command?: Command
) {
super(label, collapsibleState);
}
Expand Down

0 comments on commit 858b8b7

Please sign in to comment.