Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adjust settings to use guild configs #87

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

JacobCoffee
Copy link
Owner

@JacobCoffee JacobCoffee commented Oct 11, 2024

Description

  • more work toward per-guild settings

#74 but rebased against big refactor

Summary by Sourcery

Implement per-guild configuration settings, including GitHub and StackOverflow tags, and introduce a new command for scheduling Office Hours events. Add a health check endpoint to monitor system status and enhance the bot's configuration capabilities with a new command for guild admins. Improve logging and refactor guilds controller for better functionality.

New Features:

  • Introduce per-guild settings for managing configurations specific to each guild, including GitHub, StackOverflow tags, allowed users, and forum configurations.
  • Add a new command to schedule Office Hours events for upcoming Fridays, with options to delay the scheduling.
  • Implement a health check endpoint to assess the status of backend components, including the database and bot status.
  • Add a configuration command for guild admins to configure Byte and its features through a Discord UI.

Enhancements:

  • Refactor the guilds controller to support additional operations such as updating guild settings and retrieving specific configurations like GitHub and StackOverflow tags.
  • Improve the logging setup to include additional libraries such as httpcore and httpx.
  • Enhance the bot's welcome message to exclude bot accounts and provide more detailed logging when joining a guild.

Build:

  • Add new Makefile targets for managing the Byte database container, including starting, stopping, and refreshing the container.

Documentation:

  • Update documentation to reflect the new guild configuration options and the health check endpoint.

Tests:

  • Add tests for the new guild configuration features and the health check endpoint to ensure they function as expected.

JacobCoffee and others added 30 commits October 10, 2024 19:56
chore(ui): increase byte screen logo size
* feat: add guild configuration command

* feat: add litestar office hours command

* fix: centralize options for slashcmd and dropdown

* ci: apply pre-commit

* chore: dependecy updates

* chore: clean up utils

* fix: move moved import

* fix: attempt to fix broken modal submission

* feat:  finalize working config selection and sub selections

* ci: for some reason pre-commit is stupid and changed every fucking file
Copy link
Contributor

sourcery-ai bot commented Oct 11, 2024

Reviewer's Guide by Sourcery

This pull request implements several changes to adjust settings to use guild configurations. The changes include modifications to database models, API endpoints, and Discord bot functionality to support per-guild settings. The PR also introduces new configuration options and improves the overall structure of the project.

Sequence diagram for updating guild settings

sequenceDiagram
    actor Admin
    participant GuildsController
    participant GuildsService
    Admin->>GuildsController: PATCH /guilds/{guild_id}
    GuildsController->>GuildsService: get(guild_id)
    GuildsService-->>GuildsController: Guild
    GuildsController->>GuildsService: update(guild, setting, value)
    GuildsService-->>GuildsController: Updated Guild
    GuildsController-->>Admin: Updated Guild Schema
Loading

Architecture diagram for Guild Configuration Services

graph TD;
    subgraph GuildsService
        A[GuildsService]
        B[GitHubConfigService]
        C[SOTagsConfigService]
        D[AllowedUsersConfigService]
        E[ForumConfigService]
    end
    A -->|Provides| B
    A -->|Provides| C
    A -->|Provides| D
    A -->|Provides| E
    subgraph Database
        F[Guild]
        G[GitHubConfig]
        H[SOTagsConfig]
        I[AllowedUsersConfig]
        J[ForumConfig]
    end
    B -->|Interacts with| G
    C -->|Interacts with| H
    D -->|Interacts with| I
    E -->|Interacts with| J
    A -->|Interacts with| F
Loading

Class diagram for Guild Configuration

classDiagram
    class Guild {
        +UUID guild_id
        +String guild_name
        +String prefix
        +int help_channel_id
        +int showcase_channel_id
        +String sync_label
        +bool issue_linking
        +bool comment_linking
        +bool pep_linking
        +GitHubConfig github_config
        +SOTagsConfig[] sotags_configs
        +AllowedUsersConfig[] allowed_users
        +ForumConfig forum_config
    }
    class GitHubConfig {
        +int guild_id
        +bool discussion_sync
        +String github_organization
        +String github_repository
    }
    class SOTagsConfig {
        +int guild_id
        +String tag_name
    }
    class AllowedUsersConfig {
        +int guild_id
        +UUID user_id
    }
    class ForumConfig {
        +int guild_id
        +bool help_forum
        +String help_forum_category
        +int help_channel_id
        +bool help_thread_auto_close
        +int help_thread_auto_close_days
        +bool help_thread_notify
        +String help_thread_notify_roles
        +int help_thread_notify_days
        +bool help_thread_sync
        +bool showcase_forum
        +String showcase_forum_category
        +int showcase_channel_id
        +bool showcase_thread_auto_close
        +int showcase_thread_auto_close_days
    }
    Guild --> GitHubConfig
    Guild --> SOTagsConfig
    Guild --> AllowedUsersConfig
    Guild --> ForumConfig
Loading

File-Level Changes

Change Details Files
Refactored database models to support per-guild configurations
  • Added new models for ForumConfig, GitHubConfig, and SOTagsConfig
  • Updated Guild model to include new relationships
  • Modified existing models to use BigInteger for guild_id
  • Added new fields to Guild model (e.g., showcase_channel_id)
byte_bot/server/domain/db/models.py
byte_bot/server/lib/db/migrations/versions/003_forum_models.py
byte_bot/server/lib/db/migrations/versions/004_snowflake_fixes.py
Updated API endpoints and services to handle guild-specific configurations
  • Renamed GuildController to GuildsController
  • Added new endpoints for retrieving guild-specific configurations
  • Updated dependencies and services to support new models
  • Implemented update_guild method for modifying guild settings
byte_bot/server/domain/guilds/controllers.py
byte_bot/server/domain/guilds/dependencies.py
byte_bot/server/domain/guilds/services.py
byte_bot/server/domain/guilds/schemas.py
Implemented new Discord bot commands and views for configuration
  • Added ConfigView and related classes for guild configuration
  • Created new Config cog with configuration commands
  • Updated existing commands and views to use new guild-specific settings
byte_bot/byte/views/config.py
byte_bot/byte/plugins/config.py
byte_bot/byte/plugins/custom/litestar.py
byte_bot/byte/views/forums.py
Refactored and improved project structure
  • Moved GitHub-related functionality to a separate plugin
  • Created new utility functions and types
  • Updated import statements and reorganized modules
  • Improved error handling and logging
byte_bot/byte/plugins/github.py
byte_bot/byte/lib/utils.py
byte_bot/byte/lib/types/__init__.py
byte_bot/byte/lib/types/astral.py
byte_bot/byte/lib/types/python.py
byte_bot/byte/lib/common/__init__.py
byte_bot/byte/lib/common/mention.py
byte_bot/byte/lib/checks.py
Added system health check functionality
  • Implemented SystemController with health check endpoint
  • Created helper functions for checking database and bot status
  • Added SystemHealth DTO for health check responses
byte_bot/server/domain/system/controllers/system.py
byte_bot/server/domain/system/helpers.py
byte_bot/server/domain/system/dtos.py
Updated configuration and settings
  • Modified settings to include new configuration options
  • Updated environment variables and logging configuration
  • Added new constants and type aliases
byte_bot/byte/lib/settings.py
byte_bot/server/lib/settings.py
byte_bot/server/lib/types.py
byte_bot/server/lib/constants.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

railway-app bot commented Oct 11, 2024

🚅 Deployed to the byte-pr-87 environment in byte

Service Status Web Updated (UTC)
byte ◻️ Removed (View Logs) Web Dec 19, 2024 at 3:37 am

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @JacobCoffee - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Hardcoded internal ID found in settings. (link)

Overall Comments:

  • The changes look good overall, but consider breaking future large changes into smaller, more focused PRs for easier review and integration.
  • Some of the more complex parts of the new code, particularly in the UI views, could benefit from additional comments explaining the logic flow.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🔴 Security: 1 blocking issue
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -49,3 +113,75 @@ class GuildUpdate(CamelizedBaseModel):
issue_linking: bool | None = Field(title="Issue Linking", description="Is issue linking enabled.")
comment_linking: bool | None = Field(title="Comment Linking", description="Is comment linking enabled.")
pep_linking: bool | None = Field(title="PEP Linking", description="Is PEP linking enabled.")


class UpdateableGuildSetting(CamelizedBaseModel):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the UpdateableGuildSetting class into smaller, more focused classes.

The changes introduce more structured configuration options, which is good for maintainability. However, the UpdateableGuildSetting class has become overly complex with too many responsibilities. Consider splitting it into smaller, more focused classes:

class BaseGuildSetting(CamelizedBaseModel):
    """Base class for guild settings."""
    guild_id: UUID

class GuildModelSetting(BaseGuildSetting):
    """Settings for the guild model."""
    prefix: str
    help_channel_id: int
    showcase_channel_id: int
    sync_label: str
    issue_linking: bool
    comment_linking: bool
    pep_linking: bool

class GitHubConfigSetting(BaseGuildSetting):
    """Settings for GitHub configuration."""
    discussion_sync: bool
    github_organization: str
    github_repository: str

# Similar classes for StackOverflow, AllowedUsers, and Forum configs

class UpdateableGuildSetting(CamelizedBaseModel):
    """Allowed settings that admins can update for their guild."""
    guild_model: GuildModelSetting
    github_config: GitHubConfigSetting
    # Other config classes...

This approach maintains the structured configs while reducing complexity. It also eliminates the need for the dynamic enum creation method, which adds unnecessary abstraction. If you need an enum of all possible settings, consider creating it explicitly:

class GuildSettingField(StrEnum):
    PREFIX = "prefix"
    HELP_CHANNEL_ID = "help_channel_id"
    # ... other fields ...

This change will make the code more maintainable and easier to understand while preserving the new functionality.

@@ -42,6 +42,8 @@ class DiscordSettings(BaseSettings):
"""Discord Guild ID for development."""
DEV_USER_ID: int
"""Discord User ID for development."""
DEV_GUILD_INTERNAL_ID: int = 1136100160510902272
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 issue (security): Hardcoded internal ID found in settings.

Consider using environment variables or a configuration file to manage sensitive IDs.

batch_op.add_column(sa.Column("showcase_thread_auto_close", sa.Boolean(), nullable=False))
batch_op.add_column(sa.Column("showcase_thread_auto_close_days", sa.Integer(), nullable=True))
batch_op.add_column(sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True))
batch_op.add_column(sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Extract duplicate code into function (extract-duplicate-method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant