【问题标题】:Is it possible to perform an action with `context` on the init of the app?是否可以在应用程序的初始化时使用“上下文”执行操作?
【发布时间】:2018-09-18 16:43:36
【问题描述】:

我只是在寻找这样的东西

app.on('init', async context => {
 ...
})

基本上我只需要调用 github API,但我不确定是否有办法在不使用 Context 对象内的 API 客户端的情况下做到这一点。

【问题讨论】:

    标签: probot


    【解决方案1】:

    我最终使用了 probot-scheduler

    const createScheduler = require('probot-scheduler')
    module.exports = app => {
    
      createScheduler(app, {
        delay: false
      })
      robot.on('schedule.repository', context => {
        // this is called on startup and can access context
      })
    }

    【讨论】:

      【解决方案2】:

      我尝试了 probot-scheduler,但它不存在 - 可能在更新中被删除?

      无论如何,经过大量挖掘,我设法通过使用实际的 app 对象来做到这一点 - 它的 .auth() 方法返回一个包含 GitHubAPI 接口的承诺: https://probot.github.io/api/latest/classes/application.html#auth

      module.exports = app => {
          router.get('/hello-world', async (req, res) => {
              const github = await app.auth();
              const result = await github.repos.listForOrg({'org':'org name});
              console.log(result);
          })
      }
      

      .auth() 如果您希望访问私人数据,则获取安装的 ID。如果调用空,客户端将只能检索公共数据。

      可以通过不带参数调用.auth(),然后listInstallations()获取安装ID:

      const github = await app.auth();
      const result = github.apps.listInstallations();
      console.log(result);
      

      您会得到一个包含 ID 的数组,您可以在 .auth() 中找到。

      【讨论】:

        猜你喜欢
        • 2010-09-23
        • 2011-11-17
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多