-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.ts
More file actions
133 lines (109 loc) · 2.98 KB
/
github-api.ts
File metadata and controls
133 lines (109 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as fs from 'node:fs/promises'
import { Octokit, type RestEndpointMethodTypes } from '@octokit/rest'
import fg from 'fast-glob'
import type { ProcessedResult } from './sharp-api.js'
export type GitTreeBlob = RestEndpointMethodTypes['git']['createTree']['parameters']['tree'][number]
export const GITHUB_TOKEN = process.env['GITHUB_TOKEN']
export const GITHUB_REPOSITORY = process.env['GITHUB_REPOSITORY']!
export const [owner, repo] = GITHUB_REPOSITORY.split('/')
export const GITHUB_PULL_REQUEST = JSON.parse(process.env['GITHUB_PULL_REQUEST']!)
if (!GITHUB_TOKEN) {
console.log('::error:: There is no GITHUB_TOKEN environment variable')
process.exit(1)
}
export const api = new Octokit({
auth: `token ${GITHUB_TOKEN}`,
})
export const createComment = (content: string) => {
return api.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: GITHUB_PULL_REQUEST.number,
body: content,
})
}
const imageToBase64 = async (path: string) => {
const buffer = await fs.readFile(path)
const base64 = buffer.toString('base64')
return base64
}
export const createTreeBlobs = async (image: ProcessedResult) => {
const blobs: GitTreeBlob[] = []
if (image.convertedToAvif) {
blobs.push({
path: image.path,
mode: '040000',
type: 'blob',
sha: null,
})
}
const filePath = image.convertedToAvif ? image.avifPath! : image.path
const encodedImage = await imageToBase64(filePath)
const imageBlob = await api.rest.git.createBlob({
owner,
repo,
content: encodedImage,
encoding: 'base64',
})
blobs.push({
path: filePath,
mode: '100644',
type: 'blob',
sha: imageBlob.data.sha,
})
const mdxFiles = await fg('src/content/post/**/*.mdx')
for (const mdxPath of mdxFiles) {
console.log('::✧:: Processing MDX:', mdxPath)
const content = await fs.readFile(mdxPath, 'utf-8')
const mdxBlob = await api.rest.git.createBlob({
owner,
repo,
content: content,
encoding: 'utf-8',
})
blobs.push({
path: mdxPath,
mode: '100644',
type: 'blob',
sha: mdxBlob.data.sha,
})
}
return blobs
}
export const createCommit = async ({
message,
treeBlobs,
}: {
message: string
treeBlobs: GitTreeBlob[]
}) => {
const recentCommitSHA = GITHUB_PULL_REQUEST.head.sha
const latestCommit = await api.rest.git.getCommit({
owner,
repo,
commit_sha: recentCommitSHA,
})
const baseTreeSha = latestCommit.data.tree.sha
const newTree = await api.rest.git.createTree({
owner,
repo,
base_tree: baseTreeSha,
tree: treeBlobs,
})
console.log('✧ newTree', newTree.data.url)
const commit = await api.rest.git.createCommit({
owner,
repo,
message,
tree: newTree.data.sha,
parents: [recentCommitSHA],
})
console.log('✧ created commit', commit.data.url)
await api.rest.git.updateRef({
owner,
repo,
ref: `heads/${GITHUB_PULL_REQUEST.head.ref}`,
sha: commit.data.sha,
})
return commit.data
}