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

chore(automation): add workflow to automatically update CONTRIBUTORS.md #596

Closed
wants to merge 2 commits into from

Conversation

dylanpulver
Copy link
Collaborator

@dylanpulver dylanpulver commented Aug 30, 2024

Purpose: This PR introduces a new GitHub Actions workflow and accompanying Python script to automate the process of updating the CONTRIBUTORS.md file. This ensures that contributor tiers and badges are updated automatically based on the number of merged PRs.

Changes:

  • GitHub Actions Workflow: Adds a workflow file (.github/workflows/update_contributors.yml) that triggers on pull request merges. The workflow counts the contributor's merged PRs, updates the CONTRIBUTORS.md file, and pushes the changes to a dedicated branch (contributors-updates).
  • Python Script: Introduces a Python script (scripts/update_contributors.py) that handles the logic of determining the contributor's tier and badge based on their PR count. The script updates the CONTRIBUTORS.md file accordingly.

Impact:

  • Automation: This change automates the previously manual process of updating contributor information, reducing the likelihood of human error and ensuring consistency.
  • Branch Management: Updates are pushed to the contributors-updates branch, allowing for periodic review and merging into the main branch without cluttering the PR space.

Next Steps:

  • Review: Please review the workflow and script to ensure they align with our repository's standards.
  • Merge: Once validated, merge this PR to enable automated updates for CONTRIBUTORS.md.

Summary by CodeRabbit

  • New Features

    • Introduced an automated workflow for updating contributor tiers in the CONTRIBUTORS.md file upon merging pull requests.
    • Added a script to categorize contributors based on the number of pull requests, enhancing recognition with visual badges.
  • Bug Fixes

    • Improved accuracy and timeliness of contributor recognition through automation.

Copy link

coderabbitai bot commented Aug 30, 2024

Walkthrough

A new GitHub Actions workflow, update_contributors.yml, automates the updating of the CONTRIBUTORS.md file based on merged pull requests. It includes jobs for checking out the repository, setting up Node.js, and executing a Python script, update_contributors.py, which updates contributor tiers according to their PR counts. The changes are committed to a dedicated branch and pushed to the remote repository.

Changes

Files Change Summary
.github/workflows/update_contributors.yml, scripts/update_contributors.py Introduced a workflow to automate the updating of contributor tiers in CONTRIBUTORS.md based on merged PR counts.

Poem

🐰 In the meadow where bunnies play,
New tiers for contributors brighten the day!
With each PR, their badges gleam,
A hop, a skip, a coder's dream!
Thank you, friends, for all you do,
Your efforts shine, so bright and true! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 81b40b7 and 0ac3c34.

Files selected for processing (2)
  • .github/workflows/update_contributors.yml (1 hunks)
  • scripts/update_contributors.py (1 hunks)
Additional context used
actionlint
.github/workflows/update_contributors.yml

25-25: shellcheck reported issue in this script: SC2086:info:2:30: Double quote to prevent globbing and word splitting

(shellcheck)


30-30: shellcheck reported issue in this script: SC2086:info:1:59: Double quote to prevent globbing and word splitting

(shellcheck)

Additional comments not posted (3)
scripts/update_contributors.py (1)

1-3: Review import statements for necessity and security.

The script imports sys and os which are necessary for its functionality. However, always ensure that the use of such modules does not introduce any security vulnerabilities, especially when dealing with file paths and external inputs.

.github/workflows/update_contributors.yml (2)

3-10: Validate trigger and condition logic.

The workflow is correctly triggered on closed pull requests and includes a condition to only run if the pull request was merged. This is a good use of GitHub Actions features to ensure the workflow runs only when necessary.


15-19: Review Node.js setup necessity.

The workflow sets up Node.js but does not appear to use it in any subsequent steps, as the script being run is a Python script. Verify whether Node.js is actually needed, as this could reduce the runtime and complexity of the workflow.

Comment on lines 4 to 6
contributor = sys.argv[1]
pr_count = int(sys.argv[2])

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validate command-line arguments for robustness.

The script directly uses sys.argv for input without any form of validation. Consider adding checks to ensure that the script is not run with insufficient or malformed arguments, which could lead to runtime errors or unexpected behavior.

Suggest adding argument validation:

if len(sys.argv) < 3:
    print("Usage: python update_contributors.py <contributor> <pr_count>")
    sys.exit(1)

try:
    pr_count = int(sys.argv[2])
except ValueError:
    print("Error: pr_count must be an integer.")
    sys.exit(1)

Comment on lines 7 to 11
tiers = {
"Valued Contributor": 10,
"Frequent Contributor": 5,
"First Contributor": 1
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review the tier system for scalability and maintainability.

The tier system is hardcoded, which might limit flexibility if the tier thresholds or names need to be updated frequently. Consider externalizing these values to a configuration file or environment variables for easier management without modifying the code.

Comment on lines 13 to 36
contributors_file_path = os.path.join(os.path.dirname(__file__), '..', 'CONTRIBUTORS.md')

with open(contributors_file_path, "r") as file:
lines = file.readlines()

found = False
new_lines = []
for line in lines:
if contributor in line:
found = True
for tier, count in tiers.items():
if pr_count >= count:
line = f"| @{contributor} | ![{tier} Badge](https://img.shields.io/badge/{tier.replace(' ', '%20')}-Achieved-blue) |\n"
break
new_lines.append(line)

if not found:
for tier, count in tiers.items():
if pr_count >= count:
new_lines.append(f"| @{contributor} | ![{tier} Badge](https://img.shields.io/badge/{tier.replace(' ', '%20')}-Achieved-blue) |\n")
break

with open(contributors_file_path, "w") as file:
file.writelines(new_lines)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper handling of file operations and potential errors.

The script reads and writes to the CONTRIBUTORS.md file but does not handle potential I/O errors such as file not found or permission issues. It's crucial to add error handling around file operations to prevent the script from failing silently or crashing.

Add error handling for file operations:

try:
    with open(contributors_file_path, "r") as file:
        lines = file.readlines()
except IOError as e:
    print(f"Error reading file: {e}")
    sys.exit(1)

# Further operations...

try:
    with open(contributors_file_path, "w") as file:
        file.writelines(new_lines)
except IOError as e:
    print(f"Error writing to file: {e}")
    sys.exit(1)

Comment on lines +25 to +28
run: |
PR_COUNT=$(gh pr list --author ${{ github.actor }} --state merged --repo ${{ github.repository }} --json number --jq '. | length')
echo "PR_COUNT=$PR_COUNT" >> $GITHUB_ENV

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address shell script issues flagged by static analysis.

Static analysis tools flagged potential issues with word splitting and globbing due to unquoted variables. This can lead to unexpected behavior if variables contain spaces or special characters.

Apply this fix to address the shell script issues:

run: |
    PR_COUNT=$(gh pr list --author "${{ github.actor }}" --state merged --repo "${{ github.repository }}" --json number --jq '. | length')
    echo "PR_COUNT=$PR_COUNT" >> $GITHUB_ENV
Tools
actionlint

25-25: shellcheck reported issue in this script: SC2086:info:2:30: Double quote to prevent globbing and word splitting

(shellcheck)

Comment on lines +30 to +40
run: |
python scripts/update_contributors.py ${{ github.actor }} $PR_COUNT

- name: Commit and Push Changes to Dedicated Branch
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git checkout -B contributors-updates
git add CONTRIBUTORS.md
git commit -m "Update CONTRIBUTORS.md for ${{ github.actor }}"
git push --set-upstream origin contributors-updates
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robustness in git operations.

The steps to commit and push changes are well-defined. However, consider adding error handling or conditions to ensure that changes are only pushed if there are actual updates to CONTRIBUTORS.md, to avoid empty commits.

Add a check before committing to ensure there are changes:

run: |
    git config --local user.email "github-actions[bot]@users.noreply.github.com"
    git config --local user.name "github-actions[bot]"
    git checkout -B contributors-updates
    git add CONTRIBUTORS.md
    if git diff --staged --quiet; then
        echo "No changes to CONTRIBUTORS.md, skipping commit."
    else
        git commit -m "Update CONTRIBUTORS.md for ${{ github.actor }}"
        git push --set-upstream origin contributors-updates
Tools
actionlint

30-30: shellcheck reported issue in this script: SC2086:info:1:59: Double quote to prevent globbing and word splitting

(shellcheck)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0ac3c34 and 7b01374.

Files selected for processing (1)
  • scripts/update_contributors.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • scripts/update_contributors.py

@dylanpulver
Copy link
Collaborator Author

This approach is a bit heavy handed and we solved this with a manual approach which was better suited.

@dylanpulver dylanpulver deleted the chore/contributor-badges branch October 18, 2024 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant