-
Notifications
You must be signed in to change notification settings - Fork 14
Support GitHub /tree/<branch>/<subdir> URLs in project import — fixes the appstore "Remix on CloudPebble" button #77
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
Draft
emindeniz99
wants to merge
9
commits into
coredevices:main
Choose a base branch
from
emindeniz99:github-import-subpath
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ceaca5
Add a parser for GitHub source URLs, including /tree/, /blob/ and /co…
emindeniz99 e2c8875
Let archive imports pin the project to a subdirectory (root_hint)
emindeniz99 0d99979
Support GitHub web URLs with branch and subdirectory in project import
emindeniz99 9199fa5
Keep every legacy import form working, with tests on both sides
emindeniz99 f7ca2f2
Address review feedback on the GitHub subpath import
emindeniz99 466adba
Address the second review round: namespaces, root hints, earlier narr…
emindeniz99 916d49d
Apply the deep self-review round: one qualifier authority, kind-aware…
emindeniz99 efd1091
Validate slash-free refs before linking; bound the fallback probe loop
emindeniz99 b100c5d
Keep the linking tests off the network; fix a docstring miscount
emindeniz99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
cloudpebble/ide/static/ide/js/__tests__/github-import-prefill.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { readFileSync } from 'fs'; | ||
| import { resolve } from 'path'; | ||
|
|
||
| // project_list.js is one $(function(){...}) block; the jQuery mock runs the | ||
| // callback immediately, so loading the file executes the deep-link prefill. | ||
| // Every jQuery method is a chainable no-op except val(), which records per | ||
| // selector — enough to assert what lands in the import dialog's fields. | ||
| function makeJqueryMock(attrs) { | ||
| var elements = {}; | ||
| var $ = function(selector) { | ||
| if (typeof selector === 'function') { | ||
| selector(); | ||
| return $; | ||
| } | ||
| if (!elements[selector]) { | ||
| var store = { value: undefined, text: '', clickHandler: null }; | ||
| var el = new Proxy(store, { | ||
| get: function(target, prop) { | ||
| if (prop === 'val') { | ||
| return function(v) { | ||
| if (v === undefined) return target.value; | ||
| target.value = v; | ||
| return el; | ||
| }; | ||
| } | ||
| if (prop === 'text') { | ||
| return function(v) { | ||
| if (v === undefined) return target.text; | ||
| target.text = v; | ||
| return el; | ||
| }; | ||
| } | ||
| if (prop === 'click') { | ||
| return function(fn) { | ||
| if (typeof fn === 'function') target.clickHandler = fn; | ||
| else if (target.clickHandler) target.clickHandler(); | ||
| return el; | ||
| }; | ||
| } | ||
| if (prop === 'find') return function(sel) { return $(sel); }; | ||
| if (prop === 'attr') { | ||
| return function(name) { | ||
| if ((attrs || {})[selector]) return attrs[selector][name]; | ||
| return el; | ||
| }; | ||
| } | ||
| if (prop === 'is') return function() { return false; }; | ||
| if (prop === 'length') return 0; | ||
| if (prop === '_isMock') return true; | ||
| return function() { return el; }; | ||
| } | ||
| }); | ||
| elements[selector] = el; | ||
| } | ||
| return elements[selector]; | ||
| }; | ||
| $.Deferred = vi.fn(); | ||
| $.elements = elements; | ||
| return $; | ||
| } | ||
|
|
||
| function load(pathname, attrs) { | ||
| var code = readFileSync(resolve(__dirname, '..', 'project_list.js'), 'utf8'); | ||
| var $ = makeJqueryMock(attrs); | ||
| var chain = { then: function() { return chain; }, catch: function() { return chain; } }; | ||
| var ajax = { Post: vi.fn(function() { return chain; }), PollTask: vi.fn() }; | ||
| var fn = new Function( | ||
| '$', 'jQuery', 'gettext', 'jquery_csrf_setup', 'ga', 'Ajax', 'CloudPebble', 'location', | ||
| code | ||
| ); | ||
| fn($, $, function(s) { return s; }, vi.fn(), vi.fn(), ajax, {}, { pathname: pathname }); | ||
| return { $: $, ajax: ajax }; | ||
| } | ||
|
|
||
| function loadWithPath(pathname) { | ||
| var h = load(pathname); | ||
| return { | ||
| name: h.$('#import-github-name').val(), | ||
| url: h.$('#import-github-url').val(), | ||
| branch: h.$('#import-github-branch').val() | ||
| }; | ||
| } | ||
|
|
||
| // Drives the real import dialog: the active tab claims to be the GitHub | ||
| // pane, fields are set, and the Run button's captured handler is fired. | ||
| function submitGithubImport(fields) { | ||
| var h = load('/ide/', { '#import-prompt .tab-pane.active': { id: 'import-github' } }); | ||
| h.$('#import-github-name').val(fields.name); | ||
| h.$('#import-github-url').val(fields.url); | ||
| h.$('#import-github-branch').val(fields.branch); | ||
| h.$('#run-import').click(); | ||
| return h; | ||
| } | ||
|
|
||
| describe('GitHub import deep-link prefill', () => { | ||
| it('hands a /tree/<branch>/<subdir> URL (the appstore Remix shape) to the server whole', () => { | ||
| var fields = loadWithPath('/ide/import/github/emindeniz99/pebble-signals/tree/main/faces/slothvec'); | ||
| expect(fields.url).toBe('github.com/emindeniz99/pebble-signals/tree/main/faces/slothvec'); | ||
| expect(fields.name).toBe('slothvec'); | ||
| // The URL is authoritative for the ref — the branch box stays empty. | ||
| expect(fields.branch).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('suggests the repo name when a /tree/ URL has no subdirectory', () => { | ||
| var fields = loadWithPath('/ide/import/github/user/repo/tree/main'); | ||
| expect(fields.url).toBe('github.com/user/repo/tree/main'); | ||
| expect(fields.name).toBe('repo'); | ||
| expect(fields.branch).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('suggests the repo name for /blob/ and /commit/ URLs (tail is a file or SHA, not a name)', () => { | ||
| var blob = loadWithPath('/ide/import/github/user/repo/blob/main/src/app.js'); | ||
| expect(blob.url).toBe('github.com/user/repo/blob/main/src/app.js'); | ||
| expect(blob.name).toBe('repo'); | ||
| var commit = loadWithPath('/ide/import/github/user/repo/commit/abc123'); | ||
| expect(commit.url).toBe('github.com/user/repo/commit/abc123'); | ||
| expect(commit.name).toBe('repo'); | ||
| }); | ||
|
|
||
| it('decodes the suggested name but keeps the URL encoded', () => { | ||
| var fields = loadWithPath('/ide/import/github/user/repo/tree/main/my%20dir'); | ||
| expect(fields.url).toBe('github.com/user/repo/tree/main/my%20dir'); | ||
| expect(fields.name).toBe('my dir'); | ||
| }); | ||
|
|
||
| it('keeps the legacy /<user>/<repo>/<branch> deep-link contract', () => { | ||
| var fields = loadWithPath('/ide/import/github/user/repo/dev'); | ||
| expect(fields.url).toBe('github.com/user/repo'); | ||
| // parts[3] is the USERNAME — a pre-existing upstream quirk, kept | ||
| // bug-compatible on purpose (this PR only preserves the contract). | ||
| expect(fields.name).toBe('user'); | ||
| expect(fields.branch).toBe('dev'); | ||
| }); | ||
|
|
||
| it('keeps legacy slashed-branch deep links', () => { | ||
| var fields = loadWithPath('/ide/import/github/user/repo/feat/x'); | ||
| expect(fields.url).toBe('github.com/user/repo'); | ||
| expect(fields.branch).toBe('feat/x'); | ||
| }); | ||
|
|
||
| it('leaves the branch empty for a plain repo link (server imports the default branch)', () => { | ||
| var fields = loadWithPath('/ide/import/github/user/repo'); | ||
| expect(fields.url).toBe('github.com/user/repo'); | ||
| expect(fields.branch).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does nothing outside the import deep link', () => { | ||
| var fields = loadWithPath('/ide/'); | ||
| expect(fields.url).toBeUndefined(); | ||
| expect(fields.name).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| describe('GitHub import submit handler', () => { | ||
| it('submits an empty branch untouched (no master fallback)', () => { | ||
| var h = submitGithubImport({ name: 'proj', url: 'github.com/user/repo', branch: '' }); | ||
| expect(h.ajax.Post).toHaveBeenCalledWith('/ide/import/github', { | ||
| name: 'proj', repo: 'github.com/user/repo', branch: '', add_remote: false | ||
| }); | ||
| }); | ||
|
|
||
| it('lets /tree/ URLs through the prefix check to the server', () => { | ||
| var h = submitGithubImport({ | ||
| name: 'sloth', url: 'github.com/user/repo/tree/main/faces/slothvec', branch: '' | ||
| }); | ||
| expect(h.ajax.Post).toHaveBeenCalledWith('/ide/import/github', { | ||
| name: 'sloth', repo: 'github.com/user/repo/tree/main/faces/slothvec', | ||
| branch: '', add_remote: false | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects a non-GitHub URL before posting', () => { | ||
| var h = submitGithubImport({ name: 'p', url: 'gitlab.com/user/repo', branch: '' }); | ||
| expect(h.ajax.Post).not.toHaveBeenCalled(); | ||
| expect(h.$('.errors').text()).toContain('GitHub'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.