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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(patch text to hunk bounds): support regex for patch texts #83

Merged
merged 2 commits into from
Aug 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/github-handler/comment-handler/github-patch-format-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// 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
//
// https://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 {Range} from '../../types';

const REGEX_INDEX_OF_UPDATED_HUNK = 2;

/**
* @@ -<start line original>,<offset> +<start line updated>,<offset> @@
* i.e. @@ -132,7 +132,7 @@
*/
const REGEX_MULTILINE_RANGE = /@@ -([0-9]+,[0-9]+) \+([0-9]+,[0-9]+) @@/g;

/**
* @@ -<start line original> +<start line updated>,<offset> @@
* i.e. a deletion @@ -1 +0,0 @@
*/
const REGEX_ONELINE_TO_MULTILINE_RANGE = /@@ -([0-9]+) \+([0-9]+,[0-9]+) @@/g;
/**
* @@ -<line original> +<line updated> @@
* i.e. @@ -1 +1 @@
*/
const REGEX_ONELINE_RANGE = /@@ -([0-9]+) \+([0-9]+) @@/g;
/**
* @@ -<start line original>,<offset> +<line updated> @@
* i.e. file creation @@ -0,0 +1 @@
*/
const REGEX_MULTILINE_TO_ONELINE_RANGE = /@@ -([0-9]+,[0-9]+) \+([0-9]+) @@/g;

/**
* Parses the GitHub line-based patch text
* Example output of one output of one regex exec
*
* '@@ -0,0 +1,12 @@\n', // original text
* '0,0', // original hunk
* '1,12', // new hunk
* index: 0,
* input: '@@ -0,0 +1,12 @@\n+Hello world%0A',
* groups: undefined
* @param {string} patchText
* @returns patch ranges
*/
export function getGitHubPatchRanges(patchText: string): Range[] {
const ranges: Range[] = [];
// CASE I: multiline patch ranges
// includes non-first single-line patches
// i.e. @@ -3,4 +3,4 @@
// which only edits line 3, but is still a multiline patch range
for (
let patch = REGEX_MULTILINE_RANGE.exec(patchText);
patch !== null;
patch = REGEX_MULTILINE_RANGE.exec(patchText)
) {
// stricly interested in the updated/current github file content
const patchData = patch[REGEX_INDEX_OF_UPDATED_HUNK].split(',');
// the line number ranges of the updated text
const start = parseInt(patchData[0]);
const offset = parseInt(patchData[1]);
const range: Range = {start, end: start + offset};
ranges.push(range);
}
// CASE II: oneline text becomes multiline text
for (
let patch = REGEX_ONELINE_TO_MULTILINE_RANGE.exec(patchText);
patch !== null;
patch = REGEX_ONELINE_TO_MULTILINE_RANGE.exec(patchText)
) {
// stricly interested in the updated/current github file content
const patchData = patch[REGEX_INDEX_OF_UPDATED_HUNK].split(',');
// the line number ranges of the updated text
const start = parseInt(patchData[0]);
const offset = parseInt(patchData[1]);
const range: Range = {start, end: start + offset};
ranges.push(range);
}
// CASE III: first line of text updated
for (
let patch = REGEX_ONELINE_RANGE.exec(patchText);
patch !== null;
patch = REGEX_ONELINE_RANGE.exec(patchText)
) {
// stricly interested in the updated/current github file content
// the line number ranges of the updated text
const start = parseInt(patch[REGEX_INDEX_OF_UPDATED_HUNK]);
const range: Range = {start, end: start + 1};
ranges.push(range);
}
// CASE IV: Multiline range is reduced to one line
// 0,0 constitutes a multi-line range
for (
let patch = REGEX_MULTILINE_TO_ONELINE_RANGE.exec(patchText);
patch !== null;
patch = REGEX_MULTILINE_TO_ONELINE_RANGE.exec(patchText)
) {
// stricly interested in the updated/current github file content
// the line number ranges of the updated text
const start = parseInt(patch[REGEX_INDEX_OF_UPDATED_HUNK]);
const range: Range = {start, end: start + 1};
ranges.push(range);
}
return ranges;
}
28 changes: 28 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,31 @@ export interface CreatePullRequest {
// Whether or not maintainers can modify the PR.
maintainersCanModify: boolean;
}

/**
* The file content of the original content and the patched content
*/
export interface RawContent {
readonly old_content: string;
readonly new_content: string;
}

/**
* A range object defined by lower boundary as 'start' and upper boundary as 'end'
*/
export interface Range {
readonly start: number;
readonly end: number;
}

/**
* The range of a patch along with the raw file content
*/
export interface Patch extends Range {
readonly raw_content: string;
}

export type FilePatches = Map<string, Patch[]>;
export type RawChanges = Map<string, RawContent>;
export type PatchText = Map<string, string>;
export type FileRanges = Map<string, Range[]>;
59 changes: 59 additions & 0 deletions test/github-regex-patch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 {expect} from 'chai';
import {describe, it} from 'mocha';
import {getGitHubPatchRanges} from '../src/github-handler/comment-handler/github-patch-format-handler';

describe('Getting patch range from GitHub patch text', async () => {
it('parses original text for multiline modifies', () => {
const multiline_patch =
'@@ -1,2 +1,5 @@\n Hello world\n-!\n+Goodbye World\n+gOodBYE world\n+\n+Goodbye World';
const ranges = getGitHubPatchRanges(multiline_patch);
expect(ranges[0].start).equals(1);
expect(ranges[0].end).equals(6);
expect(ranges.length).equals(1);
});
it('parses original text for single line modifies at the start of the file', () => {
const first_line_patch = '@@ -1 +1 @@\n-Hello foo\n+';
const ranges = getGitHubPatchRanges(first_line_patch);
expect(ranges[0].start).equals(1);
expect(ranges[0].end).equals(2);
expect(ranges.length).equals(1);
});
it('parses original text for single line deletes', () => {
const single_line_to_multiline_format = '@@ -1 +0,0 @@\n-hello world';
const ranges = getGitHubPatchRanges(single_line_to_multiline_format);
expect(ranges[0].start).equals(0);
expect(ranges[0].end).equals(0);
expect(ranges.length).equals(1);
});
it('parses original text for single line file creations', () => {
const first_line_patch = "@@ -0,0 +1 @@\n+console.log('Hello World!');";
const ranges = getGitHubPatchRanges(first_line_patch);
expect(ranges[0].start).equals(1);
expect(ranges[0].end).equals(2);
expect(ranges.length).equals(1);
});
it('parses patch text with multiple patches', () => {
const first_line_patch =
'@@ -356,6 +356,7 @@ Hello\n Hello\n Hello\n Hello\n+Bye\n Hello\n Hello\n Hello\n@@ -6576,8 +6577,7 @@ Hello\n Hello\n Hello\n Hello\n-Hello\n-Hello\n+Bye\n Hello\n Hello\n Hello';
const ranges = getGitHubPatchRanges(first_line_patch);
expect(ranges[0].start).equals(356);
expect(ranges[0].end).equals(363);
expect(ranges[1].start).equals(6577);
expect(ranges[1].end).equals(6584);
expect(ranges.length).equals(2);
});
});