guibranco / gstraccini-bot-service

🤖 :octocat: GStraccini-bot automates repository management, ensuring organization and health by handling pull requests, issues, comments, and commits.
https://gstraccini.bot
MIT License
2 stars 0 forks source link

[FEATURE] Add `create labels` command #537

Closed gstraccini[bot] closed 3 days ago

gstraccini[bot] commented 1 month ago

Description

It would be helpful to have a new command that automates the creation of labels in the current repository. This command should allow users to specify certain label styles and categories through optional parameters, or fall back on the repository’s configuration file (if available) to determine which labels to create.

Example Command

The command should be structured as follows:

@gstraccini create labels

Tech Notes

  1. Optional Parameters:

    • style: Allows the user to specify a style for the labels.
    • category: Specifies which category of labels to create.
  2. Configuration Strategy:

    • If no parameters are supplied, the command should check for the presence of a repository configuration file to determine which labels to create.
    • If no configuration file is found, the command should create labels using a set of predefined options.

Example PHP Code

class LabelManager
{
    private $repository;
    private $defaultLabels = [
        'bug' => ['color' => 'f00', 'description' => 'Bug fix'],
        'feature' => ['color' => '0f0', 'description' => 'New feature'],
        // Add more predefined labels here
    ];

    public function __construct($repository)
    {
        $this->repository = $repository;
    }

    public function createLabels($style = null, $category = null)
    {
        $labels = $this->getLabelsFromConfig($category) ?? $this->defaultLabels;

        if ($style) {
            $labels = $this->applyStyleToLabels($labels, $style);
        }

        foreach ($labels as $name => $data) {
            $this->createLabel($name, $data['color'], $data['description']);
        }
    }

    private function getLabelsFromConfig($category = null)
    {
        // Check if configuration file exists in the repository
        $configFile = $this->repository . '/label_config.json';

        if (file_exists($configFile)) {
            $config = json_decode(file_get_contents($configFile), true);

            if ($category && isset($config[$category])) {
                return $config[$category];
            }

            return $config;
        }

        return null;
    }

    private function applyStyleToLabels($labels, $style)
    {
        foreach ($labels as &$label) {
            // Apply the style (e.g., change colors or formatting based on the style)
            $label['color'] = $this->getStyledColor($label['color'], $style);
        }

        return $labels;
    }

    private function getStyledColor($color, $style)
    {
        // This function modifies color based on style (e.g., apply a specific color scheme)
        // Example logic: prepend style code to the color
        return $style . $color;
    }

    private function createLabel($name, $color, $description)
    {
        // Here you would use a GitHub API or Git command to create the label
        // Example placeholder logic:
        echo "Creating label: {$name} (Color: {$color}, Description: {$description})\n";
    }
}

// Example usage:
$repository = '/path/to/repository';
$labelManager = new LabelManager($repository);

// Command to create labels with optional parameters
$labelManager->createLabels('dark', 'bugfix');

Additional Notes

gitauto-ai[bot] commented 1 month ago

Click the checkbox below to generate a PR!

@gstraccini[bot], You have 5 requests left in this cycle which refreshes on 2024-09-21 10:07:38+00:00. If you have any questions or concerns, please contact us at info@gitauto.ai.