From 50116874811f82964fccf5274354c9ef27db3e6f Mon Sep 17 00:00:00 2001 From: kumachan-mis Date: Wed, 30 Oct 2019 13:40:01 +0900 Subject: [PATCH 1/2] fix: visualize diffs of PlantUML descriptions which include both renaming and changes --- src/finder/GitHubFinder.ts | 39 +++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/finder/GitHubFinder.ts b/src/finder/GitHubFinder.ts index d01ce5636..6567e2625 100644 --- a/src/finder/GitHubFinder.ts +++ b/src/finder/GitHubFinder.ts @@ -74,9 +74,9 @@ export class GitHubPullRequestDiffFinder implements DiffFinder { return diffs; } - private getBaseHeadBranchNames($root: JQuery): string[] { + private getBaseHeadBranchNames($root: JQuery): [string, string] { const tableObjectTagName = 'div.TableObject-item.TableObject-item--primary'; - const getTagName = (baseOrHead: string): string => + const getTagName = (baseOrHead: 'base' | 'head'): string => `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')); @@ -89,9 +89,13 @@ export class GitHubPullRequestDiffFinder implements DiffFinder { headBranchName: string, $diff: JQuery ): Promise { - 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, @@ -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, @@ -111,6 +118,28 @@ export class GitHubPullRequestDiffFinder implements DiffFinder { }; } + private getBaseHeadFilePaths($diff: JQuery): [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; + } + } + console.log(filePaths); + if (filePaths.length == 1) return [filePaths[0], filePaths[0]]; + if (filePaths.length == 2) return [filePaths[0], filePaths[1]]; + else return ['', '']; + } + private async getTexts(fileUrl: string): Promise { const response = await fetch(fileUrl); if (!response.ok) return []; From 6fb1659b33c47aaddb63495da4d5e3cfbe852f1c Mon Sep 17 00:00:00 2001 From: kumachan-mis Date: Wed, 30 Oct 2019 13:53:46 +0900 Subject: [PATCH 2/2] refine code based on review --- src/finder/GitHubFinder.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/finder/GitHubFinder.ts b/src/finder/GitHubFinder.ts index 6567e2625..2325cf39d 100644 --- a/src/finder/GitHubFinder.ts +++ b/src/finder/GitHubFinder.ts @@ -134,10 +134,9 @@ export class GitHubPullRequestDiffFinder implements DiffFinder { filePath += separator; } } - console.log(filePaths); if (filePaths.length == 1) return [filePaths[0], filePaths[0]]; if (filePaths.length == 2) return [filePaths[0], filePaths[1]]; - else return ['', '']; + return ['', '']; } private async getTexts(fileUrl: string): Promise {