API Documentation
github_bot_api
Event
dataclass
Represents a GitHub webhook event.
Source code in github_bot_api/event.py
signature
instance-attribute
The signature of the event. Will only be set if a Webhook-secret is configured on the
client side (e.g. in Webhook.secret
/ if the webhook_secret parameter is
passed to accept_event()
).
GithubApp
dataclass
Represents a GitHub application and all the required details.
Source code in github_bot_api/app.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
client_id
class-attribute
instance-attribute
The GitHub App's OAuth client ID. This is required for OAuth2 authorization URL generation. Can be omitted if the app does not use OAuth2.
client_secret
class-attribute
instance-attribute
The GitHub App's OAuth client secret. This is required for OAuth2 authorization URL generation. Can be omitted if the app does not use OAuth2. Note that this must be specified if #client_id is specified.
jwt
property
Returns the JWT for your GitHub application. The JWT is the token to use with GitHub application APIs.
jwt_supplier
property
Returns a new #JwtSupplier that is used for generating JWT tokens for your GitHub application.
private_key
class-attribute
instance-attribute
RSA private key to sign the JWT with.
redirect_uri
class-attribute
instance-attribute
The GitHub App's OAuth redirect URI. This is required for OAuth2 authorization URL generation. Can be omitted if the app does not use OAuth2. This field is optional, but required for the web authorization flow with
oauth2_web_application_flow_url().
user_agent
instance-attribute
User agent of the application. This will be respected in #get_user_agent().
v3_api_url
class-attribute
instance-attribute
GitHub API base URL. Defaults to the public GitHub API.
app_client
Returns a PyGithub client for your GitHub application.
Note that the client's token will expire after 10 minutes and you will have to create a new client or update the client's token with the value returned by #jwt. It is recommended that you create a new client for each atomic operation you perform.
This requires you to install PyGithub>=1.58
.
Source code in github_bot_api/app.py
get_user_agent
Create a user agent string for the PyGithub client, including the installation if specified.
Source code in github_bot_api/app.py
installation_client
installation_client(
installation_id: int,
settings: Union[
GithubClientSettings, Dict[str, Any], None
] = None,
) -> Github
Returns a PyGithub client for your GitHub application to act in the scope of the given installation_id.
Note that the client's token will expire after 10 minutes and you will have to create a new client or update the client's token with the value returned by #jwt. It is recommended that you create a new client for each atomic operation you perform.
This requires you to install PyGithub>=1.58
.
Source code in github_bot_api/app.py
installation_token
A short-hand to retrieve a new installation token for the given installation_id.
installation_token_supplier
Create an #InstallationTokenSupplier for your GitHub application to act within the scope of the given installation_id.
Source code in github_bot_api/app.py
oauth2_access_token
oauth2_access_token(
*,
code: Optional[str] = None,
device_code: Optional[str] = None
) -> Optional[OAuth2TokenInfo]
Makes a request to GitHub to exchange an OAuth2 code for an access token.
Important: You must provide the correct code
or device_code
parameter, but not both.
Documentation: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app#generating-a-user-access-token-when-a-user-installs-your-app
Returns None if the token is not yet available (in the case of the device flow). Otherwise, returns a
TokenInfo object. If an error occurs, an #AccessTokenError is raised.
Source code in github_bot_api/app.py
oauth2_device_flow
Makes a request to GitHub to request a device code for the OAuth2 device flow.
Prerequisites:
- "Enable Device Flow" must be checked in your GitHub app's settings.
- You must have provided a
client_id
when constructing the #GithubApp instance.
Documentation: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app#using-the-device-flow-to-generate-a-user-access-token
Source code in github_bot_api/app.py
oauth2_web_application_flow_url
Returns the URL for a user to begin the OAuth2 web authorization flow.
Preconditions:
- You must have provided a client_id
when constructing the #GithubApp instance.
Documentation: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app#using-the-web-application-flow-to-generate-a-user-access-token
Source code in github_bot_api/app.py
Webhook
dataclass
Represents a GitHub webhook that listens on an HTTP endpoint for events. Event handlers can be registered using the #@on() decorator or #register() method.
Source code in github_bot_api/webhook.py
dispatch
dispatch(event: Event) -> bool
Dispatch an event on the first handler that matches it.
Returns #True only if the event was handled by a handler.
Source code in github_bot_api/webhook.py
accept_event
accept_event(
headers: Mapping[str, str],
raw_body: bytes,
webhook_secret: Optional[str] = None,
) -> Event
Converts thee HTTP headers and the raw_body to an #Event object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers |
Mapping[str, str]
|
The HTTP headers. Must have |
required |
raw_body |
bytes
|
The raw request body for the event. This is converted into a JSON payload. |
required |
webhook_secret |
Optional[str]
|
If specified, the |
None
|
Source code in github_bot_api/event.py
github_bot_api.flask
Flask binding for handling GitHub webhook events.
Note that you need to install the flask
module separately.
Example
from github_bot_api import Event, Webhook
from github_bot_api.flask import create_flask_app
def on_any_event(event: Event) -> bool:
print(event)
return True
webhook = Webhook(secret=None)
webhook.listen('*', on_any_event)
import os; os.environ['FLASK_ENV'] = 'development'
flask_app = create_flask_app(__name__, webhook)
flask_app.run()
create_event_handler
create_event_handler(
webhook: Webhook,
) -> Callable[[], Tuple[Text, int, Dict[str, str]]]
Creates an event handler flask view that interprets the received HTTP request as a GitHub application event and dispatches it via #webhook.dispatch().
Source code in github_bot_api/flask.py
create_flask_app
create_flask_app(
name: str,
webhook: Webhook,
path: str = "/event-handler",
) -> Flask
Creates a new #flask.Flask application with a POST
event handler under the given path (defaulting
to /event-handler
). This is a useful shorthand to attach your #Webhook to an HTTP server.