Skip to content

Commit

Permalink
feat(v2): docusaurus deploy: ability to configure port in git url (#4545
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>
  • Loading branch information
talesporto and Tales Porto authored Apr 9, 2021
1 parent 4efe682 commit e99bb43
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface DocusaurusConfig {
organizationName?: string;
projectName?: string;
githubHost?: string;
githubPort?: string;
plugins?: PluginConfig[];
themes?: PluginConfig[];
presets?: PresetConfig[];
Expand Down
Original file line number Diff line number Diff line change
@@ -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('[email protected]: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://[email protected]: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:[email protected]/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:[email protected]:5433/facebook/docusaurus.git',
);
});
});
50 changes: 50 additions & 0 deletions packages/docusaurus/src/commands/buildRemoteBranchUrl.ts
Original file line number Diff line number Diff line change
@@ -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`;
}
19 changes: 11 additions & 8 deletions packages/docusaurus/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)}`,
Expand Down
12 changes: 12 additions & 0 deletions website/docs/api/docusaurus.config.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions website/docs/deployment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit e99bb43

Please sign in to comment.