使用GitHub Api和Ember检索最新的提交链接和消息

Retrieve latest Commit links and message with GitHub Api and Ember

本文关键字:提交 链接 消息 最新 检索 GitHub Api Ember 使用      更新时间:2023-09-26

我用这个jsbin重现了我的案例http://emberjs.jsbin.com/xeninaceze/edit?js,输出

Github API允许我按作者获取事件列表:

API链接-API.github.com/users/:user/events

我可以访问过滤事件"PushEvent"的提交消息,这非常好,因为我可以流式传输我最新的提交消息。

var gitactivitiesPromise = function() {
  return new Ember.RSVP.Promise(function (resolve) {
    Ember.$.ajax(eventsAct, {
      success: function(events) {
        var result = [];
        events.filter(function(event) {
          return event.type == 'PushEvent';
        }).forEach(function(item){
          item.payload.commits.map(function(commit){
            result.push(store.createRecord('commit', {
              message: commit.message,
            }));
          });

        });
        resolve(result);
      },  
      error: function(reason) {
        reject(reason);
      }
    });
  });
};

问题是,我想在消息旁边流式传输,还有他自己的url链接html_url

我需要知道如何应对?由于提交url链接不在API链接中

  • api.github.com/users/:user/events

但它们在以下api 中

  • api.github.com/repo/:user/repo/commits/branch

这使得访问最新提交url链接html_url 变得更加复杂

这是我尝试做的一个很好的例子

http://zmoazeni.github.io/gitspective/#

它在推送事件中流式传输带有链接

的最新提交消息

在我看来,所有相关数据都已经存在:

{
    "id": "3414229549",
    "type": "PushEvent",
    "actor": {
      ...
      "login": "paulirish"
    },
    "repo": {
      ...
      "name": "GoogleChrome/devtools-docs"
    },
    "payload": {
      ...
      "commits": [
        {
          ...
          "message": "fish shell. really liking it.",
          "sha": "1f9740c9dd07f166cb4b92ad053b17dbc014145b"
        },
    ...

您可以以actor的形式访问作者URL,以repo的形式访问存储库。有了这个,很容易构建相关链接:

...
.forEach(function(item) {
  var repoUrl = 'https://github.com/' + item.repo.name;
  var authorUrl = 'https://github.com/' + item.actor.login;
  item.payload.commits.map(function(commit) {
    result.push(store.createRecord('commit', {
      authorUrl: authorUrl,
      repositoryUrl: repoUrl,
      commitUrl: repoUrl + '/commit/' + commit.sha,
      message: commit.message
    }));
  });
})
...

更新的JSBin:http://emberjs.jsbin.com/feyedujulu/1/edit?js,输出