feat(v2): docusaurus deploy: ability to configure port in git url (#4545)

* Creating a way to configure the port used on the guthub deploy.

* Fixing some mistakes

Documenting

Adding githubPort documentation on docusaurus.config.js.

Addind SSH protocol prefix. ssh://

Using the default protocol port instead of define it on the code.

Prettify.

* Fixing some mistakes

Documenting

Adding githubPort documentation on docusaurus.config.js.

Addind SSH protocol prefix. ssh://

Using the default protocol port instead of define it on the code.

Prettify.

* Isolating the logic to generate the url and testing it.

* Changing all the names used on tests to something more unserstandable.

* Prettify

Co-authored-by: Tales Porto <t.andrade-porto@klarna.com>
This commit is contained in:
Tales Porto 2021-04-09 18:05:16 +02:00 committed by GitHub
parent 4efe6824b3
commit e99bb43823
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 132 additions and 8 deletions

View file

@ -34,6 +34,7 @@ export interface DocusaurusConfig {
organizationName?: string;
projectName?: string;
githubHost?: string;
githubPort?: string;
plugins?: PluginConfig[];
themes?: PluginConfig[];
presets?: PresetConfig[];

View file

@ -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',
);
});
});

View file

@ -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`;
}

View file

@ -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)}`,

View file

@ -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`

View file

@ -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}