octokit / rest.js

GitHub REST API client for JavaScript

Home Page:https://octokit.github.io/rest.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API doesn't appear to work (or else docs don't specify how to use correctly)

ggprod opened this issue · comments

Checklist

Environment

Versions

What happened?

I'm attempting to use the octokit.rest.repos.getCommit endpoint which I believe corresponds to the (GET /repos/{owner}/{repo}/commits/{ref}) https://docs.github.com/en/rest/commits/commits#get-a-commit

I've tried everything I could think of as the ref parameter, the commit SHA, the branch-name alone, the branch name in the format starting with heads/.. all of them return not found for a commit that is present in the github repo. Either this endpoint and function don't work at all or there should be some documentation about the format of the ref parameter

Minimal test case to reproduce the problem

What did you expect to happen?

when calling the octokit.rest.repos.getCommit function with ref set to a commit sha the commit details are returned (including the file changed in that commit)

What the problem might be

Can you give more details e.g. how are you authenticating? is the repository/commit private? if it is private, have you tried with a public repository/commit and had the same results? (and if so could you share some sample code)

The repo is private. I have not tried with a public repo. I used github app authentication which works and all other API calls work correctly and provide expected results (I also use octokit.rest.repos.compareCommitsWithBasehead, octokit.rest.git.getCommit, octokit.rest.git.createTree, octokit.rest.git.createCommit, and others

The code in question:

const auth = createAppAuth({
    appId: parseInt(process.env.GH_APP_ID),
    privateKey: process.env.GH_APP_PK,
    clientId: process.env.GH_APP_CLIENT_ID,
    clientSecret: process.env.GH_APP_CLIENT_SECRET
  })

  // Retrieve installation access token
  const installationAuthentication = await auth({
    type: "installation",
    installationId: parseInt(process.env.GH_APP_INSTALLATION_ID)
  })

  const octokit = new Octokit({
    auth: installationAuthentication.token
  })

const files = await octokit.rest.repos.getCommit({
  owner: 'me',
  repo: 'my-repo',
  ref: 'tried-commit-sha-here-as-well-as-other-things-nothing-worked',
})

I was able to accomplish my goal by using octokit.rest.git.getCommit to get the parrent commit sha and then comparing the parent to the current commit with octokit.rest.repos.compareCommitsWithBasehead but if the call above worked it would only require a single call

Could you try with a public repository, so that you can then provide us with the inputs you're using? It's possible that the issue is with the fact that the repository is private, but if it's not generally it should be reproducible with any public repository too.

(disclaimer: I don't work for/at GitHub, so can't swear by that which is why I say "generally")

Ok, I tried with this repo and the latest master commit sha.. same error (not found)

const test = await octokit.rest.repos.getCommit({
        owner: 'oktokit',
        repo: 'rest.js',
        ref: 'a2f69ec4b91175b6057c4693cf5e07ae5f86152f'
      })

oops.. mispelled octokit.. when I corrected that it did work.. so I guess it is some authentication related issue.. the github app I created for this authentication has the following permissions

Read and write access to code and pull requests
Read access to metadata

Perhaps some other permission is necessary for octokit.rest.repos.getCommit?

@G-Rath thanks! yes, that was it.. when I granted my github app the extra read permission:
Read access to commit statuses

Then the call worked as expected (setting ref to the commit sha).