我想在网站上显示我最近从 github 提交的所有消息。这可能吗?
若要获取用户的公共事件,应使用 /users/:user/events
终结点(用户执行的事件(:
curl https://api.github.com/users/IonicaBizau/events
这将返回如下所示的 JSON 响应:
[
{
"type": "IssueCommentEvent",
...
}
{
"id": "3349705833",
"type": "PushEvent",
"actor": {...},
"repo": {...},
"payload": {
"push_id": 868451162,
"size": 13,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "0ea1...12162",
"before": "548...d4bd",
"commits": [
{
"sha": "539...0892e",
"author": {...},
"message": "Some message",
"distinct": false,
"url": "https://api.github.com/repos/owner/repo/commits/53.....92e"
},
...
]
},
"public": true,
"created_at": "2015-11-17T11:05:04Z",
"org": {...}
},
...
]
现在,您只需要过滤响应以仅包含PushEvent
项。
由于您想在网站上显示这些事件,因此您可能希望用javascript对其进行编码。下面是如何使用gh.js
(JavaScript/Node的同构GitHub API包装器(.js由我编写的示例:
// Include gh.js
const GitHub = require("gh.js");
// Create the GitHub instance
var gh = new GitHub();
// Get my public events
gh.get("users/IonicaBizau/events", (err, res) => {
if (err) { return console.error(err); }
// Filter by PushEvent type
var pushEvents = res.filter(c => {
return c.type === "PushEvent";
});
// Show the date and the repo name
console.log(pushEvents.map(c => {
return "Pushed at " + c.created_at + " in " + c.repo.name;
}).join("n"));
// => Pushed at 2015-11-17T11:05:04Z in jillix/jQuery-json-editor
// => Pushed at 2015-11-16T18:56:05Z in IonicaBizau/html-css-examples
// => Pushed at 2015-11-16T16:36:37Z in jillix/node-cb-buffer
// => Pushed at 2015-11-16T16:35:57Z in jillix/node-cb-buffer
// => Pushed at 2015-11-16T16:34:58Z in jillix/node-cb-buffer
// => Pushed at 2015-11-16T13:39:33Z in IonicaBizau/ghosty
});