Github api获得最后提交的用户

时间:2017-10-02 19:11:32

标签: github github-api

我想获得最后一个用户exe。无论回购如何,在github上提交的最后100个用户。我已经环顾了github api,但无法找到特定的api电话。

1 个答案:

答案 0 :(得分:0)

您可以使用Github Events API并过滤PushEvent

https://api.github.com/events?per_page=100

在Github上进行过最后100次提交的用户

由于PushEvent可能有多次提交,您必须为每个sizePushEvent,直到达到100.请注意,您还需要排除PushEvent 0提交。您还必须管理分页,因为您可以一次请求最多100个事件(如果一个请求不足以获得100个提交)。

使用nodeJS的示例:

var request = require("request");

const maxCommit = 100;
const accessToken = 'YOUR_ACCESS_TOKEN';

function doRequest(page){
    return new Promise(function (resolve, reject) {
        request({
          url: 'https://api.github.com/events?per_page=100&page=' + page,
          headers: {
            'User-Agent': 'Some-App',
            'Authorization': 'Token ' + accessToken
          }
        }, function (err, response, body) {
          if (!err) {
            resolve(body);
          } else {
            reject(err);
          }
        });
    })
}

async function getEvents() {
    var commitCount = 0;
    var page = 1;
    var users = [];

    while (commitCount < maxCommit) {
        var body = await doRequest(page);
        var data = JSON.parse(body);
        var pushEvents = data.filter(it => it.type == 'PushEvent' && it.payload.size > 0);
        commitCount += pushEvents.reduce((it1, it2) =>  it1 + it2.payload.size, 0);
        users = users.concat(pushEvents.map(event => ({ 
            login: event.actor.login, 
            commitCount: event.payload.size
        })));
        page++;
    }
    var count = 0;
    for (var i = 0; i < users.length; i++) {
        count += users[i].commitCount;
        if (count >= maxCommit){
            users = users.slice(0, i + 1);
            break;
        }
    }
    console.log(users);
}

getEvents();

已推送Github提交的最近100位用户

唯一改变的是我们只检查size字段是&gt; 0并为不同的用户构建地图。

使用nodeJS的示例:

var request = require("request");

const maxUser = 100;
const accessToken = 'YOUR_ACCESS_TOKEN';

function doRequest(page){
    return new Promise(function (resolve, reject) {
        request({
          url: 'https://api.github.com/events?per_page=100&page=' + page,
          headers: {
            'User-Agent': 'Some-App',
            'Authorization': 'Token ' + accessToken
          }
        }, function (err, response, body) {
          if (!err) {
            resolve(body);
          } else {
            reject(err);
          }
        });
    })
}

async function getEvents() {
    var page = 1;
    var users = {};

    while (Object.keys(users).length < maxUser) {
        var body = await doRequest(page);
        var data = JSON.parse(body);
        var pushEvents = data.filter(it => it.type == 'PushEvent' && it.payload.size > 0);
        for (var i = 0; i < pushEvents.length; i++) {
            users[pushEvents[i].actor.login] = pushEvents[i].payload.size;
            if (Object.keys(users).length == maxUser) { 
                break;
            }
        }
        page++;
    }
    console.log(users);
}

getEvents();