Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels to create view in a minimal way #4407

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion common/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { MergeMethod, MergeMethodsAvailability } from '../src/github/interface';
import { ILabel, MergeMethod, MergeMethodsAvailability } from '../src/github/interface';

export interface RemoteInfo {
owner: string;
Expand All @@ -30,6 +30,8 @@ export interface CreateParams {
compareRemote?: RemoteInfo;
compareBranch?: string;
isDraft?: boolean;
labels?: ILabel[];
isDarkTheme?: boolean;

validate?: boolean;
showTitleValidationError?: boolean;
Expand Down Expand Up @@ -59,4 +61,5 @@ export interface CreatePullRequest {
draft: boolean;
autoMerge: boolean;
autoMergeMethod?: MergeMethod;
labels: ILabel[];
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@
"category": "%command.pull.request.category%",
"icon": "$(gift)"
},
{
"command": "pr.addLabelsToNewPr",
"title": "%command.pr.addLabelsToNewPr.title%",
"category": "%command.pull.request.category%",
"icon": "$(tag)"
},
{
"command": "issue.createIssueFromSelection",
"title": "%command.issue.createIssueFromSelection.title%",
Expand Down Expand Up @@ -1329,6 +1335,10 @@
"command": "pr.copyCommentLink",
"when": "false"
},
{
"command": "pr.addLabelsToNewPr",
"when": "false"
},
{
"command": "issue.openIssue",
"when": "false"
Expand Down Expand Up @@ -1497,6 +1507,11 @@
"command": "pr.openPullRequestOnGitHub",
"when": "view == github:activePullRequest && github:hasGitHubRemotes",
"group": "navigation@3"
},
{
"command": "pr.addLabelsToNewPr",
"when": "view == github:createPullRequest",
"group": "navigation@1"
}
],
"view/item/context": [
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"command.pr.goToPreviousDiffInPr.title": "Go to Previous Diff in Pull Request",
"command.pr.copyCommentLink.title": "Copy Comment Link",
"command.pr.applySuggestion.title": "Apply Suggestion",
"command.pr.addLabelsToNewPr.title": "Add Labels",
"command.issues.category": "GitHub Issues",
"command.issue.createIssueFromSelection.title": "Create Issue From Selection",
"command.issue.createIssueFromClipboard.title": "Create Issue From Clipboard",
Expand Down
63 changes: 57 additions & 6 deletions src/github/createPRViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
titleAndBodyFrom,
} from './folderRepositoryManager';
import { GitHubRepository } from './githubRepository';
import { RepoAccessAndMergeMethods } from './interface';
import { ILabel, MergeMethod, RepoAccessAndMergeMethods } from './interface';
import { PullRequestModel } from './pullRequestModel';
import { getDefaultMergeMethod } from './pullRequestOverview';
import { ISSUE_EXPRESSION, parseIssueExpressionOutput, variableSubstitution } from './utils';
Expand All @@ -48,6 +48,7 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs

private _compareBranch: string;
private _baseBranch: string;
private _baseRemote: RemoteInfo;

private _firstLoad: boolean = true;

Expand Down Expand Up @@ -322,13 +323,16 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs
allowAutoMerge: mergeConfiguration.viewerCanAutoMerge,
mergeMethodsAvailability: mergeConfiguration.mergeMethodsAvailability,
createError: '',
isDraft: vscode.workspace.getConfiguration(SETTINGS_NAMESPACE).get(CREATE_DRAFT, false)
labels: this.labels,
isDraft: vscode.workspace.getConfiguration(SETTINGS_NAMESPACE).get(CREATE_DRAFT, false),
isDarkTheme: vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark
};

Logger.appendLine(`Initializing "create" view: ${JSON.stringify(params)}`, 'CreatePullRequestViewProvider');

this._compareBranch = this.defaultCompareBranch.name ?? '';
this._baseBranch = defaultBaseBranch;
this._baseRemote = defaultBaseRemote;

this._postMessage({
command: reset ? 'reset' : 'pr.initialize',
Expand Down Expand Up @@ -366,6 +370,7 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs
if (isBase) {
newBranch = defaultBranch;
this._baseBranch = defaultBranch;
this._baseRemote = { owner, repositoryName };
this._onDidChangeBaseRemote.fire({ owner, repositoryName });
this._onDidChangeBaseBranch.fire(defaultBranch);
} else {
Expand Down Expand Up @@ -396,6 +401,52 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs
}
}

private async enableAutoMerge(pr: PullRequestModel, autoMerge: boolean, automergeMethod: MergeMethod | undefined): Promise<void> {
if (autoMerge && automergeMethod) {
return pr.enableAutoMerge(automergeMethod);
}
}

private async setLabels(pr: PullRequestModel, labels: ILabel[]): Promise<void> {
if (labels.length > 0) {
await pr.addLabels(labels.map(label => label.name));
}
}

private labels: ILabel[] = [];
public async addLabels(): Promise<void> {
let newLabels: ILabel[] = [];

async function getLabelOptions(
folderRepoManager: FolderRepositoryManager,
labels: ILabel[],
base: RemoteInfo
): Promise<vscode.QuickPickItem[]> {
newLabels = await folderRepoManager.getLabels(undefined, { owner: base.owner, repo: base.repositoryName });

return newLabels.map(label => {
return {
label: label.name,
picked: labels.some(existingLabel => existingLabel.name === label.name)
};
});
}

const labelsToAdd = await vscode.window.showQuickPick(
getLabelOptions(this._folderRepositoryManager, this.labels, this._baseRemote),
{ canPickMany: true },
);

if (labelsToAdd && labelsToAdd.length) {
const addedLabels: ILabel[] = labelsToAdd.map(label => newLabels.find(l => l.name === label.label)!);
this.labels = addedLabels;
this._postMessage({
command: 'set-labels',
params: { labels: this.labels }
});
}
}

private async pushUpstream(compareOwner: string, compareRepositoryName: string, compareBranchName: string): Promise<{ compareUpstream: GitHubRemote, repo: GitHubRepository | undefined } | undefined> {
let createdPushRemote: GitHubRemote | undefined;
const pushRemote = this._folderRepositoryManager.repository.state.remotes.find(localRemote => {
Expand Down Expand Up @@ -482,10 +533,10 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs
if (!createdPR) {
this._throwError(message, vscode.l10n.t('There must be a difference in commits to create a pull request.'));
} else {
if (message.args.autoMerge && message.args.autoMergeMethod) {
await createdPR.enableAutoMerge(message.args.autoMergeMethod);
}
await this.autoAssign(createdPR);
await Promise.all([
this.setLabels(createdPR, message.args.labels),
this.enableAutoMerge(createdPR, message.args.autoMerge, message.args.autoMergeMethod),
this.autoAssign(createdPR)]);
await this._replyMessage(message, {});
this._onDone.fire(createdPR);
}
Expand Down
6 changes: 6 additions & 0 deletions src/view/createPullRequestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export class CreatePullRequestHelper {
}),
);

this._disposables.push(
vscode.commands.registerCommand('pr.addLabelsToNewPr', _ => {
return this._createPRViewProvider?.addLabels();
}),
);

if (usingCurrentBranchAsCompare) {
this._disposables.push(
this.repository.state.onDidChange(_ => {
Expand Down
20 changes: 20 additions & 0 deletions webviews/common/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,23 @@ body img.avatar {
::-webkit-scrollbar-corner {
display: none;
}

.labels-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
}

.label {
display: flex;
justify-content: normal;
padding: 0 8px;
border-radius: 20px;
border-style: solid;
border-width: 1px;
background: var(--vscode-badge-background);
color: var(--vscode-badge-foreground);
font-size: 11px;
line-height: 18px;
font-weight: 600;
}
11 changes: 10 additions & 1 deletion webviews/common/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultCreateParams: CreateParams = {
branchesForCompare: [],
validate: false,
showTitleValidationError: false,
labels: []
};

export class CreatePRContext {
Expand Down Expand Up @@ -133,7 +134,8 @@ export class CreatePRContext {
compareRepo: this.createParams.compareRemote!.repositoryName,
draft: !!this.createParams.isDraft,
autoMerge: !!this.createParams.autoMerge,
autoMergeMethod: this.createParams.autoMergeMethod
autoMergeMethod: this.createParams.autoMergeMethod,
labels: this.createParams.labels ?? []
};
}

Expand Down Expand Up @@ -229,6 +231,13 @@ export class CreatePRContext {
}
window.scrollTo(message.scrollPosition.x, message.scrollPosition.y);
return;

case 'set-labels':
if (!message.params) {
return;
}
this.updateState(message.params);
return;
}
};

Expand Down
31 changes: 31 additions & 0 deletions webviews/common/label.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/


import React, { ReactNode } from 'react';
import { gitHubLabelColor } from '../../src/common/utils';
import { ILabel } from '../../src/github/interface';

export interface LabelProps {
label: ILabel & { canDelete: boolean; isDarkTheme: boolean };
}

export function Label(label: ILabel & { canDelete: boolean; isDarkTheme: boolean; children?: ReactNode}) {
const { name, canDelete, color } = label;
const labelColor = gitHubLabelColor(color, label.isDarkTheme, false);
return (
<div
className="section-item label"
style={{
backgroundColor: labelColor.backgroundColor,
color: labelColor.textColor,
borderColor: `${labelColor.borderColor}`,
paddingRight: canDelete ? '2px' : '8px'
}}
>
{name}{label.children}
</div>
);
}
39 changes: 10 additions & 29 deletions webviews/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

import React, { useContext } from 'react';
import { gitHubLabelColor } from '../../src/common/utils';
import { ILabel, IMilestone } from '../../src/github/interface';
import { IMilestone } from '../../src/github/interface';
import { PullRequest } from '../common/cache';
import PullRequestContext from '../common/context';
import { Label } from '../common/label';
import { AuthorLink, Avatar } from '../components/user';
import { closeIcon, settingsIcon } from './icon';
import { Reviewer } from './reviewer';
import { nbsp } from './space';

export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue, milestone, assignees }: PullRequest) {
const {
addReviewers,
addAssignees,
addAssigneeYourself,
addLabels,
removeLabel,
addMilestone,
updatePR,
pr,
} = useContext(PullRequestContext);

return (
<div id="sidebar">
{!isIssue ? (
Expand Down Expand Up @@ -112,11 +112,16 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
</button>
) : null}
</div>

{labels.length ? (
<div className="labels-list">
{labels.map(label => (
<Label key={label.name} {...label} canDelete={hasWritePermission} />
<Label key={label.name} {...label} canDelete={hasWritePermission} isDarkTheme={pr.isDarkTheme}>
{hasWritePermission ? (
<button className="icon-button" onClick={() => removeLabel(label.name)}>
{closeIcon}️
</button>
) : null}
</Label>
))}
</div>
) : (
Expand Down Expand Up @@ -147,30 +152,6 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
);
}

function Label(label: ILabel & { canDelete: boolean }) {
const { name, canDelete, color } = label;
const { removeLabel, pr } = useContext(PullRequestContext);
const labelColor = gitHubLabelColor(color, pr.isDarkTheme, false);
return (
<div
className="section-item label"
style={{
backgroundColor: labelColor.backgroundColor,
color: labelColor.textColor,
borderColor: `${labelColor.borderColor}`,
paddingRight: canDelete ? '2px' : '8px'
}}
>
{name}
{canDelete ? (
<button className="icon-button" onClick={() => removeLabel(name)}>
{closeIcon}️
</button>
) : null}
</div>
);
}

function Milestone(milestone: IMilestone & { canDelete: boolean }) {
const { removeMilestone, updatePR, pr } = useContext(PullRequestContext);
const backgroundBadgeColor = getComputedStyle(document.documentElement).getPropertyValue(
Expand Down
10 changes: 10 additions & 0 deletions webviews/createPullRequestView/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { render } from 'react-dom';
import { CreateParams, RemoteInfo } from '../../common/views';
import PullRequestContext from '../common/createContext';
import { ErrorBoundary } from '../common/errorBoundary';
import { Label } from '../common/label';
import { AutoMerge } from '../components/automergeSelect';
import { gitCompareIcon, repoIcon } from '../components/icon';

Expand Down Expand Up @@ -127,6 +128,15 @@ export function main() {
<BranchSelect onChange={updateBaseBranch} defaultOption={params.baseBranch} branches={params.branchesForRemote} />
</div>

{params.labels && (params.labels.length > 0) ?
<div>
<label className='input-label'>Labels</label>
<div className='labels-list'>
{params.labels.map(label => <Label key={label.name} {...label} canDelete={false} isDarkTheme={!!params.isDarkTheme} />)}
</div>
</div>
: null}

<div className='wrapper'>
<label className='input-label' htmlFor='title'>Title</label>
<input
Expand Down
4 changes: 4 additions & 0 deletions webviews/createPullRequestView/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ body {
display: flex;
align-items: center;
}

.labels-list {
padding-top: 6px;
}
Loading