|
| 1 | +import { Action, Step } from '../../actions'; |
| 2 | +import { validateUser } from './checkUserPushPermission'; |
| 3 | +import simpleGit from 'simple-git'; |
| 4 | + |
| 5 | +const exec = async (req: any, action: Action): Promise<Action> => { |
| 6 | + const step = new Step('getMissingData'); |
| 7 | + |
| 8 | + try { |
| 9 | + if (!action.commitData) { |
| 10 | + action.commitData = []; |
| 11 | + } |
| 12 | + |
| 13 | + if (action.commitData.length === 0) { |
| 14 | + console.log(`commitData not found, fetching missing commits from git...`); |
| 15 | + const path = `${action.proxyGitPath}/${action.repoName}`; |
| 16 | + const git = simpleGit(path); |
| 17 | + const log = await git.log({ from: action.commitFrom, to: action.commitTo }); |
| 18 | + |
| 19 | + action.commitData = log.all.toReversed().map((entry, i, array) => { |
| 20 | + const parent = i === 0 ? action.commitFrom : array[i - 1].hash; |
| 21 | + const timestamp = Math.floor(new Date(entry.date).getTime() / 1000).toString(); |
| 22 | + return { |
| 23 | + message: entry.message || '', |
| 24 | + committer: entry.author_name || '', |
| 25 | + tree: entry.hash || '', |
| 26 | + parent: parent || '0'.repeat(40), |
| 27 | + author: entry.author_name || '', |
| 28 | + authorEmail: entry.author_email || '', |
| 29 | + commitTimestamp: timestamp, |
| 30 | + } |
| 31 | + }); |
| 32 | + console.log(`Updated commitData:`, { commitData: action.commitData }); |
| 33 | + |
| 34 | + if (action.commitFrom === '0000000000000000000000000000000000000000') { |
| 35 | + action.commitFrom = action.commitData[action.commitData.length - 1].parent; |
| 36 | + } |
| 37 | + const user = action.commitData[action.commitData.length - 1].committer; |
| 38 | + action.user = user; |
| 39 | + |
| 40 | + return await validateUser(user, action, step); |
| 41 | + } |
| 42 | + } catch (e: any) { |
| 43 | + step.setError(e.toString('utf-8')); |
| 44 | + } finally { |
| 45 | + action.addStep(step); |
| 46 | + } |
| 47 | + return action; |
| 48 | +}; |
| 49 | + |
| 50 | +exec.displayName = 'getMissingData.exec'; |
| 51 | + |
| 52 | +export { exec }; |
0 commit comments