Skip to content

Feature: GitHub autofork url#4704

Merged
balazsbajorics merged 34 commits into
masterfrom
feature/github-fork-url
Jan 15, 2024
Merged

Feature: GitHub autofork url#4704
balazsbajorics merged 34 commits into
masterfrom
feature/github-fork-url

Conversation

@balazsbajorics

@balazsbajorics balazsbajorics commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

Verbose URL https://utopia.pizza/p/?github_owner=balazsbajorics&github_repo=hydrogen-november&github_branch=main

Shorter URL https://utopia.pizza/p/?clone=balazsbajorics/hydrogen-november

Shorter URL with branch name https://utopia.pizza/p/?clone=balazsbajorics/hydrogen-november&github_branch=main

Try Me link:
Verbose URL link

Problem
The only (maintained, aside from the old zip-based repo checkout POC) working method for cloning a github repository was to create a new project, navigate to the github tab, then insert the owner / repo name, select a branch, press clone, overwrite contents of the new project.

We already had a URL-based entry point for the git checkout proof of concept which was cloning a git repo using a zip download. To my surprise it still works today, but it doesn't do the asset upload step, and does not connect the editor's github tab to the cloned repository. Plus I couldn't remember what other limitations it might have, so Balazs 2023 December decided to not spend time trying to understand if we should fix that instead of using the main github cloning feature.

What did I do
So I set out to replace the existing git auto-clone functionality with our "modern" and maintained github integration. I went down a bit of a dead end in #4660 and yesterday I decided to scrap most of my changes and come up with a vastly simplified solution.
Basically what this PR ended up doing is that it still does load (an empty) project, and then calls the same code path that runs when a user selects a repository in the github tab.

Flow of cloning a repo:
The flow is orchestrated from GithubRepositoryCloneFlow which is a react component in editor-component. This is currently our only loading flow which is not directly orchestrated from editor.tsx.

  1. Editor.tsx detects that we want to load from a github repo, and sets the EditorStore accordingly
  2. GithubRepositoryCloneFlow gets enabled, and makes sure the user is logged in and authenticated with github.
  3. Once the user is logged in and authenticated, we fire off cloneGithubRepo
    3.cloneGithubRepo creates a new totally empty project that only has a package.json. It fires PersistenceMachine.createNew so we get a proper Project ID, then saves this empty project to the server
  4. the code awaits the LOAD action being dispatched to avoid race conditions
  5. once the LOAD has been dispatched, we initiate the github cloning by calling GithubOperations.updateProjectWithBranchContent (that's the exact same code path that is called if the user clicks "Load from Branch" in the github pane)
  6. Profit

Changes to the Server
Parallel to my editor changes, I also added a new server endpoint: the ability to clone a repo without specifying a branch. Since the default branch of git repos can be arbitrarily named, the server code first figures out the default repo branch name, and then calls the existing load-a-branch code path.

Not logged in states
image
image
If the user is not logged in, or not authenticated with github, we stop the flow and wait for their action.

@github-actions

github-actions Bot commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

Try me

@balazsbajorics
balazsbajorics force-pushed the feature/github-fork-url branch from 1ee8773 to 05aef3d Compare January 8, 2024 16:36
@relativeci

relativeci Bot commented Jan 8, 2024

Copy link
Copy Markdown

Job #9890: Bundle Size — 62.25MiB (~+0.01%).

a5b7b24(current) vs df0030c master#9881(baseline)

Warning

Bundle contains 66 duplicate packages – View duplicate packages

Bundle metrics  Change 4 changes Regression 1 regression Improvement 1 improvement
                 Current
Job #9890
     Baseline
Job #9881
Regression  Initial JS 45.44MiB(~+0.01%) 45.44MiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 25.65% 19.89%
No change  Chunks 26 26
No change  Assets 30 30
Change  Modules 4365(+0.02%) 4364
Improvement  Duplicate Modules 471(-0.21%) 472
No change  Duplicate Code 30.76% 30.76%
No change  Packages 462 462
No change  Duplicate Packages 65 65
Bundle size by type  Change 1 change Regression 1 regression
                 Current
Job #9890
     Baseline
Job #9881
Regression  JS 62.24MiB (~+0.01%) 62.23MiB
Not changed  HTML 11.54KiB 11.54KiB

View job #9890 reportView feature/github-fork-url branch activity

@github-actions

github-actions Bot commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

Performance test results:
(Chart1)
(Chart2)

@balazsbajorics
balazsbajorics marked this pull request as ready for review January 9, 2024 10:38
Comment thread editor/src/templates/editor.tsx Outdated
const githubBranch = urlParams.get('github_branch')
const importURL = urlParams.get('import_url')

const githubRepoObj: GithubRepo | null = (() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this just be turned into a regular function and have it be invoked here? Feels like a lot of URL handling logic all embedded within the Editor component.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah very good point, it's became too massive

Comment thread editor/src/utils/github-auth.ts Outdated
startGithubAuthentication: startGithubAuthentication,
} as const

export function useOnClickAuthenticateWithGithub() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should this live in a separate .tsx file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

do you have a problem with the current location other than it's a ts file?

Comment thread editor/src/components/github/github-repository-clone-flow.tsx Outdated
Comment thread editor/src/components/github/github-repository-clone-flow.tsx Outdated
'branches',
githubRepo.owner,
githubRepo.repository,
'default-branch',

@ruggi ruggi Jan 12, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shall we stick with GH's (unchanged) default here? (main)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. if you say we can assume the default branch is main, that's wrong.


function createStoryboardFileWithPlaceholderContents(
projectContents: ProjectContentTreeRoot,
isItNecessary: 'create-placeholder' | 'skip-creating-placeholder',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Be nice to extract this into a type definition as it appears to be in at least 2 places.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also isItNecessary is a bad name. shouldCreatePlaceholder or just createPlaceholder would be better

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did the renaming, I did not create a type definition though because I think this is more readable. we should talk about it in the chat if you disagree :)

}),
])

// TODO make sure the EditorState knows we have a github repo connected!!!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Future work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yep, for the test I will write

return null
})

const awaitLoadActionDispatchedByPersistenceMachine = (): Promise<{ projectId: string }> => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this be a regular function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed!


export const DefaultPackageJson = {
name: 'Utopia Project',
name: 'utopia-project',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🙌

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

somewhat unrelated mini fix

githubRepo: GithubRepoWithBranch,
) {
if (didWeInitiateGitRepoDownloadSinceTheEditorLoaded) {
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this throw an error instead? This feels like something that shouldn't fail silently

@balazsbajorics balazsbajorics Jan 12, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this function gets called a couple of times because of ancestor remounts, maybe I should put a comment here

@balazsbajorics
balazsbajorics merged commit 15cd213 into master Jan 15, 2024
@balazsbajorics
balazsbajorics deleted the feature/github-fork-url branch January 15, 2024 08:53
@ruggi ruggi mentioned this pull request Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants