Commit 7a0c01ad authored by Girish Ramakrishnan's avatar Girish Ramakrishnan

Fix pagination issues

starred repos, tags are all paginated and the previous module we used
does not support it

this is the official node module
parent 6b796546
'use strict';
var assert = require('assert'),
GitHub = require('github-api');
{ Octokit } = require('@octokit/rest');
module.exports = exports = {
verifyToken: verifyToken,
......@@ -13,8 +13,8 @@ module.exports = exports = {
// translate some api errors
function handleError(callback) {
return function (error) {
if (error.response) {
if (error.response.status === 403 && error.response.data.message.indexOf('API rate limit exceeded') === 0) {
if (error) {
if (error.status === 403 && error.message.indexOf('API rate limit exceeded') === 0) {
error.message = 'GitHub rate limit exceeded. Please wait a bit.';
}
}
......@@ -27,10 +27,9 @@ function verifyToken(token, callback) {
assert.strictEqual(typeof token, 'string');
assert.strictEqual(typeof callback, 'function');
var api = new GitHub({ token: token });
var user = api.getUser();
const octokit = new Octokit({ auth: token, userAgent: 'releasebell@cloudron' });
user.listStarredRepos().then(function () {
octokit.users.getAuthenticated().then(function () {
callback();
}, handleError(callback));
}
......@@ -39,11 +38,10 @@ function getStarred(token, callback) {
assert.strictEqual(typeof token, 'string');
assert.strictEqual(typeof callback, 'function');
var api = new GitHub({ token: token });
var user = api.getUser();
const octokit = new Octokit({ auth: token, userAgent: 'releasebell@cloudron' });
user.listStarredRepos().then(function (result) {
callback(null, result.data);
octokit.paginate(octokit.activity.listReposStarredByAuthenticatedUser).then(function (result) {
callback(null, result);
}, handleError(callback));
}
......@@ -53,25 +51,25 @@ function getReleases(token, project, callback) {
assert.strictEqual(typeof project, 'object');
assert.strictEqual(typeof callback, 'function');
var api = new GitHub({ token: token });
var repo = api.getRepo(project.name);
const octokit = new Octokit({ auth: token, userAgent: 'releasebell@cloudron' });
repo.listTags().then(function (result) {
callback(null, result.data.map(function (r) { return { projectId: project.id, version: r.name, createdAt: r.createdAt, sha: r.commit.sha }; }));
const [ owner, repo ] = project.name.split('/');
octokit.paginate(octokit.repos.listTags, { owner, repo }).then(function (result) { // tags have no created_at field
callback(null, result.map(function (r) { return { projectId: project.id, version: r.name, createdAt: null, sha: r.commit.sha }; }));
}, handleError(callback));
}
// Returns { createdAt }
function getCommit(token, project, sha, callback) {
function getCommit(token, project, commit_sha, callback) {
assert.strictEqual(typeof token, 'string');
assert.strictEqual(typeof project, 'object');
assert.strictEqual(typeof sha, 'string');
assert.strictEqual(typeof commit_sha, 'string');
assert.strictEqual(typeof callback, 'function');
var api = new GitHub({ token: token });
var repo = api.getRepo(project.name);
const octokit = new Octokit({ auth: token, userAgent: 'releasebell@cloudron' });
repo.getCommit(sha).then(function (result) {
const [ owner, repo ] = project.name.split('/');
octokit.git.getCommit({ owner, repo, commit_sha }).then(function (result) {
callback(null, { createdAt: result.data.committer.date });
}, callback);
}
......@@ -83,7 +83,7 @@ function syncGithubStarredByUser(user, callback) {
if (!user.githubToken) return callback();
debug('syncGithubStarredByUser: ', user.id)
debug('syncGithubStarredByUser: ', user.id);
github.getStarred(user.githubToken, function (error, result) {
if (error) return callback(error);
......
This diff is collapsed.
......@@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@octokit/rest": "^17.6.0",
"async": "^2.6.0",
"connect-lastmile": "^1.2.1",
"connect-timeout": "^1.9.0",
......@@ -16,7 +17,6 @@
"db-migrate-mysql": "^1.1.10",
"debug": "^3.1.0",
"express": "^4.16.3",
"github-api": "^3.3.0",
"handlebars": "^4.1.2",
"ldapjs": "^1.0.2",
"mysql": "^2.15.0",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment