Skip to content

Commit 4ba7f23

Browse files
Uses command message node instead of prompt for missing remote
1 parent 2d0caf4 commit 4ba7f23

File tree

7 files changed

+79
-19
lines changed

7 files changed

+79
-19
lines changed

contributions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,6 +4804,11 @@
48044804
]
48054805
}
48064806
},
4807+
"gitlens.views.addPullRequestRemote": {
4808+
"label": "Add Pull Request Remote",
4809+
"icon": "$(add)",
4810+
"enablement": "!operationInProgress"
4811+
},
48074812
"gitlens.views.addRemote": {
48084813
"label": "Add Remote...",
48094814
"icon": "$(add)",

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7629,6 +7629,12 @@
76297629
"title": "Add Co-authors...",
76307630
"icon": "$(person-add)"
76317631
},
7632+
{
7633+
"command": "gitlens.views.addPullRequestRemote",
7634+
"title": "Add Pull Request Remote",
7635+
"icon": "$(add)",
7636+
"enablement": "!operationInProgress"
7637+
},
76327638
{
76337639
"command": "gitlens.views.addRemote",
76347640
"title": "Add Remote...",
@@ -11330,6 +11336,10 @@
1133011336
"command": "gitlens.views.addAuthors",
1133111337
"when": "false"
1133211338
},
11339+
{
11340+
"command": "gitlens.views.addPullRequestRemote",
11341+
"when": "false"
11342+
},
1133311343
{
1133411344
"command": "gitlens.views.addRemote",
1133511345
"when": "false"

src/constants.commands.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export type ContributedCommands =
176176
| 'gitlens.views.addAuthor'
177177
| 'gitlens.views.addAuthor.multi'
178178
| 'gitlens.views.addAuthors'
179+
| 'gitlens.views.addPullRequestRemote'
179180
| 'gitlens.views.addRemote'
180181
| 'gitlens.views.applyChanges'
181182
| 'gitlens.views.associateIssueWithBranch'

src/constants.commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ type InternalPlusCommands =
182182
| 'gitlens.plus.showPlans'
183183
| 'gitlens.plus.validate';
184184

185+
type InternalPullRequestViewCommands = 'gitlens.views.addPullRequestRemote';
186+
185187
type InternalScmGroupedViewCommands =
186188
| 'gitlens.views.scm.grouped.welcome.dismiss'
187189
| 'gitlens.views.scm.grouped.welcome.restore';
@@ -221,6 +223,7 @@ type InternalGlCommands =
221223
| InternalHomeWebviewViewCommands
222224
| InternalLaunchPadCommands
223225
| InternalPlusCommands
226+
| InternalPullRequestViewCommands
224227
| InternalScmGroupedViewCommands
225228
| InternalSearchAndCompareViewCommands
226229
| InternalTimelineWebviewViewCommands

src/git/utils/-webview/pullRequest.utils.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createRevisionRange } from '../revision.utils';
1010
export async function ensurePullRequestRefs(
1111
pr: PullRequest,
1212
repo: Repository,
13-
options?: { promptMessage?: string },
13+
options?: { silent?: true; promptMessage?: never } | { silent?: never; promptMessage?: string },
1414
refs?: PullRequestComparisonRefs,
1515
): Promise<LeftRightCommitCountResult | undefined> {
1616
if (pr.refs == null) return undefined;
@@ -32,7 +32,7 @@ export async function ensurePullRequestRefs(
3232
export async function ensurePullRequestRemote(
3333
pr: PullRequest,
3434
repo: Repository,
35-
options?: { promptMessage?: string },
35+
options?: { silent?: true; promptMessage?: never } | { silent?: never; promptMessage?: string },
3636
): Promise<boolean> {
3737
const identity = getRepositoryIdentityForPullRequest(pr);
3838
if (identity.remote.url == null) return false;
@@ -51,20 +51,22 @@ export async function ensurePullRequestRemote(
5151

5252
const confirm = { title: 'Add Remote' };
5353
const cancel = { title: 'Cancel', isCloseAffordance: true };
54-
const result = await window.showInformationMessage(
55-
`${
56-
options?.promptMessage ?? `Unable to find a remote for PR #${pr.id}.`
57-
}\nWould you like to add a remote for '${identity.provider.repoDomain}?`,
58-
{ modal: true },
59-
confirm,
60-
cancel,
61-
);
62-
63-
if (result === confirm) {
64-
await repo.git
65-
.remotes()
66-
.addRemoteWithResult?.(identity.provider.repoDomain, identity.remote.url, { fetch: true });
67-
return true;
54+
if (!options?.silent) {
55+
const result = await window.showInformationMessage(
56+
`${
57+
options?.promptMessage ?? `Unable to find a remote for PR #${pr.id}.`
58+
}\nWould you like to add a remote for '${identity.provider.repoDomain}?`,
59+
{ modal: true },
60+
confirm,
61+
cancel,
62+
);
63+
64+
if (result === confirm) {
65+
await repo.git
66+
.remotes()
67+
.addRemoteWithResult?.(identity.provider.repoDomain, identity.remote.url, { fetch: true });
68+
return true;
69+
}
6870
}
6971

7072
return false;

src/views/nodes/pullRequestNode.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ import type { GitBranchReference } from '../../git/models/reference';
77
import type { Repository } from '../../git/models/repository';
88
import { getAheadBehindFilesQuery, getCommitsQuery } from '../../git/queryResults';
99
import { getIssueOrPullRequestMarkdownIcon, getIssueOrPullRequestThemeIcon } from '../../git/utils/-webview/icons';
10-
import { ensurePullRequestRefs, getOrOpenPullRequestRepository } from '../../git/utils/-webview/pullRequest.utils';
11-
import { getComparisonRefsForPullRequest } from '../../git/utils/pullRequest.utils';
10+
import {
11+
ensurePullRequestRefs,
12+
ensurePullRequestRemote,
13+
getOrOpenPullRequestRepository,
14+
} from '../../git/utils/-webview/pullRequest.utils';
15+
import {
16+
getComparisonRefsForPullRequest,
17+
getRepositoryIdentityForPullRequest,
18+
} from '../../git/utils/pullRequest.utils';
1219
import { createRevisionRange } from '../../git/utils/revision.utils';
20+
import { createCommand } from '../../system/-webview/command';
1321
import { pluralize } from '../../system/string';
1422
import type { ViewsWithCommits } from '../viewBase';
1523
import { CacheableChildrenViewNode } from './abstract/cacheableChildrenViewNode';
1624
import type { ClipboardType, ViewNode } from './abstract/viewNode';
1725
import { ContextValues, getViewNodeId } from './abstract/viewNode';
1826
import { CodeSuggestionsNode } from './codeSuggestionsNode';
19-
import { MessageNode } from './common';
27+
import { CommandMessageNode, MessageNode } from './common';
2028
import { ResultsCommitsNode } from './resultsCommitsNode';
2129
import { ResultsFilesNode } from './resultsFilesNode';
2230

@@ -159,6 +167,23 @@ export async function getPullRequestChildren(
159167

160168
const repoPath = repo.path;
161169
const refs = getComparisonRefsForPullRequest(repoPath, pullRequest.refs!);
170+
const identity = getRepositoryIdentityForPullRequest(pullRequest);
171+
if (!(await ensurePullRequestRemote(pullRequest, repo, { silent: true }))) {
172+
return [
173+
new CommandMessageNode(
174+
view,
175+
parent,
176+
createCommand<[ViewNode, PullRequest, Repository]>(
177+
'gitlens.views.addPullRequestRemote',
178+
'Add Pull Request Remote...',
179+
parent,
180+
pullRequest,
181+
repo,
182+
),
183+
`Missing remote '${identity.provider.repoDomain}'. Click to add.`,
184+
),
185+
];
186+
}
162187

163188
const counts = await ensurePullRequestRefs(
164189
pullRequest,

src/views/viewCommands.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import * as StashActions from '../git/actions/stash';
2222
import * as TagActions from '../git/actions/tag';
2323
import * as WorktreeActions from '../git/actions/worktree';
2424
import { GitUri } from '../git/gitUri';
25+
import type { PullRequest } from '../git/models/pullRequest';
2526
import { RemoteResourceType } from '../git/models/remoteResource';
27+
import type { Repository } from '../git/models/repository';
2628
import { deletedOrMissing } from '../git/models/revision';
2729
import {
2830
ensurePullRequestRefs,
@@ -238,6 +240,8 @@ export class ViewCommands implements Disposable {
238240
registerViewCommand('gitlens.views.addAuthor', this.addAuthor, this),
239241
registerViewCommand('gitlens.views.addAuthor.multi', this.addAuthor, this, true),
240242

243+
registerViewCommand('gitlens.views.addPullRequestRemote', this.addPullRequestRemote, this),
244+
241245
registerViewCommand(
242246
'gitlens.views.openBranchOnRemote',
243247
n => executeCommand(GlCommand.OpenBranchOnRemote, n),
@@ -481,6 +485,16 @@ export class ViewCommands implements Disposable {
481485
return RemoteActions.add(getNodeRepoPath(node));
482486
}
483487

488+
@log()
489+
private async addPullRequestRemote(node: ViewNode, pr: PullRequest, repo: Repository) {
490+
const identity = getRepositoryIdentityForPullRequest(pr);
491+
if (identity.remote?.url == null) return;
492+
await repo.git
493+
.remotes()
494+
.addRemoteWithResult?.(identity.provider.repoDomain, identity.remote.url, { fetch: true });
495+
return node.view.refreshNode(node, true);
496+
}
497+
484498
@log()
485499
private applyChanges(node: ViewRefFileNode) {
486500
if (node.is('results-file')) {

0 commit comments

Comments
 (0)