使用 GitHub API 编辑提交消息并将其添加到文件
Editing and adding a commit message to a file using the GitHub API
我正在使用 npm GitHub API。
而我有四条数据。
- 我要更新的文件的引用
- 我要更新的文件的路径
- 我想要在这个文件中的新内容
- 这次编辑我想要的提交信息
此外,我可以对 API 进行身份验证,并可以访问此存储库。
我现在如何编辑此文件并推送此提交?
const GitHub = require('github-api')
const gh = new GitHub({
token: config.app.git_token,
}, githubUrl)
const repo = gh.getRepo(config.app.repoOwner, config.app.repoName)
repo.getRef(`heads/${config.app.repoBranch}`).then((response) => {
const ref = response.data.object.sha
const path = 'README.md'
const content = '#Foo Bar\nthis is foo bar'
const message = 'make readme foo bar'
console.log('ref to the file i want to update')
console.log(ref)
console.log('path to the file i want to update')
console.log(path)
console.log('contents i now want in this file')
console.log(content)
console.log('commit message message')
console.log(message)
// how do i now edit and add a commit to this remote file?
})
我试过使用 .commit,但到目前为止,还没有成功,我不明白如何为该函数调用生成正确的参数。
知道了!
这是执行此操作的语法:
const GitHub = require('github-api')
const gh = new GitHub({
token: config.app.git_token,
}, githubUrl)
const repo = gh.getRepo(config.app.repoOwner, config.app.repoName)
const branch = config.app.repoBranch
const path = 'README.md'
const content = '#Foo Bar\nthis is foo bar'
const message = 'add foo bar to the readme'
const options = {}
repo.writeFile(
branch,
path,
content,
message,
options
).then((r) => {
console.log(r)
})
我需要使用 .writeFile 方法!
我正在使用 npm GitHub API。
而我有四条数据。
- 我要更新的文件的引用
- 我要更新的文件的路径
- 我想要在这个文件中的新内容
- 这次编辑我想要的提交信息
此外,我可以对 API 进行身份验证,并可以访问此存储库。
我现在如何编辑此文件并推送此提交?
const GitHub = require('github-api')
const gh = new GitHub({
token: config.app.git_token,
}, githubUrl)
const repo = gh.getRepo(config.app.repoOwner, config.app.repoName)
repo.getRef(`heads/${config.app.repoBranch}`).then((response) => {
const ref = response.data.object.sha
const path = 'README.md'
const content = '#Foo Bar\nthis is foo bar'
const message = 'make readme foo bar'
console.log('ref to the file i want to update')
console.log(ref)
console.log('path to the file i want to update')
console.log(path)
console.log('contents i now want in this file')
console.log(content)
console.log('commit message message')
console.log(message)
// how do i now edit and add a commit to this remote file?
})
我试过使用 .commit,但到目前为止,还没有成功,我不明白如何为该函数调用生成正确的参数。
知道了!
这是执行此操作的语法:
const GitHub = require('github-api')
const gh = new GitHub({
token: config.app.git_token,
}, githubUrl)
const repo = gh.getRepo(config.app.repoOwner, config.app.repoName)
const branch = config.app.repoBranch
const path = 'README.md'
const content = '#Foo Bar\nthis is foo bar'
const message = 'add foo bar to the readme'
const options = {}
repo.writeFile(
branch,
path,
content,
message,
options
).then((r) => {
console.log(r)
})
我需要使用 .writeFile 方法!