Skip to content

Instantly share code, notes, and snippets.

@PatrickHeneise
Created October 6, 2021 09:54
Show Gist options
  • Save PatrickHeneise/ed02088454e8885ff5b1186476495700 to your computer and use it in GitHub Desktop.
Save PatrickHeneise/ed02088454e8885ff5b1186476495700 to your computer and use it in GitHub Desktop.
Add files to git with Octokit.js
const path = join(__dirname, 'templates', 'issues')
let files = [
'sponsor-request.md',
'suggestion.md',
'talk-feature.md',
'talk-lightning.md',
'talk-request.md'
]
try {
const committedTemplates = await github.rest.repos.getContent({
owner: owner,
repo: repo,
path: '.github/ISSUE_TEMPLATE'
})
if (
committedTemplates &&
committedTemplates.data &&
committedTemplates.data.length > 0
) {
const filtered = committedTemplates.data.map((file) => {
return file.name
})
files = files.filter((n) => !filtered.includes(n))
}
} catch (err) {
core.debug('folder not found', err)
}
const { data: latestRef } = await github.rest.git.getRef({
owner: owner,
repo: repo,
ref: 'heads/main'
})
const { data: latestCommit } = await github.rest.git.getCommit({
owner: owner,
repo: repo,
commit_sha: latestRef.object.sha
})
const tree = []
for (const file of files) {
const content = fs.readFileSync(`${path}/${file}`, {
encoding: 'base64'
})
const { data: blob } = await github.rest.git.createBlob({
owner: owner,
repo: repo,
content: content,
encoding: 'base64'
})
tree.push({
path: `.github/ISSUE_TEMPLATE/${file}`,
type: 'blob',
mode: '100644',
sha: blob.sha
})
}
const { data: gitTree } = await github.rest.git.createTree({
owner: owner,
repo: repo,
tree: tree,
base_tree: latestCommit.tree.sha
})
const { data: commit } = await github.rest.git.createCommit({
owner: owner,
repo: repo,
message: 'chore: add issue templates by GitEvents',
tree: gitTree.sha,
author: { name: 'GitEvents', email: '[email protected]' },
parents: [latestCommit.sha]
})
await github.rest.git.updateRef({
owner: owner,
repo: repo,
ref: 'heads/main',
sha: commit.sha
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment