question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

context.github.repos.getContent not working on repo fork

See original GitHub issue

Bug Report

Current Behavior I’m trying to get file content from a pull request. Some pull request are related to new branches directly created in the repository, others are related to a branch on a fork originated from the repository. In the first case I successfully get the file content while in the second I get this failure

ERROR	 (HttpError): Not Found
    HttpError: Not Found
        at /Users/andreatortosa/pcecmd/commentcmd/node_modules/@octokit/request/dist-node/index.js:66:23
        at processTicksAndRejections (internal/process/task_queues.js:97:5)
        at async sendRequestWithRetries (/Users/andreatortosa/pcecmd/commentcmd/node_modules/@octokit/auth-app/dist-node/index.js:440:12)
        at async Job.doExecute (/Users/andreatortosa/pcecmd/commentcmd/node_modules/bottleneck/light.js:405:18)
status: 404
headers: {
    access-control-allow-origin: "*"
    access-control-expose-headers: "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset"
    connection: "close"
    content-encoding: "gzip"
    content-security-policy: "default-src 'none'"
    content-type: "application/json; charset=utf-8"
    date: "Fri, 13 Nov 2020 07:53:13 GMT"
    referrer-policy: "origin-when-cross-origin, strict-origin-when-cross-origin"
    server: "openresty"
    status: "404 Not Found"
    strict-transport-security: "max-age=31536000; includeSubdomains"
    transfer-encoding: "chunked"
    x-content-type-options: "nosniff"
    x-frame-options: "deny"
    x-github-enterprise-version: "2.21.7"
    x-github-media-type: "github.v3; format=json"
    x-github-request-id: "23e1824a-1664-49b6-ba39-bf8a323de6cf"
    x-ratelimit-limit: "5000"
    x-ratelimit-remaining: "4983"
    x-ratelimit-reset: "1605257458"
    x-runtime-rack: "0.057801"
    x-xss-protection: "1; mode=block"
}
request: {
    method: "GET"
    url: "https://github.XXX.com/api/v3/repos/XXXXX/XXXXX/contents/XXXXXjson?ref=XXXXX"
    headers: {
      "accept": "application/vnd.github.v3+json",
      "user-agent": "probot/10.10.1 octokit-core.js/3.2.1 Node.js/13.10.1 (darwin; x64)",
      "authorization": "token [REDACTED]"
    }
    request: {}
}

Please notice the request is perfectly built and that I m able to run the same request with a curl

    const repoParamsGet = context.repo({owner: prHead[0], repo: repositoryName, path: jsonFiles[0], ref: prHead[1]})
      const getContentResult = await context.github.repos.getContent(
        repoParamsGet
      ).then(result => {
        // content will be base64 encoded
        return [result.data.sha, Buffer.from(result.data.content, 'base64').toString()]
      })
      logger.info('File sha is ' + getContentResult[0] + ' and its content is ' + getContentResult[1])

Expected behavior/code Being able to get the file content also from a branch of forked repository

Environment

  • Probot version(s): 10.9.3 as in package.json
  • Node/npm version: node v13.10.1, npm 6.14.8
  • OS: MacOS Catalina 10.15.7

Possible Solution

Additional context/Screenshots Add any other context about the problem here. If applicable, add screenshots to help explain.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gr2mcommented, Nov 13, 2020

I need to modify it and then update it back but the update file content api does not accept ref so I cannot find a way to do it

I’m afraid that is not possible with apps as far as I know. Access for apps are limited to installations, and an app installation is not automatically expanded to forks.

is there a way to exploit a personal access token in probot ?

you can simply set your personal access token as an environment variable. If the account of the PAT is permitted access to the fork, then you can do the following

  1. Import ProbotOctokit: const { ProbotOctokit } = require("probot")
  2. Instantiate a new octokit instance authenticated using your token: const octokit = new ProbotOctokit({ auth: process.env.MY_USER_ACCESS_TOKEN
0reactions
chris-schracommented, Jan 17, 2022

stumbled upon the very same issue, created an PR to fix the docs: #1637

Read more comments on GitHub >

github_iconTop Results From Across the Web

"context.github.repos.getContents is not a function" · Issue ...
Guessing this is related to a change in an upstream dependency (probot-config, probot itself, etc.) that has deprecated one of the modules used ......
Read more >
Getting error while reading repo contents · Issue #1668 - GitHub
I am new with using probot and my experience till now is great, but I am facing problems with something, So I am...
Read more >
Setting mediaType doesn't work · Issue #243 - GitHub
I'm trying to get the contents of a file in a repository with: github.rest.repos.getContent({ owner, repo, path, mediaType: { format: "raw+json" } ...
Read more >
context.config reads from primary branch, not current commit
Bug Report Current Behavior context.config() reads the configuration from the primary branch (i.e. master) rather than the current commit.
Read more >
How to narrow down Types for a `octokit.repos.getContent ...
type getContentResponseData = Endpoints["GET /repos/{owner}/{repo}/contents/{path}"]["response"]["data"]; // type getContentResponseData = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found