mheap/octokit-commit-multiple-files

Files to delete are not found using getContent

Akrabut opened this issue · 3 comments

Regarding #7

Is the current release working for you?
Logging octokit.repos.getContent prints undefined.

octokit.repos.getContents issues the following request:

fetch("https://api.github.com/repos/<organization-name>/<repo-name>/contents/<path-to-file>?ref=<branch-name>", {
  "headers": {
    "accept": "application/vnd.github.v3+json",
    "accept-language": "en-US,en;q=0.9",
    "authorization": "...",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "cross-site"
  },
  "referrer": "...",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": null,
  "method": "HEAD",
  "mode": "cors",
  "credentials": "include"
});

Note that getContents expect a string branch name as ref and not sha, which could be the cause for your error.

The changes the work for me are:

async function fileExistsInRepo(octokit, owner, repo, path, branch) {
  try {
    await octokit.repos.getContents({
      method: "HEAD",
      owner,
      repo,
      path,
      ref: branch
    });
    return true;
  } catch (e) {
    return false;
  }
}

With branch being branchName rather than baseTree.

mheap commented

Afternoon! Yes, the code is working for me and threw an error when using getContents

It looks like @octokit/rest.js v17 uses getContents whilst v18 uses getContent

From https://github.com/octokit/plugin-rest-endpoint-methods.js/releases/tag/v4.0.0:

octokit.repos.getContents() has been renamed to octokit.repos.getContent()

Can you confirm which version of @octokit/rest.js you're using?

Oh wow, I was using v16.something.
Sorry for the hassle 😄

mheap commented

No hassle at all. If that was the only thing broken, I could potentially make it work with multiple versions. Would that be useful?