How do I make use of the GitHub API?

依然范特西╮ 提交于 2019-12-20 04:28:15

问题


I am not sure what it means when it tells me to use:

GET /repos/:owner/:repo/commits/:sha

How can I use that API call to retrieve the information I am looking for?


回答1:


GET /repos/:owner/:repo/commits/:sha

  • GET is the HTTP method to use in order to invoke this API endpoint.

  • :owner is the name of the user or organization (eg. octocat is the name of a user).

  • :repo is the name of a repository owned by the chosen user or organization (eg. the octocat user shares the Spoon-Knife repository)

  • :sha is the 40 bytes long unique identifier of a Git Object

  • You have to prepend you call with the root endpoint -> https://api.github.com.

Considering this, in order to show

  • The commit bb4cc8d3b2e14b3af5df699876dd4ff3acd00b7f
  • In the repository Spoon-Knife
  • Belonging to the user octocat

One would issue a GET Http call to the following url

https://api.github.com/repos/octocat/Spoon-Knife/commits/bb4cc8d3b2e14b3af5df699876dd4ff3acd00b7f

For instance, using the following curl command...

$ curl https://api.github.com/repos/octocat/Spoon-Knife/commits/bb4cc8d3b2e14b3
af5df699876dd4ff3acd00b7f

...will return the following Json payload

{
  "sha": "bb4cc8d3b2e14b3af5df699876dd4ff3acd00b7f",
  "commit": {
    "author": {
      "name": "The Octocat",
      "email": "octocat@nowhere.com",
      "date": "2014-02-04T22:38:36Z"
    },
    "committer": {
      "name": "The Octocat",
      "email": "octocat@nowhere.com",
      "date": "2014-02-12T23:18:55Z"
    },
    "message": "Create styles.css and updated README",

...[snipped for brevity]...

      "patch": "@@ -0,0 +1,17 @@\n+* {\n+  margin:0px;\n+  padding:0px;\n+}\n+\n+#octocat {\n+  display: block;\n+  width:384px;\n+  margin: 50px auto;\n+}\n+\n+p {\n+  display: block;\n+  width: 400px;\n+  margin: 50px auto;\n+  font: 30px Monaco,\"Courier New\",\"DejaVu Sans Mono\",\"Bitstream Vera Sans Mono\",monospace;\n+}"
    }
  ]
}

Update

As rightfully pointed at by @matsjoyce, numerous libraries abstract this low level operations and expose a more user friendly interface. Most of them are listed at https://developer.github.com/libraries/



来源:https://stackoverflow.com/questions/26193577/how-do-i-make-use-of-the-github-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!