Kev*_*die 2 javascript github github-api
我正在开发个人网站构建器。我的目标是能够在浏览器中操作我的页面,然后点击“发布”按钮,并将新的 HTML 文件提交到托管我的代码的 Github 存储库。
我已经能够弄清楚生成更新的 HTML 之前的所有事情。然而,在浏览完 GitHub 文档后,我无法真正理解如何使用 API 在存储库之前处理文件或将文件提交到存储库。有人可以帮我吗?
PS:我将使用JavaScript生成 HTML 和 API 调用。此外,如果有更好的方法来实现我的目标,我也愿意接受。
您需要考虑以下因素:
\n下面是一个使用 Node.js 和 Octokit API 使用createOrUpdateFileContents方法的工作示例。在这里阅读更多相关信息
\n该端点将使用新文件内容创建一个新的提交,您甚至可以指定提交者和作者。
\n| 更新 GitHub 中的文件内容 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 | View in Runme |
|---|
// The content of the file should be base64\nconst content = Buffer.from(`Hello world at ${new Date().toUTCString()}`, \'utf8\').toString(\'base64\');\n\n// If the file already exists, you need to provide the sha in order to update the file content.\nconst file = await octokit.rest.repos.getContent({\n owner: owner,\n repo: repo,\n path: path,\n branch: branch,\n });\nconst { sha } = file.data;\nconst fileContent = await octokit.rest.repos.createOrUpdateFileContents({\n owner,\n repo,\n path,\n sha: sha,\n message: \'This is the commit message generated via GitHub API\',\n content,\n committer: {\n name: \'demo\',\n email: \'demo@example.com\'\n },\n author: {\n name: \'demo\',\n email: \'demo@example.com\'\n }\n });\n\nconst { commit: { html_url } } = fileContent.data;\n\nconsole.log(`Content updated, see changes at ${html_url}`);\n\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2536 次 |
| 最近记录: |