|
1 | 1 | import fs from 'fs'; |
2 | 2 | import path from 'path'; |
| 3 | +import signale from 'signale'; |
3 | 4 | import {GitHub} from '@actions/github/lib/github'; |
4 | 5 | import {Context} from '@actions/github/lib/context'; |
5 | | -import {Response, GitCreateTreeResponse, GitCreateCommitResponse} from '@octokit/rest'; |
6 | 6 | import {getCommitMessage, getWorkspace} from './misc'; |
| 7 | +import {commit} from './command'; |
7 | 8 |
|
8 | | -export const filesToBlobs = async (files: object, octokit: GitHub, context: Context) => await Promise.all(Object.values(files).map(file => createBlob(file, octokit, context))); |
9 | | - |
10 | | -export const createTree = async (blobs: object, octokit: GitHub, context: Context) => { |
11 | | - return await octokit.git.createTree({ |
12 | | - owner: context.repo.owner, |
13 | | - repo: context.repo.repo, |
14 | | - base_tree: (await getCommit(octokit, context)).data.tree.sha, |
15 | | - tree: Object.values(blobs).map(blob => ({ |
16 | | - path: blob.path, |
17 | | - type: 'blob', |
18 | | - mode: '100644', |
19 | | - sha: blob.sha, |
20 | | - })), |
21 | | - }); |
22 | | -}; |
23 | | - |
24 | | -export const createCommit = async (tree: Response<GitCreateTreeResponse>, octokit: GitHub, context: Context) => { |
25 | | - return await octokit.git.createCommit({ |
26 | | - owner: context.repo.owner, |
27 | | - repo: context.repo.repo, |
28 | | - tree: tree.data.sha, |
29 | | - parents: [context.sha], |
30 | | - message: getCommitMessage(), |
31 | | - }); |
32 | | -}; |
33 | | - |
34 | | -export const updateRef = async (commit: Response<GitCreateCommitResponse>, name: string, octokit: GitHub, context: Context) => { |
35 | | - if (!await existsRef(name, octokit, context)) { |
36 | | - await createRef(name, octokit, context); |
37 | | - } |
38 | | - await octokit.git.updateRef({ |
39 | | - owner: context.repo.owner, |
40 | | - repo: context.repo.repo, |
41 | | - ref: getRef(name), |
42 | | - sha: commit.data.sha, |
43 | | - }); |
| 9 | +export const push = async (files: object, name: string, octokit: GitHub, context: Context) => { |
| 10 | + // const commit = await createCommit(files, octokit, context); |
| 11 | + // await updateRef(commit.data.sha, name, octokit, context); |
| 12 | + await updateRef(await commit(), name, octokit, context); |
44 | 13 | }; |
45 | 14 |
|
46 | 15 | const getCommit = async (octokit: GitHub, context: Context) => { |
@@ -80,10 +49,55 @@ const createBlob = async (filePath: string, octokit: GitHub, context: Context) = |
80 | 49 | }; |
81 | 50 |
|
82 | 51 | const createRef = async (name: string, octokit: GitHub, context: Context) => { |
| 52 | + signale.info('Create Ref'); |
83 | 53 | await octokit.git.createRef({ |
84 | 54 | owner: context.repo.owner, |
85 | 55 | repo: context.repo.repo, |
86 | 56 | ref: getRef(name), |
87 | 57 | sha: context.sha, |
88 | 58 | }); |
89 | 59 | }; |
| 60 | + |
| 61 | +const filesToBlobs = async (files: object, octokit: GitHub, context: Context) => Object.values(files).map(async file => await createBlob(file, octokit, context)); |
| 62 | + |
| 63 | +const createTree = async (blobs: object, octokit: GitHub, context: Context) => { |
| 64 | + signale.info('Create Tree'); |
| 65 | + return await octokit.git.createTree({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + base_tree: (await getCommit(octokit, context)).data.tree.sha, |
| 69 | + tree: Object.values(blobs).map(blob => ({ |
| 70 | + path: blob.path, |
| 71 | + type: 'blob', |
| 72 | + mode: '100644', |
| 73 | + sha: blob.sha, |
| 74 | + })), |
| 75 | + }); |
| 76 | +}; |
| 77 | + |
| 78 | +const createCommit = async (files: object, octokit: GitHub, context: Context) => { |
| 79 | + const blobs = await filesToBlobs(files, octokit, context); |
| 80 | + const tree = await createTree(blobs, octokit, context); |
| 81 | + signale.info('Create Commit'); |
| 82 | + return await octokit.git.createCommit({ |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + tree: tree.data.sha, |
| 86 | + parents: [context.sha], |
| 87 | + message: getCommitMessage(), |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | +const updateRef = async (commit: string, name: string, octokit: GitHub, context: Context) => { |
| 92 | + if (!await existsRef(name, octokit, context)) { |
| 93 | + await createRef(name, octokit, context); |
| 94 | + } |
| 95 | + |
| 96 | + signale.info('Update Ref'); |
| 97 | + await octokit.git.updateRef({ |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + ref: getRef(name), |
| 101 | + sha: commit, |
| 102 | + }); |
| 103 | +}; |
0 commit comments