0

我在互联网上遇到了以下代码。

const { Octokit } = require("@octokit/rest");
const { Base64 } = require("js-base64");
const fs = require("fs");    
const { Octokit } = require("@octokit/rest");
    const { Base64 } = require("js-base64");
    const fs = require("fs");
    
    require("dotenv").config();
    
    const octokit = new Octokit({
      auth: process.env.GITHUB_ACCESS_TOKEN,
    });
    
    const main = async () => {
      try {
        const content = fs.readFileSync("./input.txt", "utf-8");
        const contentEncoded = Base64.encode(content);
    
        const { data } = await octokit.repos.createOrUpdateFileContents({
          // replace the owner and email with your own details
          owner: "your-github-account",
          repo: "octokit-create-file-example",
          path: "OUTPUT.md",
          message: "feat: Added OUTPUT.md programatically",
          content: contentEncoded,
          committer: {
            name: `Octokit Bot`,
            email: "your-email",
          },
          author: {
            name: "Octokit Bot",
            email: "your-email",
          },
        });
    
        console.log(data);
      } catch (err) {
        console.error(err);
      }
    };
    
    main();

我成功地在 GitHub 中创建了一个文件。有没有办法用 Octokit js 更新现有文件(比如向文件中添加一些数据)?

4

1 回答 1

0

我相信您会做同样的事情,但您必须在调用中提供一个sha属性,createOrUpdateFileContents其中包含您要替换的 blob 的 sha: https ://docs.github.com/en/rest/reference/repos#create-or -更新文件内容

于 2021-11-30T20:31:18.100 回答