How to edit files using (pro)bot on GitHub?

Viewed 440

I'm building a GitHub bot using probot framework for editing the README.md of the repository but so far I could not find a way to edit files using probot and neither using any other GitHub bot framework.

So is it possible to edit files using bots on GitHub? If yes please give me some tutorial links or references.

For example I want to add a specific line to the end of README.md on every commit.

2 Answers

I'm adding code for how I did it using probot, I did it with help of @OscarDOM's answer,
Index.ts

import { Probot } from "probot";

export = ({ app }: { app: Probot }) => {
  app.on("issues.opened", async (context) => { // You can change the "issues.opened" according to your use
    var sha;
    var a = Buffer.from("ABCd");
    var content = a.toString("base64");
    await context.octokit.repos
      .getContent({
        owner: "your_github_username",
        repo: "repo_name",
        path: "file_path",
      })
      .then((result) => {
        sha = result.data.sha;
      });
    await context.octokit.repos.createOrUpdateFileContents({
      owner: "your_github_username",
      repo: "repo_name",
      path: "file_path",
      message: "hilo",
      content: content,
      sha: sha,
    });
  });
Related