Gitea-VSCode/src/issueProvider.ts

80 lines
2.8 KiB
TypeScript
Raw Normal View History

import * as vscode from 'vscode';
import { Issue } from './issue';
2020-08-16 20:26:14 +02:00
import { Config } from './config';
import { GiteaConnector } from './giteaConnector';
2019-04-09 14:37:58 +02:00
2020-08-16 20:26:14 +02:00
export class IssueProvider implements vscode.TreeDataProvider<Issue> {
private _onDidChangeTreeData: vscode.EventEmitter<Issue | undefined | null | void> = new vscode.EventEmitter<Issue | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<Issue | undefined | null | void> = this._onDidChangeTreeData.event;
2020-08-16 20:26:14 +02:00
private state: string;
private issueList: Issue[] = [];
2020-08-16 20:26:14 +02:00
constructor(state: string) {
this.state = state;
}
2020-08-16 20:26:14 +02:00
public getTreeItem(element: Issue): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element;
}
2020-08-16 20:26:14 +02:00
public async getIssuesAsync() {
this.issueList = [];
const config = new Config();
const giteaConnector = new GiteaConnector(config.token, config.sslVerify);
const issues = [];
let page = 1;
2020-08-16 20:26:14 +02:00
while (page < 11) {
const issuesOfPage = (await giteaConnector.getIssues(config.repoApiUrl, this.state, page)).data;
issues.push(...issuesOfPage);
issuesOfPage.forEach((c) => {
2020-08-19 08:00:27 +02:00
c.label = `#${c.number} - ${c.title}`;
2020-08-16 20:26:14 +02:00
c.issueId = c.number;
c.assignee = c.assignee === null ? 'Nobody' : c.assignee;
c.creator = c.user.login;
});
page++;
if (issues.length < 10) {
break;
}
}
this.issueList = issues as Issue[];
this.issueList.forEach((issue: Issue) => {
issue.command = {
command: 'giteaIssues.openIssue',
title: '',
arguments: [issue],
};
issue.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
});
2019-04-09 14:37:58 +02:00
}
2020-08-16 20:26:14 +02:00
public async refresh() {
await this.getIssuesAsync();
this._onDidChangeTreeData.fire();
2019-04-09 14:37:58 +02:00
}
2020-08-16 20:26:14 +02:00
public getChildren(element?: Issue): vscode.ProviderResult<any[]> {
return this.createChildNodes(element, this.issueList);
2019-04-12 08:09:19 +02:00
}
2020-08-16 20:26:14 +02:00
private createChildNodes(element: Issue | undefined, issues: Issue[]) {
for (const issue of issues) {
if (element === issue) {
let childItems: vscode.TreeItem[] = [
new vscode.TreeItem('Assignee - ' + element.assignee, vscode.TreeItemCollapsibleState.None),
new vscode.TreeItem('State - ' + element.state, vscode.TreeItemCollapsibleState.None),
new vscode.TreeItem('ID - ' + element.issueId, vscode.TreeItemCollapsibleState.None),
new vscode.TreeItem('From - ' + element.creator, vscode.TreeItemCollapsibleState.None),
];
return Promise.resolve(childItems);
}
}
return issues;
2019-04-12 08:09:19 +02:00
}
}
2020-08-16 20:26:14 +02:00