๐Ÿงช Code Review with CellMage: Integrating with GitHub/GitLabยถ

CellMageโ€™s integration with GitHub and GitLab allows you to bring repositories and pull/merge requests directly into your notebooks for powerful AI-assisted code reviews and analysis. This tutorial will show you how to use these integrations effectively.

๐Ÿ› ๏ธ Setting Up the Integrationsยถ

GitHub Integration Setupยถ

  1. First, install CellMage with GitHub support:

pip install "cellmage[github]"
  1. Create a GitHub Personal Access Token:

    • Go to GitHub โ†’ Settings โ†’ Developer settings โ†’ Personal access tokens

    • Generate a new token with at least repo scope (for private repositories) or public_repo (for public repos)

    • Copy your token

  2. Configure your environment:

# In your terminal or .env file
export GITHUB_TOKEN=your_personal_access_token

GitLab Integration Setupยถ

  1. First, install CellMage with GitLab support:

pip install "cellmage[gitlab]"
  1. Create a GitLab Personal Access Token:

    • Go to GitLab โ†’ Preferences โ†’ Access Tokens

    • Create a token with api scope for full access or read_api for read-only access

    • Copy your token

  2. Configure your environment:

# In your terminal or .env file
export GITLAB_URL=https://gitlab.com  # Or your self-hosted GitLab URL
export GITLAB_PAT=your_gitlab_personal_access_token

๐Ÿš€ Basic Usageยถ

Letโ€™s start with basic repository analysis:

# Load the CellMage extension
%load_ext cellmage

Fetching a GitHub Repositoryยถ

# Fetch a GitHub repository and add it to chat history
%github username/repository

# Now ask the LLM about it
%%llm
Based on the GitHub repository I just provided, can you describe its architecture
and main components? What patterns does it use?

Fetching a GitLab Repositoryยถ

# Fetch a GitLab repository and add it to chat history
%gitlab namespace/project

# Now ask the LLM about it
%%llm
Can you analyze this GitLab project's structure and explain its key modules?

๐Ÿ” Code Review Workflowsยถ

GitHub Pull Request Reviewยถ

Pull requests contain valuable context about code changes. Letโ€™s review one:

# Fetch a specific pull request
%github username/repository --pr 123

# Ask for a code review
%%llm --persona code_reviewer
Please review this pull request, focusing on:
1. Potential bugs or edge cases
2. Performance considerations
3. Adherence to best practices
4. Security concerns
5. Test coverage

GitLab Merge Request Reviewยถ

Similarly, for GitLab merge requests:

# Fetch a specific merge request
%gitlab namespace/project --mr 456

# Ask for a code review
%%llm --persona code_reviewer
Please perform a comprehensive review of this merge request. Identify any issues,
suggest improvements, and highlight what's been done well.

๐ŸŽ›๏ธ Advanced Optionsยถ

Both GitHub and GitLab integrations provide options to customize the content fetched:

GitHub Advanced Optionsยถ

# Clean mode (focuses on code by excluding non-essential files)
%github username/repository --clean

# Add as system message instead of user message
%github username/repository --system

# Only display content without adding to history
%github username/repository --show

# Include full code content (may be very large)
%github username/repository --full-code

# Exclude specific directories, files, or extensions
%github username/repository --exclude-dir node_modules --exclude-ext .json

GitLab Advanced Optionsยถ

# Clean mode
%gitlab namespace/project --clean

# Add as system message
%gitlab namespace/project --system

# Only display content without adding to history
%gitlab namespace/project --show

๐Ÿ“‹ Real-World Code Review Scenariosยถ

Letโ€™s look at some real-world scenarios where CellMage + GitHub/GitLab integration shines:

1. Code Architecture Assessmentยถ

# Fetch repository
%github username/complex-project

%%llm --persona architecture_expert
Based on this repository:
1. Create a high-level architecture diagram (describe it in text)
2. Identify the main design patterns in use
3. Suggest improvements to the overall architecture
4. Highlight any potential scalability concerns

2. Security Auditยถ

# Fetch repository with focus on code
%github username/webapp --clean

%%llm --persona security_expert
Perform a security audit of this codebase:
1. Identify potential security vulnerabilities
2. Check for common OWASP issues like SQL injection, XSS, etc.
3. Analyze authentication and authorization mechanisms
4. Suggest security improvements

3. Performance Reviewยถ

# Fetch a performance-related pull request
%github username/performance-project --pr 42

%%llm --model gpt-4o
This PR aims to improve performance. Please:
1. Analyze the approach taken
2. Identify any potential performance bottlenecks that remain
3. Suggest alternative optimization strategies
4. Explain the trade-offs between readability and performance in this PR

4. Onboarding to New Codebaseยถ

# Fetch repository
%gitlab namespace/new-project

%%llm
I'm a new developer joining this project. Could you:
1. Explain how the codebase is structured
2. Identify the key entry points and core functionality
3. Outline the main dependencies and how they're used
4. Suggest where to start if I need to add a new feature

๐Ÿ’ก Best Practices for AI-Assisted Code Reviewยถ

When using CellMage with GitHub/GitLab for code reviews:

  1. Use the right persona: Different codebases benefit from different reviewer personas

  2. Be specific in your prompts: Ask about specific aspects you want reviewed

  3. Handle large repositories carefully: Use --clean and exclusion options for big repos

  4. Combine with snippets: Add project documentation as snippets for better context

  5. Break down complex reviews: Review one component or issue at a time

  6. Verify AI suggestions: Always validate recommendations before implementing

๐Ÿง  Creating a Custom Code Reviewer Personaยถ

To optimize your code review workflow, create a specialized code reviewer persona:

---
name: Technical Code Reviewer
model: gpt-4o
temperature: 0.1
description: A detail-oriented code reviewer focused on technical quality and best practices
---
You are an experienced senior software engineer conducting a detailed code review.

Focus on these aspects in code review:
1. Correctness: Identify logical errors, edge cases, and potential bugs
2. Performance: Highlight inefficient algorithms, unnecessary computations, and optimization opportunities
3. Maintainability: Assess code organization, naming conventions, and documentation
4. Security: Point out potential vulnerabilities or security anti-patterns
5. Testing: Evaluate test coverage and suggest additional test cases

When reviewing:
- Format your review as a structured list of findings
- Rate issues by severity (Critical/Major/Minor)
- Always explain WHY something is problematic, not just WHAT is wrong
- Where appropriate, suggest specific code improvements
- Acknowledge good practices and well-written code
- Consider language-specific idioms and best practices

๐Ÿš€ Next Stepsยถ

Now that youโ€™ve learned how to use CellMage for code review with GitHub and GitLab, explore: