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

Pull from a github folder #4739

Merged
merged 8 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/remix-ide-e2e/src/tests/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,18 @@ module.exports = {
.waitForElementVisible('*[data-shared="tooltipPopup"]')
.waitForElementContainsText('*[data-shared="tooltipPopup"]', 'initiating fileManager and calling "open" ...')
.waitForElementContainsText('*[data-shared="tooltipPopup"]', 'initiating terminal and calling "log" ...')
},

'Import Github folder from URL params #group4': function (browser: NightwatchBrowser) {
browser
.url('http://127.0.0.1:8080/#ghfolder=https://github.com/ethereum/remix-project/tree/master/apps/remix-ide/contracts/hardhat')
.refreshPage()
.waitForElementVisible('*[data-id="treeViewLitreeViewItemcontracts"]', 40000)
.currentWorkspaceIs('code-sample')
.openFile('contracts')
.openFile('contracts/Lock.sol')
.getEditorValue((content) => {
browser.assert.ok(content.indexOf('contract Lock {') !== -1, 'content does contain "contract Lock {"')
})
}
}
10 changes: 8 additions & 2 deletions libs/remix-core-plugin/src/lib/compiler-content-imports.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'
import { Plugin } from '@remixproject/engine'
import { RemixURLResolver } from '@remix-project/remix-url-resolver'
import { RemixURLResolver, githubFolderResolver } from '@remix-project/remix-url-resolver'

const profile = {
name: 'contentImport',
displayName: 'content import',
version: '0.0.1',
methods: ['resolve', 'resolveAndSave', 'isExternalUrl']
methods: ['resolve', 'resolveAndSave', 'isExternalUrl', 'resolveGithubFolder']
}

export type ResolvedImport = {
Expand Down Expand Up @@ -212,4 +212,10 @@ export class CompilerImports extends Plugin {
throw new Error(e)
}
}

async resolveGithubFolder (url) {
const ghFolder = {}
await githubFolderResolver(url, ghFolder, 3)
return ghFolder
}
}
3 changes: 2 additions & 1 deletion libs/remix-ui/workspace/src/lib/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type UrlParametersType = {
address: string
opendir: string,
blockscout: string,
ghfolder: string
}

const basicWorkspaceInit = async (workspaces: { name: string; isGitRepo: boolean; }[], workspaceProvider) => {
Expand Down Expand Up @@ -80,7 +81,7 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
plugin.setWorkspace({ name, isLocalhost: false })
dispatch(setCurrentWorkspace({ name, isGitRepo: false }))
await loadWorkspacePreset('gist-template')
} else if (params.code || params.url || params.shareCode) {
} else if (params.code || params.url || params.shareCode || params.ghfolder) {
await createWorkspaceTemplate('code-sample', 'code-template')
plugin.setWorkspace({ name: 'code-sample', isLocalhost: false })
dispatch(setCurrentWorkspace({ name: 'code-sample', isGitRepo: false }))
Expand Down
14 changes: 12 additions & 2 deletions libs/remix-ui/workspace/src/lib/actions/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export type UrlParametersType = {
shareCode: string
url: string
language: string
ghfolder: string
}

export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDefault', opts?) => {
Expand Down Expand Up @@ -285,10 +286,8 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
}
if (params.url) {
const data = await plugin.call('contentImport', 'resolve', params.url)

path = data.cleanUrl
content = data.content

try {
content = JSON.parse(content) as any
if (content.language && content.language === 'Solidity' && content.sources) {
Expand All @@ -307,6 +306,17 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
await workspaceProvider.set(path, content)
}
}
if (params.ghfolder) {
try {
const files = await plugin.call('contentImport', 'resolveGithubFolder', params.ghfolder)
for (const [path, content] of Object.entries(files)) {
await workspaceProvider.set(path, content)
}
} catch (e) {
console.log(e)
}
}

return path
} catch (e) {
console.error(e)
Expand Down
49 changes: 49 additions & 0 deletions libs/remix-url-resolver/src/github-folder-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// eslint-disable-next-line no-unused-vars
import axios, { AxiosResponse } from 'axios'

export type GithubItem = {
name: string
path: string
sha: string
size: number
url: string
html_url: string
git_url: string
download_url: string | null
type: 'dir' | 'file'
_links: {
self: string
git: string
html: string
}
}

export const githubFolderResolver = async (url, obj = {}, maxDepth, depth?, rootPath?) => {
depth = depth ? depth : 0
const child = await pullFolder(url)
depth = depth++
const urlObj = new URL(url);
const pathname = urlObj.pathname;
const pathParts = pathname.split('/');
const folderPath = pathParts.slice(5).join('/');
rootPath = rootPath || folderPath

if (!Array.isArray(child)) return obj
for (const item of child) {
Copy link
Member

Choose a reason for hiding this comment

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

Add a check to ensure that child can be iterated over, because it throws an error when i pass in a path that points to a github file.

if (item.type === 'file') {
const response: AxiosResponse = await axios.get(item.download_url)
obj[item.path.replace(rootPath, '')] = response.data
} else if (maxDepth > depth) {
// dir
await githubFolderResolver(item.html_url, obj, maxDepth, depth, rootPath)
}
}
return obj
}

const pullFolder = async (url) => {
const response = await axios.get('https://ghfolderpull.remixproject.org', { params: { ghfolder: url } });
const data: Array<GithubItem> = await response.data;
return data
}

1 change: 1 addition & 0 deletions libs/remix-url-resolver/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { RemixURLResolver } from './resolve'
export { githubFolderResolver } from './github-folder-resolver'
Loading