create plugin github public and private

This commit is contained in:
NEOLPAR 2022-09-01 20:04:45 +01:00
parent 1c67772973
commit 4de090b4c6
3 changed files with 71 additions and 4 deletions

View File

@ -32,10 +32,12 @@ export function createPluginsStore() {
case "npm": case "npm":
pluginData.npmToken = auth pluginData.npmToken = auth
break break
case "github":
pluginData.githubToken = auth
break
} }
let res = await API.createPlugin(pluginData) let res = await API.createPlugin(pluginData)
console.log("RESP", res)
let newPlugin = res.plugins[0] let newPlugin = res.plugins[0]
update(state => { update(state => {

View File

@ -3,6 +3,7 @@ import {
extractPluginTarball, extractPluginTarball,
createNpmPlugin, createNpmPlugin,
createUrlPlugin, createUrlPlugin,
createGithubPlugin,
} from "../../utilities/fileSystem" } from "../../utilities/fileSystem"
import { getGlobalDB } from "@budibase/backend-core/tenancy" import { getGlobalDB } from "@budibase/backend-core/tenancy"
import { generatePluginID, getPluginParams } from "../../db/utils" import { generatePluginID, getPluginParams } from "../../db/utils"
@ -61,7 +62,10 @@ export async function create(ctx: any) {
directory = directoryNpm directory = directoryNpm
break break
case "github": case "github":
console.log("github") const { metadata: metadataGithub, directory: directoryGithub } =
await createGithubPlugin(ctx, url, name, githubToken)
metadata = metadataGithub
directory = directoryGithub
break break
case "url": case "url":
const { metadata: metadataUrl, directory: directoryUrl } = const { metadata: metadataUrl, directory: directoryUrl } =

View File

@ -31,6 +31,7 @@ const MemoryStream = require("memorystream")
const { getAppId } = require("@budibase/backend-core/context") const { getAppId } = require("@budibase/backend-core/context")
const tar = require("tar") const tar = require("tar")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const { NodeVM } = require("vm2")
const TOP_LEVEL_PATH = join(__dirname, "..", "..", "..") const TOP_LEVEL_PATH = join(__dirname, "..", "..", "..")
const NODE_MODULES_PATH = join(TOP_LEVEL_PATH, "node_modules") const NODE_MODULES_PATH = join(TOP_LEVEL_PATH, "node_modules")
@ -378,8 +379,69 @@ exports.createUrlPlugin = async (url, name = "", headers = {}) => {
return await downloadUnzipPlugin(name, url, headers) return await downloadUnzipPlugin(name, url, headers)
} }
exports.createGithubPlugin = async (ctx, url, name = "", token = "") => {
let githubRepositoryUrl
let githubUrl
if (url.includes(".git")) {
githubRepositoryUrl = token
? url.replace("https://", `https://${token}@`)
: url
githubUrl = url.replace(".git", "")
} else {
githubRepositoryUrl = token
? `${url}.git`.replace("https://", `https://${token}@`)
: `${url}.git`
githubUrl = url
}
const githubApiUrl = githubUrl.replace(
"https://github.com/",
"https://api.github.com/repos/"
)
const headers = token ? { Authorization: `Bearer ${token}` } : {}
try {
const pluginRaw = await fetch(githubApiUrl, { headers })
if (pluginRaw.status !== 200) {
throw `Repository not found`
}
let pluginDetails = await pluginRaw.json()
const pluginName = pluginDetails.name || name
const path = join(budibaseTempDir(), pluginName)
// Remove first if exists
if (fs.existsSync(path)) {
fs.rmSync(path, { recursive: true, force: true })
}
fs.mkdirSync(path)
const script = `
module.exports = async () => {
const child_process = require('child_process')
child_process.execSync(\`git clone ${githubRepositoryUrl} ${join(
budibaseTempDir(),
pluginName
)}\`);
}
`
const scriptRunner = new NodeVM({
require: {
external: true,
builtin: ["child_process"],
root: "./",
},
}).run(script)
await scriptRunner()
return await getPluginMetadata(path)
} catch (e) {
throw e.message
}
}
const downloadUnzipPlugin = async (name, url, headers = {}) => { const downloadUnzipPlugin = async (name, url, headers = {}) => {
console.log(name, url, headers)
const path = join(budibaseTempDir(), name) const path = join(budibaseTempDir(), name)
try { try {
// Remove first if exists // Remove first if exists
@ -407,7 +469,6 @@ const downloadUnzipPlugin = async (name, url, headers = {}) => {
exports.downloadUnzipPlugin = downloadUnzipPlugin exports.downloadUnzipPlugin = downloadUnzipPlugin
const getPluginMetadata = async path => { const getPluginMetadata = async path => {
console.log(path)
let metadata = {} let metadata = {}
try { try {
const pkg = fs.readFileSync(join(path, "package.json"), "utf8") const pkg = fs.readFileSync(join(path, "package.json"), "utf8")