LoginSignup
0

GitAPIを使ってコミット履歴を眺める

Last updated at Posted at 2023-01-12

やりたいこと

GithubのWebAPIであるGitAPIには、指定したリポジトリのコミット内容を見る機能があります。Githubからコミット履歴を見るだけでは、コミットのコメント見て「こんな事したなぁ」となる程度です。
そこで今回、GitAPIでコミット情報を見てより詳細な情報を手に入れてみようと思います。

やってみたこと

・pythonのjsonとrequestsライブラリを使ってGitAPIにアクセス
・リストとして帰ってきたコミット情報から、一部の情報の遷移をリストにしてみる

GitAPIへのアクセス

"https://api.github.com/repos/所有者名/リポジトリ名/commits"
にアクセスすると、その所有者が持つリポジトリのコミット履歴を確認することができます。
Pythonを使ってアクセスするには、requests.getを使いましょう。
response = requests.get(https://api.github.com/repos/someone/hogehoge/commits)
jsonでloadsすることで、コミット情報を辞書型リストとして取得することができます。
data = json.loads(response.text)
{}1つがリストの1次元分なので、工夫すればコミット者名や日時の詳細などの情報のみを抜き出すことも可能です。
注意することとして、試しにGitAPIにアクセスすると見える通り、このページから分かるのはコミットそのものの情報であることです。コミットに追随するファイル情報やコミット者情報などは、表示されているURLに飛び直す必要があります。
私はファイル名の情報が欲しかったので、アクセスした後更に["url"]のurlにアクセスし、["files"]["ファイルの番目"]["filename"]を見ました。

実行結果は以下のようになります

    "sha": "59cb4e3f51bdb76a1bedd105fbd5fb3be82adf1a",
    "node_id": "MDY6Q29tbWl0MTA0MDY1MTY5OjU5Y2I0ZTNmNTFiZGI3NmExYmVkZDEwNWZiZDVmYjNiZTgyYWRmMWE=",
    "commit": {
      "author": {
        "name": "dependabot[bot]",
        "email": "49699333+dependabot[bot]@users.noreply.github.com",
        "date": "2019-12-27T09:00:22Z"
      },
      "committer": {
        "name": "Phoebe Schmidt",
        "email": "phoebe.schmidt@contentful.com",
        "date": "2019-12-27T09:00:22Z"
      },
      "message": "build(deps): bump handlebars from 4.0.11 to 4.5.3 (#135)\n\nBumps [handlebars](https://github.com/wycats/handlebars.js) from 4.0.11 to 4.5.3.\r\n- [Release notes](https://github.com/wycats/handlebars.js/releases)\r\n- [Changelog](https://github.com/wycats/handlebars.js/blob/master/release-notes.md)\r\n- [Commits](https://github.com/wycats/handlebars.js/compare/v4.0.11...v4.5.3)\r\n\r\nSigned-off-by: dependabot[bot] <support@github.com>",
      "tree": {
        "sha": "201d165acef15f78a8bd53d821850fdcbe1058b1",
        "url": "https://api.github.com/repos/contentful/the-example-app.nodejs/git/trees/201d165acef15f78a8bd53d821850fdcbe1058b1"
      },
      "url": "https://api.github.com/repos/contentful/the-example-app.nodejs/git/commits/59cb4e3f51bdb76a1bedd105fbd5fb3be82adf1a",
      "comment_count": 0,
      "verification": {
        "verified": false,
        "reason": "unsigned",
        "signature": null,
        "payload": null
      }
    },
    "url": "https://api.github.com/repos/contentful/the-example-app.nodejs/commits/59cb4e3f51bdb76a1bedd105fbd5fb3be82adf1a",
    "html_url": "https://github.com/contentful/the-example-app.nodejs/commit/59cb4e3f51bdb76a1bedd105fbd5fb3be82adf1a",
    "comments_url": "https://api.github.com/repos/contentful/the-example-app.nodejs/commits/59cb4e3f51bdb76a1bedd105fbd5fb3be82adf1a/comments",
    "author": {
      "login": "dependabot[bot]",
      "id": 49699333,
      "node_id": "MDM6Qm90NDk2OTkzMzM=",
      "avatar_url": "https://avatars.githubusercontent.com/in/29110?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/dependabot%5Bbot%5D",
      "html_url": "https://github.com/apps/dependabot",
      "followers_url": "https://api.github.com/users/dependabot%5Bbot%5D/followers",
      "following_url": "https://api.github.com/users/dependabot%5Bbot%5D/following{/other_user}",
      "gists_url": "https://api.github.com/users/dependabot%5Bbot%5D/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/dependabot%5Bbot%5D/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/dependabot%5Bbot%5D/subscriptions",
      "organizations_url": "https://api.github.com/users/dependabot%5Bbot%5D/orgs",
      "repos_url": "https://api.github.com/users/dependabot%5Bbot%5D/repos",
      "events_url": "https://api.github.com/users/dependabot%5Bbot%5D/events{/privacy}",
      "received_events_url": "https://api.github.com/users/dependabot%5Bbot%5D/received_events",
      "type": "Bot",
      "site_admin": false
    },
    "committer": {
      "login": "phoebeschmidt",
      "id": 13663991,
      "node_id": "MDQ6VXNlcjEzNjYzOTkx",
      "avatar_url": "https://avatars.githubusercontent.com/u/13663991?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/phoebeschmidt",
      "html_url": "https://github.com/phoebeschmidt",
      "followers_url": "https://api.github.com/users/phoebeschmidt/followers",
      "following_url": "https://api.github.com/users/phoebeschmidt/following{/other_user}",
      "gists_url": "https://api.github.com/users/phoebeschmidt/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/phoebeschmidt/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/phoebeschmidt/subscriptions",
      "organizations_url": "https://api.github.com/users/phoebeschmidt/orgs",
      "repos_url": "https://api.github.com/users/phoebeschmidt/repos",
      "events_url": "https://api.github.com/users/phoebeschmidt/events{/privacy}",
      "received_events_url": "https://api.github.com/users/phoebeschmidt/received_events",
      "type": "User",
      "site_admin": false
    },
    "parents": [
      {
        "sha": "fa92ca3f8f0db1fbadb79b33f078f38981a4bee2",
        "url": "https://api.github.com/repos/contentful/the-example-app.nodejs/commits/fa92ca3f8f0db1fbadb79b33f078f38981a4bee2",
        "html_url": "https://github.com/contentful/the-example-app.nodejs/commit/fa92ca3f8f0db1fbadb79b33f078f38981a4bee2"
      }```

どうもGitAPIでは一度に表示できるコミットは30件が限界のようですので、それ以上を見たい場合は、ページを切り替えてもう一度やる必要があります
"https://api.github.com/repos/所有者名/リポジトリ名/commits?page=ページ数"

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0