Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to PUT to the file pertaining to a specific branch despite providing it in the path #461

Open
CodeWithSubramani opened this issue Jun 12, 2022 · 9 comments
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs

Comments

@CodeWithSubramani
Copy link

octokit.request(PUT /repos/looker-bi-eng/gcsc_intel_auto_prod/contents/intl_ppc/views/inline_table.view.lkml?ref=dev-subramani-ravichandran-cxzc, {.....

Although, I am passing the branch name in the ref parameter, the request is being redirected the file on the master branch. Can you help?

@oscard0m
Copy link
Member

Hi @subramani-r-0595,

Could you share a more complete snippet of code, the Octokit version you are using and the response you are obtaining?

Thanks

@gr2m gr2m added the Type: Support Any questions, information, or general needs around the SDK or GitHub APIs label Jun 25, 2022
@kentcdodds
Copy link

I have a similar question. Are query params supposed to be supported? There's no documentation for it on octokit (as far as I can tell), but I believe this is how the GitHub's API works for specifying a branch.

@gr2m
Copy link
Contributor

gr2m commented Oct 24, 2022

Parameters you set to a GET or HEAD request are automatically passed as query parameters. E.g.

request("GET /orgs/{org}/repos", {
  org: "octokit",
  type: "public"
})

Sends a GET request to https://api.github.com/orgs/octokit/repos?type=public

For other request methods such as POST or PUT, you have to use URL templates in order for parameters to be sent as query parameters, e.g

await octokit.request('POST /repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}', {
  owner: 'octokit',
  repo: 'request.js',
  release_id: 1234,
  name: "my-release-asset.text",
  data: "lorem ipsum"
})

For the case of updating a file, all parameters are expected to be passed as URL path and body parameters, none as URL query parameters, see documentation. The branch parameter is to be sent as part of the request body.

Could you share a snippet of code that is not behaving as expected? Does it work when you use fetch or another library, or some other tool like curl?

@kentcdodds
Copy link

In my case I'm doing a GET and was just surprised by the lack of documentation. When doing a search I landed here. I've not tried it yet, but if it is supported it should probably be documented

@gr2m
Copy link
Contributor

gr2m commented Oct 24, 2022

Yeah, the documentation is ... lacking.

The gist is you can take any of GItHub's REST API endpoint documented at https://docs.github.com/en/rest/, pass the route as first argument, and the parameters as second, independent of how they are transported.

If you want to use Octokit, you'll probably also want to use the all-batteries included library at https://github.com/octokit/octokit.js. If bundle size is a concern, use https://github.com/octokit/core.js. If you have any more questions / would like to discuss your use case, you know how to reach me 👋🏼

@kentcdodds
Copy link

Thanks! I'll give it a shot and report back whether I have any issues.

@kentcdodds
Copy link

That worked great. Thanks a bunch! Here's the code I'm using it in for others who want a reference (no pun intended):

async function downloadFile(path: string) {
  const {data} = (await octokit.request(
    `GET /repos/{owner}/{repo}/contents/{path}`,
    {
      owner: 'kentcdodds',
      repo: 'kentcdodds.com',
      path,
      ref,
    },
  )) as {data: {content?: string; encoding?: string}}

  if (!data.content || !data.encoding) {
    console.error(data)
    throw new Error(
      `Tried to get ${path} but got back something that was unexpected. It doesn't have a content or encoding property`,
    )
  }

  //                                lol
  const encoding = data.encoding as Parameters<typeof Buffer.from>['1']
  return Buffer.from(data.content, encoding).toString()
}

@gr2m
Copy link
Contributor

gr2m commented Oct 24, 2022

It's odd that you need to type the response. octokit.request should give you full type support

GET /repos/{owner}/{repo}/contents/{path} is a weird endpoint as it can return one of 4 types as documented at https://docs.github.com/en/rest/repos/contents#get-repository-content. Depending on your use case you should probably account for the possibility that path is not a file

See my TypeScript playground here

I really should finally encapsulate the "get file contents" use case into a re-usable plugin, because this endpoint really is pain. I created one to create/update a file: https://github.com/octokit/plugin-create-or-update-text-file.js#readme

Lastly, if the repository from which you download the files is public and you are worried about rate limiting, you can download static filed data from, you can download it from https://raw.githubusercontent.com which is not rate limited , e.g. https://raw.githubusercontent.com/octokit/core.js/master/README.md

@kentcdodds
Copy link

Thanks for the tips @gr2m!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
None yet
Development

No branches or pull requests

4 participants