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

fix: handle addition-only and deletion-only changes #144

Merged
merged 5 commits into from
Oct 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import {Hunk} from '../../../types';
import {adjustHunkUp, adjustHunkDown} from '../../hunk-utils';

interface PartitionedHunks {
validHunks: Map<string, Hunk[]>;
Expand All @@ -24,6 +25,13 @@ interface PartitionedFileHunks {
invalidFileHunks: Hunk[];
}

function hunkOverlaps(validHunk: Hunk, suggestedHunk: Hunk): boolean {
return (
suggestedHunk.oldStart >= validHunk.newStart &&
suggestedHunk.oldEnd <= validHunk.newEnd
);
}

function partitionFileHunks(
pullRequestHunks: Hunk[],
suggestedHunks: Hunk[]
Expand All @@ -40,16 +48,33 @@ function partitionFileHunks(
candidateHunk = pullRequestHunks[i];
}
if (!candidateHunk) {
invalidFileHunks.push(suggestedHunk);
return;
}
// if deletion only or addition only
if (
suggestedHunk.oldStart >= candidateHunk.newStart &&
suggestedHunk.oldEnd <= candidateHunk.newEnd
suggestedHunk.newEnd < suggestedHunk.newStart ||
suggestedHunk.oldEnd < suggestedHunk.oldStart
) {
// try using previous line
let adjustedHunk = adjustHunkUp(suggestedHunk);
if (adjustedHunk && hunkOverlaps(candidateHunk, adjustedHunk)) {
validFileHunks.push(adjustedHunk);
return;
}

// try using next line
adjustedHunk = adjustHunkDown(suggestedHunk);
if (adjustedHunk && hunkOverlaps(candidateHunk, adjustedHunk)) {
validFileHunks.push(adjustedHunk);
return;
}
} else if (hunkOverlaps(candidateHunk, suggestedHunk)) {
validFileHunks.push(suggestedHunk);
} else {
invalidFileHunks.push(suggestedHunk);
return;
}

invalidFileHunks.push(suggestedHunk);
});
return {validFileHunks, invalidFileHunks};
}
Expand Down
22 changes: 20 additions & 2 deletions src/github-handler/diff-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,25 @@ export function parseAllHunks(diff: string): Map<string, Hunk[]> {
let normalLines = 0;
let changeSeen = false;
const newLines: string[] = [];
let previousLine: string | null = null;
let nextLine: string | null = null;

chunk.changes.forEach(change => {
// strip off leading '+', '-', or ' ' and trailing carriage return
const content = change.content.substring(1).replace(/[\n\r]+$/g, '');
if (change.type === 'normal') {
normalLines++;
if (changeSeen) {
if (nextLine === null) {
nextLine = content;
}
} else {
previousLine = content;
}
} else {
if (change.type === 'add') {
// strip off leading '+' and trailing carriage return
newLines.push(change.content.substring(1).replace(/[\n\r]+$/g, ''));
newLines.push(content);
}
if (!changeSeen) {
oldStart += normalLines;
Expand All @@ -68,13 +79,20 @@ export function parseAllHunks(diff: string): Map<string, Hunk[]> {
});
const newEnd = newStart + chunk.newLines - normalLines - 1;
const oldEnd = oldStart + chunk.oldLines - normalLines - 1;
return {
let hunk: Hunk = {
oldStart: oldStart,
oldEnd: oldEnd,
newStart: newStart,
newEnd: newEnd,
newContent: newLines,
};
if (previousLine) {
hunk = {...hunk, previousLine: previousLine};
}
if (nextLine) {
hunk = {...hunk, nextLine: nextLine};
}
return hunk;
});
hunksByFile.set(filename, chunks);
});
Expand Down
51 changes: 51 additions & 0 deletions src/github-handler/hunk-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {Hunk} from '../types';

/**
* Shift a Hunk up one line so it starts one line earlier.
* @param {Hunk} hunk
* @returns {Hunk | null} the adjusted Hunk or null if there is no preceeding line.
*/
export function adjustHunkUp(hunk: Hunk): Hunk | null {
if (!hunk.previousLine) {
return null;
}
return {
oldStart: hunk.oldStart - 1,
oldEnd: hunk.oldEnd,
newStart: hunk.newStart - 1,
newEnd: hunk.newEnd,
newContent: [hunk.previousLine, ...hunk.newContent],
};
}

/**
* Shift a Hunk up one line so it ends one line later.
* @param {Hunk} hunk
* @returns {Hunk | null} the adjusted Hunk or null if there is no following line.
*/
export function adjustHunkDown(hunk: Hunk): Hunk | null {
if (!hunk.nextLine) {
return null;
}
return {
oldStart: hunk.oldStart,
oldEnd: hunk.oldEnd + 1,
newStart: hunk.newStart,
newEnd: hunk.newEnd + 1,
newContent: hunk.newContent.concat(hunk.nextLine),
};
}
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,6 @@ export interface Hunk {
readonly newStart: number;
readonly newEnd: number;
readonly newContent: string[];
readonly previousLine?: string;
readonly nextLine?: string;
}
35 changes: 34 additions & 1 deletion test/diff-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ describe('parseAllHunks', () => {
newStart: 5,
newEnd: 5,
newContent: [" args: ['sleep', '301']"],
nextLine: "- name: 'ubuntu'",
previousLine: "- name: 'ubuntu'",
},
]);
});
Expand All @@ -57,6 +59,7 @@ describe('parseAllHunks', () => {
newStart: 7,
newEnd: 8,
newContent: [" args: ['foobar']", ' id: asdf'],
previousLine: "- name: 'ubuntu'",
},
]);
});
Expand All @@ -74,6 +77,7 @@ describe('parseAllHunks', () => {
newStart: 5,
newEnd: 6,
newContent: [" args: ['sleep', '30']", " id: 'foobar'"],
previousLine: "- name: 'ubuntu'",
},
]);
});
Expand All @@ -91,6 +95,8 @@ describe('parseAllHunks', () => {
newStart: 2,
newEnd: 3,
newContent: ["- name: 'foo'", " args: ['sleep 1']"],
nextLine: "- name: 'ubuntu'",
previousLine: 'steps:',
},
]);
});
Expand All @@ -109,6 +115,8 @@ describe('parseAllHunks', () => {
newStart: 2,
newEnd: 2,
newContent: ["- name: 'foo'"],
nextLine: "- name: 'ubuntu'",
previousLine: 'steps:',
},
]);
});
Expand All @@ -118,7 +126,32 @@ describe('parseAllHunks', () => {
expect(allHunks.size).to.equal(1);
const hunks = allHunks.get('cloudbuild.yaml');
expect(hunks).to.eql([
{oldStart: 4, oldEnd: 5, newStart: 4, newEnd: 3, newContent: []},
{
oldStart: 4,
oldEnd: 5,
newStart: 4,
newEnd: 3,
newContent: [],
nextLine: "- name: 'ubuntu'",
previousLine: " args: ['echo', 'foobar']",
},
]);
});
it('parses additions', () => {
const diff = readFileSync(resolve(fixturePath, 'addition.diff')).toString();
const allHunks = parseAllHunks(diff);
expect(allHunks.size).to.equal(1);
const hunks = allHunks.get('cloudbuild.yaml');
expect(hunks).to.eql([
{
oldStart: 6,
oldEnd: 5,
newStart: 6,
newEnd: 6,
newContent: [" id: 'added'"],
nextLine: "- name: 'ubuntu'",
previousLine: " args: ['sleep', '30']",
},
]);
});
});
11 changes: 11 additions & 0 deletions test/fixtures/diffs/addition.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
diff --git a/cloudbuild.yaml b/cloudbuild.yaml
index cac8fbc..f4cbead 100644
--- a/cloudbuild.yaml
+++ b/cloudbuild.yaml
@@ -3,5 +3,6 @@ steps:
args: ['echo', 'foobar']
- name: 'ubuntu'
args: ['sleep', '30']
+ id: 'added'
- name: 'ubuntu'
args: ['echo', 'done']
2 changes: 1 addition & 1 deletion test/fixtures/diffs/deletion.diff
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ index cac8fbc..bc796a0 100644
-- name: 'ubuntu'
- args: ['sleep', '30']
- name: 'ubuntu'
args: ['echo', 'done']
args: ['echo', 'done']
2 changes: 1 addition & 1 deletion test/fixtures/diffs/many-to-many.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ index cac8fbc..87f387c 100644
+- name: 'foo'
+ args: ['sleep 1']
- name: 'ubuntu'
args: ['echo', 'done']
args: ['echo', 'done']
2 changes: 1 addition & 1 deletion test/fixtures/diffs/many-to-one.diff
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ index cac8fbc..71fda1b 100644
- args: ['sleep', '30']
+- name: 'foo'
- name: 'ubuntu'
args: ['echo', 'done']
args: ['echo', 'done']
2 changes: 1 addition & 1 deletion test/fixtures/diffs/one-line-to-many-newline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ index 07e5def..ca56e25 100644
- args: ['sleep', '30']
\ No newline at end of file
+ args: ['sleep', '30']
+ id: 'foobar'
+ id: 'foobar'
88 changes: 88 additions & 0 deletions test/hunk-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {describe, it, before} from 'mocha';
import {setup} from './util';
import {expect} from 'chai';
import {adjustHunkUp, adjustHunkDown} from '../src/github-handler/hunk-utils';

before(() => {
setup();
});

describe('adjustHunkUp', () => {
it('returns a new hunk if there is a previous line', () => {
const hunk = {
oldStart: 5,
oldEnd: 5,
newStart: 5,
newEnd: 5,
newContent: [" args: ['sleep', '301']"],
nextLine: "- name: 'ubuntu'",
previousLine: "- name: 'ubuntu'",
};
const adjustedHunk = adjustHunkUp(hunk);
expect(adjustedHunk).to.eql({
oldStart: 4,
oldEnd: 5,
newStart: 4,
newEnd: 5,
newContent: ["- name: 'ubuntu'", " args: ['sleep', '301']"],
});
});
it('returns null if there is no previous line', () => {
const hunk = {
oldStart: 5,
oldEnd: 5,
newStart: 5,
newEnd: 5,
newContent: [" args: ['sleep', '301']"],
};
const adjustedHunk = adjustHunkUp(hunk);
expect(adjustedHunk).to.eql(null);
});
});

describe('adjustHunkDown', () => {
it('returns a new hunk if there is a next line', () => {
const hunk = {
oldStart: 5,
oldEnd: 5,
newStart: 5,
newEnd: 5,
newContent: [" args: ['sleep', '301']"],
nextLine: "- name: 'ubuntu'",
previousLine: "- name: 'ubuntu'",
};
const adjustedHunk = adjustHunkDown(hunk);
expect(adjustedHunk).to.eql({
oldStart: 5,
oldEnd: 6,
newStart: 5,
newEnd: 6,
newContent: [" args: ['sleep', '301']", "- name: 'ubuntu'"],
});
});
it('returns null if there is no previous line', () => {
const hunk = {
oldStart: 5,
oldEnd: 5,
newStart: 5,
newEnd: 5,
newContent: [" args: ['sleep', '301']"],
};
const adjustedHunk = adjustHunkDown(hunk);
expect(adjustedHunk).to.eql(null);
});
});