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 1 commit
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
91 changes: 91 additions & 0 deletions src/github-handler/comment-handler/diff-text-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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_UPDATED_HUNK = 2;

/**
* 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
*
*/
const REGEX_MANY_MANY = /@@ -([0-9]+,[0-9]+) \+([0-9]+,[0-9]+) @@\n/g;
const REGEX_ONE_MANY = /@@ -([0-9]+) \+([0-9]+,[0-9]+) @@\n/g;
const REGEX_ONE_ONE = /@@ -([0-9]+) \+([0-9]+) @@\n/g;
const REGEX_MANY_ONE = /@@ -([0-9]+) \+([0-9]+,[0-9]+) @@\n/g;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename these and document what scenarios these cases match to rather than the representation.


/**
* Parses the GitHub line-based patch text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parses this into what/for what purpose?

* @param {string} patchText
* @returns patch ranges
*/
export function getGitHubPatchRanges(patchText: string): Range[] {
const ranges: Range[] = [];
for (
let patch = REGEX_MANY_MANY.exec(patchText);
patch !== null;
patch = REGEX_MANY_MANY.exec(patchText)
) {
// stricly interested in the updated/current github file content
const patchData = patch[REGEX_INDEX_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);
}
for (
let patch = REGEX_ONE_MANY.exec(patchText);
patch !== null;
patch = REGEX_ONE_MANY.exec(patchText)
) {
// stricly interested in the updated/current github file content
const patchData = patch[REGEX_INDEX_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);
}
for (
let patch = REGEX_ONE_ONE.exec(patchText);
patch !== null;
patch = REGEX_ONE_ONE.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_UPDATED_HUNK]);
const range: Range = {start, end: start + 1};
ranges.push(range);
}
for (
let patch = REGEX_MANY_ONE.exec(patchText);
patch !== null;
patch = REGEX_MANY_ONE.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_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[]>;
88 changes: 88 additions & 0 deletions test/regex-patch.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 {expect} from 'chai';
import {describe, it} from 'mocha';
import {getGitHubPatchRanges} from '../src/github-handler/comment-handler/diff-text-handler';

describe('Getting patch range from patch text', async () => {
describe('GitHub patch text', () => {
const gitHubListFilesData = [
{
sha: 'a1d470fa4d7b04450715e3e02d240a34517cd988',
filename: 'Readme.md',
status: 'modified',
additions: 4,
deletions: 1,
changes: 5,
blob_url:
'https://github.com/TomKristie/HelloWorld/blob/eb53f3871f56e8dd6321e44621fe6ac2da1bc120/Readme.md',
raw_url:
'https://github.com/TomKristie/HelloWorld/raw/eb53f3871f56e8dd6321e44621fe6ac2da1bc120/Readme.md',
contents_url:
'https://api.github.com/repos/TomKristie/HelloWorld/contents/Readme.md?ref=eb53f3871f56e8dd6321e44621fe6ac2da1bc120',
patch:
'@@ -1,2 +1,5 @@\n Hello world\n-!\n+Goodbye World\n+gOodBYE world\n+\n+Goodbye World',
},
{
sha: '8b137891791fe96927ad78e64b0aad7bded08bdc',
filename: 'foo/foo.txt',
status: 'modified',
additions: 1,
deletions: 1,
changes: 2,
blob_url:
'https://github.com/TomKristie/HelloWorld/blob/eb53f3871f56e8dd6321e44621fe6ac2da1bc120/foo/foo.txt',
raw_url:
'https://github.com/TomKristie/HelloWorld/raw/eb53f3871f56e8dd6321e44621fe6ac2da1bc120/foo/foo.txt',
contents_url:
'https://api.github.com/repos/TomKristie/HelloWorld/contents/foo/foo.txt?ref=eb53f3871f56e8dd6321e44621fe6ac2da1bc120',
patch: '@@ -1 +1 @@\n-Hello foo\n+',
},
{
sha: '3b18e512dba79e4c8300dd08aeb37f8e728b8dad',
filename: 'helloworld.txt',
status: 'removed',
additions: 0,
deletions: 1,
changes: 1,
blob_url:
'https://github.com/TomKristie/HelloWorld/blob/f5da827a725a701302da7db2da16b1678f52fdcc/helloworld.txt',
raw_url:
'https://github.com/TomKristie/HelloWorld/raw/f5da827a725a701302da7db2da16b1678f52fdcc/helloworld.txt',
contents_url:
'https://api.github.com/repos/TomKristie/HelloWorld/contents/helloworld.txt?ref=f5da827a725a701302da7db2da16b1678f52fdcc',
patch: '@@ -1 +0,0 @@\n-hello world',
},
];
it('parses original text for multiline modifies', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 4 regexes/cases above, but only 3 test cases.

const patch = gitHubListFilesData[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split out these fixtures and rename them so it's clear what the scenario is. Reading this test case by itself makes it hard to find the input data.

const ranges = getGitHubPatchRanges(patch.patch);
expect(ranges[0].start).equals(1);
expect(ranges[0].end).equals(6);
});
it('parses original text for single line modifies at the start of the file', () => {
const patch = gitHubListFilesData[1];
const ranges = getGitHubPatchRanges(patch.patch);
expect(ranges[0].start).equals(1);
expect(ranges[0].end).equals(2);
});
it('parses original text for single line deletes', () => {
const patch = gitHubListFilesData[2];
const ranges = getGitHubPatchRanges(patch.patch);
expect(ranges[0].start).equals(0);
expect(ranges[0].end).equals(0);
});
});
});