|
1 | 1 | import { promises as fs } from "fs"; |
2 | 2 | import * as path from "path"; |
3 | | -import type { |
4 | | - CommitMessage, |
5 | | - FileAddition, |
6 | | - FileDeletion, |
7 | | -} from "./github/graphql/generated/types"; |
8 | | -import { |
9 | | - createCommitOnBranchQuery, |
10 | | - createRefMutation, |
11 | | - getRepositoryMetadata, |
12 | | - GitHubClient, |
13 | | -} from "./github/graphql/queries"; |
14 | | -import type { CreateCommitOnBranchMutationVariables } from "./github/graphql/generated/operations"; |
15 | | -import type { Logger } from "./logging"; |
16 | | - |
17 | | -export const commitFilesFromDirectory = async (args: { |
18 | | - octokit: GitHubClient; |
| 3 | +import type { FileAddition } from "./github/graphql/generated/types.js"; |
| 4 | +import { CommitFilesFromBase64Args, CommitFilesResult } from "./core.js"; |
| 5 | +import { commitFilesFromBuffers } from "./node.js"; |
| 6 | + |
| 7 | +export type CommitFilesFromDirectoryArgs = Omit< |
| 8 | + CommitFilesFromBase64Args, |
| 9 | + "fileChanges" |
| 10 | +> & { |
19 | 11 | /** |
20 | | - * The root of the github repository. |
| 12 | + * The directory to consider the root of the repository when calculating |
| 13 | + * file paths |
21 | 14 | */ |
22 | 15 | workingDirectory?: string; |
23 | | - owner: string; |
24 | | - repository: string; |
25 | | - branch: string; |
26 | | - /** |
27 | | - * The current commit that the target branch is at |
28 | | - */ |
29 | | - baseBranch: string; |
30 | | - /** |
31 | | - * The commit message |
32 | | - */ |
33 | | - message: CommitMessage; |
34 | 16 | fileChanges: { |
35 | | - /** |
36 | | - * File paths (relative to the repository root) |
37 | | - */ |
38 | 17 | additions?: string[]; |
39 | 18 | deletions?: string[]; |
40 | 19 | }; |
41 | | - log?: Logger; |
42 | | -}) => { |
43 | | - const { |
44 | | - octokit, |
45 | | - workingDirectory = process.cwd(), |
46 | | - owner, |
47 | | - repository, |
48 | | - branch, |
49 | | - baseBranch, |
50 | | - message, |
51 | | - fileChanges, |
52 | | - log, |
53 | | - } = args; |
54 | | - const repositoryNameWithOwner = `${owner}/${repository}`; |
55 | | - const baseRef = `refs/heads/${baseBranch}`; |
| 20 | +}; |
56 | 21 |
|
| 22 | +export const commitFilesFromDirectory = async ({ |
| 23 | + workingDirectory = process.cwd(), |
| 24 | + fileChanges, |
| 25 | + ...otherArgs |
| 26 | +}: CommitFilesFromDirectoryArgs): Promise<CommitFilesResult> => { |
57 | 27 | const additions: FileAddition[] = await Promise.all( |
58 | 28 | (fileChanges.additions || []).map(async (p) => { |
59 | | - const fileContents = await fs.readFile(path.join(workingDirectory, p)); |
60 | | - const base64Contents = Buffer.from(fileContents).toString("base64"); |
61 | 29 | return { |
62 | 30 | path: p, |
63 | | - contents: base64Contents, |
| 31 | + contents: await fs.readFile(path.join(workingDirectory, p)), |
64 | 32 | }; |
65 | 33 | }), |
66 | 34 | ); |
67 | 35 |
|
68 | | - const deletions: FileDeletion[] = |
69 | | - fileChanges.deletions?.map((p) => ({ |
70 | | - path: p, |
71 | | - })) ?? []; |
72 | | - |
73 | | - log?.debug(`Getting repo info ${repositoryNameWithOwner}`); |
74 | | - const info = await getRepositoryMetadata(octokit, { |
75 | | - owner: args.owner, |
76 | | - name: args.repository, |
77 | | - ref: baseRef, |
78 | | - }); |
79 | | - log?.debug(`Repo info: ${JSON.stringify(info, null, 2)}`); |
80 | | - |
81 | | - if (!info) { |
82 | | - throw new Error(`Repository ${repositoryNameWithOwner} not found`); |
83 | | - } |
84 | | - |
85 | | - const oid = info.ref?.target?.oid; |
86 | | - |
87 | | - if (!info) { |
88 | | - throw new Error(`Ref ${baseRef} not found`); |
89 | | - } |
90 | | - |
91 | | - log?.debug(`Creating branch ${branch} from commit ${oid}}`); |
92 | | - const refId = await createRefMutation(octokit, { |
93 | | - input: { |
94 | | - repositoryId: info.id, |
95 | | - name: `refs/heads/${branch}`, |
96 | | - oid, |
| 36 | + return commitFilesFromBuffers({ |
| 37 | + ...otherArgs, |
| 38 | + fileChanges: { |
| 39 | + additions, |
| 40 | + deletions: fileChanges.deletions, |
97 | 41 | }, |
98 | 42 | }); |
99 | | - |
100 | | - log?.debug(`Created branch with refId ${JSON.stringify(refId, null, 2)}`); |
101 | | - |
102 | | - const refIdStr = refId.createRef?.ref?.id; |
103 | | - |
104 | | - if (!refIdStr) { |
105 | | - throw new Error(`Failed to create branch ${branch}`); |
106 | | - } |
107 | | - |
108 | | - await log?.debug(`Creating commit on branch ${args.branch}`); |
109 | | - const createCommitMutation: CreateCommitOnBranchMutationVariables = { |
110 | | - input: { |
111 | | - branch: { |
112 | | - id: refIdStr, |
113 | | - }, |
114 | | - expectedHeadOid: oid, |
115 | | - message, |
116 | | - fileChanges: { |
117 | | - additions, |
118 | | - deletions, |
119 | | - }, |
120 | | - }, |
121 | | - }; |
122 | | - log?.debug(JSON.stringify(createCommitMutation, null, 2)); |
123 | | - |
124 | | - const result = await createCommitOnBranchQuery(octokit, createCommitMutation); |
125 | | - return result.createCommitOnBranch?.ref?.id ?? null; |
126 | 43 | }; |
0 commit comments