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

Add link checker to Husky pre-commit hook (#840) #854

Closed
wants to merge 3 commits into from

Conversation

Ravish990
Copy link

@Ravish990 Ravish990 commented Jan 28, 2025

Problem

Broken links were not flagged during commits, leading to outdated or invalid links being introduced into the codebase.

Solution

Integrated linkinator: Checks for broken links in HTML files.

Added check-links.js: Recursively checks all HTML files and reports broken links.

Updated package.json:

Added a check:links script for running the link checker manually.

Configured lint-staged to check .html files during pre-commit.

Configured Husky pre-commit hooks: Blocks commits with broken links.

Using Husky 7 version.

Fixed linting issues in check-links.js.

Added Node.jsenvironment support in .eslintrc.js.

Key Changes

check-links.js: Script to validate HTML links using linkinator.

*package.json: *

Added check:links.

Updated various packages.

Used Husky 7 version.

Added glob and linkinator.

Testing

Tested the link checker locally: Verified with HTML files containing broken and valid links.

Verified pre-commit hook functionality: Ensured broken links block commits while valid links allow commits to proceed.

Summary by Sourcery

Added a link check to the pre-commit hooks, blocking commits with broken links.****

Copy link

sourcery-ai bot commented Jan 28, 2025

Reviewer's Guide by Sourcery

This pull request introduces a link checker using linkinator to ensure all links in HTML files are valid before committing. It integrates with Husky pre-commit hooks to prevent commits with broken links and provides a manual check script.

Sequence diagram for the link checking process during commit

sequenceDiagram
    actor Developer
    participant Git
    participant Husky
    participant LinkChecker
    participant HTML

    Developer->>Git: git commit
    Git->>Husky: trigger pre-commit hook
    Husky->>LinkChecker: check staged HTML files
    LinkChecker->>HTML: scan for links
    HTML-->>LinkChecker: return links
    LinkChecker->>LinkChecker: validate links
    alt broken links found
        LinkChecker-->>Husky: return error (exit 1)
        Husky-->>Git: abort commit
        Git-->>Developer: show error message
    else all links valid
        LinkChecker-->>Husky: return success (exit 0)
        Husky-->>Git: proceed with commit
        Git-->>Developer: commit successful
    end
Loading

File-Level Changes

Change Details Files
Added a script to check for broken links in HTML files using linkinator.
  • Implemented a function to recursively check all HTML files.
  • Utilized linkinator to validate links.
  • Reported broken links with file and URL information.
  • Exited with an error code if broken links are found.
check-links.js
Configured package.json to include the link checker and update dependencies.
  • Added a check:links script to run the link checker manually.
  • Added glob and linkinator as dependencies.
  • Updated husky to version 7.
  • Configured lint-staged to check .html files during pre-commit.
package.json
Configured Husky pre-commit hook to run the link checker.
  • Added a command to execute the check-links.js script before each commit.
.husky/pre-commit

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the _community [BOT ONLY] PR label for community contributions. Used for tracking label Jan 28, 2025
Copy link

netlify bot commented Jan 28, 2025

Deploy Preview for neurobagel-annotator ready!

Name Link
🔨 Latest commit 4776607
🔍 Latest deploy log https://app.netlify.com/sites/neurobagel-annotator/deploys/6798669adbf9bf000862b3d8
😎 Deploy Preview https://deploy-preview-854--neurobagel-annotator.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.

@rmanaem rmanaem requested review from rmanaem and removed request for rmanaem January 28, 2025 17:59
Copy link
Contributor

@rmanaem rmanaem left a comment

Choose a reason for hiding this comment

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

Thanks for taking this issue on @Ravish990 and congratz on your first contribution to Neurobagel! 🥯
We noticed that some of the broken links are actually in the .vue file so I updated the issue to include checking those as well, please take a look.
Aside from that I left a few review comments please have a look and respond.

Comment on lines -56 to -64

// Disallow line breaks before the closing bracket on a multiline opening tag, i.e. -
// Yes: <tagname
// attribute1=""
// attribute2="">
// No : <tagname
// attribute1=""
// attribute2=""
// >
Copy link
Contributor

Choose a reason for hiding this comment

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

Was removal of comments done automatically?

Comment on lines +4 to +12
echo "Running pre-commit checks..."

# Run the link checker
echo "Checking links in HTML files..."
node check-links.js
LINK_CHECK_EXIT_CODE=$?

# Run lint-staged
echo "Running lint-staged..."
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is the right place for this code, we usually run the check/linting commands using lint-staged to apply only to staged files and then use husky to run lint-staged pre-commit.

Comment on lines +14 to +31
LINT_STAGED_EXIT_CODE=$?

# Check the exit codes of both processes
if [ $LINK_CHECK_EXIT_CODE -ne 0 ]; then
echo "Link check failed. Please fix the broken links before committing."
fi

if [ $LINT_STAGED_EXIT_CODE -ne 0 ]; then
echo "Lint-staged failed. Please fix the linting issues before committing."
fi

# Abort commit if any checks fail
if [ $LINK_CHECK_EXIT_CODE -ne 0 ] || [ $LINT_STAGED_EXIT_CODE -ne 0 ]; then
echo "Pre-commit checks failed. Aborting commit."
exit 1
fi

echo "All checks passed! Proceeding with commit."
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is needed since lint-staged as part of its flow displays the checks that failed/passed.

Comment on lines +1 to +47
const { check } = require('linkinator');
const glob = require('glob');

async function run(files) {
// If no staged files are passed, fall back to all HTML files
if (!files || files.length === 0) {
console.log('No HTML files staged. Checking all HTML files in the repository...');
files = glob.sync('**/*.html', { ignore: 'node_modules/**' });

if (files.length === 0) {
console.log('No HTML files found. Skipping link check.');
process.exit(0);
}
}

let allLinksValid = true;

for (const file of files) {
console.log(`Checking links in ${file}...`);
const results = await check({
path: file,
recurse: true,
concurrency: 5
});

for (const link of results.links) {
if (link.state === 'BROKEN') {
console.error(`❌ Broken link found in ${file}: ${link.url}`);
allLinksValid = false;
}
}
}

if (allLinksValid) {
console.log('✅ All links are valid!');
process.exit(0);
} else {
console.error('❌ Broken links detected. ');
process.exit(1);
}
}

const stagedFiles = process.argv.slice(2);
run(stagedFiles).catch((err) => {
console.error('Unexpected error:', err);
process.exit(1);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for implementing this script to check for broken links but I believe the functionality implemented here is already supported natively by linkinator.

@@ -38,7 +42,6 @@
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-cypress": "^3.6.0",
"eslint-plugin-vue": "^9.32.0",
"husky": "^9.1.7",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why revert husky back to v7?

@alyssadai alyssadai added the flag:check Issue needs attention before further action label Feb 11, 2025
@rmanaem rmanaem removed the flag:check Issue needs attention before further action label Feb 13, 2025
@rmanaem
Copy link
Contributor

rmanaem commented Feb 13, 2025

Hey @Ravish990, I'm going to close this PR for now due to inactivity. If you're still interested in working on this, feel free to reopen it or ping us on the related issue, and we'd be happy to discuss your implementation further. Thanks for your contribution!

@rmanaem rmanaem closed this Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
_community [BOT ONLY] PR label for community contributions. Used for tracking
Projects
Status: Review - Done
Development

Successfully merging this pull request may close these issues.

3 participants