Is it possible to perform an action with `context` on the init of the app?

340 Views Asked by At

I'm simply looking for something like this

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

Basically I just need to make to calls to the github API, but I'm not sure there is a way to do it without using the API client inside the Context object.

2

There are 2 best solutions below

0
On BEST ANSWER

I ended up using 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
  })
}

0
On

I tried probot-scheduler but it didn't exist - perhaps removed in an update?

In any case, I managed to do it after lots of digging by using the actual app object - it's .auth() method returns a promise containing the GitHubAPI interface: 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() takes the ID of the installation if you wish to access private data. If called empty, the client will can only retrieve public data.

You can get the installation ID by calling .auth() without paramaters, and then listInstallations():

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

You get an array including IDs that you can in .auth().