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] Handle missing labels from Dependabot PRs and automatically create them #310

Open guibranco opened 5 months ago

guibranco commented 5 months ago

Description

When Dependabot creates automatic pull requests, it sometimes assigns labels that do not exist within the repository. As a result, Dependabot creates a comment indicating that the label is invalid. The goal is to process this comment, create the missing label(s) in the repository, and then assign them to the issue.

Tech Notes

  1. Webhook Handling:

    • Parse the webhook payload from GitHub to detect comments created by Dependabot.
    • Check if the comment contains invalid labels that do not exist in the repository.
  2. Label Creation and Assignment:

    • Automatically create the missing labels.
    • Assign the newly created labels to the pull request or issue.
  3. PHP Example Implementation:

    Below is a PHP example using the Request class from the GuiBranco/Pancake library to handle this feature:

    use GuiBranco\Pancake\Request;
    
    class LabelHandler
    {
       private $token;
       private $repoOwner;
       private $repoName;
       private $apiUrl = 'https://api.github.com';
    
       public function __construct($token, $repoOwner, $repoName)
       {
           $this->token = $token;
           $this->repoOwner = $repoOwner;
           $this->repoName = $repoName;
       }
    
       public function handleWebhook($payload)
       {
           $commentBody = $payload['comment']['body'];
           $issueNumber = $payload['issue']['number'];
    
           // Extract missing labels from the comment
           preg_match_all('/The following labels could not be found: `([^`]+)`/', $commentBody, $matches);
           $missingLabels = explode('`, `', $matches[1][0]);
    
           foreach ($missingLabels as $label) {
               $this->createLabel($label);
               $this->addLabelToIssue($issueNumber, $label);
           }
       }
    
       private function createLabel($label)
       {
           $url = "{$this->apiUrl}/repos/{$this->repoOwner}/{$this->repoName}/labels";
    
           $request = new Request();
           $response = $request->post($url, [
               'headers' => [
                   'Authorization' => "token {$this->token}",
                   'Accept'        => 'application/vnd.github.v3+json',
               ],
               'json' => [
                   'name'        => $label,
                   'color'       => 'f0f0f0', // Example color code
                   'description' => 'Created by automation for missing labels.',
               ],
           ]);
    
           return $response->getBody();
       }
    
       private function addLabelToIssue($issueNumber, $label)
       {
           $url = "{$this->apiUrl}/repos/{$this->repoOwner}/{$this->repoName}/issues/{$issueNumber}/labels";
    
           $request = new Request();
           $response = $request->post($url, [
               'headers' => [
                   'Authorization' => "token {$this->token}",
                   'Accept'        => 'application/vnd.github.v3+json',
               ],
               'json' => [$label],
           ]);
    
           return $response->getBody();
       }
    }
    
    // Usage example
    $labelHandler = new LabelHandler('your-github-token', 'your-repo-owner', 'your-repo-name');
    
    // Example payload from GitHub webhook
    $payload = [
       'comment' => ['body' => 'The following labels could not be found: `github-actions`, `dependencies`'],
       'issue'   => ['number' => 123]
    ];
    
    $labelHandler->handleWebhook($payload);
  4. Resources:

Expected Outcome

Tech notes

image

The following labels could not be found: `github-actions`, `dependencies`.

Additional information

Example comment

gitauto-ai[bot] commented 1 month ago

Hey, I'm a bit lost here! Not sure which file I should be fixing. Could you give me a bit more to go on? Maybe add some details to the issue or drop a comment with some extra hints? Thanks!

Have feedback or need help? Feel free to email info@gitauto.ai.