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

Enforce PR Standards #5941

Closed
wants to merge 6 commits into from
Closed

Enforce PR Standards #5941

wants to merge 6 commits into from

Conversation

sharadregoti
Copy link
Contributor

@sharadregoti sharadregoti commented Jan 31, 2025

User description

For internal users - Please add a Jira DX PR ticket to the subject!



Preview Link


Description


Screenshots (if appropriate)


Checklist

  • I have added a preview link to the PR description.
  • I have reviewed the suggestions made by our AI (PR Agent) and updated them accordingly (spelling errors, rephrasing, etc.)
  • I have reviewed the guidelines for contributing to this repository.
  • I have read the technical guidelines for contributing to this repository.
  • Make sure you have started your change off our latest master.
  • I labeled the PR

PR Type

Enhancement, Tests, Configuration changes


Description

  • Introduced a GitHub Action to enforce PR standards.

  • Added a script to validate PR checklist compliance.

  • Updated PR template with new checklist items and labeling guidelines.

  • Integrated dependencies for GitHub API and markdown parsing.


Changes walkthrough 📝

Relevant files
Enhancement
github.js
GitHub API integration for PR validation                                 

scripts/pr-review-bot/github.js

  • Added functions to fetch PR details and post comments.
  • Integrated GitHub API for PR management.
  • Implemented error handling for API calls.
  • +71/-0   
    index.js
    Automated PR checklist validation and closure                       

    scripts/pr-review-bot/index.js

  • Implemented checklist validation logic.
  • Parsed markdown to identify checklist items.
  • Automated PR closure for incomplete checklists.
  • +51/-0   
    Documentation
    pull_request_template.md
    Updated PR template with new checklist items                         

    .github/pull_request_template.md

  • Added new checklist items for internal users.
  • Updated labeling guidelines for PRs.
  • Enhanced clarity of checklist instructions.
  • +5/-1     
    Configuration changes
    enforce-pr-standards.yaml
    GitHub Action for enforcing PR standards                                 

    .github/workflows/enforce-pr-standards.yaml

  • Created a GitHub Action for PR standards enforcement.
  • Configured Node.js environment for script execution.
  • Integrated PR checklist validation script.
  • +30/-0   
    package.json
    Project setup for PR review bot                                                   

    scripts/pr-review-bot/package.json

  • Defined project metadata and dependencies.
  • Included @octokit/rest and marked libraries.
  • +17/-0   
    Dependencies
    package-lock.json
    Dependency lock file for PR review bot                                     

    scripts/pr-review-bot/package-lock.json

  • Added dependencies for GitHub API and markdown parsing.
  • Locked dependency versions for consistency.
  • +194/-0 

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    github-actions bot commented Jan 31, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 8e6dd58)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 Security concerns

    Sensitive information exposure:
    The GITHUB_TOKEN is being used in the workflow. Ensure that it is securely managed and not exposed in logs or error messages.

    ⚡ Recommended focus areas for review

    Possible Issue

    The pull_number variable is being used without ensuring it is properly converted to an integer. This could lead to unexpected behavior when interacting with the GitHub API.

    // const pull_number = parseInt(process.env.PR_NUMBER); // Convert to integer
    const pull_number =  process.env.PR_NUMBER; // Convert to integer
    Code Duplication

    The dependency installation step is duplicated in the workflow file, which could be optimized to avoid redundancy.

    const checklistItems = lexer
        .filter(item => item.type === 'list') // Filter out non-list items and comments
    Hardcoded Logic

    The script only checks the last two checklist items for completion. This hardcoded logic may not be flexible enough for future checklist changes.

    const lastTwoListItems = checklistItems.slice(-2);
    
    
    // Create message for comment on PR
    for (let index = 0; index < lastTwoListItems.length; index++) {
        const element = lastTwoListItems[index];
        if (!element.checked) {
            checklistFailedTitles += index + 1 + ": " + element.text + '\n'
        }
    }

    Copy link
    Contributor

    github-actions bot commented Jan 31, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 8e6dd58

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Correctly mark PR as draft

    Ensure the commentOnPullRequest function does not attempt to close the pull request
    by setting its state to 'closed' when the intention is to mark it as a draft.

    scripts/pr-review-bot/github.js [62]

    -state: 'closed', // Set draft to true
    +draft: true, // Correctly set draft to true
    Suggestion importance[1-10]: 10

    Why: The current code incorrectly sets the PR state to 'closed' instead of marking it as a draft. This suggestion fixes a critical bug that could lead to unintended behavior, making it highly impactful.

    10
    Convert pull_number to an integer

    Ensure that pull_number is converted to an integer before using it in API calls to
    avoid potential type-related issues.

    scripts/pr-review-bot/github.js [7]

    -const pull_number =  process.env.PR_NUMBER; // Convert to integer
    +const pull_number = parseInt(process.env.PR_NUMBER, 10); // Convert to integer
    Suggestion importance[1-10]: 9

    Why: Converting pull_number to an integer is crucial to avoid potential type-related issues when using it in API calls. This suggestion directly addresses a potential bug and improves the robustness of the code.

    9
    Validate GITHUB_TOKEN before usage

    Handle cases where process.env.GITHUB_TOKEN is undefined or invalid to prevent
    runtime errors when creating the Octokit instance.

    scripts/pr-review-bot/github.js [8]

    -const github_token = process.env.GITHUB_TOKEN
    +const github_token = process.env.GITHUB_TOKEN;
    +if (!github_token) {
    +    throw new Error("GITHUB_TOKEN is not defined");
    +}
    Suggestion importance[1-10]: 8

    Why: Adding validation for GITHUB_TOKEN ensures that the application fails gracefully with a clear error message if the token is undefined or invalid. This improves error handling and prevents runtime issues.

    8
    General
    Add error handling for file reading

    Add error handling for the fs.readFileSync call to gracefully handle cases where the
    file does not exist or cannot be read.

    scripts/pr-review-bot/index.js [9]

    -const fileContent = fs.readFileSync(filePath, 'utf8');
    +let fileContent;
    +try {
    +    fileContent = fs.readFileSync(filePath, 'utf8');
    +} catch (error) {
    +    console.error(`Error reading file at ${filePath}:`, error);
    +    process.exit(1);
    +}
    Suggestion importance[1-10]: 7

    Why: Adding error handling for fs.readFileSync ensures that the application can handle cases where the file does not exist or cannot be read, improving the robustness and reliability of the script.

    7

    Previous suggestions

    Suggestions up to commit 542373e
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Fix incorrect draft status update

    Avoid closing the pull request when updating its status to draft, as the state:
    'closed' parameter is incorrect for marking it as a draft.

    scripts/pr-review-bot/github.js [58-62]

     await octokit.rest.pulls.update({
         owner,
         repo,
         pull_number,
    -    state: 'closed', // Set draft to true
    +    draft: true, // Correctly mark as draft
     });
    Suggestion importance[1-10]: 10

    Why: The suggestion corrects a critical mistake where the pull request is being closed instead of being marked as a draft. This fix directly addresses a functional error in the script.

    10
    Ensure pull_number is an integer

    Validate that pull_number is correctly parsed as an integer before using it in API
    calls to prevent potential runtime errors.

    scripts/pr-review-bot/github.js [7]

    -const pull_number =  process.env.PR_NUMBER; // Convert to integer
    +const pull_number = parseInt(process.env.PR_NUMBER, 10); // Ensure it is an integer
    Suggestion importance[1-10]: 9

    Why: Parsing pull_number as an integer ensures that it is correctly formatted for API calls, preventing potential runtime errors. This is a critical fix for the functionality of the script.

    9
    Handle missing GitHub token error

    Add error handling for cases where process.env.GITHUB_TOKEN is undefined to avoid
    authentication failures.

    scripts/pr-review-bot/github.js [8]

    -const github_token = process.env.GITHUB_TOKEN
    +const github_token = process.env.GITHUB_TOKEN;
    +if (!github_token) {
    +    throw new Error("GITHUB_TOKEN is not defined in the environment variables.");
    +}
    Suggestion importance[1-10]: 8

    Why: Adding error handling for a missing GITHUB_TOKEN ensures the script fails gracefully and provides clear feedback, which is essential for debugging and avoiding authentication failures.

    8
    General
    Handle file read errors gracefully

    Ensure fs.readFileSync is wrapped in a try-catch block to handle potential file read
    errors gracefully.

    scripts/pr-review-bot/index.js [9]

    -const fileContent = fs.readFileSync(filePath, 'utf8');
    +let fileContent;
    +try {
    +    fileContent = fs.readFileSync(filePath, 'utf8');
    +} catch (error) {
    +    console.error(`Error reading file at ${filePath}:`, error);
    +    throw error;
    +}
    Suggestion importance[1-10]: 7

    Why: Wrapping fs.readFileSync in a try-catch block improves the script's robustness by handling potential file read errors, which is a good practice for error management.

    7
    Suggestions up to commit a5e9a0a
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Fix incorrect PR state update logic

    Avoid closing the pull request when updating its state to draft, as the state:
    'closed' parameter will permanently close the PR instead of marking it as draft.

    scripts/pr-review-bot/github.js [58-62]

     await octokit.rest.pulls.update({
         owner,
         repo,
         pull_number,
    -    state: 'closed', // Set draft to true
    +    draft: true, // Correctly set draft to true
     });
    Suggestion importance[1-10]: 10

    Why: The suggestion resolves a major issue where the PR is being permanently closed instead of being marked as a draft. Correcting this logic is essential for the intended functionality of the script.

    10
    Convert pull_number to an integer

    Ensure that pull_number is explicitly converted to an integer before being used in
    API calls, as it is currently being passed as a string from the environment
    variable, which may lead to unexpected behavior.

    scripts/pr-review-bot/github.js [7]

    -const pull_number =  process.env.PR_NUMBER; // Convert to integer
    +const pull_number = parseInt(process.env.PR_NUMBER, 10); // Convert to integer
    Suggestion importance[1-10]: 9

    Why: The suggestion addresses a critical issue where pull_number is used as a string, which could lead to unexpected behavior in API calls. Explicitly converting it to an integer ensures correctness and prevents potential bugs.

    9
    General
    Add error handling for file reading

    Add error handling for the fs.readFileSync call to gracefully handle cases where the
    file path is incorrect or the file does not exist, preventing the script from
    crashing.

    scripts/pr-review-bot/index.js [9]

    -const fileContent = fs.readFileSync(filePath, 'utf8');
    +let fileContent;
    +try {
    +    fileContent = fs.readFileSync(filePath, 'utf8');
    +} catch (error) {
    +    console.error(`Error reading file at ${filePath}:`, error);
    +    process.exit(1);
    +}
    Suggestion importance[1-10]: 8

    Why: Adding error handling for the fs.readFileSync call improves the robustness of the script by preventing crashes when the file path is incorrect or the file does not exist.

    8
    Remove duplicate dependency installation step

    Remove the duplicate "Install dependencies" step in the workflow to avoid
    unnecessary redundancy and improve efficiency.

    .github/workflows/enforce-pr-standards.yaml [21-24]

     - name: Install dependencies
       run: cd scripts/pr-review-bot/ && npm install
     
    -- name: Install dependencies
    -  run: cd scripts/pr-review-bot/ && npm install
    -
    Suggestion importance[1-10]: 7

    Why: Removing the duplicate "Install dependencies" step eliminates redundancy, improving the efficiency of the workflow without affecting functionality.

    7
    Suggestions up to commit a5e9a0a
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Correct PR state update logic

    Avoid setting the pull request state to 'closed' when intending to mark it as a
    draft, as this can cause unintended closure of the PR.

    scripts/pr-review-bot/github.js [62]

    -state: 'closed', // Set draft to true
    +draft: true, // Correctly mark as draft
    Suggestion importance[1-10]: 10

    Why: The current code incorrectly sets the PR state to 'closed' instead of marking it as a draft. This suggestion fixes a critical logic error that could unintentionally close PRs, disrupting workflows.

    10
    Convert pull_number to integer

    Ensure that pull_number is converted to an integer before being used in API calls to
    avoid potential type-related issues.

    scripts/pr-review-bot/github.js [7]

    -const pull_number =  process.env.PR_NUMBER; // Convert to integer
    +const pull_number = parseInt(process.env.PR_NUMBER, 10); // Convert to integer
    Suggestion importance[1-10]: 9

    Why: Converting pull_number to an integer ensures type consistency and prevents potential issues when using it in API calls. This is a critical fix as incorrect types could lead to runtime errors.

    9
    Validate GITHUB_TOKEN environment variable

    Handle cases where process.env.GITHUB_TOKEN is undefined or empty to prevent
    authentication errors.

    scripts/pr-review-bot/github.js [8]

    -const github_token = process.env.GITHUB_TOKEN
    +const github_token = process.env.GITHUB_TOKEN;
    +if (!github_token) {
    +    throw new Error("GITHUB_TOKEN is not defined");
    +}
    Suggestion importance[1-10]: 8

    Why: Adding validation for GITHUB_TOKEN ensures that the script fails early with a clear error message if the token is missing, preventing authentication errors during runtime.

    8
    General
    Add error handling for file reading

    Add error handling for the fs.readFileSync call to gracefully handle cases where the
    file does not exist or cannot be read.

    scripts/pr-review-bot/index.js [9]

    -const fileContent = fs.readFileSync(filePath, 'utf8');
    +let fileContent;
    +try {
    +    fileContent = fs.readFileSync(filePath, 'utf8');
    +} catch (error) {
    +    console.error(`Error reading file at ${filePath}:`, error);
    +    process.exit(1);
    +}
    Suggestion importance[1-10]: 7

    Why: Adding error handling for fs.readFileSync improves the robustness of the script by gracefully handling cases where the file does not exist or cannot be read, preventing unexpected crashes.

    7
    Suggestions up to commit 245feb3
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Parse environment variable as integer

    Ensure that process.env.PR_NUMBER is parsed as an integer before being used in API
    calls, as it might cause unexpected behavior if it remains a string.

    scripts/pr-review-bot/github.js [7]

    -const pull_number =  process.env.PR_NUMBER; // Convert to integer
    +const pull_number = parseInt(process.env.PR_NUMBER, 10); // Convert to integer
    Suggestion importance[1-10]: 9

    Why: Parsing process.env.PR_NUMBER as an integer is crucial to prevent potential issues when using it in API calls, as it might cause unexpected behavior if it remains a string. This suggestion directly addresses a potential bug and ensures robustness.

    9
    Add error handling for PR body retrieval

    Add error handling for the await getPRBody() call to ensure the script does not fail
    silently if the PR body cannot be retrieved.

    scripts/pr-review-bot/index.js [7]

    -const prBody = await getPRBody()
    +let prBody;
    +try {
    +    prBody = await getPRBody();
    +} catch (error) {
    +    console.error("Failed to fetch PR body:", error);
    +    process.exit(1);
    +}
    Suggestion importance[1-10]: 9

    Why: Adding error handling for the getPRBody call ensures the script does not fail silently if the PR body cannot be retrieved. This is a critical improvement for reliability and debugging.

    9
    Validate GitHub token existence

    Add a check to ensure github_token is defined and not empty before creating the
    Octokit instance to prevent runtime errors.

    scripts/pr-review-bot/github.js [11]

    +if (!github_token) {
    +    throw new Error("GitHub token is not defined");
    +}
     const octokit = new Octokit({ auth: github_token });
    Suggestion importance[1-10]: 8

    Why: Adding a check for the existence of github_token ensures that the script does not proceed with an undefined or empty token, which would lead to runtime errors. This is a valuable improvement for error prevention.

    8
    Safeguard against undefined user login

    Handle cases where pullRequest.user or pullRequest.user.login might be undefined to
    avoid runtime errors when constructing the comment message.

    scripts/pr-review-bot/github.js [38]

    -const author = pullRequest.user.login;
    +const author = pullRequest.user?.login || "unknown user";
    Suggestion importance[1-10]: 8

    Why: Handling cases where pullRequest.user or pullRequest.user.login might be undefined prevents runtime errors and ensures the script can handle edge cases gracefully. This is a practical enhancement for robustness.

    8

    Copy link

    netlify bot commented Jan 31, 2025

    PS. Pls add /docs/nightly to the end of url

    Name Link
    🔨 Latest commit 79bfc83
    🔍 Latest deploy log https://app.netlify.com/sites/tyk-docs/deploys/679ccc6841f1290008191661
    😎 Deploy Preview https://deploy-preview-5941--tyk-docs.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    Copy link
    Contributor

    Persistent review updated to latest commit 79bfc83

    Copy link
    Contributor

    Persistent review updated to latest commit 245feb3

    @sharadregoti
    Copy link
    Contributor Author

    PR Checklist Failed

    @sharadregoti This PR does not pass all the checklist mentioned in description, because of which we are closing the PR. Re-open it when all checklist items are passed

    Failed Items

    1: Make sure you have started your change off our latest master.
    2: I labeled the PR

    @sharadregoti
    Copy link
    Contributor Author

    PR Checklist Failed

    @sharadregoti This PR does not pass some of the required checklist items mentioned in description, because of which we are closing the PR. Re-open it when all checklist items are passed

    Failed Items

    1: Make sure you have started your change off our latest master.
    2: I labeled the PR

    @sharadregoti
    Copy link
    Contributor Author

    PR Checklist Failed

    @sharadregoti This PR does not pass some of the required checklist items mentioned in description, because of which we are closing the PR. Re-open it when all checklist items are passed

    Failed Items

    1: For internal users - Please add a Jira DX PR ticket to the subject!
    2: I have added the appropriate release label to this PR:

    • If it is for a future release, label it as

    @sharadregoti
    Copy link
    Contributor Author

    PR Checklist Failed

    @sharadregoti This PR does not pass some of the required checklist items mentioned in description, because of which we are closing the PR. Re-open it when all checklist items are passed

    Failed Items

    1: For internal users - Please add a Jira DX PR ticket to the subject!
    2: I have added the appropriate release label to this PR:

    • If it is for a future release, label it as future-release and specify the version (e.g., future-release, 6.0).
    • If it should be merged into an older version, use the specific version label (e.g., 4.1, 5.1).
    • If no label is added, it will be assumed that the PR should be merged into the latest current version (e.g., 5.5) and master.

    @sharadregoti sharadregoti reopened this Jan 31, 2025
    Copy link
    Contributor

    Persistent review updated to latest commit 245feb3

    Copy link
    Contributor

    Persistent review updated to latest commit a5e9a0a

    Copy link
    Contributor

    PR Checklist Failed

    @sharadregoti This PR does not pass some of the required checklist items mentioned in description, because of which we are closing the PR. Re-open it when all checklist items are passed

    Failed Items

    1: For internal users - Please add a Jira DX PR ticket to the subject!
    2: I have added the appropriate release label to this PR:

    • If it is for a future release, label it as future-release and specify the version (e.g., future-release, 6.0).
    • If it should be merged into an older version, use the specific version label (e.g., 4.1, 5.1).
    • If no label is added, it will be assumed that the PR should be merged into the latest current version (e.g., 5.5) and master.

    @github-actions github-actions bot closed this Jan 31, 2025
    Copy link
    Contributor

    Persistent review updated to latest commit 542373e

    @sharadregoti sharadregoti reopened this Jan 31, 2025
    Copy link
    Contributor

    PR Checklist Failed

    @sharadregoti This PR does not meet all the required checklist items mentioned in the description. As a result, we are closing the PR. Please re-open it once all checklist items are completed (ensure they are checked in the description).

    Failed Items

    1: For internal users - Please add a Jira DX PR ticket to the subject!
    2: I have added the appropriate release label to this PR:

    • If it is for a future release, label it as future-release and specify the version (e.g., future-release, 6.0).
    • If it should be merged into an older version, use the specific version label (e.g., 4.1, 5.1).
    • If no label is added, it will be assumed that the PR should be merged into the latest current version (e.g., 5.5) and master.

    @github-actions github-actions bot closed this Jan 31, 2025
    Copy link
    Contributor

    Persistent review updated to latest commit 8e6dd58

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant