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: visualize diffs of PlantUML descriptions which include both renaming and changes #267

Merged
merged 4 commits into from
Oct 30, 2019
Merged
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
38 changes: 33 additions & 5 deletions src/finder/GitHubFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ export class GitHubPullRequestDiffFinder implements DiffFinder {
return diffs;
}

private getBaseHeadBranchNames($root: JQuery<Node>): string[] {
private getBaseHeadBranchNames($root: JQuery<Node>): [string, string] {
Copy link
Member

Choose a reason for hiding this comment

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

👍

const tableObjectTagName = 'div.TableObject-item.TableObject-item--primary';
const getTagName = (baseOrHead: string): string =>
const getTagName = (baseOrHead: 'base' | 'head'): string =>
Copy link
Member

Choose a reason for hiding this comment

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

👍

`span.commit-ref.css-truncate.user-select-contain.expandable.${baseOrHead}-ref`;
const $baseRef = $root.find(tableObjectTagName + ' ' + getTagName('base'));
const $headRef = $root.find(tableObjectTagName + ' ' + getTagName('head'));
Expand All @@ -89,9 +89,13 @@ export class GitHubPullRequestDiffFinder implements DiffFinder {
headBranchName: string,
$diff: JQuery<Node>
): Promise<UmlDiffContent> {
const filePath = $diff.find('div.file-info a').text();
const [baseFilePath, headFilePath] = this.getBaseHeadFilePaths($diff);
const $diffBlock = $diff.find('div.js-file-content.Details-content--hidden');
if (filePath.match('.*\\.(plantuml|pu|puml)') == null || $diffBlock.length == 0) {
if (
(baseFilePath == '' && headFilePath == '') ||
$diffBlock.length == 0 ||
$diffBlock.find('div.data.highlight.empty').length > 0
) {
return {
$diff: $(),
baseBranchName,
Expand All @@ -100,7 +104,10 @@ export class GitHubPullRequestDiffFinder implements DiffFinder {
headTexts: [],
};
}
const fileUrls = [baseBranchName, headBranchName].map(branchName => blobRoot + '/' + branchName + '/' + filePath);
const fileUrls = [
blobRoot + '/' + baseBranchName + '/' + baseFilePath,
blobRoot + '/' + headBranchName + '/' + headFilePath,
];
const [baseTexts, headTexts] = await Promise.all(fileUrls.map(fileUrl => this.getTexts(fileUrl)));
return {
$diff: $diffBlock,
Expand All @@ -111,6 +118,27 @@ export class GitHubPullRequestDiffFinder implements DiffFinder {
};
}

private getBaseHeadFilePaths($diff: JQuery<Node>): [string, string] {
const title = $diff.find('div.file-info a').attr('title');
if (!title) return ['', ''];
const separator = ' → ';
const fragments = title.split(separator);
const filePaths = [];
let filePath = '';
for (const fragment of fragments) {
filePath += fragment;
if (filePath.match(/^.*\.(plantuml|pu|puml)$/)) {
filePaths.push(filePath);
filePath = '';
} else {
filePath += separator;
}
}
if (filePaths.length == 1) return [filePaths[0], filePaths[0]];
if (filePaths.length == 2) return [filePaths[0], filePaths[1]];
return ['', ''];
}

private async getTexts(fileUrl: string): Promise<string[]> {
const response = await fetch(fileUrl);
if (!response.ok) return [];
Expand Down