2

I have come across the below code on the internet.

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();

I was successfully able to create a file in GitHub. Is there a way to update an existing file (like adding some data to the file) with Octokit js?

Harshatej M
  • 61
  • 2
  • 7

2 Answers2

1

I believe you do the same thing but you have to provide a sha property in your call to createOrUpdateFileContents which contains the sha of the blob you are replacing: https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents

Samuel Johnson
  • 243
  • 3
  • 10
0

I had the same problem, I made 2 requests one for the sha and the other to update the contents as such:

import { Octokit } from "@octokit/rest";
import fetch from "node-fetch";

const octokit = new Octokit({

    baseUrl: "https://api.github.com",
    auth: "<your-token>",
    request: {
        fetch: fetch, // in node -v17.9.0, fetch() is an experimental feature so you'll have to add this line
    }
});

async function main() {

    const OWNER_NAME = "<owner-name>"
    const REPO_NAME = "<repo-name>"
    const FILE_PATH = "path/to/file.txt"
    const AUTHOR_NAME = "<your-name>"
    const AUTHOR_EMAIL = "<your-email>"

    const user_info = {
        name: AUTHOR_NAME,
        email: AUTHOR_EMAIL
    }

    const result = await octokit.request(`GET /repos/${OWNER_NAME}/${REPO_NAME}/contents/${FILE_PATH}`, {
        owner: OWNER_NAME,
        repo: REPO_NAME,
        file_path: FILE_PATH,
        branch: "main"
    });

    await octokit.rest.repos.createOrUpdateFileContents({
        owner: OWNER_NAME,
        repo: REPO_NAME,
        path: FILE_PATH,
        message: "Updating contents of file.txt",
        content: Buffer.from("Hi! I'm the new content").toString('base64'),
        committer: { ...user_info },
        author: { ...user_info },
        sha: result.data.sha
    })
}

main();