问题
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. theoctocat
user shares the Spoon-Knife repository):sha
is the 40 bytes long unique identifier of a Git ObjectYou 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