11const { exec } = require ( 'node:child_process' )
2- // const { existsSync } = require('node:fs')
3- // const fs = require('node:fs/promises')
4- // const os = require('node:os')
5- // const path = require('node:path')
62const { promisify } = require ( 'node:util' )
73const execPromised = promisify ( exec )
84
9- // const axios = require('axios')
10-
11- // const { encryptValue, decryptValue } = require('../../../db/utils')
12-
13- // async function cloneRepository (url, branch, workingDir) {
14- // try {
15- // await execPromised(`git clone -b ${branch} --depth 1 --single-branch ${url.toString()} .`, { cwd: workingDir })
16- // } catch (err) {
17- // const output = err.stdout + err.stderr
18- // // Token does not have access to clone the repo
19- // if (/unable to access/.test(output)) {
20- // const result = new Error('Permission denied')
21- // result.code = 'invalid_token'
22- // result.cause = err
23- // throw result
24- // }
25- // // Remote branch does not exist
26- // if (/Could not find remote branch|Remote branch .+ not found/.test(output)) {
27- // const result = new Error('Branch not found')
28- // result.code = 'invalid_branch'
29- // throw result
30- // }
31- // let error
32- // // Fallback - try to extract the 'fatal' line from the output
33- // const m = /fatal: (.*)/.exec(output)
34- // if (m) {
35- // error = new Error('Failed to clone repository: ' + m[1])
36- // } else {
37- // error = new Error('Failed to clone repository')
38- // }
39- // error.cause = err
40- // throw error
41- // }
42- // }
43-
445module . exports . init = async function ( app ) {
456 // Check if git is installed
467 try {
@@ -77,101 +38,6 @@ module.exports.init = async function (app) {
7738 azure . pushToRepository ( repoOptions , snapshot , options )
7839 }
7940 }
80- // async function pushToRepository (repoOptions, snapshot, options) {
81- // let workingDir
82- // try {
83- // const token = repoOptions.token
84- // const branch = repoOptions.branch || 'main'
85- // if (!/^https:\/\/github.com/i.test(repoOptions.url)) {
86- // throw new Error('Only GitHub repositories are supported')
87- // }
88- // const url = new URL(repoOptions.url)
89- // url.username = 'x-access-token'
90- // url.password = token
91-
92- // // 2. get user details so we can properly attribute the commit
93- // let userDetails
94- // try {
95- // userDetails = await axios.get('https://api.github.com/user', {
96- // headers: {
97- // Accept: 'application/vnd.github+json',
98- // Authorization: `Bearer ${token}`,
99- // 'X-GitHub-Api-Version': '2022-11-28'
100- // }
101- // })
102- // } catch (err) {
103- // const result = new Error('Invalid git token')
104- // result.code = 'invalid_token'
105- // result.cause = err
106- // throw result
107- // }
108-
109- // const userGitName = userDetails.data.login
110- // const userGitEmail = `${userDetails.data.id}+${userDetails.data.login}@users .noreply.github.com`
111- // const author = `${userGitName} <${userGitEmail}>`.replace(/"/g, '\\"')
112- // workingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'flowfuse-git-repo-'))
113-
114- // // 3. clone repo
115- // await cloneRepository(url, branch, workingDir)
116-
117- // // 4. set username/email
118- // await execPromised('git config user.email "no-reply@flowfuse.com"', { cwd: workingDir })
119- // await execPromised('git config user.name "FlowFuse"', { cwd: workingDir })
120- // // For local dev - disable gpg signing in case its set in global config
121- // await execPromised('git config commit.gpgsign false', { cwd: workingDir })
122-
123- // // 5. export snapshot
124- // const exportOptions = {
125- // credentialSecret: repoOptions.credentialSecret,
126- // components: {
127- // flows: true,
128- // credentials: true
129- // }
130- // }
131- // const result = await app.db.controllers.Snapshot.exportSnapshot(snapshot, exportOptions)
132- // const snapshotExport = app.db.views.ProjectSnapshot.snapshotExport(result)
133- // if (snapshotExport.settings?.settings?.palette?.npmrc) {
134- // const enc = encryptValue(repoOptions.credentialSecret, snapshotExport.settings.settings?.palette?.npmrc)
135- // snapshotExport.settings.settings.palette.npmrc = { $: enc }
136- // }
137- // const snapshotFile = path.join(workingDir, repoOptions.path || 'snapshot.json').replace(/"/g, '')
138- // await fs.writeFile(snapshotFile, JSON.stringify(snapshotExport, null, 4))
139-
140- // // 6. stage file
141- // await execPromised(`git add "${snapshotFile}"`, { cwd: workingDir })
142-
143- // // 7. commit
144- // await execPromised(`git commit -m "Update snapshot\n\nSnapshot updated by FlowFuse Pipeline '${options.pipeline.name.replace(/"/g, '')}', triggered by ${options.user.username.replace(/"/g, '')}" --author="${author}"`, { cwd: workingDir })
145-
146- // try {
147- // // 8. push
148- // await execPromised('git push', { cwd: workingDir })
149- // } catch (err) {
150- // const output = err.stdout + err.stderr
151- // if (/unable to access/.test(output)) {
152- // const result = new Error('Permission denied')
153- // result.code = 'invalid_token'
154- // result.cause = err
155- // throw result
156- // }
157- // let error
158- // const m = /fatal: (.*)/.exec(output)
159- // if (m) {
160- // error = new Error('Failed to push repository: ' + m[1])
161- // } else {
162- // error = Error('Failed to push repository')
163- // }
164- // error.cause = err
165- // throw error
166- // }
167- // } finally {
168- // if (workingDir) {
169- // try {
170- // await fs.rm(workingDir, { recursive: true, force: true })
171- // } catch (err) {}
172- // }
173- // }
174- // }
17541
17642 /**
17743 * Push a snapshot to a git repository
@@ -187,61 +53,7 @@ module.exports.init = async function (app) {
18753 return azure . pullFromRepository ( repoOptions )
18854 }
18955 }
190- // async function pullFromRepository (repoOptions) {
191- // let workingDir
192- // try {
193- // const token = repoOptions.token
194- // const branch = repoOptions.branch || 'main'
195- // if (!/^https:\/\/github.com/i.test(repoOptions.url)) {
196- // throw new Error('Only GitHub repositories are supported')
197- // }
198- // const url = new URL(repoOptions.url)
199- // url.username = 'x-access-token'
200- // url.password = token
201-
202- // workingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'flowfuse-git-repo-'))
203-
204- // // 3. clone repo
205- // await cloneRepository(url, branch, workingDir)
206-
207- // const snapshotFile = path.join(workingDir, repoOptions.path || 'snapshot.json').replace(/"/g, '')
208-
209- // if (!existsSync(snapshotFile)) {
210- // throw new Error('Snapshot file not found in repository')
211- // }
21256
213- // try {
214- // const snapshotContent = await fs.readFile(snapshotFile, 'utf8')
215- // const snapshot = JSON.parse(snapshotContent)
216- // if (snapshot.settings?.env) {
217- // const keys = Object.keys(snapshot.settings.env)
218- // keys.forEach((key) => {
219- // const env = snapshot.settings.env[key]
220- // if (env.hidden && env.$) {
221- // // Decrypt the value if it is encrypted
222- // env.value = decryptValue(repoOptions.credentialSecret, env.$)
223- // delete env.$
224- // }
225- // })
226- // }
227- // if (snapshot.settings?.settings?.palette?.npmrc) {
228- // const npmrc = snapshot.settings.settings.palette.npmrc
229- // if (typeof npmrc === 'object' && npmrc.$) {
230- // snapshot.settings.settings.palette.npmrc = decryptValue(repoOptions.credentialSecret, npmrc.$)
231- // }
232- // }
233- // return snapshot
234- // } catch (err) {
235- // throw new Error('Failed to read snapshot file: ' + err.message)
236- // }
237- // } finally {
238- // if (workingDir) {
239- // try {
240- // await fs.rm(workingDir, { recursive: true, force: true })
241- // } catch (err) {}
242- // }
243- // }
244- // }
24557 return {
24658 pushToRepository,
24759 pullFromRepository
0 commit comments