使用GitAPI浏览提交历史

我想做的事情

在Github的Web API中,GitAPI具有查看指定存储库的提交内容的功能。仅通过查看Github上的提交历史,我们只能看到提交的评论,大概可以得出”我做了这样的事情”。因此,本次我们将尝试通过GitAPI获取提交信息,以获得更详细的信息。

尝试过的事情 (shì qí guò de

使用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函数加载json数据,可以将提交信息作为字典列表获取。data = json.loads(response.text)。由于一个{}对应列表的一维数据,通过适当的处理可以提取出仅包含提交者姓名和日期等详细信息的数据。需要注意的是,正如试图访问GitAPI所示,通过该页面可以看到的只是提交本身的信息。要获取提交所对应的文件信息和提交者信息等,需要跳转到显示的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=ページ数"
广告
将在 10 秒后关闭
bannerAds