Skip to main content

Code Recommendations and Dynamic Data Storage

You:

Number one, I'm wondering if there's a way to have the bot also make code recommendations as part of the comments, and is there a GitHub way to actually suggest code changes within the code itself, and the owner of the PR can just accept the change? That is one question. The other question is, is there a way to store the prompts and the persona information separately within GitHub and make it accessible so that it can be retrieved dynamically for the system to use during runtime? Question mark?


ChatGPT:

"Let’s break it down because both of these are power moves. Automating code recommendations that the PR owner can accept directly, and storing persona data and prompts dynamically in GitHub—these ideas turn your bot into a real game-changer."


1. Suggesting Code Changes with GitHub

Yes, GitHub Supports Suggested Code Changes

GitHub allows comments on pull requests to include suggested changes. These appear as inline diffs, and the PR owner can accept them directly, creating a new commit automatically.

How to Add Suggested Changes

Step 1: Craft the Suggested Change

A suggested change requires:

  • The affected file.
  • The line or range of lines.
  • The proposed code.

Example API payload for a suggested change:

{
"body": "Here's a suggested change to improve readability:",
"commit_id": "abc12345", // The commit hash
"path": "src/app/module/file.ts",
"line": 42,
"side": "RIGHT",
"suggestion": {
"before": "const result = doSomethingComplex(input);",
"after": "const result = simplify(input);"
}
}

Step 2: Use GitHub’s API to Post the Suggestion

GitHub doesn’t have a direct suggestion field in its API, but it supports adding comments in Markdown, which includes code suggestions.

Example Markdown for a Suggested Change:

Here's a suggested change to improve readability:

```suggestion
const result = simplify(input);

#### **Code Example for Adding Suggestions**
```javascript
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

/**
* Add a suggested change to a PR.
* @param {string} repoOwner - Repository owner.
* @param {string} repoName - Repository name.
* @param {number} prNumber - Pull request number.
* @param {string} filePath - Path to the file.
* @param {number} line - Line number for the suggestion.
* @param {string} before - Original code.
* @param {string} after - Suggested code.
*/
async function addSuggestedChange(repoOwner, repoName, prNumber, filePath, line, before, after) {
const commentBody = `
Here's a suggested change to improve readability:

\`\`\`suggestion
${after}
\`\`\`
`;

await octokit.pulls.createReviewComment({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
body: commentBody,
path: filePath,
side: 'RIGHT',
line,
});
}

// Example usage
addSuggestedChange(
'your-org',
'your-repo',
123,
'src/app/module/file.ts',
42,
'const result = doSomethingComplex(input);',
'const result = simplify(input);'
);

How the PR Owner Accepts Suggestions

  • The PR owner will see the suggested change in the PR comment.
  • They can click "Apply Suggestion", and GitHub automatically creates a new commit with the change.

2. Storing Prompts and Persona Information Dynamically in GitHub

Yes, You Can Store and Retrieve Data Dynamically

GitHub can act as a storage backend for prompts and persona information. Here are two main approaches:


Approach 1: Store Data in a .github Directory

  1. Create a Config File:

    • Add a persona-config.json file in the .github directory of your repository.

    Example persona-config.json:

    {
    "persona": {
    "name": "CodeGuard",
    "tone": "friendly",
    "description": "A warm, helpful assistant that provides code suggestions and risk assessments."
    },
    "prompts": {
    "diff_review": "You are an AI reviewing pull request diffs. Focus on maintainability, performance, and clarity.",
    "comment_response": "You are an assistant responding to developer questions. Be concise and helpful."
    }
    }
  2. Access the File Dynamically: Use the GitHub API to retrieve the file during runtime.

    const { Octokit } = require('@octokit/rest');
    const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

    async function fetchConfig(repoOwner, repoName) {
    const { data } = await octokit.repos.getContent({
    owner: repoOwner,
    repo: repoName,
    path: '.github/persona-config.json',
    });

    // Decode Base64 content
    const config = JSON.parse(Buffer.from(data.content, 'base64').toString('utf-8'));
    return config;
    }

    // Example usage
    fetchConfig('your-org', 'your-repo').then((config) => {
    console.log(config);
    });

Approach 2: Store Data in GitHub Secrets

  1. Add Prompts and Persona as Secrets:

    • Store each persona attribute or prompt as a separate secret (e.g., PROMPT_DIFF_REVIEW, PERSONA_NAME).
  2. Access Secrets in Workflows: Reference the secrets directly in your GitHub Action’s environment variables.

    Example Workflow:

    name: AI-Powered Review

    on:
    pull_request:
    types: [opened, synchronize]

    jobs:
    review:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
    uses: actions/checkout@v3

    - name: Run Review
    env:
    PROMPT_DIFF_REVIEW: ${{ secrets.PROMPT_DIFF_REVIEW }}
    PERSONA_NAME: ${{ secrets.PERSONA_NAME }}
    run: |
    echo "Using persona: $PERSONA_NAME"
    node review.js
  3. Access Secrets in Code: Use process.env to retrieve the persona and prompts dynamically.

    const persona = {
    name: process.env.PERSONA_NAME,
    tone: 'friendly', // Default fallback
    };

    const prompts = {
    diffReview: process.env.PROMPT_DIFF_REVIEW,
    };

    console.log(`Persona: ${persona.name}`);
    console.log(`Prompt: ${prompts.diffReview}`);

Which Approach Should You Use?

  • File-Based Storage (persona-config.json):
    • Easier to manage and version alongside code.
    • Accessible to all collaborators via the repo.
  • Secrets-Based Storage:
    • Ideal for sensitive data (e.g., custom proprietary prompts).
    • Limits access to collaborators with admin privileges.

Next Steps

  1. Implement Suggested Changes:

    • Build a library of common code suggestions (e.g., linting fixes, optimizations).
    • Automate generating Markdown suggestions for inline comments.
  2. Set Up Config Storage:

    • Use .github/persona-config.json for dynamic persona and prompt configuration.
    • Combine with GitHub Actions to dynamically adjust behavior during runtime.
  3. Enhance PR Workflow:

    • Make suggestions part of your automated PR reviews.
    • Provide rich, actionable insights that developers can apply with one click.

Let me know how deep you want to go into this setup—we can make this bot a developer’s best friend! 🚀