From 43fa615e05ef5b95dbf0e93b19e37baaae6649e9 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 19 Jun 2023 15:10:21 +0200
Subject: [PATCH] fix: Revert "feat(github): forkOrgs, forkCreate (#18770)"

This reverts commit 2f77695ba5171532ecc4946288c0d00e52f03bc1.
---
 docs/usage/self-hosted-configuration.md   | 20 +--------
 lib/config/options/index.ts               | 22 ----------
 lib/constants/error-messages.ts           |  1 -
 lib/modules/platform/github/index.spec.ts | 24 +---------
 lib/modules/platform/github/index.ts      | 53 +++++++----------------
 lib/modules/platform/github/types.ts      |  1 -
 lib/modules/platform/types.ts             |  2 -
 lib/workers/repository/error.spec.ts      |  2 -
 lib/workers/repository/error.ts           |  6 ---
 9 files changed, 19 insertions(+), 112 deletions(-)

diff --git a/docs/usage/self-hosted-configuration.md b/docs/usage/self-hosted-configuration.md
index 342cc4b221..a46d563be6 100644
--- a/docs/usage/self-hosted-configuration.md
+++ b/docs/usage/self-hosted-configuration.md
@@ -415,26 +415,10 @@ In practice, it is implemented by converting the `force` configuration into a `p
 This is set to `true` by default, meaning that any settings (such as `schedule`) take maximum priority even against custom settings existing inside individual repositories.
 It will also override any settings in `packageRules`.
 
-## forkCreate
-
-If you're using an app token which is _not_ allowed to fork, set this to `false`.
-
-## forkOrgs
-
-This configuration option lets you choose which organization or account you want repositories forked into when "fork mode" is enabled.
-
-You may set more than one organization or account.
-This can be handy if you're migrating from user-based forks to organization-based forks.
-
-If you've set multiple `forkOrgs` then Renovate will:
-
-1. Check if a fork exists in the preferred organization
-1. If no fork exists in the preferred org: Renovate checks the fallback organizations
-
-Renovate always creates the new fork in the _first_ organization in the `forkOrgs` list.
-
 ## forkToken
 
+You probably don't need this option - it is an experimental setting developed for the Forking Renovate hosted GitHub App.
+
 If this value is configured then Renovate:
 
 - forks the target repository into the account that owns the PAT
diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts
index d1beac3bb6..ce143bdf4c 100644
--- a/lib/config/options/index.ts
+++ b/lib/config/options/index.ts
@@ -444,28 +444,6 @@ const options: RenovateOptions[] = [
     supportedPlatforms: ['github'],
     experimental: true,
   },
-  {
-    name: 'forkOrgs',
-    description:
-      'The preferred organizations to create or find forked repositories, when in fork mode.',
-    stage: 'repository',
-    type: 'array',
-    subType: 'string',
-    globalOnly: true,
-    supportedPlatforms: ['github'],
-    experimental: true,
-  },
-  {
-    name: 'forkCreate',
-    description:
-      'Decide if Renovate creates a fork at runtime when in fork mode.',
-    stage: 'repository',
-    type: 'boolean',
-    default: true,
-    globalOnly: true,
-    supportedPlatforms: ['github'],
-    experimental: true,
-  },
   {
     name: 'githubTokenWarn',
     description: 'Display warnings about GitHub token not being set.',
diff --git a/lib/constants/error-messages.ts b/lib/constants/error-messages.ts
index 4e047fac01..95daa453ee 100644
--- a/lib/constants/error-messages.ts
+++ b/lib/constants/error-messages.ts
@@ -23,7 +23,6 @@ export const REPOSITORY_ACCESS_FORBIDDEN = 'forbidden';
 export const REPOSITORY_ARCHIVED = 'archived';
 export const REPOSITORY_BLOCKED = 'blocked';
 export const REPOSITORY_CANNOT_FORK = 'cannot-fork';
-export const REPOSITORY_NO_FORK = 'no-fork';
 export const REPOSITORY_DISABLED = 'disabled';
 export const REPOSITORY_CLOSED_ONBOARDING = 'disabled-closed-onboarding';
 export const REPOSITORY_DISABLED_BY_CONFIG = 'disabled-by-config';
diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts
index cc52c835e5..fd917c0a98 100644
--- a/lib/modules/platform/github/index.spec.ts
+++ b/lib/modules/platform/github/index.spec.ts
@@ -7,7 +7,6 @@ import {
   PLATFORM_UNKNOWN_ERROR,
   REPOSITORY_CANNOT_FORK,
   REPOSITORY_NOT_FOUND,
-  REPOSITORY_NO_FORK,
   REPOSITORY_RENAMED,
 } from '../../../constants/error-messages';
 import * as repository from '../../../util/cache/repository';
@@ -349,26 +348,10 @@ describe('modules/platform/github/index', () => {
       const config = await github.initRepo({
         repository: 'some/repo',
         forkToken: 'true',
-        forkCreate: true,
       });
       expect(config).toMatchSnapshot();
     });
 
-    it('throws when no forking allowed', async () => {
-      const repo = 'some/repo';
-      const branch = 'master';
-      const scope = httpMock.scope(githubApiHost);
-      forkInitRepoMock(scope, repo, false, 200, branch);
-      await expect(
-        github.initRepo({
-          repository: 'some/repo',
-          forkToken: 'true',
-          forkOrgs: ['renovate-bot'],
-          forkCreate: false,
-        })
-      ).rejects.toThrow(REPOSITORY_NO_FORK);
-    });
-
     it('throws when cannot fork due to username error', async () => {
       const repo = 'some/repo';
       const branch = 'master';
@@ -379,7 +362,6 @@ describe('modules/platform/github/index', () => {
         github.initRepo({
           repository: 'some/repo',
           forkToken: 'true',
-          forkCreate: true,
         })
       ).rejects.toThrow(REPOSITORY_CANNOT_FORK);
     });
@@ -415,13 +397,12 @@ describe('modules/platform/github/index', () => {
       scope.get('/user').reply(200, {
         login: 'forked',
       });
+      // getBranchCommit
       scope.post(`/repos/${repo}/forks`).reply(500);
       await expect(
         github.initRepo({
           repository: 'some/repo',
           forkToken: 'true',
-          forkOrgs: ['forked'],
-          forkCreate: true,
         })
       ).rejects.toThrow(REPOSITORY_CANNOT_FORK);
     });
@@ -429,10 +410,10 @@ describe('modules/platform/github/index', () => {
     it('should update fork when using forkToken', async () => {
       const scope = httpMock.scope(githubApiHost);
       forkInitRepoMock(scope, 'some/repo', true);
-      scope.patch('/repos/forked/repo/git/refs/heads/master').reply(200);
       scope.get('/user').reply(200, {
         login: 'forked',
       });
+      scope.patch('/repos/forked/repo/git/refs/heads/master').reply(200);
       const config = await github.initRepo({
         repository: 'some/repo',
         forkToken: 'true',
@@ -2307,7 +2288,6 @@ describe('modules/platform/github/index', () => {
         await github.initRepo({
           repository: 'some/repo',
           forkToken: 'true',
-          forkCreate: true,
         });
       });
 
diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts
index 8768bcb1d7..d00cc2df63 100644
--- a/lib/modules/platform/github/index.ts
+++ b/lib/modules/platform/github/index.ts
@@ -20,7 +20,6 @@ import {
   REPOSITORY_EMPTY,
   REPOSITORY_FORKED,
   REPOSITORY_NOT_FOUND,
-  REPOSITORY_NO_FORK,
   REPOSITORY_RENAMED,
 } from '../../../constants/error-messages';
 import { logger } from '../../../logger';
@@ -258,29 +257,18 @@ export async function getJsonFile(
   return JSON5.parse(raw);
 }
 
-export async function getForkOrgs(
-  token: string,
-  forkOrgs?: string[]
-): Promise<string[]> {
-  const destinationOrgs = forkOrgs ?? [];
-  if (!destinationOrgs.length) {
-    if (!config.renovateForkUser) {
-      try {
-        logger.debug('Determining fork user from API');
-        const userDetails = await getUserDetails(
-          platformConfig.endpoint,
-          token
-        );
-        config.renovateForkUser = userDetails.username;
-      } catch (err) {
-        logger.debug({ err }, 'Error getting username for forkToken');
-      }
-    }
-    if (config.renovateForkUser) {
-      destinationOrgs.push(config.renovateForkUser);
+export async function getForkOrgs(token: string): Promise<string[]> {
+  // This function will be adapted later to support configured forkOrgs
+  if (!config.renovateForkUser) {
+    try {
+      logger.debug('Determining fork user from API');
+      const userDetails = await getUserDetails(platformConfig.endpoint, token);
+      config.renovateForkUser = userDetails.username;
+    } catch (err) {
+      logger.debug({ err }, 'Error getting username for forkToken');
     }
   }
-  return destinationOrgs;
+  return config.renovateForkUser ? [config.renovateForkUser] : [];
 }
 
 export async function listForks(
@@ -311,11 +299,10 @@ export async function listForks(
 
 export async function findFork(
   token: string,
-  repository: string,
-  forkOrgs?: string[]
+  repository: string
 ): Promise<GhRestRepo | null> {
   const forks = await listForks(token, repository);
-  const orgs = await getForkOrgs(token, forkOrgs);
+  const orgs = await getForkOrgs(token);
   if (!orgs.length) {
     throw new Error(REPOSITORY_CANNOT_FORK);
   }
@@ -333,17 +320,14 @@ export async function findFork(
 
 export async function createFork(
   token: string,
-  repository: string,
-  forkOrgs?: string[]
+  repository: string
 ): Promise<GhRestRepo> {
   let forkedRepo: GhRestRepo | undefined;
   try {
-    const organization = (await getForkOrgs(token, forkOrgs))[0];
     forkedRepo = (
       await githubApi.postJson<GhRestRepo>(`repos/${repository}/forks`, {
         token,
         body: {
-          organization,
           name: config.parentRepo!.replace('/', '-_-'),
           default_branch_only: true, // no baseBranches support yet
         },
@@ -364,8 +348,6 @@ export async function createFork(
 export async function initRepo({
   endpoint,
   repository,
-  forkCreate,
-  forkOrgs,
   forkToken,
   renovateUsername,
   cloneSubmodules,
@@ -507,7 +489,7 @@ export async function initRepo({
     // save parent name then delete
     config.parentRepo = config.repository;
     config.repository = null;
-    let forkedRepo = await findFork(forkToken, repository, forkOrgs);
+    let forkedRepo = await findFork(forkToken, repository);
     if (forkedRepo) {
       config.repository = forkedRepo.full_name;
       const forkDefaultBranch = forkedRepo.default_branch;
@@ -583,15 +565,10 @@ export async function initRepo({
         }
         throw new ExternalHostError(err);
       }
-    } else if (forkCreate) {
+    } else {
       logger.debug('Forked repo is not found - attempting to create it');
       forkedRepo = await createFork(forkToken, repository);
       config.repository = forkedRepo.full_name;
-    } else {
-      logger.debug(
-        'Forked repo is not found and forkCreate=false so need to abort'
-      );
-      throw new Error(REPOSITORY_NO_FORK);
     }
   }
 
diff --git a/lib/modules/platform/github/types.ts b/lib/modules/platform/github/types.ts
index 0f0af37526..ef01406661 100644
--- a/lib/modules/platform/github/types.ts
+++ b/lib/modules/platform/github/types.ts
@@ -90,7 +90,6 @@ export interface LocalRepoConfig {
   prReviewsRequired: boolean;
   repoForceRebase?: boolean;
   parentRepo: string | null;
-  forkOrgs?: string[];
   forkToken?: string;
   prList: GhPr[] | null;
   issueList: any[] | null;
diff --git a/lib/modules/platform/types.ts b/lib/modules/platform/types.ts
index 09b5464009..4a0d5a825b 100644
--- a/lib/modules/platform/types.ts
+++ b/lib/modules/platform/types.ts
@@ -37,8 +37,6 @@ export interface RepoParams {
   repository: string;
   endpoint?: string;
   gitUrl?: GitUrlOption;
-  forkCreate?: boolean;
-  forkOrgs?: string[];
   forkToken?: string;
   forkProcessing?: 'enabled' | 'disabled';
   renovateUsername?: string;
diff --git a/lib/workers/repository/error.spec.ts b/lib/workers/repository/error.spec.ts
index ea625ea473..07cd0838ed 100644
--- a/lib/workers/repository/error.spec.ts
+++ b/lib/workers/repository/error.spec.ts
@@ -20,7 +20,6 @@ import {
   REPOSITORY_FORKED,
   REPOSITORY_MIRRORED,
   REPOSITORY_NOT_FOUND,
-  REPOSITORY_NO_FORK,
   REPOSITORY_NO_PACKAGE_FILES,
   REPOSITORY_RENAMED,
   REPOSITORY_UNINITIATED,
@@ -54,7 +53,6 @@ describe('workers/repository/error', () => {
       CONFIG_VALIDATION,
       REPOSITORY_ARCHIVED,
       REPOSITORY_MIRRORED,
-      REPOSITORY_NO_FORK,
       REPOSITORY_RENAMED,
       REPOSITORY_BLOCKED,
       REPOSITORY_NOT_FOUND,
diff --git a/lib/workers/repository/error.ts b/lib/workers/repository/error.ts
index a15a55b2e9..bcb0cffc91 100644
--- a/lib/workers/repository/error.ts
+++ b/lib/workers/repository/error.ts
@@ -24,7 +24,6 @@ import {
   REPOSITORY_MIRRORED,
   REPOSITORY_NOT_FOUND,
   REPOSITORY_NO_CONFIG,
-  REPOSITORY_NO_FORK,
   REPOSITORY_NO_PACKAGE_FILES,
   REPOSITORY_RENAMED,
   REPOSITORY_UNINITIATED,
@@ -69,11 +68,6 @@ export default async function handleError(
     delete config.branchList;
     return err.message;
   }
-  if (err.message === REPOSITORY_NO_FORK) {
-    logger.info('Repository has no fork - skipping');
-    delete config.branchList;
-    return err.message;
-  }
   if (err.message === REPOSITORY_MIRRORED) {
     logger.info('Repository is a mirror - skipping');
     delete config.branchList;
-- 
GitLab