tonic + npm: github-api

node v0.12.18
version: 1.0.0
endpointsharetweet
This is an example of one might go about getting webhooks for a user's repositories. This example WILL NOT WORK here. Webhooks are a protected resource and so you need to be authenticated to see them. High level: - Create an in instance of the GitHub client - Fetch the user's repository data - Convert the raw data into Repos - Fetch the webhooks from the repositories
var GitHub = require("github-api"); var Promise = require("es6-promise").Promise; var gh = new GitHub(); // check our rate-limit status // since we're unauthenticated the limit is 60 requests per hour gh.getRateLimit().getRateLimit() .then(function(resp) { console.log('Limit remaining: ' + resp.data.rate.remaining); // date constructor takes epoch milliseconds and we get epoch seconds console.log('Reset date: ' + new Date(resp.data.rate.reset * 1000)); }).catch(function(error) { console.log('Error fetching rate limit', error.message); }); var user = 'slunk32'; gh.getUser(user).getRepos() // turn the json objects we fetched into `Repository`s .then(function(httpPromise) { return httpPromise.data.map(function(repoJson) { // console.log('repo ' + repoJson.name); return gh.getRepo(user, repoJson.name); }); }) // Curry promises to fetch webhooks .then(function(repos) { console.log(repos); return Promise.all(repos.map(function(repo) { return repo.listHooks(); })); }) // Fetch the webhooks json .then(function(listOfListOfHooks) { listOfListOfHooks = listOfListOfHooks || []; // monkey-patch for non-authenticated users listOfListOfHooks.forEach(function(hooksHttpResonse) { console.log('hooks for ' + hooksHttpResonse.config.url); console.log(hooksHttpResonse.data); }); }) .catch(function(error) { console.log('an error occurred fetching the webhooks', error); });
Loading…

no comments

    sign in to comment