|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { |
| 4 | + CurrentBranchRepoNode, |
| 5 | + getCurrentBranchWorkflowRunNodes, |
| 6 | +} from "./current-branch/currentBranchRepoNode"; |
| 7 | +import { getCurrentBranch, getGitHubContext } from "../git/repository"; |
| 8 | + |
| 9 | +import { NoRunForBranchNode } from "./current-branch/noRunForBranchNode"; |
| 10 | +import { WorkflowJobNode } from "./workflows/workflowJobNode"; |
| 11 | +import { WorkflowRunNode } from "./workflows/workflowRunNode"; |
| 12 | +import { WorkflowStepNode } from "./workflows/workflowStepNode"; |
| 13 | +import { logDebug } from "../log"; |
| 14 | + |
| 15 | +type CurrentBranchTreeNode = |
| 16 | + | CurrentBranchRepoNode |
| 17 | + | WorkflowRunNode |
| 18 | + | WorkflowJobNode |
| 19 | + | WorkflowStepNode |
| 20 | + | NoRunForBranchNode; |
| 21 | + |
| 22 | +export class CurrentBranchTreeProvider |
| 23 | + implements vscode.TreeDataProvider<CurrentBranchTreeNode> |
| 24 | +{ |
| 25 | + private _onDidChangeTreeData = |
| 26 | + new vscode.EventEmitter<CurrentBranchTreeNode | null>(); |
| 27 | + readonly onDidChangeTreeData = this._onDidChangeTreeData.event; |
| 28 | + |
| 29 | + refresh(): void { |
| 30 | + this._onDidChangeTreeData.fire(null); |
| 31 | + } |
| 32 | + |
| 33 | + getTreeItem( |
| 34 | + element: CurrentBranchTreeNode |
| 35 | + ): vscode.TreeItem | Thenable<vscode.TreeItem> { |
| 36 | + return element; |
| 37 | + } |
| 38 | + |
| 39 | + async getChildren( |
| 40 | + element?: CurrentBranchTreeNode | undefined |
| 41 | + ): Promise<CurrentBranchTreeNode[]> { |
| 42 | + if (!element) { |
| 43 | + const gitHubContext = await getGitHubContext(); |
| 44 | + if (!gitHubContext) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + if (gitHubContext.repos.length === 1) { |
| 49 | + return ( |
| 50 | + (await getCurrentBranchWorkflowRunNodes(gitHubContext.repos[0])) || [] |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (gitHubContext.repos.length > 1) { |
| 55 | + return gitHubContext.repos |
| 56 | + .map((repoContext): CurrentBranchRepoNode | undefined => { |
| 57 | + const currentBranch = getCurrentBranch(repoContext.repositoryState); |
| 58 | + if (!currentBranch) { |
| 59 | + logDebug(`Could not find current branch for ${repoContext.name}`); |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + |
| 63 | + return new CurrentBranchRepoNode(repoContext, currentBranch); |
| 64 | + }) |
| 65 | + .filter((x) => x !== undefined) as CurrentBranchRepoNode[]; |
| 66 | + } |
| 67 | + } else if (element instanceof CurrentBranchRepoNode) { |
| 68 | + return element.getRuns(); |
| 69 | + } else if (element instanceof WorkflowRunNode) { |
| 70 | + return element.getJobs(); |
| 71 | + } else if (element instanceof WorkflowJobNode) { |
| 72 | + return element.getSteps(); |
| 73 | + } |
| 74 | + |
| 75 | + return []; |
| 76 | + } |
| 77 | +} |
0 commit comments