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

时间:2015-12-13 19:01:50

标签: javascript ember.js github-api

我用这个jsbin http://emberjs.jsbin.com/xeninaceze/edit?js,output

复制了我的案例

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);
      }
    });
  });
};

问题是我想在 msg 旁边流式传输自己的网址链接html_url

我需要知道我如何解决它?因为提交URL链接不在API链接

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

但他们在以下api

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

这使得访问最新提交URL链接html_url

变得更加复杂

这是我想要做的一个很好的例子

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

它在推送活动中汇总了最新的提交消息链接

1 个答案:

答案 0 :(得分:2)

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

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

您可以将作者网址actor访问,将存储库访问为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,output