是否可以在应用程序的初始化过程中使用“上下文”执行操作?

时间:2018-09-18 16:43:37

标签: probot

我只是在寻找这样的东西

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

基本上,我只需要对github API进行调用,但是我不确定是否可以在不使用Context对象内部使用API​​客户端的情况下进行此操作。

2 个答案:

答案 0 :(得分:0)

我最终使用了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
  })
}

答案 1 :(得分:0)

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

无论如何,在使用大量实际app对象进行大量挖掘之后,我设法做到了-它的.auth()方法返回一个包含GitHubAPI接口的Promise: 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);

您将获得一个数组,其中包含可以在.auth()中使用的ID。