guibranco / gstraccini-bot

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

[FEATURE] Add command for updating repository action variable #549

Open guibranco opened 4 days ago

guibranco commented 4 days ago

Description:

We need to add a new command to the bot that allows it to update or create repository action variables using the GitHub REST API. The command will take two required parameters: the variable name and the variable value. The bot should use the GuiBranco\Pancake\Request class to call the GitHub API and either update the existing variable or create it if it doesn't already exist.

Functionality:

PHP Example Code:

<?php

use GuiBranco\Pancake\Request;

class GitHubBot {
    private $githubToken;
    private $repositoryOwner;
    private $repositoryName;

    public function __construct($githubToken, $repositoryOwner, $repositoryName) {
        $this->githubToken = $githubToken;
        $this->repositoryOwner = $repositoryOwner;
        $this->repositoryName = $repositoryName;
    }

    // Function to update or create repository variable
    public function updateRepositoryVariable($variableName, $variableValue) {
        $url = "https://api.github.com/repos/{$this->repositoryOwner}/{$this->repositoryName}/actions/variables";
        $headers = [
            'Authorization' => 'Bearer ' . $this->githubToken,
            'Accept' => 'application/vnd.github.v3+json',
            'User-Agent' => 'GitHub-Bot'
        ];

        // Check if the variable exists by fetching all variables
        $response = Request::get($url, [], $headers);
        $variables = json_decode($response->getBody(), true);
        $existingVariable = null;

        foreach ($variables['variables'] as $variable) {
            if ($variable['name'] === $variableName) {
                $existingVariable = $variable;
                break;
            }
        }

        // Prepare payload for updating or creating the variable
        $data = json_encode([
            'name' => $variableName,
            'value' => $variableValue
        ]);

        if ($existingVariable) {
            // Update the variable if it exists
            $updateUrl = "{$url}/{$variableName}";
            $response = Request::patch($updateUrl, $data, $headers);
            echo "Updated variable '{$variableName}' successfully.\n";
        } else {
            // Create the variable if it doesn't exist
            $response = Request::post($url, $data, $headers);
            echo "Created new variable '{$variableName}' successfully.\n";
        }

        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
            return true;
        } else {
            echo "Failed to update/create variable.\n";
            return false;
        }
    }
}

// Usage example
$bot = new GitHubBot('your-github-token', 'repo-owner', 'repo-name');
$bot->updateRepositoryVariable('MY__VAR', 'new-value');

Explanation:

  1. GitHubBot Class:

    • The GitHubBot class encapsulates the functionality to update or create GitHub repository action variables using the REST API.
    • It accepts the GitHub token, repository owner, and repository name in the constructor.
  2. updateRepositoryVariable Function:

    • This function:
      • Fetches all existing variables from the repository.
      • Checks if the variable with the given name exists.
      • If it exists, it updates the variable with the new value via a PATCH request.
      • If it doesn't exist, it creates a new variable via a POST request.
  3. GuiBranco\Pancake\Request:

    • The Request class from the GuiBranco\Pancake library is used to make HTTP requests to the GitHub API.
  4. GitHub API URLs:

    • The API endpoint for repository action variables is:
      • Fetching all variables: GET /repos/{owner}/{repo}/actions/variables
      • Updating a variable: PATCH /repos/{owner}/{repo}/actions/variables/{name}
      • Creating a variable: POST /repos/{owner}/{repo}/actions/variables

Acceptance Criteria:

gitauto-ai[bot] commented 4 days ago

Hello @guibranco, you have reached your request limit of 5, your cycle will refresh on 2024-09-21 10:07:38+00:00. Consider subscribing if you want more requests. If you have any questions or concerns, please contact us at info@gitauto.ai.