// // "Current Branch" section: the pull requests and workflow runs that belong to // the branch checked out in the workspace (or to the configured branch filter). // const builders = require("./builders.js"); const config = require("../util/config.js"); const nodes = require("./nodes.js"); const { NodeProvider } = require("./provider.js"); const { node, message, errorNode } = nodes; class BranchProvider extends NodeProvider { roots() { const store = this.store; if (!store.router.baseUrls.length) { return [message("No Gitea instance configured. Open the extension preferences.")]; } if (!store.hasAnyToken) { return [message("No token stored. Run “Gitea → Set Token…” to sign in.")]; } if (!store.repos.length) { if (store.unmatchedHosts.length) return builders.unmatchedHostNodes(store); return [ message( store.loading ? "Loading…" : "No repository in this workspace points at a configured Gitea instance.", ), ]; } const filter = config.branchFilter(); const sections = store.repos.map((repo) => this.repoSection(repo, filter)); return store.repos.length === 1 ? sections[0].children : sections; } repoSection(repo, filter) { const branch = this.store.branchFor(repo); const label = filter.mode === "all" ? "all branches" : branch || "no branch (detached HEAD)"; return node("repo", repo.fullName, { identifier: `branch-repo-${repo.key}`, description: label, tooltip: `${repo.htmlUrl}\nBranch filter: ${label}`, contextValue: "repo", repo: repo, data: { repoKey: repo.key }, expanded: true, children: [this.pullRequestsFolder(repo, branch), this.runsFolder(repo, branch)], }); } pullRequestsFolder(repo, branch) { const { items, error } = this.store.pullRequestsFor(repo.key); const matching = branch ? items.filter((pull) => pull.head && pull.head.ref === branch) : items; let children; if (error) children = [errorNode(error)]; else if (!matching.length) { children = [ message( branch ? `No open pull request from ${branch}.` : "No open pull requests.", ), ]; } else { children = matching.map((pull) => builders.pullRequestNode(this.store, repo, pull)); } return node("prFolder", "Pull Requests", { identifier: `branch-prs-${repo.key}`, description: matching.length ? String(matching.length) : "", contextValue: "prFolder", repo: repo, expanded: true, children: children, }); } runsFolder(repo, branch) { const { runs, error, legacy } = this.store.runsFor(repo.key); const matching = branch ? runs.filter((run) => run.head_branch === branch) : runs; let children; if (error) children = [errorNode(error)]; else if (!matching.length) { children = [ message(branch ? `No runs for ${branch}.` : "No workflow runs."), ]; } else { children = matching.map((run) => builders.runNode(this.store, repo, run, { showBranch: !branch }), ); } return node("runsFolder", legacy ? "Recent Runs" : "Workflow Runs", { identifier: `branch-runs-${repo.key}`, description: matching.length ? String(matching.length) : "", tooltip: legacy ? "This Gitea server predates the workflow runs API; showing recent tasks instead." : "", contextValue: "runsFolder", repo: repo, expanded: true, children: children, }); } } exports.BranchProvider = BranchProvider;