diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index d91b30f577..988626f552 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -34,6 +34,7 @@ export interface DocusaurusConfig { organizationName?: string; projectName?: string; githubHost?: string; + githubPort?: string; plugins?: PluginConfig[]; themes?: PluginConfig[]; presets?: PresetConfig[]; diff --git a/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts b/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts new file mode 100644 index 0000000000..46f445fc5b --- /dev/null +++ b/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts @@ -0,0 +1,57 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {buildUrl} from '../buildRemoteBranchUrl'; + +describe('remoteeBranchUrl', () => { + test('should build a normal ssh url', async () => { + const url = buildUrl( + 'github.com', + undefined, + undefined, + 'facebook', + 'docusaurus', + true, + ); + expect(url).toEqual('git@github.com:facebook/docusaurus.git'); + }); + test('should build a ssh url with port', async () => { + const url = buildUrl( + 'github.com', + '422', + undefined, + 'facebook', + 'docusaurus', + true, + ); + expect(url).toEqual('ssh://git@github.com:422/facebook/docusaurus.git'); + }); + test('should build a normal http url', async () => { + const url = buildUrl( + 'github.com', + undefined, + 'user:pass', + 'facebook', + 'docusaurus', + false, + ); + expect(url).toEqual('https://user:pass@github.com/facebook/docusaurus.git'); + }); + test('should build a normal http url', async () => { + const url = buildUrl( + 'github.com', + '5433', + 'user:pass', + 'facebook', + 'docusaurus', + false, + ); + expect(url).toEqual( + 'https://user:pass@github.com:5433/facebook/docusaurus.git', + ); + }); +}); diff --git a/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts b/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts new file mode 100644 index 0000000000..44cbc949e5 --- /dev/null +++ b/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts @@ -0,0 +1,50 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export function buildUrl( + githubHost: string, + githubPort: string | undefined, + gitCredentials: string | undefined, + organizationName: string, + projectName: string, + useSSH: boolean | undefined, +) { + return useSSH + ? buildSshUrl(githubHost, organizationName, projectName, githubPort) + : buildHttpsUrl( + gitCredentials, + githubHost, + organizationName, + projectName, + githubPort, + ); +} + +function buildSshUrl( + githubHost: string, + organizationName: string, + projectName: string, + githubPort: string | undefined, +) { + if (githubPort) { + return `ssh://git@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`; + } + return `git@${githubHost}:${organizationName}/${projectName}.git`; +} + +function buildHttpsUrl( + gitCredentials: string | undefined, + githubHost: string, + organizationName: string, + projectName: string, + githubPort: string | undefined, +) { + if (githubPort) { + return `https://${gitCredentials}@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`; + } + return `https://${gitCredentials}@${githubHost}/${organizationName}/${projectName}.git`; +} diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 748f268cd9..ddf1e0695a 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -13,6 +13,7 @@ import build from './build'; import {BuildCLIOptions} from '@docusaurus/types'; import path from 'path'; import os from 'os'; +import {buildUrl} from './buildRemoteBranchUrl'; // GIT_PASS env variable should not appear in logs function obfuscateGitPass(str) { @@ -99,21 +100,23 @@ export default async function deploy( const githubHost = process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com'; + const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort; - const useSSH = process.env.USE_SSH; const gitPass: string | undefined = process.env.GIT_PASS; let gitCredentials = `${gitUser}`; if (gitPass) { gitCredentials = `${gitCredentials}:${gitPass}`; } - const sshRemoteBranch: string = `git@${githubHost}:${organizationName}/${projectName}.git`; - const nonSshRemoteBranch: string = `https://${gitCredentials}@${githubHost}/${organizationName}/${projectName}.git`; - - const remoteBranch = - useSSH && useSSH.toLowerCase() === 'true' - ? sshRemoteBranch - : nonSshRemoteBranch; + const useSSH = process.env.USE_SSH; + const remoteBranch = buildUrl( + githubHost, + githubPort, + gitCredentials, + organizationName, + projectName, + useSSH !== undefined && useSSH.toLowerCase() === 'true', + ); console.log( `${chalk.cyan('Remote branch:')} ${obfuscateGitPass(remoteBranch)}`, diff --git a/website/docs/api/docusaurus.config.js.md b/website/docs/api/docusaurus.config.js.md index 8857deb278..5af7569d27 100644 --- a/website/docs/api/docusaurus.config.js.md +++ b/website/docs/api/docusaurus.config.js.md @@ -204,6 +204,18 @@ module.exports = { }; ``` +### `githubPort` {#githubPort} + +- Type: `string` + +The port of your server. Useful if you are using GitHub Enterprise. + +```js title="docusaurus.config.js" +module.exports = { + githubPort: '22', +}; +``` + ### `themeConfig` {#themeconfig} - Type: `Object` diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index 3c163d3c9b..175a7bab80 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -99,6 +99,7 @@ GitHub enterprise installations should work in the same manner as github.com; yo | Name | Description | | ------------- | ----------------------------------------------- | | `GITHUB_HOST` | The domain name of your GitHub enterprise site. | +| `GITHUB_PORT` | The port of your GitHub enterprise site. | ### Deploy {#deploy}