如何通过GitHub API找到某个用户提交的所有提交?

时间:2014-02-17 21:17:10

标签: web-services rest github github-api

假设我已获得必要的GitHub身份验证凭据,我如何从GitHub API获取某个用户的所有提交?

工作代码

在下面你会找到我写的extract的简化CoffeeScript Meteor application,它实现了在接受的答案中建议的策略。在此片段之外,代码需要一个全局变量Configuration,它应该包含一个属性user作为GitHub用户名,一个属性token是该用户的GitHub身份验证令牌。该代码还将发现的提交存储在全局Meteor集合Commits中。这段代码中可能存在错误,但它至少在某种程度上起作用。欢迎提出改进建议。

logger = new Logger("github")

getResource = (uri, done) ->
  options =
    auth: "token:#{Configuration.token}"
    headers:
      "User-Agent": "GitHub Comments"
  HTTP.call("get", uri, options, done)

getCommits = (user, userUrl, time, done, page=1, numCommits=0) ->
  eventsUrl = "#{userUrl}/events?page=#{page}"
  logger.debug("Getting events page #{page} for user #{Configuration.user}", eventsUrl)
  getResource(eventsUrl, (error, result) ->
    if error?
      logger.warn("Error", error)
      throw new Error("Couldn't get events for user #{Configuration.user}")

    events = (event for event in result.data when event.type == "PushEvent" && event.payload.commits.length > 0)
    commits = []
    for event in events
      for commit in event.payload.commits when commit.author.email == user.email
        commits.push(
          sha: commit.sha
          url: commit.url
        )

    logger.debug("Found #{commits.length} commits attributable to user #{user.email} on events page #{page}")
    for commit in commits
      logger.debug("Getting details for commit #{commit.sha}", commit.url)
      try
        commitDetails = getResource(commit.url).data
      catch err
        logger.warn("Couldn't get details for commit #{commit.url}", err)
        continue

      Commits.insert(commit)
      ++numCommits

    if result.data.length > 0
      logger.debug("Calling getCommits for next page with numCommits #{numCommits}")
      getCommits(user, userUrl, time, done, page+1, numCommits)
    else
      done(numCommits)
  )

pollGitHub = ->
  logger.info("Polling GitHub for data")

  start = moment()

  userUrl = "https://api.github.com/users/#{Configuration.user}"
  getResource(userUrl, (error, result) ->
    if error?
      throw new Error("Couldn't get data for user #{Configuration.user}")

    user = result.data
    if !user.email
      throw new Error("Need to be able to identify user by email")

    logger.debug("Removing all previous commits belonging to #{Configuration.user}")
    Commits.remove(user: Configuration.user)
    numCommits = 0
    getCommits(user, userUrl, start, (numCommits) ->
      taken = moment().diff(start, "seconds")
      logger.info("Inserted #{numCommits} commits for user #{Configuration.user} in #{taken} seconds")
    )
  )

Meteor.startup(->
  pollGitHub()
)

1 个答案:

答案 0 :(得分:2)

您可以选择几种不同的路线:

回购/提交

迭代用户回购并获取每个

的提交
GET /users/:user/repos
GET /repos/:owner/:repo/commits

http://developer.github.com/v3/repos/#list-user-repositories http://developer.github.com/v3/repos/commits/#list-commits-on-a-repository

推送活动

GET /users/:user/events

http://developer.github.com/v3/activity/events/types/#pushevent http://developer.github.com/v3/activity/events/#list-events-that-a-user-has-received

只要有人推,就会触发推送事件,因为要查看需要推送的提交,如果查看用户推送事件,那么您将看到相关的提交!