📜  github 问题 api - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:58.522000             🧑  作者: Mango

Github 问题 API - Shell-Bash

Github 提供的问题 API 允许开发者通过 API 访问 Github 上的问题(Issues)以及与之相关的数据,例如标签(Labels)、里程碑(milestone)和评论(comments)等。本文将介绍如何使用 Shell-Bash 编写程序来访问 Github 问题 API 并获取相关数据。

1. 准备工作

在使用 Github 问题 API 之前,需要先获得 Github Personal Access Token。可以在 Github 设置页面中的 Developer settings > Personal Access Tokens 中创建一个新的 Token。创建 Token 时需要设置权限,为了使用 Github 问题 API,需要勾选 repo 和 read:org 两个权限,如图所示:

Github Personal Access Token

创建 Token 后,将其存储在 Shell-Bash 的环境变量中,例如:

export GITHUB_TOKEN=your_token_here
2. 获取问题(Issues)数据

使用 Github 问题 API 获取问题数据需要向以下 URL 发送 GET 请求:

https://api.github.com/repos/{owner}/{repo}/issues

其中 {owner}{repo} 分别代表问题所属的仓库的拥有者和仓库名称。例如,如果要获取开源项目 Electron 的 issues,可以使用以下 Endpoint:

https://api.github.com/repos/electron/electron/issues

将上述 URL 作为 curl 命令的参数,即可获取 JSON 格式的问题数据:

curl https://api.github.com/repos/electron/electron/issues | jq .

使用 jq 工具格式化 JSON 数据,结果如下:

[
  {
    "url": "https://api.github.com/repos/electron/electron/issues/31328",
    "repository_url": "https://api.github.com/repos/electron/electron",
    "labels_url": "https://api.github.com/repos/electron/electron/issues/31328/labels{/name}",
    "comments_url": "https://api.github.com/repos/electron/electron/issues/31328/comments",
    "events_url": "https://api.github.com/repos/electron/electron/issues/31328/events",
    "html_url": "https://github.com/electron/electron/issues/31328",
    "id": 996953008,
    "node_id": "MDU6SXNzdWU5OTY5NTMwMDg=",
    "number": 31328,
    "title": "Concurrent use of session unexpectedly affects other windows",
    "user": {
      "login": "pablohisao",
      "id": 37221557,
      "node_id": "MDQ6VXNlcjM3MjIxNTU3",
      "avatar_url": "https://avatars.githubusercontent.com/u/37221557?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/pablohisao",
      "html_url": "https://github.com/pablohisao",
      "followers_url": "https://api.github.com/users/pablohisao/followers",
      "following_url": "https://api.github.com/users/pablohisao/following{/other_user}",
      "gists_url": "https://api.github.com/users/pablohisao/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/pablohisao/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/pablohisao/subscriptions",
      "organizations_url": "https://api.github.com/users/pablohisao/orgs",
      "repos_url": "https://api.github.com/users/pablohisao/repos",
      "events_url": "https://api.github.com/users/pablohisao/events{/privacy}",
      "received_events_url": "https://api.github.com/users/pablohisao/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [],
    "milestone": null,
    "comments": 0,
    "created_at": "2021-09-18T12:39:59Z",
    "updated_at": "2021-09-18T12:39:59Z",
    "closed_at": null,
    "author_association": "NONE",
    "active_lock_reason": null,
    "body": "**Describe the bug**\r\n<!-- A clear and concise description of what the bug is. -->\r\n\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. ...\r\n2. ...\r\netc.",
    "performed_via_github_app": null
  },
  ...
]
3. 获取标签(Labels)数据

同样可以使用 Github 问题 API 获取问题的标签数据。需要向以下 URL 发送 GET 请求:

https://api.github.com/repos/{owner}/{repo}/labels

其中参数含义同上。例如,获取 Electron 项目的标签数据可以使用以下 Endpoint:

https://api.github.com/repos/electron/electron/labels

发送 curl 请求,获得 JSON 格式的标签数据:

curl https://api.github.com/repos/electron/electron/labels | jq .

结果如下:

[
  {
    "id": 351645103,
    "node_id": "MDU6TGFiZWwzNTE2NDUxMDM=",
    "url": "https://api.github.com/repos/electron/electron/labels/app-bar",
    "name": "app-bar",
    "description": "The 'app-bar' label is used for issues relating to the Electron `app` module. Specifically, it will often be used for issues with the Electron `Menu` class and other similar module classes (e.g. `MenuItem`, `BrowserWindow`, etc.). Adding this label to an issue may help the Electron community better understand the type of problem being presented.",
    "color": "cfcfcf",
    "default": false,
    "created_at": "2021-06-07T19:59:13Z",
    "updated_at": "2021-09-17T21:34:10Z"
  },
  ...
]
4. 获取里程碑(milestone)数据

Github 问题 API 还可以获取问题所在仓库的里程碑数据(milestone)。需要向以下 URL 发送 GET 请求:

https://api.github.com/repos/{owner}/{repo}/milestones

其中,参数含义同上。例如,获取 Electron 项目的里程碑数据,可以使用以下 Endpoint:

https://api.github.com/repos/electron/electron/milestones

发送 curl 请求,获得 JSON 格式的里程碑数据:

curl https://api.github.com/repos/electron/electron/milestones | jq .

结果如下:

[
  {
    "url": "https://api.github.com/repos/electron/electron/milestones/66",
    "html_url": "https://github.com/electron/electron/milestone/66",
    "labels_url": "https://api.github.com/repos/electron/electron/milestones/66/labels",
    "id": 4448044,
    "node_id": "MDk6TWlsZXN0b25lNDQ0ODA0NA==",
    "number": 66,
    "title": "15.x backmerge",
    "description": "",
    "creator": {
      "login": "codebytere",
      "id": 3996787,
      "node_id": "MDQ6VXNlcjM5OTY3ODc=",
      "avatar_url": "https://avatars.githubusercontent.com/u/3996787?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/codebytere",
      "html_url": "https://github.com/codebytere",
      "followers_url": "https://api.github.com/users/codebytere/followers",
      "following_url": "https://api.github.com/users/codebytere/following{/other_user}",
      "gists_url": "https://api.github.com/users/codebytere/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/codebytere/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/codebytere/subscriptions",
      "organizations_url": "https://api.github.com/users/codebytere/orgs",
      "repos_url": "https://api.github.com/users/codebytere/repos",
      "events_url": "https://api.github.com/users/codebytere/events{/privacy}",
      "received_events_url": "https://api.github.com/users/codebytere/received_events",
      "type": "User",
      "site_admin": false
    },
    "open_issues": 50,
    "closed_issues": 2124,
    "state": "open",
    "created_at": "2021-08-16T20:06:16Z",
    "updated_at": "2021-09-19T02:22:18Z",
    "due_on": null,
    "closed_at": null
  },
  ...
]
5. 获取评论数据

最后,Github 问题 API 还可以获取问题(Issue)的评论数据。需要向以下 URL 发送 GET 请求:

https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/comments

其中 {issue_number} 是问题的编号。例如,获取 Electron 项目 Issue #31328 的评论数据,可以使用以下 Endpoint:

https://api.github.com/repos/electron/electron/issues/31328/comments

发送 curl 请求,获得 JSON 格式的评论数据:

curl https://api.github.com/repos/electron/electron/issues/31328/comments | jq .

结果如下:

[
  {
    "url": "https://api.github.com/repos/electron/electron/issues/comments/944818512",
    "html_url": "https://github.com/electron/electron/issues/31328#issuecomment-944818512",
    "issue_url": "https://api.github.com/repos/electron/electron/issues/31328",
    "id": 944818512,
    "node_id": "MDEyOklzc3VlQ29tbWVudDk0NDgxODUxMg==",
    "user": {
      "login": "pablohisao",
      "id": 37221557,
      "node_id": "MDQ6VXNlcjM3MjIxNTU3",
      "avatar_url": "https://avatars.githubusercontent.com/u/37221557?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/pablohisao",
      "html_url": "https://github.com/pablohisao",
      "followers_url": "https://api.github.com/users/pablohisao/followers",
      "following_url": "https://api.github.com/users/pablohisao/following{/other_user}",
      "gists_url": "https://api.github.com/users/pablohisao/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/pablohisao/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/pablohisao/subscriptions",
      "organizations_url": "https://api.github.com/users/pablohisao/orgs",
      "repos_url": "https://api.github.com/users/pablohisao/repos",
      "events_url": "https://api.github.com/users/pablohisao/events{/privacy}",
      "received_events_url": "https://api.github.com/users/pablohisao/received_events",
      "type": "User",
      "site_admin": false
    },
    "created_at": "2021-09-18T12:44:17Z",
    "updated_at": "2021-09-18T12:44:17Z",
    "author_association": "NONE",
    "body": "@MarshallOfSound Thanks for your answer. I'll test and let you know."
  },
  ...
]
6. 总结

本文介绍了如何使用 Shell-Bash 编程语言来访问 Github 问题 API,获取问题、标签、里程碑和评论等数据。需要使用 curl 工具发送 GET 请求,并使用 jq 工具格式化 JSON 数据。使用 Github 问题 API 可以方便地获取 Github 项目的相关信息,帮助开发者更好地管理和维护自己的开源项目。