问题
So iv been looking around trying to find out how i can go to a specified github page and get the last commit value and bind this into a value in my application, nothing seems to make sense and there aren't many if any good examples to base anything on. As well as noone seeming to want to share their knowledge on this topic.
Im trying to get the last commit value only from a github page, and use that as a value in my application, can someone give me an example of how to do this? I am using C# with a WPF project type.
回答1:
If you want to clone the repository locally and inspect it, you could use GitSharp libgit2sharp. If that is not an option for you then you can use the github API. The url you are after is:
https://api.github.com/repos/<repo_path>/commits
e.g. https://api.github.com/repos/NancyFx/Nancy/commits
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
using (var response = client.GetAsync("https://api.github.com/repos/NancyFx/Nancy/commits").Result)
{
var json = response.Content.ReadAsStringAsync().Result;
dynamic commits = JArray.Parse(json);
string lastCommit = commits[0].commit.message;
}
}
As mentioned in comments, this will couple your implementation to github, so be sure that your app doesn't need to work with other git hosts in the future if you choose the 2nd option.
来源:https://stackoverflow.com/questions/29877783/github-last-commit