[英]How to get latest commit date - Github API
我正在編寫一個git reps的小型數據庫,我想知道如果我在我的數據庫中列出了rep,我將如何繼續並獲取最新提交的日期。
我從來沒有使用過github API,而且我很難繞過它。
如果有人能幫我搞清楚,我會非常感激。 PHP或JS優先,因為我發現的所有例子都在ruby中。
老問題,但我想指出(至少使用api v3)你可以使用branches api來獲取特定分支的最新提交日期。 我假設在你的情況下你對主人感興趣。
看起來像這樣:
https://api.github.com/repos/:owner/:repo/branches/master
如果您使用PHP作為示例 ,我將使用cURL
而不是file_get_contents
,因為您需要配置allow-url-fopen 。
GitHub還要求您在標題中發送user-agent
: https : //developer.github.com/v3/#user-agent-required
例如,您的PHP代碼看起來像;
$objCurl = curl_init();
//The repo we want to get
curl_setopt($objCurl, CURLOPT_URL, "https://api.github.com/repos/google/blueprint/commits");
//To comply with https://developer.github.com/v3/#user-agent-required
curl_setopt($objCurl, CURLOPT_USERAGENT, "StackOverflow-29845346");
//Skip verification (kinda insecure)
curl_setopt($objCurl, CURLOPT_SSL_VERIFYPEER, false);
//Get the response
$response = curl_exec($objCurl);
print_r( json_decode($response, true) );
注意:您將能夠繼續使用file_get_contents
並發送user-agent
標頭。 看到這個答案
我想回答這個問題,所以我做了一個非常小的演示,了解如何獲取最新提交的日期。
輸出:
ta-dachi
master
2019-03-21T14:50:22Z <----- What you want
b80126c3ea900cd7c92729e652b2e8214ff014d8
https://github.com/ta-dachi/eatsleepcode.tech/tree/master
的index.html
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>React Local</title>
<script
type="application/javascript"
src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"
></script>
<script
type="application/javascript"
src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"
></script>
<script
type="application/javascript"
src="https://unpkg.com/@babel/standalone/babel.min.js"
></script>
<script
type="application/javascript"
src="https://unpkg.com/whatwg-fetch@3.0.0/dist/fetch.umd.js"
></script>
</head>
<body>
<div id="root"></div>
<script type="text/jsx" src="index.jsx"></script>
</body>
index.jsx
/**
* See https://developer.github.com/v3/repos/branches/#get-branch
*
* Example Github api request:
* https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master
*/
class LatestCommitComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
author: "",
branch: "",
date: "",
sha: "",
link: ""
};
}
componentDidMount() {
// Replace this with your own repo
// https://api.github.com/repos/:owner/:repo/branches/master
fetch(
"https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master"
)
.then(response => {
response.json().then(json => {
console.log(json);
this.setState({
author: json.commit.author.login,
branch: json.name,
date: json.commit.commit.author.date,
sha: json.commit.sha,
link: json._links.html
});
});
})
.catch(error => {
console.log(error);
});
}
render() {
return (
<div>
<div>{this.state.author}</div>
<div>{this.state.branch}</div>
<div>{this.state.date}</div>
<div>{this.state.sha}</div>
<div>{this.state.link}</div>
</div>
);
}
}
ReactDOM.render(<LatestCommitComponent />, document.getElementById("root"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.