Skip to content

Fixes #1913 - Adds setting to change the PR associated to a given commit #3024

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Added

- Adds support for OpenAI's GPT-4 Turbo and latest Anthropic models for GitLens' experimental AI features — closes [#3005](https://github.com/gitkraken/vscode-gitlens/issues/3005)
- Adds a `gitlens.sortPullRequestsBy` setting to change which PR is associated with a commit - closes [#1913](https://github.com/gitkraken/vscode-gitlens/issues/1913) thanks to [PR #3024](https://github.com/gitkraken/vscode-gitlens/pull/3024) by Aaron Miller ([@aaron-skydio](https://github.com/aaron-skydio))

### Changed

Expand Down
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3416,6 +3416,25 @@
"markdownDescription": "Specifies how contributors are sorted in quick pick menus and views",
"scope": "window",
"order": 40
},
"gitlens.sortPullRequestsBy": {
"type": "string",
"default": "updated:desc",
"enum": [
"updated:desc",
"updated:asc",
"created:desc",
"created:asc"
],
"enumDescriptions": [
"Picks the most recently updated pull request",
"Picks the least recently updated pull request",
"Picks the most recently created pull request",
"Picks the first created pull request"
],
"markdownDescription": "Specifies how to select the pull request associated with a commit or branch",
"scope": "window",
"order": 50
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export interface Config {
readonly sortContributorsBy: ContributorSorting;
readonly sortTagsBy: TagSorting;
readonly sortRepositoriesBy: RepositoriesSorting;
readonly sortPullRequestsBy: PullRequestSorting;
readonly statusBar: {
readonly alignment: 'left' | 'right';
readonly command: StatusBarCommand;
Expand Down Expand Up @@ -255,6 +256,7 @@ export const enum CodeLensCommand {
export type CodeLensScopes = 'document' | 'containers' | 'blocks';
export type ContributorSorting = 'count:desc' | 'count:asc' | 'date:desc' | 'date:asc' | 'name:asc' | 'name:desc';
export type RepositoriesSorting = 'discovered' | 'lastFetched:desc' | 'lastFetched:asc' | 'name:asc' | 'name:desc';
export type PullRequestSorting = 'updated:desc' | 'updated:asc' | 'created:desc' | 'created:asc';
export type CustomRemoteType =
| 'AzureDevOps'
| 'Bitbucket'
Expand Down
38 changes: 36 additions & 2 deletions src/plus/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ title
state
additions
deletions
createdAt
updatedAt
closedAt
mergeable
Expand Down Expand Up @@ -596,6 +597,7 @@ export class GitHubApi implements Disposable {
number
title
state
createdAt
updatedAt
closedAt
mergedAt
Expand Down Expand Up @@ -677,17 +679,46 @@ export class GitHubApi implements Disposable {
| undefined;
}

let queryField: string;
let queryOrder: string;
let timeFunc: (pr: GitHubPullRequest) => number;

switch (configuration.get('sortPullRequestsBy')) {
case 'created:asc':
queryField = 'CREATED_AT';
queryOrder = 'ASC';
timeFunc = pr => -new Date(pr.createdAt).getTime();
break;
case 'created:desc':
queryField = 'CREATED_AT';
queryOrder = 'DESC';
timeFunc = pr => new Date(pr.createdAt).getTime();
break;
case 'updated:asc':
queryField = 'UPDATED_AT';
queryOrder = 'ASC';
timeFunc = pr => -new Date(pr.updatedAt).getTime();
break;
case 'updated:desc':
queryField = 'UPDATED_AT';
queryOrder = 'DESC';
timeFunc = pr => new Date(pr.updatedAt).getTime();
break;
}

try {
const query = `query getPullRequestForCommit(
$owner: String!
$repo: String!
$ref: GitObjectID!
$queryField: PullRequestOrderField!
$queryOrder: OrderDirection!
$avatarSize: Int
) {
repository(name: $repo, owner: $owner) {
object(oid: $ref) {
... on Commit {
associatedPullRequests(first: 2, orderBy: {field: UPDATED_AT, direction: DESC}) {
associatedPullRequests(first: 2, orderBy: {field: $queryField, direction: $queryOrder}) {
nodes {
author {
login
Expand All @@ -698,6 +729,7 @@ export class GitHubApi implements Disposable {
number
title
state
createdAt
updatedAt
closedAt
mergedAt
Expand All @@ -723,6 +755,8 @@ export class GitHubApi implements Disposable {
owner: owner,
repo: repo,
ref: ref,
queryField: queryField,
queryOrder: queryOrder,
},
scope,
cancellation,
Expand All @@ -739,7 +773,7 @@ export class GitHubApi implements Disposable {
(a, b) =>
(a.repository.owner.login === owner ? -1 : 1) - (b.repository.owner.login === owner ? -1 : 1) ||
(a.state === 'MERGED' ? -1 : 1) - (b.state === 'MERGED' ? -1 : 1) ||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),
timeFunc(b) - timeFunc(a),
);
}

Expand Down
1 change: 1 addition & 0 deletions src/plus/github/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface GitHubPullRequest {
id: string;
title: string;
state: GitHubPullRequestState;
createdAt: string;
updatedAt: string;
closedAt: string | null;
mergedAt: string | null;
Expand Down
14 changes: 14 additions & 0 deletions src/webviews/apps/settings/partials/sorting.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ <h2>Sorting</h2>
</div>
</div>
</div>

<div class="setting">
<div class="setting__input">
<label for="sortPullRequestsBy">Pick the pull request that was</label>
<div class="select-container">
<select id="sortPullRequestsBy" name="sortPullRequestsBy" data-setting>
<option value="updated:desc">most recently updated</option>
<option value="updated:asc">least recently updated</option>
<option value="created:desc">most recently created</option>
<option value="created:asc">first created</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
Expand Down