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

[FEATURE] Handle missing labels from Dependabot PRs and automatically create them #310

Open
guibranco opened this issue Jun 7, 2024 · 1 comment · May be fixed by #671
Open

[FEATURE] Handle missing labels from Dependabot PRs and automatically create them #310

guibranco opened this issue Jun 7, 2024 · 1 comment · May be fixed by #671
Labels
comments Related to commands that runs on comments enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🏷️ labels Labels related pull request Related to commands that runs on pull requests question Further information is requested 🛠 WIP Work in progress

Comments

@guibranco
Copy link
Owner

guibranco commented Jun 7, 2024

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

  • The PHP script will process Dependabot comments, create any missing labels, and assign them to the relevant issues or pull requests.

Tech notes

image

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

Additional information

Example comment

@guibranco guibranco added enhancement New feature or request question Further information is requested comments Related to commands that runs on comments pull request Related to commands that runs on pull requests labels Jun 7, 2024
@guibranco guibranco added help wanted Extra attention is needed good first issue Good for newcomers labels Jul 16, 2024
@guibranco guibranco added the 🏷️ labels Labels related label Sep 11, 2024
@guibranco guibranco changed the title [FEATURE] Create label based on dependabot comment in PR [FEATURE] Handle missing labels from Dependabot PRs and automatically create them Sep 13, 2024
@guibranco guibranco added the hacktoberfest Participation in the Hacktoberfest event label Oct 9, 2024
@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Oct 31, 2024
Copy link
Contributor

gitauto-ai bot commented Oct 31, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comments Related to commands that runs on comments enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🏷️ labels Labels related pull request Related to commands that runs on pull requests question Further information is requested 🛠 WIP Work in progress
Projects
Status: No status
1 participant