Ben Chuanlong Du's Blog

It is never too late to learn.

Manipulate Pull Requests Using GitHub Rest APIs

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [4]:
!pip install github_rest_api
Defaulting to user installation because normal site-packages is not writeable
Collecting github_rest_api
  Downloading github_rest_api-0.24.0-py3-none-any.whl (10 kB)
Requirement already satisfied: psutil>=5.9.4 in /usr/local/lib/python3.10/dist-packages (from github_rest_api) (5.9.5)
Requirement already satisfied: requests>=2.28.2 in /usr/local/lib/python3.10/dist-packages (from github_rest_api) (2.30.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28.2->github_rest_api) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28.2->github_rest_api) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28.2->github_rest_api) (2.0.2)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28.2->github_rest_api) (2023.5.7)
Installing collected packages: github_rest_api
Successfully installed github_rest_api-0.24.0
In [5]:
from github_rest_api import Repository
In [5]:
repo = Repository(token="", owner="legendu-net", repo="icon")
repo
Out[5]:
<github_rest_api.github.Repository at 0x7fbeef605660>
In [6]:
repo.list_pull_requests()
Out[6]:
[{'url': 'https://api.github.com/repos/legendu-net/icon/pulls/70',
  'id': 1298933949,
  'node_id': 'PR_kwDOHkKAZc5NbCi9',
  'html_url': 'https://github.com/legendu-net/icon/pull/70',
  'diff_url': 'https://github.com/legendu-net/icon/pull/70.diff',
  'patch_url': 'https://github.com/legendu-net/icon/pull/70.patch',
  'issue_url': 'https://api.github.com/repos/legendu-net/icon/issues/70',
  'number': 70,
  'state': 'open',
  'locked': False,
  'title': 'Merge dev into main',
  'user': {'login': 'dclong',
   'id': 824507,
   'node_id': 'MDQ6VXNlcjgyNDUwNw==',
   'avatar_url': 'https://avatars.githubusercontent.com/u/824507?v=4',
   'gravatar_id': '',
   'url': 'https://api.github.com/users/dclong',
   'html_url': 'https://github.com/dclong',
   'followers_url': 'https://api.github.com/users/dclong/followers',
   'following_url': 'https://api.github.com/users/dclong/following{/other_user}',
   'gists_url': 'https://api.github.com/users/dclong/gists{/gist_id}',
   'starred_url': 'https://api.github.com/users/dclong/starred{/owner}{/repo}',
   'subscriptions_url': 'https://api.github.com/users/dclong/subscriptions',
   'organizations_url': 'https://api.github.com/users/dclong/orgs',
   'repos_url': 'https://api.github.com/users/dclong/repos',
   'events_url': 'https://api.github.com/users/dclong/events{/privacy}',
   'received_events_url': 'https://api.github.com/users/dclong/received_events',
   'type': 'User',
   'site_admin': False},
  'body': 'Automated changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action',
  'created_at': '2023-04-01T23:54:25Z',
  'updated_at': '2023-04-01T23:54:27Z',
  'closed_at': None,
  'merged_at': None,
  'merge_commit_sha': '9df1331f63b4d76f2eff766b6417aece069be84e',
  'assignee': {'login': 'dclong',
   'id': 824507,
   'node_id': 'MDQ6VXNlcjgyNDUwNw==',
   'avatar_url': 'https://avatars.githubusercontent.com/u/824507?v=4',
   'gravatar_id': '',
   'url': 'https://api.github.com/users/dclong',
   'html_url': 'https://github.com/dclong',
   'followers_url': 'https://api.github.com/users/dclong/followers',
   'following_url': 'https://api.github.com/users/dclong/following{/other_user}',
   'gists_url': 'https://api.github.com/users/dclong/gists{/gist_id}',
   'starred_url': 'https://api.github.com/users/dclong/starred{/owner}{/repo}',
   'subscriptions_url': 'https://api.github.com/users/dclong/subscriptions',
   'organizations_url': 'https://api.github.com/users/dclong/orgs',
   'repos_url': 'https://api.github.com/users/dclong/repos',
   'events_url': 'https://api.github.com/users/dclong/events{/privacy}',
   'received_events_url': 'https://api.github.com/users/dclong/received_events',
   'type': 'User',
   'site_admin': False},
  'assignees': [{'login': 'dclong',
    'id': 824507,
    'node_id': 'MDQ6VXNlcjgyNDUwNw==',
    'avatar_url': 'https://avatars.githubusercontent.com/u/824507?v=4',
    'gravatar_id': '',
    'url': 'https://api.github.com/users/dclong',
    'html_url': 'https://github.com/dclong',
    'followers_url': 'https://api.github.com/users/dclong/followers',
    'following_url': 'https://api.github.com/users/dclong/following{/other_user}',
    'gists_url': 'https://api.github.com/users/dclong/gists{/gist_id}',
    'starred_url': 'https://api.github.com/users/dclong/starred{/owner}{/repo}',
    'subscriptions_url': 'https://api.github.com/users/dclong/subscriptions',
    'organizations_url': 'https://api.github.com/users/dclong/orgs',
    'repos_url': 'https://api.github.com/users/dclong/repos',
    'events_url': 'https://api.github.com/users/dclong/events{/privacy}',
    'received_events_url': 'https://api.github.com/users/dclong/received_events',
    'type': 'User',
    'site_admin': False}],
  'requested_reviewers': [],
  'requested_teams': [],
  'labels': [],
  'milestone': None,
  'draft': False,
  'commits_url': 'https://api.github.com/repos/legendu-net/icon/pulls/70/commits',
  'review_comments_url': 'https://api.github.com/repos/legendu-net/icon/pulls/70/comments',
  'review_comment_url': 'https://api.github.com/repos/legendu-net/icon/pulls/comments{/number}',
  'comments_url': 'https://api.github.com/repos/legendu-net/icon/issues/70/comments',
  'statuses_url': 'https://api.github.com/repos/legendu-net/icon/statuses/bc4f913ba57ee33bf6a855d88be92c49d69c9a57',
  'head': {'label': 'legendu-net:dev',
   'ref': 'dev',
   'sha': 'bc4f913ba57ee33bf6a855d88be92c49d69c9a57',
   'user': {'login': 'legendu-net',
    'id': 104755004,
    'node_id': 'O_kgDOBj5vPA',
    'avatar_url': 'https://avatars.githubusercontent.com/u/104755004?v=4',
    'gravatar_id': '',
    'url': 'https://api.github.com/users/legendu-net',
    'html_url': 'https://github.com/legendu-net',
    'followers_url': 'https://api.github.com/users/legendu-net/followers',
    'following_url': 'https://api.github.com/users/legendu-net/following{/other_user}',
    'gists_url': 'https://api.github.com/users/legendu-net/gists{/gist_id}',
    'starred_url': 'https://api.github.com/users/legendu-net/starred{/owner}{/repo}',
    'subscriptions_url': 'https://api.github.com/users/legendu-net/subscriptions',
    'organizations_url': 'https://api.github.com/users/legendu-net/orgs',
    'repos_url': 'https://api.github.com/users/legendu-net/repos',
    'events_url': 'https://api.github.com/users/legendu-net/events{/privacy}',
    'received_events_url': 'https://api.github.com/users/legendu-net/received_events',
    'type': 'Organization',
    'site_admin': False},
   'repo': {'id': 507674725,
    'node_id': 'R_kgDOHkKAZQ',
    'name': 'icon',
    'full_name': 'legendu-net/icon',
    'private': False,
    'owner': {'login': 'legendu-net',
     'id': 104755004,
     'node_id': 'O_kgDOBj5vPA',
     'avatar_url': 'https://avatars.githubusercontent.com/u/104755004?v=4',
     'gravatar_id': '',
     'url': 'https://api.github.com/users/legendu-net',
     'html_url': 'https://github.com/legendu-net',
     'followers_url': 'https://api.github.com/users/legendu-net/followers',
     'following_url': 'https://api.github.com/users/legendu-net/following{/other_user}',
     'gists_url': 'https://api.github.com/users/legendu-net/gists{/gist_id}',
     'starred_url': 'https://api.github.com/users/legendu-net/starred{/owner}{/repo}',
     'subscriptions_url': 'https://api.github.com/users/legendu-net/subscriptions',
     'organizations_url': 'https://api.github.com/users/legendu-net/orgs',
     'repos_url': 'https://api.github.com/users/legendu-net/repos',
     'events_url': 'https://api.github.com/users/legendu-net/events{/privacy}',
     'received_events_url': 'https://api.github.com/users/legendu-net/received_events',
     'type': 'Organization',
     'site_admin': False},
    'html_url': 'https://github.com/legendu-net/icon',
    'description': None,
    'fork': False,
    'url': 'https://api.github.com/repos/legendu-net/icon',
    'forks_url': 'https://api.github.com/repos/legendu-net/icon/forks',
    'keys_url': 'https://api.github.com/repos/legendu-net/icon/keys{/key_id}',
    'collaborators_url': 'https://api.github.com/repos/legendu-net/icon/collaborators{/collaborator}',
    'teams_url': 'https://api.github.com/repos/legendu-net/icon/teams',
    'hooks_url': 'https://api.github.com/repos/legendu-net/icon/hooks',
    'issue_events_url': 'https://api.github.com/repos/legendu-net/icon/issues/events{/number}',
    'events_url': 'https://api.github.com/repos/legendu-net/icon/events',
    'assignees_url': 'https://api.github.com/repos/legendu-net/icon/assignees{/user}',
    'branches_url': 'https://api.github.com/repos/legendu-net/icon/branches{/branch}',
    'tags_url': 'https://api.github.com/repos/legendu-net/icon/tags',
    'blobs_url': 'https://api.github.com/repos/legendu-net/icon/git/blobs{/sha}',
    'git_tags_url': 'https://api.github.com/repos/legendu-net/icon/git/tags{/sha}',
    'git_refs_url': 'https://api.github.com/repos/legendu-net/icon/git/refs{/sha}',
    'trees_url': 'https://api.github.com/repos/legendu-net/icon/git/trees{/sha}',
    'statuses_url': 'https://api.github.com/repos/legendu-net/icon/statuses/{sha}',
    'languages_url': 'https://api.github.com/repos/legendu-net/icon/languages',
    'stargazers_url': 'https://api.github.com/repos/legendu-net/icon/stargazers',
    'contributors_url': 'https://api.github.com/repos/legendu-net/icon/contributors',
    'subscribers_url': 'https://api.github.com/repos/legendu-net/icon/subscribers',
    'subscription_url': 'https://api.github.com/repos/legendu-net/icon/subscription',
    'commits_url': 'https://api.github.com/repos/legendu-net/icon/commits{/sha}',
    'git_commits_url': 'https://api.github.com/repos/legendu-net/icon/git/commits{/sha}',
    'comments_url': 'https://api.github.com/repos/legendu-net/icon/comments{/number}',
    'issue_comment_url': 'https://api.github.com/repos/legendu-net/icon/issues/comments{/number}',
    'contents_url': 'https://api.github.com/repos/legendu-net/icon/contents/{+path}',
    'compare_url': 'https://api.github.com/repos/legendu-net/icon/compare/{base}...{head}',
    'merges_url': 'https://api.github.com/repos/legendu-net/icon/merges',
    'archive_url': 'https://api.github.com/repos/legendu-net/icon/{archive_format}{/ref}',
    'downloads_url': 'https://api.github.com/repos/legendu-net/icon/downloads',
    'issues_url': 'https://api.github.com/repos/legendu-net/icon/issues{/number}',
    'pulls_url': 'https://api.github.com/repos/legendu-net/icon/pulls{/number}',
    'milestones_url': 'https://api.github.com/repos/legendu-net/icon/milestones{/number}',
    'notifications_url': 'https://api.github.com/repos/legendu-net/icon/notifications{?since,all,participating}',
    'labels_url': 'https://api.github.com/repos/legendu-net/icon/labels{/name}',
    'releases_url': 'https://api.github.com/repos/legendu-net/icon/releases{/id}',
    'deployments_url': 'https://api.github.com/repos/legendu-net/icon/deployments',
    'created_at': '2022-06-26T20:12:56Z',
    'updated_at': '2023-01-31T19:59:33Z',
    'pushed_at': '2023-04-01T23:54:26Z',
    'git_url': 'git://github.com/legendu-net/icon.git',
    'ssh_url': 'git@github.com:legendu-net/icon.git',
    'clone_url': 'https://github.com/legendu-net/icon.git',
    'svn_url': 'https://github.com/legendu-net/icon',
    'homepage': None,
    'size': 328,
    'stargazers_count': 0,
    'watchers_count': 0,
    'language': 'Go',
    'has_issues': True,
    'has_projects': True,
    'has_downloads': True,
    'has_wiki': True,
    'has_pages': False,
    'has_discussions': False,
    'forks_count': 0,
    'mirror_url': None,
    'archived': False,
    'disabled': False,
    'open_issues_count': 1,
    'license': {'key': 'apache-2.0',
     'name': 'Apache License 2.0',
     'spdx_id': 'Apache-2.0',
     'url': 'https://api.github.com/licenses/apache-2.0',
     'node_id': 'MDc6TGljZW5zZTI='},
    'allow_forking': True,
    'is_template': False,
    'web_commit_signoff_required': False,
    'topics': [],
    'visibility': 'public',
    'forks': 0,
    'open_issues': 1,
    'watchers': 0,
    'default_branch': 'dev'}},
  'base': {'label': 'legendu-net:main',
   'ref': 'main',
   'sha': 'b7fd126af582f4ab1fd8e4ca81b38d51c31e395a',
   'user': {'login': 'legendu-net',
    'id': 104755004,
    'node_id': 'O_kgDOBj5vPA',
    'avatar_url': 'https://avatars.githubusercontent.com/u/104755004?v=4',
    'gravatar_id': '',
    'url': 'https://api.github.com/users/legendu-net',
    'html_url': 'https://github.com/legendu-net',
    'followers_url': 'https://api.github.com/users/legendu-net/followers',
    'following_url': 'https://api.github.com/users/legendu-net/following{/other_user}',
    'gists_url': 'https://api.github.com/users/legendu-net/gists{/gist_id}',
    'starred_url': 'https://api.github.com/users/legendu-net/starred{/owner}{/repo}',
    'subscriptions_url': 'https://api.github.com/users/legendu-net/subscriptions',
    'organizations_url': 'https://api.github.com/users/legendu-net/orgs',
    'repos_url': 'https://api.github.com/users/legendu-net/repos',
    'events_url': 'https://api.github.com/users/legendu-net/events{/privacy}',
    'received_events_url': 'https://api.github.com/users/legendu-net/received_events',
    'type': 'Organization',
    'site_admin': False},
   'repo': {'id': 507674725,
    'node_id': 'R_kgDOHkKAZQ',
    'name': 'icon',
    'full_name': 'legendu-net/icon',
    'private': False,
    'owner': {'login': 'legendu-net',
     'id': 104755004,
     'node_id': 'O_kgDOBj5vPA',
     'avatar_url': 'https://avatars.githubusercontent.com/u/104755004?v=4',
     'gravatar_id': '',
     'url': 'https://api.github.com/users/legendu-net',
     'html_url': 'https://github.com/legendu-net',
     'followers_url': 'https://api.github.com/users/legendu-net/followers',
     'following_url': 'https://api.github.com/users/legendu-net/following{/other_user}',
     'gists_url': 'https://api.github.com/users/legendu-net/gists{/gist_id}',
     'starred_url': 'https://api.github.com/users/legendu-net/starred{/owner}{/repo}',
     'subscriptions_url': 'https://api.github.com/users/legendu-net/subscriptions',
     'organizations_url': 'https://api.github.com/users/legendu-net/orgs',
     'repos_url': 'https://api.github.com/users/legendu-net/repos',
     'events_url': 'https://api.github.com/users/legendu-net/events{/privacy}',
     'received_events_url': 'https://api.github.com/users/legendu-net/received_events',
     'type': 'Organization',
     'site_admin': False},
    'html_url': 'https://github.com/legendu-net/icon',
    'description': None,
    'fork': False,
    'url': 'https://api.github.com/repos/legendu-net/icon',
    'forks_url': 'https://api.github.com/repos/legendu-net/icon/forks',
    'keys_url': 'https://api.github.com/repos/legendu-net/icon/keys{/key_id}',
    'collaborators_url': 'https://api.github.com/repos/legendu-net/icon/collaborators{/collaborator}',
    'teams_url': 'https://api.github.com/repos/legendu-net/icon/teams',
    'hooks_url': 'https://api.github.com/repos/legendu-net/icon/hooks',
    'issue_events_url': 'https://api.github.com/repos/legendu-net/icon/issues/events{/number}',
    'events_url': 'https://api.github.com/repos/legendu-net/icon/events',
    'assignees_url': 'https://api.github.com/repos/legendu-net/icon/assignees{/user}',
    'branches_url': 'https://api.github.com/repos/legendu-net/icon/branches{/branch}',
    'tags_url': 'https://api.github.com/repos/legendu-net/icon/tags',
    'blobs_url': 'https://api.github.com/repos/legendu-net/icon/git/blobs{/sha}',
    'git_tags_url': 'https://api.github.com/repos/legendu-net/icon/git/tags{/sha}',
    'git_refs_url': 'https://api.github.com/repos/legendu-net/icon/git/refs{/sha}',
    'trees_url': 'https://api.github.com/repos/legendu-net/icon/git/trees{/sha}',
    'statuses_url': 'https://api.github.com/repos/legendu-net/icon/statuses/{sha}',
    'languages_url': 'https://api.github.com/repos/legendu-net/icon/languages',
    'stargazers_url': 'https://api.github.com/repos/legendu-net/icon/stargazers',
    'contributors_url': 'https://api.github.com/repos/legendu-net/icon/contributors',
    'subscribers_url': 'https://api.github.com/repos/legendu-net/icon/subscribers',
    'subscription_url': 'https://api.github.com/repos/legendu-net/icon/subscription',
    'commits_url': 'https://api.github.com/repos/legendu-net/icon/commits{/sha}',
    'git_commits_url': 'https://api.github.com/repos/legendu-net/icon/git/commits{/sha}',
    'comments_url': 'https://api.github.com/repos/legendu-net/icon/comments{/number}',
    'issue_comment_url': 'https://api.github.com/repos/legendu-net/icon/issues/comments{/number}',
    'contents_url': 'https://api.github.com/repos/legendu-net/icon/contents/{+path}',
    'compare_url': 'https://api.github.com/repos/legendu-net/icon/compare/{base}...{head}',
    'merges_url': 'https://api.github.com/repos/legendu-net/icon/merges',
    'archive_url': 'https://api.github.com/repos/legendu-net/icon/{archive_format}{/ref}',
    'downloads_url': 'https://api.github.com/repos/legendu-net/icon/downloads',
    'issues_url': 'https://api.github.com/repos/legendu-net/icon/issues{/number}',
    'pulls_url': 'https://api.github.com/repos/legendu-net/icon/pulls{/number}',
    'milestones_url': 'https://api.github.com/repos/legendu-net/icon/milestones{/number}',
    'notifications_url': 'https://api.github.com/repos/legendu-net/icon/notifications{?since,all,participating}',
    'labels_url': 'https://api.github.com/repos/legendu-net/icon/labels{/name}',
    'releases_url': 'https://api.github.com/repos/legendu-net/icon/releases{/id}',
    'deployments_url': 'https://api.github.com/repos/legendu-net/icon/deployments',
    'created_at': '2022-06-26T20:12:56Z',
    'updated_at': '2023-01-31T19:59:33Z',
    'pushed_at': '2023-04-01T23:54:26Z',
    'git_url': 'git://github.com/legendu-net/icon.git',
    'ssh_url': 'git@github.com:legendu-net/icon.git',
    'clone_url': 'https://github.com/legendu-net/icon.git',
    'svn_url': 'https://github.com/legendu-net/icon',
    'homepage': None,
    'size': 328,
    'stargazers_count': 0,
    'watchers_count': 0,
    'language': 'Go',
    'has_issues': True,
    'has_projects': True,
    'has_downloads': True,
    'has_wiki': True,
    'has_pages': False,
    'has_discussions': False,
    'forks_count': 0,
    'mirror_url': None,
    'archived': False,
    'disabled': False,
    'open_issues_count': 1,
    'license': {'key': 'apache-2.0',
     'name': 'Apache License 2.0',
     'spdx_id': 'Apache-2.0',
     'url': 'https://api.github.com/licenses/apache-2.0',
     'node_id': 'MDc6TGljZW5zZTI='},
    'allow_forking': True,
    'is_template': False,
    'web_commit_signoff_required': False,
    'topics': [],
    'visibility': 'public',
    'forks': 0,
    'open_issues': 1,
    'watchers': 0,
    'default_branch': 'dev'}},
  '_links': {'self': {'href': 'https://api.github.com/repos/legendu-net/icon/pulls/70'},
   'html': {'href': 'https://github.com/legendu-net/icon/pull/70'},
   'issue': {'href': 'https://api.github.com/repos/legendu-net/icon/issues/70'},
   'comments': {'href': 'https://api.github.com/repos/legendu-net/icon/issues/70/comments'},
   'review_comments': {'href': 'https://api.github.com/repos/legendu-net/icon/pulls/70/comments'},
   'review_comment': {'href': 'https://api.github.com/repos/legendu-net/icon/pulls/comments{/number}'},
   'commits': {'href': 'https://api.github.com/repos/legendu-net/icon/pulls/70/commits'},
   'statuses': {'href': 'https://api.github.com/repos/legendu-net/icon/statuses/bc4f913ba57ee33bf6a855d88be92c49d69c9a57'}},
  'author_association': 'CONTRIBUTOR',
  'auto_merge': None,
  'active_lock_reason': None}]
In [9]:
files = repo.list_pull_request_files(70)
files
Out[9]:
[{'sha': 'ffd73f21bb3e23e6a3431e2891df05330da513f6',
  'filename': 'utils/data/vscode/settings.json',
  'status': 'modified',
  'additions': 1,
  'deletions': 0,
  'changes': 1,
  'blob_url': 'https://github.com/legendu-net/icon/blob/bc4f913ba57ee33bf6a855d88be92c49d69c9a57/utils%2Fdata%2Fvscode%2Fsettings.json',
  'raw_url': 'https://github.com/legendu-net/icon/raw/bc4f913ba57ee33bf6a855d88be92c49d69c9a57/utils%2Fdata%2Fvscode%2Fsettings.json',
  'contents_url': 'https://api.github.com/repos/legendu-net/icon/contents/utils%2Fdata%2Fvscode%2Fsettings.json?ref=bc4f913ba57ee33bf6a855d88be92c49d69c9a57',
  'patch': '@@ -15,6 +15,7 @@\n     ],\n     "extensions.autoUpdate": false,\n     "python.linting.mypyEnabled": false,\n+    "python.linting.pylintEnabled": true,\n     "python.linting.pylintArgs": [\n         "--extension-pkg-whitelist=numpy,cv2,pyspark",\n         "--generated-members=cv2.*,pyspark.*",'}]
In [10]:
len(files)
Out[10]:
1

Add Comment to a PR

In [ ]:
repo.create_issue_comment(1596, "testing adding comment")

Comments