Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow GIT_USER env var to be unset if SSH is used #5840

Merged
merged 17 commits into from
Nov 10, 2021

Conversation

wpyoga
Copy link
Contributor

@wpyoga wpyoga commented Oct 31, 2021

Motivation

Fixes #3454 and allows me to run yarn deploy with one less environment variable.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

I used this on my own GitHub Pages site, and the remote URL is git@github.com:wpyoga/wpyoga.github.io.git:

$ yarn add ../docusaurus_not-require-use-ssh/packages/docusaurus
$ yarn install
$ DEPLOYMENT_BRANCH=gh-pages yarn deploy

@wpyoga wpyoga requested review from lex111 and slorber as code owners October 31, 2021 03:01
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 31, 2021
@netlify
Copy link

netlify bot commented Oct 31, 2021

✔️ [V2]
Built without sensitive environment variables

🔨 Explore the source changes: 29ae674

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/618ba0f96a5f3c0008293460

😎 Browse the preview: https://deploy-preview-5840--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Oct 31, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 78
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5840--docusaurus-2.netlify.app/

@Josh-Cena Josh-Cena added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Oct 31, 2021
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
@wpyoga
Copy link
Contributor Author

wpyoga commented Oct 31, 2021

@Josh-Cena Thanks for the input :)

I wonder why the ESLint doesn't give a warning for this?

    currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w.\-/_]+(\.git)?/) !== null;

After the colon, we have:

[\w.\-/_]

Why does this hyphen need to be escaped?

@Josh-Cena
Copy link
Collaborator

[\w.\-/_]

Because without the escape it becomes a range mark, as in [A-Z]. If the dash is placed last then you don't need the escape either: [\w./_-]

I'm not confident to say that this PR is safe to merge although it looks pretty good to me. Please resolve the conflicts and we can wait for @slorber to review this :D

wpyoga and others added 4 commits October 31, 2021 13:36
…onfig.js (facebook#5841)

* feat: allow user to specify deploymentBranch property in docusaurus.config.js

* docs: remove extra backtick

* docs: fix broken code block
* docs: fix i18n routes to feature requests

* Add redirect rules
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
@Josh-Cena Josh-Cena force-pushed the not-require-use-ssh branch from 2fa5e81 to 4c94610 Compare October 31, 2021 06:42
@Josh-Cena
Copy link
Collaborator

Oops, shouldn't have force-pushed your branch😅 Anyways, fixed the branch.

@wpyoga

This comment has been minimized.

@wpyoga
Copy link
Contributor Author

wpyoga commented Oct 31, 2021

Because without the escape it becomes a range mark, as in [A-Z]. If the dash is placed last then you don't need the escape either: [\w./_-]

Thank you, I've modified the regex :)

@Josh-Cena

This comment has been minimized.

@wpyoga

This comment has been minimized.

@Josh-Cena

This comment has been minimized.

@wpyoga

This comment has been minimized.

@Josh-Cena

This comment has been minimized.

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that looks good to me overall, but not totally confident without running this myself.

Also, we have this code:

  const gitPass: string | undefined = process.env.GIT_PASS;
  let gitCredentials = `${gitUser}`;
  if (gitPass) {
    gitCredentials = `${gitCredentials}:${gitPass}`;
  }

Probably requires some refactoring, ensuring we don't have a 'undefined' string being passed to later method: I'd rather have a real undefined (non-string) value (even if it's ignored later in the process, it's more explicit.

Wouldn't it be easier if the doc recommended using a ssh URL? We could avoid adding the extra env variable in the commands, and only show them in another tab?

examples/classic-typescript/README.md Outdated Show resolved Hide resolved

const repoUrlUseSSH =
currentRepoUrl.match(/^ssh:\/\//) !== null ||
currentRepoUrl.match(/^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/) !== null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do these regexp come from? If you found there somewhere I'd appreciate a link to an immutable URL as comment.

Can you add test cases for those?

Copy link
Collaborator

@Josh-Cena Josh-Cena Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems... intuitive to me🧐

It's either ssh://*** or user@server:project.git

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A regexp is a less readable shorthand for real code and must be tested in the same way. Similarly an if/else is intuitive until there's a bug in it 🤪

@Josh-Cena
Copy link
Collaborator

After the refactoring the changes make a lot of sense: the git credentials aren't even used if using SSH. The only case I'm not sure of is inferring useSSH = true when the source repo is using SSH🧐 I think in most instances that's a valid assumption?

Copy link
Collaborator

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is safe. The deployment works like this:

  • The source repository is cloned. If the clone happens through SSH (the repo URL is SSH), it means there's already an SSH key associated in the environment to carry out all further interactions with GitHub.
  • The deploy command does the build.
  • In case of a cross-repo publish, the deployment repo is cloned & checked out. If using HTTPS, GH will ask for the username & password for authentication; if using SSH, the clone will always succeed (just like cloning the source repo). Note: the user here doesn't have to be the same that made the deployment commit. She just needs to have push access to the deployment repo.
  • The build output is committed to the deployment branch. It requires setting the git username & email locally, which is not handled by the deploy command itself. (I wonder if it's possible to also enable GPG signing)
  • The commit is pushed to the deployment origin. Already authenticated during the clone step—should always succeed.

The workflow is a bit messy and lacking explanations. I'll try to add some inline comments & refactor since we also have #5829 waiting to be merged

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks fine but I haven't tested it in practice 😅 if there are errors we could patch it later anyway

Should this be merged before #5829 ? I don't know

if (
process.env.USE_SSH === undefined &&
// ssh://***: explicit protocol
(/^ssh:\/\//.test(sourceRepoUrl) ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about having an external/tested hasSSHProtocol(sourceRepoUrl) fn?

@Josh-Cena
Copy link
Collaborator

Yeah, we can merge this and then I can test with canary myself... I don't see any clear pitfalls, but there may be corner cases

@Josh-Cena Josh-Cena merged commit f5732e7 into facebook:main Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[v2]: Not require GIT_USER when using ssh
4 participants