From 4f48cd1a4e0022e14266b070d433520fb805b277 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 8 Jul 2020 06:49:54 +0200
Subject: [PATCH] refactor: remove platform.getPrFiles (#6702)

---
 lib/platform/__snapshots__/index.spec.ts.snap |  4 ----
 lib/platform/azure/index.ts                   |  5 -----
 lib/platform/bitbucket-server/index.ts        |  5 -----
 lib/platform/bitbucket/index.ts               |  5 -----
 lib/platform/common.ts                        |  1 -
 lib/platform/gitea/index.ts                   |  6 ------
 lib/platform/github/index.ts                  |  5 -----
 lib/platform/gitlab/index.ts                  |  5 -----
 lib/util/git/__snapshots__/index.spec.ts.snap |  8 +-------
 lib/util/git/index.spec.ts                    | 20 +------------------
 lib/util/git/index.ts                         | 10 ++--------
 lib/workers/pr/code-owners.spec.ts            | 15 +++++++-------
 lib/workers/pr/code-owners.ts                 |  5 +++--
 13 files changed, 15 insertions(+), 79 deletions(-)

diff --git a/lib/platform/__snapshots__/index.spec.ts.snap b/lib/platform/__snapshots__/index.spec.ts.snap
index efde7b4bfc..bdab051f49 100644
--- a/lib/platform/__snapshots__/index.spec.ts.snap
+++ b/lib/platform/__snapshots__/index.spec.ts.snap
@@ -22,7 +22,6 @@ Array [
   "getIssueList",
   "getPr",
   "getPrBody",
-  "getPrFiles",
   "getPrList",
   "getRepoForceRebase",
   "getRepos",
@@ -56,7 +55,6 @@ Array [
   "getIssueList",
   "getPr",
   "getPrBody",
-  "getPrFiles",
   "getPrList",
   "getRepoForceRebase",
   "getRepos",
@@ -90,7 +88,6 @@ Array [
   "getIssueList",
   "getPr",
   "getPrBody",
-  "getPrFiles",
   "getPrList",
   "getRepoForceRebase",
   "getRepos",
@@ -124,7 +121,6 @@ Array [
   "getIssueList",
   "getPr",
   "getPrBody",
-  "getPrFiles",
   "getPrList",
   "getRepoForceRebase",
   "getRepos",
diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts
index 51f169b63c..21f84fc091 100644
--- a/lib/platform/azure/index.ts
+++ b/lib/platform/azure/index.ts
@@ -233,11 +233,6 @@ export async function getPrList(): Promise<AzurePr[]> {
   return config.prList;
 }
 
-/* istanbul ignore next */
-export async function getPrFiles(pr: Pr): Promise<string[]> {
-  return git.getBranchFiles(pr.branchName, pr.targetBranch);
-}
-
 export async function getPr(pullRequestId: number): Promise<Pr | null> {
   logger.debug(`getPr(${pullRequestId})`);
   if (!pullRequestId) {
diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts
index 9fe43c99e2..8594d033b5 100644
--- a/lib/platform/bitbucket-server/index.ts
+++ b/lib/platform/bitbucket-server/index.ts
@@ -365,11 +365,6 @@ export async function getPrList(_args?: any): Promise<Pr[]> {
   return config.prList;
 }
 
-/* istanbul ignore next */
-export async function getPrFiles(pr: Pr): Promise<string[]> {
-  return git.getBranchFiles(pr.branchName, pr.targetBranch);
-}
-
 // TODO: coverage
 // istanbul ignore next
 export async function findPr({
diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts
index 19089d509d..bec3c4e3ee 100644
--- a/lib/platform/bitbucket/index.ts
+++ b/lib/platform/bitbucket/index.ts
@@ -211,11 +211,6 @@ export async function getPrList(): Promise<Pr[]> {
   return config.prList;
 }
 
-/* istanbul ignore next */
-export async function getPrFiles(pr: Pr): Promise<string[]> {
-  return git.getBranchFiles(pr.branchName, pr.targetBranch);
-}
-
 export async function findPr({
   branchName,
   prTitle,
diff --git a/lib/platform/common.ts b/lib/platform/common.ts
index 143212a95b..c29d119465 100644
--- a/lib/platform/common.ts
+++ b/lib/platform/common.ts
@@ -188,7 +188,6 @@ export interface Platform {
   getVulnerabilityAlerts(): Promise<VulnerabilityAlert[]>;
   initRepo(config: RepoParams): Promise<RepoConfig>;
   getPrList(): Promise<Pr[]>;
-  getPrFiles(pr: Pr): Promise<string[]>;
   ensureIssueClosing(title: string): Promise<void>;
   ensureIssue(
     issueConfig: EnsureIssueConfig
diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts
index 5519512ac4..aa069ebbf2 100644
--- a/lib/platform/gitea/index.ts
+++ b/lib/platform/gitea/index.ts
@@ -468,11 +468,6 @@ const platform: Platform = {
     return config.prList;
   },
 
-  /* istanbul ignore next */
-  async getPrFiles(pr: Pr): Promise<string[]> {
-    return git.getBranchFiles(pr.branchName, pr.targetBranch);
-  },
-
   async getPr(number: number): Promise<Pr | null> {
     // Search for pull request in cached list or attempt to query directly
     const prList = await platform.getPrList();
@@ -883,7 +878,6 @@ export const {
   getPr,
   getPrBody,
   getPrList,
-  getPrFiles,
   getRepoForceRebase,
   getRepos,
   getVulnerabilityAlerts,
diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts
index 1dba085435..b73778b55e 100644
--- a/lib/platform/github/index.ts
+++ b/lib/platform/github/index.ts
@@ -907,11 +907,6 @@ export async function getPrList(): Promise<Pr[]> {
   return config.prList;
 }
 
-/* istanbul ignore next */
-export async function getPrFiles(pr: Pr): Promise<string[]> {
-  return git.getBranchFiles(pr.branchName, pr.targetBranch);
-}
-
 export async function findPr({
   branchName,
   prTitle,
diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts
index 5f81f17376..4d524e29a1 100644
--- a/lib/platform/gitlab/index.ts
+++ b/lib/platform/gitlab/index.ts
@@ -1017,11 +1017,6 @@ export async function getPrList(): Promise<Pr[]> {
   return config.prList;
 }
 
-/* istanbul ignore next */
-export async function getPrFiles(pr: Pr): Promise<string[]> {
-  return git.getBranchFiles(pr.branchName, pr.targetBranch);
-}
-
 function matchesState(state: string, desiredState: string): boolean {
   if (desiredState === PR_STATE_ALL) {
     return true;
diff --git a/lib/util/git/__snapshots__/index.spec.ts.snap b/lib/util/git/__snapshots__/index.spec.ts.snap
index f068e0bb58..0ad158e031 100644
--- a/lib/util/git/__snapshots__/index.spec.ts.snap
+++ b/lib/util/git/__snapshots__/index.spec.ts.snap
@@ -2,13 +2,7 @@
 
 exports[`platform/git getBranchCommit(branchName) should throw if branch does not exist 1`] = `[Error: Cannot fetch commit for branch that does not exist: not_found]`;
 
-exports[`platform/git getBranchFiles(branchName, baseBranchName?) detects changed files 1`] = `
-Array [
-  "some-new-file",
-]
-`;
-
-exports[`platform/git getBranchFiles(branchName, baseBranchName?) detects changed files compared to current base branch 1`] = `
+exports[`platform/git getBranchFiles(branchName) detects changed files compared to current base branch 1`] = `
 Array [
   "some-new-file",
 ]
diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts
index 592e6c204a..635fe2d2e9 100644
--- a/lib/util/git/index.spec.ts
+++ b/lib/util/git/index.spec.ts
@@ -145,25 +145,7 @@ describe('platform/git', () => {
     });
   });
 
-  describe('getBranchFiles(branchName, baseBranchName?)', () => {
-    it('detects changed files', async () => {
-      const hex = await git.getBranchCommit('master');
-      await git.createBranch('renovate/branch_with_changes', hex);
-      const file = {
-        name: 'some-new-file',
-        contents: 'some new-contents',
-      };
-      await git.commitFiles({
-        branchName: 'renovate/branch_with_changes',
-        files: [file],
-        message: 'Create something',
-      });
-      const branchFiles = await git.getBranchFiles(
-        'renovate/branch_with_changes',
-        'master'
-      );
-      expect(branchFiles).toMatchSnapshot();
-    });
+  describe('getBranchFiles(branchName)', () => {
     it('detects changed files compared to current base branch', async () => {
       const hex = await git.getBranchCommit('master');
       await git.createBranch('renovate/branch_with_changes', hex);
diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts
index 6612fb90d1..d2d3aea5c1 100644
--- a/lib/util/git/index.ts
+++ b/lib/util/git/index.ts
@@ -435,15 +435,9 @@ export async function getBranchLastCommitTime(
   }
 }
 
-export async function getBranchFiles(
-  branchName: string,
-  baseBranchName?: string
-): Promise<string[]> {
+export async function getBranchFiles(branchName: string): Promise<string[]> {
   try {
-    const diff = await git.diffSummary([
-      branchName,
-      baseBranchName || config.baseBranch,
-    ]);
+    const diff = await git.diffSummary([branchName, config.baseBranch]);
     return diff.files.map((file) => file.file);
   } catch (err) /* istanbul ignore next */ {
     checkForPlatformFailure(err);
diff --git a/lib/workers/pr/code-owners.spec.ts b/lib/workers/pr/code-owners.spec.ts
index f353cb7c68..d6999d28b3 100644
--- a/lib/workers/pr/code-owners.spec.ts
+++ b/lib/workers/pr/code-owners.spec.ts
@@ -1,9 +1,10 @@
 import { mock } from 'jest-mock-extended';
-import { fs, platform } from '../../../test/util';
+import { fs, git } from '../../../test/util';
 import { Pr } from '../../platform';
 import { codeOwnersForPr } from './code-owners';
 
 jest.mock('../../util/fs');
+jest.mock('../../util/git');
 
 describe('workers/pr/code-owners', () => {
   describe('codeOwnersForPr', () => {
@@ -14,7 +15,7 @@ describe('workers/pr/code-owners', () => {
     });
     it('returns global code owner', async () => {
       fs.readLocalFile.mockResolvedValueOnce(['* @jimmy'].join('\n'));
-      platform.getPrFiles.mockResolvedValueOnce(['README.md']);
+      git.getBranchFiles.mockResolvedValueOnce(['README.md']);
       const codeOwners = await codeOwnersForPr(pr);
       expect(codeOwners).toEqual(['@jimmy']);
     });
@@ -22,7 +23,7 @@ describe('workers/pr/code-owners', () => {
       fs.readLocalFile.mockResolvedValueOnce(
         ['* @jimmy', 'package.json @john @maria'].join('\n')
       );
-      platform.getPrFiles.mockResolvedValueOnce(['package.json']);
+      git.getBranchFiles.mockResolvedValueOnce(['package.json']);
       const codeOwners = await codeOwnersForPr(pr);
       expect(codeOwners).toEqual(['@john', '@maria']);
     });
@@ -36,13 +37,13 @@ describe('workers/pr/code-owners', () => {
           ' package.json @john @maria  ',
         ].join('\n')
       );
-      platform.getPrFiles.mockResolvedValueOnce(['package.json']);
+      git.getBranchFiles.mockResolvedValueOnce(['package.json']);
       const codeOwners = await codeOwnersForPr(pr);
       expect(codeOwners).toEqual(['@john', '@maria']);
     });
     it('returns empty array when no code owners set', async () => {
       fs.readLocalFile.mockResolvedValueOnce(null);
-      platform.getPrFiles.mockResolvedValueOnce(['package.json']);
+      git.getBranchFiles.mockResolvedValueOnce(['package.json']);
       const codeOwners = await codeOwnersForPr(pr);
       expect(codeOwners).toEqual([]);
     });
@@ -50,7 +51,7 @@ describe('workers/pr/code-owners', () => {
       fs.readLocalFile.mockResolvedValueOnce(
         ['package-lock.json @mike'].join('\n')
       );
-      platform.getPrFiles.mockResolvedValueOnce(['yarn.lock']);
+      git.getBranchFiles.mockResolvedValueOnce(['yarn.lock']);
       const codeOwners = await codeOwnersForPr(pr);
       expect(codeOwners).toEqual([]);
     });
@@ -75,7 +76,7 @@ describe('workers/pr/code-owners', () => {
           }
           return Promise.resolve(null);
         });
-        platform.getPrFiles.mockResolvedValueOnce(['README.md']);
+        git.getBranchFiles.mockResolvedValueOnce(['README.md']);
         const codeOwners = await codeOwnersForPr(pr);
         expect(codeOwners).toEqual(['@mike']);
       });
diff --git a/lib/workers/pr/code-owners.ts b/lib/workers/pr/code-owners.ts
index d2522d5068..21930d3104 100644
--- a/lib/workers/pr/code-owners.ts
+++ b/lib/workers/pr/code-owners.ts
@@ -1,7 +1,8 @@
 import ignore from 'ignore';
 import { logger } from '../../logger';
-import { Pr, platform } from '../../platform';
+import { Pr } from '../../platform';
 import { readLocalFile } from '../../util/fs';
+import { getBranchFiles } from '../../util/git';
 
 export async function codeOwnersForPr(pr: Pr): Promise<string[]> {
   try {
@@ -15,7 +16,7 @@ export async function codeOwnersForPr(pr: Pr): Promise<string[]> {
       return [];
     }
 
-    const prFiles = await platform.getPrFiles(pr);
+    const prFiles = await getBranchFiles(pr.branchName);
     const rules = codeOwnersFile
       .split('\n')
       .map((line) => line.trim())
-- 
GitLab