From 429ee29d83c7e633ca1be23a96269c246c584faa Mon Sep 17 00:00:00 2001 From: wpyoga Date: Sun, 31 Oct 2021 01:12:10 +0700 Subject: [PATCH 01/14] feat: allow GIT_USER env var to be unset if SSH is used --- examples/classic-typescript/README.md | 10 ++++++++- examples/classic/README.md | 10 ++++++++- examples/facebook/README.md | 10 ++++++++- .../templates/facebook/README.md | 10 ++++++++- .../templates/shared/README.md | 10 ++++++++- packages/docusaurus/src/commands/deploy.ts | 22 ++++++++++++++----- website/docs/deployment.mdx | 13 +++-------- 7 files changed, 64 insertions(+), 21 deletions(-) diff --git a/examples/classic-typescript/README.md b/examples/classic-typescript/README.md index 55d0c3ef41c9..aaba2fa1e16e 100644 --- a/examples/classic-typescript/README.md +++ b/examples/classic-typescript/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/examples/classic/README.md b/examples/classic/README.md index 55d0c3ef41c9..aaba2fa1e16e 100644 --- a/examples/classic/README.md +++ b/examples/classic/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/examples/facebook/README.md b/examples/facebook/README.md index 1b65ca0890bd..6ffad61fe7c7 100644 --- a/examples/facebook/README.md +++ b/examples/facebook/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/create-docusaurus/templates/facebook/README.md b/packages/create-docusaurus/templates/facebook/README.md index 1b65ca0890bd..6ffad61fe7c7 100644 --- a/packages/create-docusaurus/templates/facebook/README.md +++ b/packages/create-docusaurus/templates/facebook/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/create-docusaurus/templates/shared/README.md b/packages/create-docusaurus/templates/shared/README.md index 55d0c3ef41c9..aaba2fa1e16e 100644 --- a/packages/create-docusaurus/templates/shared/README.md +++ b/packages/create-docusaurus/templates/shared/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 7c157b164f83..0873b01dba52 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -63,8 +63,22 @@ This behavior can have SEO impacts and create relative link issues. throw new Error('Git not installed or on the PATH!'); } + const currentRepoUrl = shell + .exec('git config --get remote.origin.url') + .stdout.trim(); + + const repoUrlUseSSH = + currentRepoUrl.match(/^ssh:\/\//) !== null || + currentRepoUrl.match(/^([\w\-]+@)?[\w.\-]+:[\w.\-\/_]+(\.git)?/) !== null; + + const envUseSSH = + process.env.USE_SSH !== undefined && + process.env.USE_SSH.toLowerCase() === 'true'; + + const useSSH = envUseSSH || repoUrlUseSSH; + const gitUser = process.env.GIT_USER; - if (!gitUser) { + if (!gitUser && !useSSH) { throw new Error('Please set the GIT_USER environment variable!'); } @@ -128,14 +142,13 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`); gitCredentials = `${gitCredentials}:${gitPass}`; } - const useSSH = process.env.USE_SSH; const remoteBranch = buildUrl( githubHost, githubPort, gitCredentials, organizationName, projectName, - useSSH !== undefined && useSSH.toLowerCase() === 'true', + useSSH, ); console.log( @@ -143,9 +156,6 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`); ); // Check if this is a cross-repo publish. - const currentRepoUrl = shell - .exec('git config --get remote.origin.url') - .stdout.trim(); const crossRepoPublish = !currentRepoUrl.endsWith( `${organizationName}/${projectName}.git`, ); diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index c593ace7cb5f..fdd44f4cd611 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -116,20 +116,15 @@ By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb. ### Environment settings {#environment-settings} -Specify the Git user as an environment variable. - -| Name | Description | -| --- | --- | -| `GIT_USER` | The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your GitHub username. The specified `GIT_USER` must have push access to the repository specified in the combination of `organizationName` and `projectName`. | - -Optional parameters, also set as environment variables: +Optional parameters, set as environment variables: | Name | Description | | --- | --- | | `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. | +| `GIT_USER` | The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your GitHub username. The specified `GIT_USER` must have push access to the repository specified in the combination of `organizationName` and `projectName`. If SSH is not used, this env variable is required. Otherwise, it is ignored. | +| `GIT_PASS` | Password (or token) of the `git` user (specified by `GIT_USER`). For example, to facilitate non-interactive deployment (e.g. continuous deployment) | | `DEPLOYMENT_BRANCH` | The branch that the website will be deployed to, defaults to `gh-pages`. For GitHub Pages Organization repos (`config.projectName` ending in `github.io`), this env variable is required. | | `CURRENT_BRANCH` | The branch that contains the latest docs changes that will be deployed. Usually, the branch will be `main`, but it could be any branch (default or otherwise) except for `gh-pages`. If nothing is set for this variable, then the current branch will be used. | -| `GIT_PASS` | Password (or token) of the `git` user (specified by `GIT_USER`). For example, to facilitate non-interactive deployment (e.g. continuous deployment) | GitHub enterprise installations should work in the same manner as github.com; you only need to set the organization's GitHub Enterprise host as an environment variable: @@ -229,7 +224,6 @@ jobs: - name: Release to GitHub Pages env: USE_SSH: true - GIT_USER: git run: | git config --global user.email "actions@github.com" git config --global user.name "gh-actions" @@ -361,7 +355,6 @@ trigger: - npm run deploy environment: USE_SSH: true - GIT_USER: $DRONE_COMMIT_AUTHOR GITHUB_PRIVATE_KEY: from_secret: "git_deploy_private_key" ``` From 7f9fcf8b7787f9ff8b829fef8c9d08c5a601e2ef Mon Sep 17 00:00:00 2001 From: William Poetra Yoga Date: Sun, 31 Oct 2021 12:28:22 +0700 Subject: [PATCH 02/14] fix: packages/docusaurus/src/commands/deploy.ts Co-authored-by: Joshua Chen --- packages/docusaurus/src/commands/deploy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 0873b01dba52..6fe294f9c7a0 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -69,7 +69,7 @@ This behavior can have SEO impacts and create relative link issues. const repoUrlUseSSH = currentRepoUrl.match(/^ssh:\/\//) !== null || - currentRepoUrl.match(/^([\w\-]+@)?[\w.\-]+:[\w.\-\/_]+(\.git)?/) !== null; + currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w.\-/_]+(\.git)?/) !== null; const envUseSSH = process.env.USE_SSH !== undefined && From 729f92242a48758bea3ed397bdf24e3115ebf5f5 Mon Sep 17 00:00:00 2001 From: William Poetra Yoga Date: Sun, 31 Oct 2021 11:29:22 +0700 Subject: [PATCH 03/14] feat: allow user to specify deploymentBranch property in docusaurus.config.js (#5841) * feat: allow user to specify deploymentBranch property in docusaurus.config.js * docs: remove extra backtick * docs: fix broken code block --- packages/docusaurus-types/src/index.d.ts | 1 + packages/docusaurus/src/commands/deploy.ts | 13 +++++++++---- packages/docusaurus/src/server/configValidation.ts | 1 + website/docs/api/docusaurus.config.js.md | 12 ++++++++++++ website/docs/configuration.md | 2 +- website/docs/deployment.mdx | 3 ++- 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index 7a87d6b543e3..dd676a3cedea 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -39,6 +39,7 @@ export interface DocusaurusConfig { noIndex: boolean; organizationName?: string; projectName?: string; + deploymentBranch?: string; githubHost?: string; githubPort?: string; plugins?: PluginConfig[]; diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 6fe294f9c7a0..05cc6e6860d9 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -123,13 +123,18 @@ This behavior can have SEO impacts and create relative link issues. // - Site url: https://.github.io const isGitHubPagesOrganizationDeploy = projectName.indexOf('.github.io') !== -1; - if (isGitHubPagesOrganizationDeploy && !process.env.DEPLOYMENT_BRANCH) { + if ( + isGitHubPagesOrganizationDeploy && + !process.env.DEPLOYMENT_BRANCH && + !siteConfig.deploymentBranch + ) { throw new Error(`For GitHub pages organization deployments, 'docusaurus deploy' does not assume anymore that 'master' is your default Git branch. -Please provide the branch name to deploy to as an environment variable. -Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`); +Please provide the branch name to deploy to as an environment variable, for example DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master . +You can also set the deploymentBranch property in docusaurus.config.js .`); } - const deploymentBranch = process.env.DEPLOYMENT_BRANCH || 'gh-pages'; + const deploymentBranch = + process.env.DEPLOYMENT_BRANCH || siteConfig.deploymentBranch || 'gh-pages'; console.log(`${chalk.cyan('deploymentBranch:')} ${deploymentBranch}`); const githubHost = diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index 7f181622b02a..13567f3bbc8d 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -143,6 +143,7 @@ export const ConfigSchema = Joi.object({ .default(DEFAULT_CONFIG.onDuplicateRoutes), organizationName: Joi.string().allow(''), projectName: Joi.string().allow(''), + deploymentBranch: Joi.string().optional(), customFields: Joi.object().unknown().default(DEFAULT_CONFIG.customFields), githubHost: Joi.string(), plugins: Joi.array().items(PluginSchema).default(DEFAULT_CONFIG.plugins), diff --git a/website/docs/api/docusaurus.config.js.md b/website/docs/api/docusaurus.config.js.md index a5425a3e488a..638388dd2b85 100644 --- a/website/docs/api/docusaurus.config.js.md +++ b/website/docs/api/docusaurus.config.js.md @@ -194,6 +194,18 @@ module.exports = { }; ``` +### `deploymentBranch` {#deploymentbranch} + +- Type: `string` + +The name of the branch to deploy the static files to. Used by the deployment command. + +```js title="docusaurus.config.js" +module.exports = { + deploymentBranch: 'gh-pages', +}; +``` + ### `githubHost` {#githubhost} - Type: `string` diff --git a/website/docs/configuration.md b/website/docs/configuration.md index 60af67e7498a..58e21652ced2 100644 --- a/website/docs/configuration.md +++ b/website/docs/configuration.md @@ -29,7 +29,7 @@ They are used in a number of places such as your site's title and headings, brow ### Deployment configurations {#deployment-configurations} -Deployment configurations such as `projectName` and `organizationName` are used when you deploy your site with the `deploy` command. +Deployment configurations such as `projectName`, `organizationName`, and optionally `deploymentBranch` are used when you deploy your site with the `deploy` command. It is recommended to check the [deployment docs](deployment.mdx) for more information. diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index fdd44f4cd611..aedbf92fc97a 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -75,6 +75,7 @@ First, modify your `docusaurus.config.js` and add the required params: | --- | --- | | `organizationName` | The GitHub user or organization that owns the repository. If you are the owner, it is your GitHub username. In the case of Docusaurus, it is "_facebook_" which is the GitHub organization that owns Docusaurus. | | `projectName` | The name of the GitHub repository. For example, the repository name for Docusaurus is "docusaurus", so the project name is "docusaurus". | +| `deploymentBranch` | The name of the branch to deploy the static files to. This defaults to "gh-pages" for non-organization GitHub Pages repos. If the repo name ends in ".github.io", you have to either specify this property or set the environment variable `DEPLOYMENT_BRANCH`. | | `url` | URL for your GitHub Page's user/organization page. This is commonly https://_username_.github.io. | | `baseUrl` | Base URL for your project. For projects hosted on GitHub pages, it follows the format "/_projectName_/". For https://github.com/facebook/docusaurus, `baseUrl` is `/docusaurus/`. | @@ -123,7 +124,7 @@ Optional parameters, set as environment variables: | `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. | | `GIT_USER` | The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your GitHub username. The specified `GIT_USER` must have push access to the repository specified in the combination of `organizationName` and `projectName`. If SSH is not used, this env variable is required. Otherwise, it is ignored. | | `GIT_PASS` | Password (or token) of the `git` user (specified by `GIT_USER`). For example, to facilitate non-interactive deployment (e.g. continuous deployment) | -| `DEPLOYMENT_BRANCH` | The branch that the website will be deployed to, defaults to `gh-pages`. For GitHub Pages Organization repos (`config.projectName` ending in `github.io`), this env variable is required. | +| `DEPLOYMENT_BRANCH` | The branch that the website will be deployed to, defaults to `gh-pages`. For GitHub Pages Organization repos (`config.projectName` ending in `github.io`), you need to either set this env variable or specify the `deploymentBranch` param in `docusaurus.config.js`. | | `CURRENT_BRANCH` | The branch that contains the latest docs changes that will be deployed. Usually, the branch will be `main`, but it could be any branch (default or otherwise) except for `gh-pages`. If nothing is set for this variable, then the current branch will be used. | GitHub enterprise installations should work in the same manner as github.com; you only need to set the organization's GitHub Enterprise host as an environment variable: From 2fa5e81d6eb667a7d83dd30050dc967573d3e4c1 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 31 Oct 2021 13:32:30 +0800 Subject: [PATCH 04/14] docs: fix i18n routes to feature requests (#5843) * docs: fix i18n routes to feature requests * Add redirect rules --- website/src/featureRequests/FeatureRequestsPlugin.js | 11 ++++++++--- website/static/_redirects | 12 ++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/website/src/featureRequests/FeatureRequestsPlugin.js b/website/src/featureRequests/FeatureRequestsPlugin.js index 8afaa6a9414f..1902657b7671 100644 --- a/website/src/featureRequests/FeatureRequestsPlugin.js +++ b/website/src/featureRequests/FeatureRequestsPlugin.js @@ -5,13 +5,18 @@ * LICENSE file in the root directory of this source tree. */ -/** @type {import('@docusaurus/types').Plugin} */ -function FeatureRequestsPlugin() { +const {normalizeUrl} = require('@docusaurus/utils'); + +/** + * @param {import('@docusaurus/types').LoadContext} context + * @returns {import('@docusaurus/types').Plugin} + */ +function FeatureRequestsPlugin(context) { return { name: 'feature-requests-plugin', async contentLoaded({actions}) { actions.addRoute({ - path: '/feature-requests', + path: normalizeUrl([context.baseUrl, '/feature-requests']), exact: false, component: '@site/src/featureRequests/FeatureRequestsPage', }); diff --git a/website/static/_redirects b/website/static/_redirects index 086cf210c64f..fd20eee24114 100644 --- a/website/static/_redirects +++ b/website/static/_redirects @@ -26,8 +26,16 @@ https://docusaurus.io/docs/zh-cn/* https://v1.docusaurus.io/docs/zh-cn/:spl # Feature requests redirects -/feedback/* /feature-requests/:splat 301! -/feature-requests/* /feature-requests 200 +/feedback/* /feature-requests/:splat 301! +/fr/feedback/* /fr/feature-requests/:splat 301! +/pt-BR/feedback/* /pt-BR/feature-requests/:splat 301! +/ko/feedback/* /ko/feature-requests/:splat 301! +/zh-CN/feedback/* /zh-CN/feature-requests/:splat 301! +/feature-requests/* /feature-requests 200 +/fr/feature-requests/* /fr/feature-requests 200 +/pt-BR/feature-requests/* /pt-BR/feature-requests 200 +/ko/feature-requests/* /ko/feature-requests 200 +/zh-CN/feature-requests/* /zh-CN/feature-requests 200 # FALLBACK: PUT THIS LAST (rules above are more important) From 40049c7c4f528b970f9b1ac2c12f2528d8c87400 Mon Sep 17 00:00:00 2001 From: wpyoga Date: Sun, 31 Oct 2021 01:12:10 +0700 Subject: [PATCH 05/14] feat: allow GIT_USER env var to be unset if SSH is used --- examples/classic-typescript/README.md | 10 ++++++++- examples/classic/README.md | 10 ++++++++- examples/facebook/README.md | 10 ++++++++- .../templates/facebook/README.md | 10 ++++++++- .../templates/shared/README.md | 10 ++++++++- packages/docusaurus/src/commands/deploy.ts | 22 ++++++++++++++----- website/docs/deployment.mdx | 13 +++-------- 7 files changed, 64 insertions(+), 21 deletions(-) diff --git a/examples/classic-typescript/README.md b/examples/classic-typescript/README.md index 55d0c3ef41c9..aaba2fa1e16e 100644 --- a/examples/classic-typescript/README.md +++ b/examples/classic-typescript/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/examples/classic/README.md b/examples/classic/README.md index 55d0c3ef41c9..aaba2fa1e16e 100644 --- a/examples/classic/README.md +++ b/examples/classic/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/examples/facebook/README.md b/examples/facebook/README.md index 1b65ca0890bd..6ffad61fe7c7 100644 --- a/examples/facebook/README.md +++ b/examples/facebook/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/create-docusaurus/templates/facebook/README.md b/packages/create-docusaurus/templates/facebook/README.md index 1b65ca0890bd..6ffad61fe7c7 100644 --- a/packages/create-docusaurus/templates/facebook/README.md +++ b/packages/create-docusaurus/templates/facebook/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/create-docusaurus/templates/shared/README.md b/packages/create-docusaurus/templates/shared/README.md index 55d0c3ef41c9..aaba2fa1e16e 100644 --- a/packages/create-docusaurus/templates/shared/README.md +++ b/packages/create-docusaurus/templates/shared/README.md @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv ### Deployment +Using SSH: + +``` +$ USE_SSH=true yarn deploy +``` + +Not using SSH: + ``` -$ GIT_USER= USE_SSH=true yarn deploy +$ GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 6b7f0d135d92..086e257a8bc6 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -63,8 +63,22 @@ This behavior can have SEO impacts and create relative link issues. throw new Error('Git not installed or on the PATH!'); } + const currentRepoUrl = shell + .exec('git config --get remote.origin.url') + .stdout.trim(); + + const repoUrlUseSSH = + currentRepoUrl.match(/^ssh:\/\//) !== null || + currentRepoUrl.match(/^([\w\-]+@)?[\w.\-]+:[\w.\-\/_]+(\.git)?/) !== null; + + const envUseSSH = + process.env.USE_SSH !== undefined && + process.env.USE_SSH.toLowerCase() === 'true'; + + const useSSH = envUseSSH || repoUrlUseSSH; + const gitUser = process.env.GIT_USER; - if (!gitUser) { + if (!gitUser && !useSSH) { throw new Error('Please set the GIT_USER environment variable!'); } @@ -133,14 +147,13 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); gitCredentials = `${gitCredentials}:${gitPass}`; } - const useSSH = process.env.USE_SSH; const remoteBranch = buildUrl( githubHost, githubPort, gitCredentials, organizationName, projectName, - useSSH !== undefined && useSSH.toLowerCase() === 'true', + useSSH, ); console.log( @@ -148,9 +161,6 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); ); // Check if this is a cross-repo publish. - const currentRepoUrl = shell - .exec('git config --get remote.origin.url') - .stdout.trim(); const crossRepoPublish = !currentRepoUrl.endsWith( `${organizationName}/${projectName}.git`, ); diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index ff492bd640bb..aedbf92fc97a 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -117,20 +117,15 @@ By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb. ### Environment settings {#environment-settings} -Specify the Git user as an environment variable. - -| Name | Description | -| --- | --- | -| `GIT_USER` | The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your GitHub username. The specified `GIT_USER` must have push access to the repository specified in the combination of `organizationName` and `projectName`. | - -Optional parameters, also set as environment variables: +Optional parameters, set as environment variables: | Name | Description | | --- | --- | | `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. | +| `GIT_USER` | The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your GitHub username. The specified `GIT_USER` must have push access to the repository specified in the combination of `organizationName` and `projectName`. If SSH is not used, this env variable is required. Otherwise, it is ignored. | +| `GIT_PASS` | Password (or token) of the `git` user (specified by `GIT_USER`). For example, to facilitate non-interactive deployment (e.g. continuous deployment) | | `DEPLOYMENT_BRANCH` | The branch that the website will be deployed to, defaults to `gh-pages`. For GitHub Pages Organization repos (`config.projectName` ending in `github.io`), you need to either set this env variable or specify the `deploymentBranch` param in `docusaurus.config.js`. | | `CURRENT_BRANCH` | The branch that contains the latest docs changes that will be deployed. Usually, the branch will be `main`, but it could be any branch (default or otherwise) except for `gh-pages`. If nothing is set for this variable, then the current branch will be used. | -| `GIT_PASS` | Password (or token) of the `git` user (specified by `GIT_USER`). For example, to facilitate non-interactive deployment (e.g. continuous deployment) | GitHub enterprise installations should work in the same manner as github.com; you only need to set the organization's GitHub Enterprise host as an environment variable: @@ -230,7 +225,6 @@ jobs: - name: Release to GitHub Pages env: USE_SSH: true - GIT_USER: git run: | git config --global user.email "actions@github.com" git config --global user.name "gh-actions" @@ -362,7 +356,6 @@ trigger: - npm run deploy environment: USE_SSH: true - GIT_USER: $DRONE_COMMIT_AUTHOR GITHUB_PRIVATE_KEY: from_secret: "git_deploy_private_key" ``` From 4c9461076ab147c2151b6d273058f812b694cad7 Mon Sep 17 00:00:00 2001 From: William Poetra Yoga Date: Sun, 31 Oct 2021 12:28:22 +0700 Subject: [PATCH 06/14] fix: packages/docusaurus/src/commands/deploy.ts Co-authored-by: Joshua Chen --- packages/docusaurus/src/commands/deploy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 086e257a8bc6..05cc6e6860d9 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -69,7 +69,7 @@ This behavior can have SEO impacts and create relative link issues. const repoUrlUseSSH = currentRepoUrl.match(/^ssh:\/\//) !== null || - currentRepoUrl.match(/^([\w\-]+@)?[\w.\-]+:[\w.\-\/_]+(\.git)?/) !== null; + currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w.\-/_]+(\.git)?/) !== null; const envUseSSH = process.env.USE_SSH !== undefined && From 4f5d35efe8f12b0e4e97e40788408b2c0b6e1353 Mon Sep 17 00:00:00 2001 From: wpyoga Date: Sun, 31 Oct 2021 13:49:59 +0700 Subject: [PATCH 07/14] fix: avoid escaping hyphen in regex --- packages/docusaurus/src/commands/deploy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 05cc6e6860d9..a680a0a39efc 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -69,7 +69,7 @@ This behavior can have SEO impacts and create relative link issues. const repoUrlUseSSH = currentRepoUrl.match(/^ssh:\/\//) !== null || - currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w.\-/_]+(\.git)?/) !== null; + currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/) !== null; const envUseSSH = process.env.USE_SSH !== undefined && From e0234b7711c84b9b122639c6e36365c951283a46 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Sun, 7 Nov 2021 17:39:50 +0800 Subject: [PATCH 08/14] Refactor --- examples/classic-typescript/README.md | 10 +---- examples/classic/README.md | 10 +---- examples/facebook/README.md | 10 +---- .../__tests__/buildRemoteBranchUrl.test.ts | 33 ++++---------- .../src/commands/buildRemoteBranchUrl.ts | 31 +++---------- packages/docusaurus/src/commands/deploy.ts | 44 +++++++++++-------- website/docs/deployment.mdx | 2 +- 7 files changed, 43 insertions(+), 97 deletions(-) diff --git a/examples/classic-typescript/README.md b/examples/classic-typescript/README.md index aaba2fa1e16e..55d0c3ef41c9 100644 --- a/examples/classic-typescript/README.md +++ b/examples/classic-typescript/README.md @@ -26,16 +26,8 @@ This command generates static content into the `build` directory and can be serv ### Deployment -Using SSH: - -``` -$ USE_SSH=true yarn deploy -``` - -Not using SSH: - ``` -$ GIT_USER= yarn deploy +$ GIT_USER= USE_SSH=true yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/examples/classic/README.md b/examples/classic/README.md index aaba2fa1e16e..55d0c3ef41c9 100644 --- a/examples/classic/README.md +++ b/examples/classic/README.md @@ -26,16 +26,8 @@ This command generates static content into the `build` directory and can be serv ### Deployment -Using SSH: - -``` -$ USE_SSH=true yarn deploy -``` - -Not using SSH: - ``` -$ GIT_USER= yarn deploy +$ GIT_USER= USE_SSH=true yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/examples/facebook/README.md b/examples/facebook/README.md index 6ffad61fe7c7..1b65ca0890bd 100644 --- a/examples/facebook/README.md +++ b/examples/facebook/README.md @@ -26,16 +26,8 @@ This command generates static content into the `build` directory and can be serv ### Deployment -Using SSH: - -``` -$ USE_SSH=true yarn deploy -``` - -Not using SSH: - ``` -$ GIT_USER= yarn deploy +$ GIT_USER= USE_SSH=true yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts b/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts index 46f445fc5bf0..e97e193cfeaa 100644 --- a/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts +++ b/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts @@ -5,50 +5,33 @@ * LICENSE file in the root directory of this source tree. */ -import {buildUrl} from '../buildRemoteBranchUrl'; +import {buildSshUrl, buildHttpsUrl} from '../buildRemoteBranchUrl'; describe('remoteeBranchUrl', () => { test('should build a normal ssh url', async () => { - const url = buildUrl( - 'github.com', - undefined, - undefined, - 'facebook', - 'docusaurus', - true, - ); + const url = buildSshUrl('github.com', 'facebook', 'docusaurus'); 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, - ); + const url = buildSshUrl('github.com', 'facebook', 'docusaurus', '422'); 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, + const url = buildHttpsUrl( 'user:pass', + 'github.com', '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', + const url = buildHttpsUrl( 'user:pass', + 'github.com', 'facebook', 'docusaurus', - false, + '5433', ); 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 index cfb1b8c03dbd..a6603f0e40e0 100644 --- a/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts +++ b/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts @@ -5,44 +5,25 @@ * LICENSE file in the root directory of this source tree. */ -export function buildUrl( +export function buildSshUrl( githubHost: string, - githubPort: string | undefined, - gitCredentials: string | undefined, organizationName: string, projectName: string, - useSSH: boolean | undefined, + githubPort?: string, ): string { - 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, +export function buildHttpsUrl( + gitCredentials: string, githubHost: string, organizationName: string, projectName: string, - githubPort: string | undefined, -) { + githubPort?: string, +): string { if (githubPort) { return `https://${gitCredentials}@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`; } diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index a680a0a39efc..e4100d0c365f 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -13,7 +13,7 @@ import build from './build'; import {BuildCLIOptions} from '@docusaurus/types'; import path from 'path'; import os from 'os'; -import {buildUrl} from './buildRemoteBranchUrl'; +import {buildSshUrl, buildHttpsUrl} from './buildRemoteBranchUrl'; // GIT_PASS env variable should not appear in logs function obfuscateGitPass(str: string) { @@ -68,8 +68,8 @@ This behavior can have SEO impacts and create relative link issues. .stdout.trim(); const repoUrlUseSSH = - currentRepoUrl.match(/^ssh:\/\//) !== null || - currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/) !== null; + /^ssh:\/\//.test(currentRepoUrl) || // ssh://***: explicit protocol + /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(currentRepoUrl); // git@github.com:facebook/docusaurus.git const envUseSSH = process.env.USE_SSH !== undefined && @@ -79,7 +79,9 @@ This behavior can have SEO impacts and create relative link issues. const gitUser = process.env.GIT_USER; if (!gitUser && !useSSH) { - throw new Error('Please set the GIT_USER environment variable!'); + throw new Error( + 'Please set the GIT_USER environment variable, or use SSH instead!', + ); } // The branch that contains the latest docs changes that will be deployed. @@ -121,8 +123,7 @@ This behavior can have SEO impacts and create relative link issues. // Organization deploys looks like: // - Git repo: https://github.com//.github.io // - Site url: https://.github.io - const isGitHubPagesOrganizationDeploy = - projectName.indexOf('.github.io') !== -1; + const isGitHubPagesOrganizationDeploy = projectName.includes('.github.io'); if ( isGitHubPagesOrganizationDeploy && !process.env.DEPLOYMENT_BRANCH && @@ -141,21 +142,26 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com'; const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort; - const gitPass: string | undefined = process.env.GIT_PASS; - let gitCredentials = `${gitUser}`; - if (gitPass) { - gitCredentials = `${gitCredentials}:${gitPass}`; + let remoteBranch: string; + if (gitUser) { + const gitPass = process.env.GIT_PASS; + const gitCredentials = gitPass ? `${gitUser}:${gitPass}` : `${gitUser}`; + remoteBranch = buildHttpsUrl( + gitCredentials, + githubHost, + organizationName, + projectName, + githubPort, + ); + } else { + remoteBranch = buildSshUrl( + githubHost, + organizationName, + projectName, + githubPort, + ); } - const remoteBranch = buildUrl( - githubHost, - githubPort, - gitCredentials, - organizationName, - projectName, - useSSH, - ); - console.log( `${chalk.cyan('Remote branch:')} ${obfuscateGitPass(remoteBranch)}`, ); diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index b54c6bbc0d91..dbdaf70a2116 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -140,7 +140,7 @@ By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb. | Name | Description | | --- | --- | -| `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. | +| `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. If the source repo URL is an SSH URL (e.g. `git@github.com:facebook/docusaurus.git`), `USE_SSH` is inferred to be `true`. | | `GIT_USER` | The username for a GitHub account that **has push access to the target repo**. For your own repositories, this will usually be your GitHub username. Required if SSH is not used, and ignored otherwise. | | `GIT_PASS` | Personal access token of the git user (specified by `GIT_USER`), to facilitate non-interactive deployment (e.g. continuous deployment) | | `CURRENT_BRANCH` | The source branch. Usually, the branch will be `main` or `master`, but it could be any branch except for `gh-pages`. If nothing is set for this variable, then the current branch from which `docusaurus deploy` is invoked will be used. | From 08c224745c096311f7c48c62803f52fbe5fb6c3f Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 9 Nov 2021 09:30:11 +0800 Subject: [PATCH 09/14] Update deployment.mdx --- website/docs/deployment.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index 1c2d1921da35..94c0d4543fa4 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -141,7 +141,7 @@ By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb. | Name | Description | | --- | --- | | `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. If the source repo URL is an SSH URL (e.g. `git@github.com:facebook/docusaurus.git`), `USE_SSH` is inferred to be `true`. | -| `GIT_USER` | The username for a GitHub account that **has push access to the deployment repo**. For your own repositories, this will usually be your GitHub username. Required if SSH is not used, and ignored otherwise. | +| `GIT_USER` | The username for a GitHub account that **has push access to the deployment repo**. For your own repositories, this will usually be your GitHub username. If specified, this will override the `USE_SSH` config and HTTPS is used; required if not using SSH. | | `GIT_PASS` | Personal access token of the git user (specified by `GIT_USER`), to facilitate non-interactive deployment (e.g. continuous deployment) | | `CURRENT_BRANCH` | The source branch. Usually, the branch will be `main` or `master`, but it could be any branch except for `gh-pages`. If nothing is set for this variable, then the current branch from which `docusaurus deploy` is invoked will be used. | From 7d25b55b3d0ddff76da0210c793c3247d1db2402 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Tue, 9 Nov 2021 10:09:22 +0800 Subject: [PATCH 10/14] Make SSH higher priority --- packages/docusaurus/src/commands/deploy.ts | 12 ++++++------ website/docs/deployment.mdx | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index e4100d0c365f..f7a953da8bf6 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -143,18 +143,18 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort; let remoteBranch: string; - if (gitUser) { - const gitPass = process.env.GIT_PASS; - const gitCredentials = gitPass ? `${gitUser}:${gitPass}` : `${gitUser}`; - remoteBranch = buildHttpsUrl( - gitCredentials, + if (useSSH) { + remoteBranch = buildSshUrl( githubHost, organizationName, projectName, githubPort, ); } else { - remoteBranch = buildSshUrl( + const gitPass = process.env.GIT_PASS; + const gitCredentials = gitPass ? `${gitUser!}:${gitPass}` : gitUser!; + remoteBranch = buildHttpsUrl( + gitCredentials, githubHost, organizationName, projectName, diff --git a/website/docs/deployment.mdx b/website/docs/deployment.mdx index 94c0d4543fa4..09030520aecb 100644 --- a/website/docs/deployment.mdx +++ b/website/docs/deployment.mdx @@ -141,7 +141,7 @@ By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb. | Name | Description | | --- | --- | | `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. If the source repo URL is an SSH URL (e.g. `git@github.com:facebook/docusaurus.git`), `USE_SSH` is inferred to be `true`. | -| `GIT_USER` | The username for a GitHub account that **has push access to the deployment repo**. For your own repositories, this will usually be your GitHub username. If specified, this will override the `USE_SSH` config and HTTPS is used; required if not using SSH. | +| `GIT_USER` | The username for a GitHub account that **has push access to the deployment repo**. For your own repositories, this will usually be your GitHub username. Required if not using SSH, and ignored otherwise. | | `GIT_PASS` | Personal access token of the git user (specified by `GIT_USER`), to facilitate non-interactive deployment (e.g. continuous deployment) | | `CURRENT_BRANCH` | The source branch. Usually, the branch will be `main` or `master`, but it could be any branch except for `gh-pages`. If nothing is set for this variable, then the current branch from which `docusaurus deploy` is invoked will be used. | From c3fcacd0237f09132154153ef0142a377e88e734 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Tue, 9 Nov 2021 12:05:34 +0800 Subject: [PATCH 11/14] Only infer but not override --- packages/docusaurus/src/commands/deploy.ts | 47 +++++++++++++--------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index f7a953da8bf6..3cd0b17aacb6 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -63,32 +63,39 @@ This behavior can have SEO impacts and create relative link issues. throw new Error('Git not installed or on the PATH!'); } - const currentRepoUrl = shell - .exec('git config --get remote.origin.url') + // Source repo is the repo from where the command is invoked + const sourceRepoUrl = shell + .exec('git config --get remote.origin.url', {silent: true}) .stdout.trim(); - const repoUrlUseSSH = - /^ssh:\/\//.test(currentRepoUrl) || // ssh://***: explicit protocol - /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(currentRepoUrl); // git@github.com:facebook/docusaurus.git + // The source branch; defaults to the currently checked out branch + const sourceBranch = + process.env.CURRENT_BRANCH || + shell.exec('git rev-parse --abbrev-ref HEAD', {silent: true}).stdout.trim(); + + const gitUser = process.env.GIT_USER; - const envUseSSH = + let useSSH = process.env.USE_SSH !== undefined && process.env.USE_SSH.toLowerCase() === 'true'; - const useSSH = envUseSSH || repoUrlUseSSH; - - const gitUser = process.env.GIT_USER; if (!gitUser && !useSSH) { - throw new Error( - 'Please set the GIT_USER environment variable, or use SSH instead!', - ); + // If USE_SSH is unspecified: try inferring from repo URL + if ( + process.env.USE_SSH === undefined && + // ssh://***: explicit protocol + (/^ssh:\/\//.test(sourceRepoUrl) || + // git@github.com:facebook/docusaurus.git + /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(sourceRepoUrl)) + ) { + useSSH = true; + } else { + throw new Error( + 'Please set the GIT_USER environment variable, or explicitly specify USE_SSH instead!', + ); + } } - // The branch that contains the latest docs changes that will be deployed. - const currentBranch = - process.env.CURRENT_BRANCH || - shell.exec('git rev-parse --abbrev-ref HEAD').stdout.trim(); - const organizationName = process.env.ORGANIZATION_NAME || process.env.CIRCLE_PROJECT_USERNAME || @@ -167,14 +174,14 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); ); // Check if this is a cross-repo publish. - const crossRepoPublish = !currentRepoUrl.endsWith( + const crossRepoPublish = !sourceRepoUrl.endsWith( `${organizationName}/${projectName}.git`, ); // We don't allow deploying to the same branch unless it's a cross publish. - if (currentBranch === deploymentBranch && !crossRepoPublish) { + if (sourceBranch === deploymentBranch && !crossRepoPublish) { throw new Error( - `You cannot deploy from this branch (${currentBranch}).` + + `You cannot deploy from this branch (${sourceBranch}).` + '\nYou will need to checkout to a different branch!', ); } From fe821bd450ae1e11182172b61ac7c0a04cf2c814 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Wed, 10 Nov 2021 18:21:57 +0800 Subject: [PATCH 12/14] Add tests --- ...RemoteBranchUrl.test.ts => deploy.test.ts} | 29 ++++++++++--- .../src/commands/buildRemoteBranchUrl.ts | 31 -------------- packages/docusaurus/src/commands/deploy.ts | 42 +++++++++++++++---- 3 files changed, 57 insertions(+), 45 deletions(-) rename packages/docusaurus/src/commands/__tests__/{buildRemoteBranchUrl.test.ts => deploy.test.ts} (51%) delete mode 100644 packages/docusaurus/src/commands/buildRemoteBranchUrl.ts diff --git a/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts b/packages/docusaurus/src/commands/__tests__/deploy.test.ts similarity index 51% rename from packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts rename to packages/docusaurus/src/commands/__tests__/deploy.test.ts index e97e193cfeaa..4bf0bf901f48 100644 --- a/packages/docusaurus/src/commands/__tests__/buildRemoteBranchUrl.test.ts +++ b/packages/docusaurus/src/commands/__tests__/deploy.test.ts @@ -5,18 +5,18 @@ * LICENSE file in the root directory of this source tree. */ -import {buildSshUrl, buildHttpsUrl} from '../buildRemoteBranchUrl'; +import {buildSshUrl, buildHttpsUrl, hasSSHProtocol} from '../deploy'; -describe('remoteeBranchUrl', () => { - test('should build a normal ssh url', async () => { +describe('remoteBranchUrl', () => { + test('should build a normal ssh url', () => { const url = buildSshUrl('github.com', 'facebook', 'docusaurus'); expect(url).toEqual('git@github.com:facebook/docusaurus.git'); }); - test('should build a ssh url with port', async () => { + test('should build a ssh url with port', () => { const url = buildSshUrl('github.com', 'facebook', 'docusaurus', '422'); expect(url).toEqual('ssh://git@github.com:422/facebook/docusaurus.git'); }); - test('should build a normal http url', async () => { + test('should build a normal http url', () => { const url = buildHttpsUrl( 'user:pass', 'github.com', @@ -25,7 +25,7 @@ describe('remoteeBranchUrl', () => { ); expect(url).toEqual('https://user:pass@github.com/facebook/docusaurus.git'); }); - test('should build a normal http url', async () => { + test('should build a normal http url', () => { const url = buildHttpsUrl( 'user:pass', 'github.com', @@ -38,3 +38,20 @@ describe('remoteeBranchUrl', () => { ); }); }); + +describe('hasSSHProtocol', () => { + test('should recognize explicit SSH protocol', () => { + const url = 'ssh://git@github.com:422/facebook/docusaurus.git'; + expect(hasSSHProtocol(url)).toEqual(true); + }); + + test('should recognize implied SSH protocol', () => { + const url = 'git@github.com:facebook/docusaurus.git'; + expect(hasSSHProtocol(url)).toEqual(true); + }); + + test('should not recognize HTTPS', () => { + const url = 'https://user:pass@github.com/facebook/docusaurus.git'; + expect(hasSSHProtocol(url)).toEqual(false); + }); +}); diff --git a/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts b/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts deleted file mode 100644 index a6603f0e40e0..000000000000 --- a/packages/docusaurus/src/commands/buildRemoteBranchUrl.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * 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 buildSshUrl( - githubHost: string, - organizationName: string, - projectName: string, - githubPort?: string, -): string { - if (githubPort) { - return `ssh://git@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`; - } - return `git@${githubHost}:${organizationName}/${projectName}.git`; -} - -export function buildHttpsUrl( - gitCredentials: string, - githubHost: string, - organizationName: string, - projectName: string, - githubPort?: string, -): string { - 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 3cd0b17aacb6..f0428751e7b7 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -13,7 +13,6 @@ import build from './build'; import {BuildCLIOptions} from '@docusaurus/types'; import path from 'path'; import os from 'os'; -import {buildSshUrl, buildHttpsUrl} from './buildRemoteBranchUrl'; // GIT_PASS env variable should not appear in logs function obfuscateGitPass(str: string) { @@ -38,6 +37,39 @@ function shellExecLog(cmd: string) { } } +export function buildSshUrl( + githubHost: string, + organizationName: string, + projectName: string, + githubPort?: string, +): string { + if (githubPort) { + return `ssh://git@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`; + } + return `git@${githubHost}:${organizationName}/${projectName}.git`; +} + +export function buildHttpsUrl( + gitCredentials: string, + githubHost: string, + organizationName: string, + projectName: string, + githubPort?: string, +): string { + if (githubPort) { + return `https://${gitCredentials}@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`; + } + return `https://${gitCredentials}@${githubHost}/${organizationName}/${projectName}.git`; +} + +export function hasSSHProtocol(sourceRepoUrl: string): boolean { + return ( + /^ssh:\/\//.test(sourceRepoUrl) || // ssh://***: explicit protocol, usually when using a port number + // git@github.com:facebook/docusaurus.git + /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(sourceRepoUrl) + ); +} + export default async function deploy( siteDir: string, cliOptions: Partial = {}, @@ -81,13 +113,7 @@ This behavior can have SEO impacts and create relative link issues. if (!gitUser && !useSSH) { // If USE_SSH is unspecified: try inferring from repo URL - if ( - process.env.USE_SSH === undefined && - // ssh://***: explicit protocol - (/^ssh:\/\//.test(sourceRepoUrl) || - // git@github.com:facebook/docusaurus.git - /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(sourceRepoUrl)) - ) { + if (process.env.USE_SSH === undefined && hasSSHProtocol(sourceRepoUrl)) { useSSH = true; } else { throw new Error( From fb7760c49d93d250af74866dc3cefc532b1a54ed Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Wed, 10 Nov 2021 18:32:00 +0800 Subject: [PATCH 13/14] Fix tests --- .../src/commands/__tests__/deploy.test.ts | 7 ++++++- packages/docusaurus/src/commands/deploy.ts | 14 +++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus/src/commands/__tests__/deploy.test.ts b/packages/docusaurus/src/commands/__tests__/deploy.test.ts index 4bf0bf901f48..f57bd9aae241 100644 --- a/packages/docusaurus/src/commands/__tests__/deploy.test.ts +++ b/packages/docusaurus/src/commands/__tests__/deploy.test.ts @@ -50,8 +50,13 @@ describe('hasSSHProtocol', () => { expect(hasSSHProtocol(url)).toEqual(true); }); - test('should not recognize HTTPS', () => { + test('should not recognize HTTPS with credentials', () => { const url = 'https://user:pass@github.com/facebook/docusaurus.git'; expect(hasSSHProtocol(url)).toEqual(false); }); + + test('should not recognize plain HTTPS URL', () => { + const url = 'https://github.com:5433/facebook/docusaurus.git'; + expect(hasSSHProtocol(url)).toEqual(false); + }); }); diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index f0428751e7b7..aad52413c500 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -63,11 +63,15 @@ export function buildHttpsUrl( } export function hasSSHProtocol(sourceRepoUrl: string): boolean { - return ( - /^ssh:\/\//.test(sourceRepoUrl) || // ssh://***: explicit protocol, usually when using a port number - // git@github.com:facebook/docusaurus.git - /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(sourceRepoUrl) - ); + try { + if (new URL(sourceRepoUrl).protocol === 'ssh') { + return true; + } + return false; + } catch { + // Fails when there isn't a protocol + return /^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(sourceRepoUrl); // git@github.com:facebook/docusaurus.git + } } export default async function deploy( From 29ae6749ce118e688a739ba62c7526d18bd295a8 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Wed, 10 Nov 2021 18:37:22 +0800 Subject: [PATCH 14/14] Fix --- packages/docusaurus/src/commands/deploy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index aad52413c500..75c37d05db25 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -64,7 +64,7 @@ export function buildHttpsUrl( export function hasSSHProtocol(sourceRepoUrl: string): boolean { try { - if (new URL(sourceRepoUrl).protocol === 'ssh') { + if (new URL(sourceRepoUrl).protocol === 'ssh:') { return true; } return false;