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

Using context.config return error 'Cannot read property 'getContents' of undefined' #987

Closed
johlju opened this issue Aug 16, 2019 · 8 comments

Comments

@johlju
Copy link

commented Aug 16, 2019

Bug Report

Current Behavior
When running the below code (start it through a run a jest test)

module.exports = (app) => {
  app.on('pull_request.opened', async context => {
    const config = await context.config('config.yml', { perform: false })

    app.log.debug('Perform: %s', config.perform)
  })
}

I get the following error

TypeError: Cannot read property 'getContents' of undefined

It looks like it is having problem with this line.

const response = await this.github.repos.getContents(params)

Am I missing something for this to work?

Expected behavior/code
Should return the default configration.

Environment

  • Probot version(s): 9.3.1
  • Node/npm version: 10.16.3 / 6.10.3
  • OS: [e.g. OSX 10.13.4, Windows 10]: Windows 10

Possible Solution
None

Additional context/Screenshots
This is the full output from the test.

FAIL  test/index.test.js
  DebugProbotConfig
    When pull request is opened
      When executing
        × Should set the correct label (21ms)

  ● DebugProbotConfig › When pull request is opened › When executing › Should set the correct label

    TypeError: Cannot read property 'getContents' of undefined

      1 | module.exports = (app) => {
      2 |   app.on('pull_request.opened', async context => {
    > 3 |     const config = await context.config('config.yml', { perform: false })
        |                                  ^
      4 | 
      5 |     app.log.debug('Perform: %s', config.perform)
      6 |   })

      at Context.<anonymous> (node_modules/probot/src/context.ts:230:48)
      at step (node_modules/probot/lib/context.js:32:23)
      at Object.next (node_modules/probot/lib/context.js:13:53)
      at node_modules/probot/lib/context.js:7:71
      at Object.<anonymous>.__awaiter (node_modules/probot/lib/context.js:3:12)
      at Context.Object.<anonymous>.Context.loadYaml (node_modules/probot/lib/context.js:225:16)
      at Context.<anonymous> (node_modules/probot/src/context.ts:191:31)
      at step (node_modules/probot/lib/context.js:32:23)
      at Object.next (node_modules/probot/lib/context.js:13:53)
      at node_modules/probot/lib/context.js:7:71
      at Object.<anonymous>.__awaiter (node_modules/probot/lib/context.js:3:12)
      at Context.Object.<anonymous>.Context.config (node_modules/probot/lib/context.js:182:16)
      at config (index.js:3:34)
      at Application.<anonymous> (node_modules/probot/src/application.ts:448:17)
      at step (node_modules/probot/lib/application.js:43:23)
      at Object.next (node_modules/probot/lib/application.js:24:53)
      at fulfilled (node_modules/probot/lib/application.js:15:58)

15:24:57.233Z ERROR event: Cannot read property 'getContents' of undefined
  TypeError: Cannot read property 'getContents' of undefined
      at Context.<anonymous> (C:\Source\DebugProbotConfig\node_modules\probot\src\context.ts:230:48)
      at step (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:32:23)
      at Object.next (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:13:53)
      at C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:7:71
      at new Promise (<anonymous>)
      at Object.<anonymous>.__awaiter (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:3:12)
      at Context.Object.<anonymous>.Context.loadYaml (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:225:16)
      at Context.<anonymous> (C:\Source\DebugProbotConfig\node_modules\probot\src\context.ts:191:31)
      at step (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:32:23)
      at Object.next (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:13:53)
      at C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:7:71
      at new Promise (<anonymous>)
      at Object.<anonymous>.__awaiter (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:3:12)
      at Context.Object.<anonymous>.Context.config (C:\Source\DebugProbotConfig\node_modules\probot\lib\context.js:182:16)
      at config (C:\Source\DebugProbotConfig\index.js:3:34)
      at Application.<anonymous> (C:\Source\DebugProbotConfig\node_modules\probot\src\application.ts:448:17)
      at step (C:\Source\DebugProbotConfig\node_modules\probot\lib\application.js:43:23)
      at Object.next (C:\Source\DebugProbotConfig\node_modules\probot\lib\application.js:24:53)
      at fulfilled (C:\Source\DebugProbotConfig\node_modules\probot\lib\application.js:15:58)
@welcome

This comment has been minimized.

Copy link

commented Aug 16, 2019

Thanks for opening this issue. A contributor should be by to give feedback soon. In the meantime, please check out the contributing guidelines and explore other ways you can get involved.

@issue-label-bot issue-label-bot bot added the bug 🐞 label Aug 16, 2019

@issue-label-bot

This comment has been minimized.

Copy link

commented Aug 16, 2019

Issue-Label Bot is automatically applying the label bug 🐞 to this issue, with a confidence of 0.89. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@johlju johlju changed the title Using context.config return error 'this.github.repos.getContents is not a function' Using context.config return error 'Cannot read property 'getContents' of undefined' Aug 16, 2019

@johlju

This comment has been minimized.

Copy link
Author

commented Aug 16, 2019

When running the code to reproduce the error I get another error than in the actual GitHub App, in the GitHub app I get the error

TypeError: this.github.repos.getContents is not a function

From on of the tests.

relabeler › When pull request is opened › When executing › Should write correct comment

    TypeError: this.github.repos.getContents is not a function

      49 | 
      50 |     if (context.payload.pull_request.state === 'open') {
    > 51 |       let config = await context.config('relabeler.yml', { perform: false })
         |                                  ^
      52 | 
      53 |       if (typeof config.perform === 'undefined') {
      54 |         config = {

      at Context.<anonymous> (node_modules/probot/src/context.ts:230:48)
      at step (node_modules/probot/lib/context.js:32:23)
      at Object.next (node_modules/probot/lib/context.js:13:53)
      at node_modules/probot/lib/context.js:7:71
      at Object.<anonymous>.__awaiter (node_modules/probot/lib/context.js:3:12)
      at Context.Object.<anonymous>.Context.loadYaml (node_modules/probot/lib/context.js:225:16)
      at Context.<anonymous> (node_modules/probot/src/context.ts:191:31)
      at step (node_modules/probot/lib/context.js:32:23)
      at Object.next (node_modules/probot/lib/context.js:13:53)
      at node_modules/probot/lib/context.js:7:71
      at Object.<anonymous>.__awaiter (node_modules/probot/lib/context.js:3:12)
      at Context.Object.<anonymous>.Context.config (node_modules/probot/lib/context.js:182:16)
      at config (index.js:51:34)
      at Application.<anonymous> (node_modules/probot/src/application.ts:448:17)
      at step (node_modules/probot/lib/application.js:43:23)
      at Object.next (node_modules/probot/lib/application.js:24:53)
      at fulfilled (node_modules/probot/lib/application.js:15:58)
@gr2m

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2019

Did you mock context.github in your tests?

@johlju

This comment has been minimized.

Copy link
Author

commented Aug 16, 2019

When you say that I'm not certain. 🤔 Is there an example somewhere that shoes how to mock that?

I tried to follow the examples, actually had this working in a prior Probot version (without the config part) ~a year ago. Trying to move it to newest Probot, and tried follow examples (as it has changed a lot since then). Please note that I'm learning as I go here, so I could have missed something.

Here is the test as it is now with the above error.

https://github.com/johlju/relabeler/blob/ab08c84b9f1d4963d821031ac009429a125690bf/test/index.test.js#L138-L150

Happy to send in a PR to add to the testing documentation if needed when I know how this works.

GitHub
A GitHub App that is able to label issues and pull requests on events. - johlju/relabeler
@gr2m

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2019

No worries, I ran into the same problems :) Here is where the context.github methods are currently mocked

https://github.com/johlju/relabeler/blob/ab08c84b9f1d4963d821031ac009429a125690bf/test/index.test.js#L81-L100

You have to update the mocked methods the same way you do in your code. See https://github.com/probot/probot/releases/tag/v9.0.0 for migration instruction.

Ideally we would not mock context.github methods but instead mock the requests, so that name method changes will not cause such problems. And upgrading should be simpler as all methods are deprecated first, and you will see the deprecation messages in your tests.

GitHub
A GitHub App that is able to label issues and pull requests on events. - johlju/relabeler
GitHub
🤖 A framework for building GitHub Apps to automate and improve your workflow - probot/probot
@johlju

This comment has been minimized.

Copy link
Author

commented Aug 16, 2019

@gr2m Thank you so much! I was on the totally wrong track trying to figure out why this didn't work! Just one more letter in the name in that location you referenced, and I had even made comments to remind me what the section did! I have sat with this for several hours 😞😆 Test passing now! Finally back to a clean state so I can continue. 😄
For reference for others: https://github.com/johlju/relabeler/blob/23beb079587e2927c75e2650094b98c3b11ae98f/test/index.test.js#L98

Ideally we would not mock context.github methods but instead mock the requests, so that name method changes will not cause such problems.

Do you mean using nock() instead to mock the requests?

GitHub
A GitHub App that is able to label issues and pull requests on events. - johlju/relabeler
@gr2m

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2019

Do you mean using nock() instead to mock the requests?

Ideally we would have a test where we could test an entire lifecycle, from a mocked webhook to mocked requests that are sent in the handlers. It's definitely high on my priority list for Probot, but am currently busy working on underlying Octokit libraries.

@gr2m gr2m closed this Aug 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.