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

Improve heuritistic for determining if "Rebase and Merge" was used when merging pull request #434

Merged
merged 3 commits into from
Jul 29, 2022
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
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,29 @@
"@types/core-js": "^2.5.5",
"@types/dedent": "^0.7.0",
"@types/inquirer": "^8.2.1",
"@types/jest": "^28.1.4",
"@types/jest": "^28.1.6",
"@types/lodash": "^4.14.182",
"@types/node": "^18.0.3",
"@types/node": "^18.6.2",
"@types/safe-json-stringify": "^1.1.2",
"@types/yargs": "^17.0.10",
"@types/yargs-parser": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"eslint": "^8.19.0",
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"eslint": "^8.20.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-jest": "^26.6.0",
"eslint-plugin-prettier": "^4.2.1",
"graphql-config": "^4.3.1",
"graphql-config": "^4.3.3",
"husky": "^8.0.1",
"jest": "^28.1.2",
"jest": "^28.1.3",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^13.0.3",
"nock": "^13.2.8",
"nock": "^13.2.9",
"prettier": "^2.7.1",
"strip-ansi": "^6.0.1",
"ts-jest": "^28.0.5",
"ts-node": "^10.8.2",
"ts-jest": "^28.0.7",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
}
}
15 changes: 2 additions & 13 deletions src/lib/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,8 @@ export async function getIsCommitInBranch(
);
return true;
} catch (e) {
const isSpawnError = e instanceof SpawnError;
if (isSpawnError) {
const commitNotInBranch = e.context.code === 1 && e.context.stderr === '';
const commitNotExist =
e.context.code === 128 &&
e.context.stderr.includes('Not a valid object name');

if (commitNotInBranch || commitNotExist) {
return false;
}
}

throw e;
logger.warn('getIsCommitInBranch threw', e);
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ exports[`fetchCommitByPullNumber snapshot request/response makes the right queri
"query CommitByPullNumber($repoOwner: String!, $repoName: String!, $pullNumber: Int!) {
repository(owner: $repoOwner, name: $repoName) {
pullRequest(number: $pullNumber) {
commits {
commits(last: 1) {
totalCount
edges {
node {
commit {
message
}
}
}
}
mergeCommit {
oid
Expand Down
19 changes: 15 additions & 4 deletions src/lib/github/v4/fetchCommits/fetchCommitByPullNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ export async function fetchCommitsByPullNumber(options: {
repository(owner: $repoOwner, name: $repoName) {
pullRequest(number: $pullNumber) {
# used to determine if "Rebase and Merge" strategy was used
commits {
commits(last: 1) {
totalCount
edges {
node {
commit {
message
}
}
}
}

mergeCommit {
Expand Down Expand Up @@ -82,13 +89,16 @@ export async function fetchCommitsByPullNumber(options: {
throw new BackportError(`The PR #${pullNumber} is not merged`);
}

const possiblyRebaseAndMergeStrategy =
const lastCommitInPullRequest = pullRequestNode.commits.edges[0].node.commit;
const firstCommitInBaseBranch = mergeCommit.history.edges[0].node;
const isRebaseAndMergeStrategy =
pullRequestNode.commits.totalCount > 0 &&
mergeCommit.history.edges.every(
(c) => c.node.committedDate === mergeCommit.committedDate
);
) &&
lastCommitInPullRequest.message === firstCommitInBaseBranch.message;

if (possiblyRebaseAndMergeStrategy) {
if (isRebaseAndMergeStrategy) {
const commits = await fetchCommitsForRebaseAndMergeStrategy(
options,
pullRequestNode.commits.totalCount
Expand All @@ -107,6 +117,7 @@ interface CommitByPullNumberResponse {
pullRequest: {
commits: {
totalCount: number;
edges: { node: { commit: { message: string } } }[];
};

mergeCommit: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ export async function fetchCommitsForRebaseAndMergeStrategy(
} = options;

const query = gql`
query CommitByPullNumber(
query CommitsForRebaseAndMergeStrategy(
$repoOwner: String!
$repoName: String!
$pullNumber: Int!
$commitsTotalCount: Int!
) {
repository(owner: $repoOwner, name: $repoName) {
pullRequest(number: $pullNumber) {
number
commits(first: $commitsTotalCount) {
totalCount
edges {
Expand All @@ -50,6 +51,13 @@ export async function fetchCommitsForRebaseAndMergeStrategy(
oid
message
committedDate
associatedPullRequests(first: 1) {
edges {
node {
number
}
}
}
}
}
}
Expand Down Expand Up @@ -88,13 +96,17 @@ export async function fetchCommitsForRebaseAndMergeStrategy(
pullRequestNode.mergeCommit.history.edges.reverse();

const didUseRebaseAndMergeStrategy = commitsInBaseBranch.every((c, i) => {
const hasIdenticalCommittedDate =
const hasSameCommittedDate =
c.node.committedDate === pullRequestNode.mergeCommit?.committedDate;

const hasIdenticalCommitMessages =
const hasSameCommitMessages =
c.node.message === commitsInPullRequest[i].node.commit.message;

return hasIdenticalCommittedDate && hasIdenticalCommitMessages;
const hasSamePullNumber =
c.node.associatedPullRequests.edges[0].node.number ===
pullRequestNode.number;

return hasSameCommittedDate && hasSameCommitMessages && hasSamePullNumber;
});

if (didUseRebaseAndMergeStrategy) {
Expand All @@ -111,6 +123,7 @@ export async function fetchCommitsForRebaseAndMergeStrategy(
interface Response {
repository: {
pullRequest: {
number: number;
commits: {
totalCount: number;
edges: {
Expand All @@ -130,6 +143,13 @@ interface Response {
oid: string;
message: string;
committedDate: string;
associatedPullRequests: {
edges: {
node: {
number: number;
};
}[];
};
};
}[];
};
Expand Down
Loading