|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { TreeDecorationProvider } from './treeDecorationProviders'; |
| 8 | +import { createCommitsNodeUri, fromCommitsNodeUri, Schemes } from '../common/uri'; |
| 9 | +import { FolderRepositoryManager } from '../github/folderRepositoryManager'; |
| 10 | +import { PullRequestModel } from '../github/pullRequestModel'; |
| 11 | +import { RepositoriesManager } from '../github/repositoriesManager'; |
| 12 | + |
| 13 | +export class CommitsDecorationProvider extends TreeDecorationProvider { |
| 14 | + |
| 15 | + constructor(private readonly _repositoriesManager: RepositoriesManager) { |
| 16 | + super(); |
| 17 | + } |
| 18 | + |
| 19 | + registerPullRequestPropertyChangedListeners(_folderManager: FolderRepositoryManager, model: PullRequestModel): vscode.Disposable { |
| 20 | + return model.onDidChange(e => { |
| 21 | + if (e.timeline) { |
| 22 | + // Timeline changed, which may include new commits, so update the decoration |
| 23 | + const uri = createCommitsNodeUri(model.remote.owner, model.remote.repositoryName, model.number); |
| 24 | + this._onDidChangeFileDecorations.fire(uri); |
| 25 | + } |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + provideFileDecoration( |
| 30 | + uri: vscode.Uri, |
| 31 | + _token: vscode.CancellationToken, |
| 32 | + ): vscode.ProviderResult<vscode.FileDecoration> { |
| 33 | + if (uri.scheme !== Schemes.CommitsNode) { |
| 34 | + return undefined; |
| 35 | + } |
| 36 | + |
| 37 | + const params = fromCommitsNodeUri(uri); |
| 38 | + if (!params) { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + |
| 42 | + const folderManager = this._repositoriesManager.getManagerForRepository(params.owner, params.repo); |
| 43 | + |
| 44 | + if (folderManager) { |
| 45 | + const repo = folderManager.findExistingGitHubRepository({ owner: params.owner, repositoryName: params.repo }); |
| 46 | + if (repo) { |
| 47 | + const pr = repo.getExistingPullRequestModel(params.prNumber); |
| 48 | + if (pr) { |
| 49 | + const commitsCount = pr.item.commits.length; |
| 50 | + return { |
| 51 | + badge: commitsCount.toString(), |
| 52 | + tooltip: vscode.l10n.t('{0} commits', commitsCount) |
| 53 | + }; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return undefined; |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments