Skip to main content

Webhook

payload_handler_commits

@app.post("/webhook/commits", status_code=http.HTTPStatus.ACCEPTED)
async def payload_handler_commits(request: Request)

Triggered when a commit event is received from GitHub.

When a payload is received, the commit's message, repository, author, timestamp, action type, and URL are extracted to create a Commit object. A string representation of the object is sent to the bot using send_payload_message along with the branch the commit was pushed to.

Arguments:

  • request (Request): Request header of the payload.

Raises:

  • AttributeError: Raised if no branch is specified in the response.

Returns:

None

payload_handler_issues

@app.post("/webhook/issues", status_code=http.HTTPStatus.ACCEPTED)
async def payload_handler_issues(request: Request)

Triggered when an issue event is received from GitHub.

When a payload is received, the issue's body, action type, repository URL, associated user, and time of creation are extracted from the response object and used to create an Issue object. A string representation of the object is sent to the bot using send_payload_message.

Arguments:

  • request (Request): Request header of the payload.

Raises:

  • AttributeError: Raised if no branch is specified in the response.

Returns:

None

payload_handler_pr

@app.post("/webhook/pull-request", status_code=http.HTTPStatus.ACCEPTED)
async def payload_handler_pr(request: Request)

Triggered when a pull-request event is received from GitHub.

When a payload is received, the pull request's reviewer, status, review body, reviewer requested flag, action, repository, associated user, URL, and action type are extracted from the response object and used to create a PullRequest object. A string representation of the object is sent to the bot using send_payload_message.

Arguments:

  • request (Request): Request header of the payload.

Raises:

  • AttributeError: Raised if no branch is specified in the response.

Returns:

None

startup_event

@app.on_event("startup")
async def startup_event()

Run the Discord bot as an asyncio task before starting the FastAPI server.

This is needed to prevent the bot from blocking the server from executing any further code.

Returns:

None

DiscordCollabyBot

class DiscordCollabyBot(Bot)

Discord implementation of CollabyBot.

Extends the Bot class from discord.ext.commands. on_message() and on_ready() are overridden methods from the discord module for handling events in Discord.

Methods

on_message(message): Triggered when a message is received.

on_ready(): Triggered when the bot is operational.

handle_responses(message): str Creates a response to a message received in Discord.

get_commands(): Send a message listing all of CollabyBot's Discord slash commands.

send_payload_message(payload, event): Direct a payload to the correct Discord channels.

pull_requests(): Subscribe a channel to pull request notifications.

issues(): Subscribe a channel to pull issue notifications.

commits(): Subscribe a channel to commit notifications.

ping(): Respond with 'pong'.

open_pull_requests(repo): Respond with a list of a repository's open pull requests.

add(repo): Add a repository to CollabyBot.

jira_setup_token(): Setup a Jira token to monitor issues in a Jira workspace.

jira_get_issue(): Respond with information about a Jira issue.

jira_assign_issue(): Assign a Jira ticket to a user.

jira_unassign_issue(): Unassign a Jira ticket to a user.

add_all_commands(bot): Register all slash commands with the Discord bot.

on_ready

async def on_ready()

Triggered when the bot becomes operational.

This method overrides discord.on_ready(), which is called when the bot is finished preparing data received from Discord.

Returns:

None

on_message

async def on_message(message)

Triggered when a message is received.

This method overrides discord.on_message(), which is called every time a message is sent in the server where the bot is running. It will parse the contents of the message, send a response, and look for bot commands in the message.

Arguments:

  • message: Message received in Discord.

Returns:

None

send_payload_message

async def send_payload_message(payload, event, repo, branch='main')

This method will send payloads to Discord channels subscribed to

the corresponding event type.

Arguments:

  • payload: The payload formatted as a notification string.
  • event: The event type of the payload.

Returns:

None

on_mention

async def on_mention(message)

Triggered when the bot is mentioned.

Arguments:

  • message:

Returns:

None

get_commands

@commands.command(name='commands', description='List all supported commands.')
async def get_commands(ctx)

Send a message listing all of CollabyBot's Discord slash commands.

This method implements the /commands Discord command.

Returns:

None

ping

@commands.command(name='ping', description='Respond with pong')
async def ping(ctx)

Send 'pong' in response to 'ping'.

This method implements the /ping Discord command.

Returns:

None

pull_requests

@commands.command(
name='pull-requests',
description='Subscribe to pull request notifications in this channel.')
async def pull_requests(ctx, repo='')

Subscribe a channel to pull request notifications.

A repository must be specified by providing an argument to the command. If no argument was given, responds with a list of available repositories.

Arguments:

  • repo (str): Name of repository to subscribe to.

Returns:

None

issues

@commands.command(
name='issues',
description="Subscribe to issue notifications in this channel.")
async def issues(ctx, repo='')

Subscribe a channel to issue notifications.

A repository must be specified by providing an argument to the command. If no argument was given, responds with a list of available repositories.

Arguments:

  • repo (str): Name of repository to subscribe to.

Returns:

None

add

@commands.command(
name='add',
description=
'Add a repo to the list of repositories you want notifications from.')
async def add(ctx, repo='')

Add a repository to CollabyBot's list of repositories.

When a repo is added, its branches are retrieved using the GitHub API via PyGithub's Github class. Subscriber lists for each event type are initialized to empty lists (or a dict of empty lists with branches as keys for commits).

Arguments:

  • repo (str): The full name of the repository to add.

Returns:

None

get_repos

@commands.command(name='get-repos',
description='See the list of repos added to CollabyBot.')
async def get_repos(ctx)

Get a list of repositories added to CollabyBot.

Returns:

None

commits

@commands.command(
name='commits',
description='Subscribe to commit notifications in this channel.')
async def commits(ctx, repo='', branch='')

Subscribe a channel to commit notifications.

A repository must be specified by providing an argument to the command. If no argument was given, responds with a list of available repositories. If an optional branch argument was given, subscribes to events from that branch only.

Arguments:

  • repo (str): Name of repository to subscribe to.
  • branch (str): Name of branch to subscribe to.

Returns:

None

open_pull_requests

@commands.command(name='open-pull-requests',
description='Show open pull requests in testing repo.')
async def open_pull_requests(ctx, repo='')

Get a list of a repository's open pull requests.

Connect to a public repository using a Github object, then get all of the repo's open PR's and format them as a list with links and send it as a Discord embed.

Arguments:

  • repo: The repository to get PRs from.

Returns:

None

jira_setup_token

@commands.command(name='jira-setup-token',
description='Set up Jira Token to monitor Jira Issues.')
async def jira_setup_token(ctx)

Setup a Jira token to monitor issues in a Jira workspace.

Requires an email address, workspace URL, and Jira authentication token. If any of them are not passed as arguments to the command, responds with usage instructions. If they are all present, a dict of Jira info is created and added to the list of Jira subscribers. If the user ID is already in the list of subscribers, simply update the info.

Returns:

None

jira_get_issue

@commands.command(
name='jira',
description=
'Retrieves summary, description, issuetype, assignee, and parent of Jira issue.'
)
async def jira_get_issue(ctx)

Respond with information about a Jira issue.

Requires an issue ID to be passed as an argument. If no argument is present, responds with usage instruction. If the sending user doesn't have a token added to the list of Jira subscribers, instructs the user to add one.

Returns:

None

jira_assign_issue

@commands.command(name='jira-assign',
description='Assign a person to Jira Issue')
async def jira_assign_issue(ctx)

Assign a Jira ticket to a user.

If the ticket already has an assignee, ask the user if they want to reassign it. If they respond yes, reassign it; if they respond no, don't reassign it.

Returns:

None

jira_unassign_issue

@commands.command(name='jira-unassign',
description='Unassigns a person from Jira Issue')
async def jira_unassign_issue(ctx)

Unassign a Jira ticket to a user.

Requires a ticket ID and an assignable user to be passed to the /jira-assign command as arguments.

Returns:

None

jira_get_sprint

@commands.command(name='sprint', description='Returns sprint summary.')
async def jira_get_sprint(ctx)

Get information about the current sprint in a Jira project.

Sends a list of issues in the sprint as well as a burndown chart of the sprint's progress. Command requires a project ID as an argument. If one isn't provided, present the user with a list of projects in the authenticated workspace.

Returns:

None

add_all_commands

@classmethod
def add_all_commands(cls, bot)

Register all commands with the bot.

This method is only called once after initializing the DiscordCollabyBot object from the main script in order to register all command methods with the bot.

Arguments:

  • bot: The DiscordCollabyBot instance.

Returns:

None