diff --git a/README.md b/README.md index 7106dffe..64fad27c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ This GitHub Action automatically checks for typos in the files changed in a pull ## Usage +> [!NOTE] +> If you want GitHub Actions to approve PRs, you must grant permission to it at: project settings -> Actions -> General -> Allow GitHub Actions to create and approve pull requests. + +![Typocop Logo](typocop-approval.png) + 1. **Using Typocop GitHub Action:** 1. Copy the `.github/workflows/typocop.yml` file into your project repository. @@ -70,7 +75,7 @@ This GitHub Action automatically checks for typos in the files changed in a pull ```bash gem install typocop # install - GITHUB_TOKEN=your_token PULL_REQUEST_ID=your_pull_request_id typocop execute # run action + GITHUB_TOKEN=your_token PULL_REQUEST_ID=the_request_id GITHUB_BASE_REF=branch_base_name typocop execute # run action ``` ![Typocop demo](typocop.gif "") diff --git a/lib/typocop.rb b/lib/typocop.rb index 91870dbf..9aa67802 100644 --- a/lib/typocop.rb +++ b/lib/typocop.rb @@ -11,25 +11,27 @@ require 'typocop/repo' require 'typo_checker' -GITHUB_TOKEN = ENV['GITHUB_TOKEN'] || '' -PULL_ID = ENV['PULL_REQUEST_ID'] -GITHUB_BASE_REF = ENV['GITHUB_BASE_REF'] || 'main' +GITHUB_TOKEN = ENV.fetch('GITHUB_TOKEN') { raise 'GITHUB_TOKEN is required' } +PULL_ID = ENV.fetch('PULL_REQUEST_ID') { raise 'PULL_REQUEST_ID is required' } +GITHUB_BASE_REF = ENV.fetch('GITHUB_BASE_REF') { raise 'GITHUB_BASE_REF is required' } BASE_BRANCH = GITHUB_BASE_REF.start_with?('origin/') ? GITHUB_BASE_REF : "origin/#{GITHUB_BASE_REF}" module Typocop def self.execute(settings) + repo = Repo.new + paths = repo.patch_additions.map(&:path) + + return unless paths.any? + excludes = settings.excludes skips = settings.skips - typo_checker = TypoChecker::Checker.new(excludes, skips, stdoutput = false) + typo_checker = TypoChecker::Checker.new(paths: paths, excludes: excludes, skips: skips, stdoutput: false) found_typos = typo_checker.scan_repo('.') - if found_typos.empty? - puts 'No typos.' - else - cops = Cops.new(found_typos) - repo = Repo.new - client = Client.new(repo) - client.execute(cops.cops) - end + puts 'No typos found' if found_typos.empty? + + cops = Cops.new(found_typos) + client = Client.new(repo) + client.execute(cops.cops) end end diff --git a/lib/typocop/client.rb b/lib/typocop/client.rb index 0a5d1f76..6e4a9c0b 100644 --- a/lib/typocop/client.rb +++ b/lib/typocop/client.rb @@ -99,7 +99,7 @@ def suggestion_comment(typos) end def line_content(cop) - patch = @repo.patch_additions.find { |patch| patch.path == cop.path } + patch = @repo.patch_additions.find { |p| p.path == cop.path } patch.added_lines.find { |line| line.new_lineno == cop.line }.content end diff --git a/lib/typocop/settings.rb b/lib/typocop/settings.rb index f700c20b..e51f13b0 100644 --- a/lib/typocop/settings.rb +++ b/lib/typocop/settings.rb @@ -13,12 +13,10 @@ def initialize(setting_path) private def load_settings(setting_path) - begin - YAML.load_file(setting_path) - rescue StandardError => e - puts "Error loading YAML file: #{e.message}" - return {} - end + YAML.load_file(setting_path) + rescue StandardError => e + puts "Error loading YAML file: #{e.message}" + {} end end end diff --git a/lib/typocop/version.rb b/lib/typocop/version.rb index 583ada2d..fdca804b 100644 --- a/lib/typocop/version.rb +++ b/lib/typocop/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Typocop - VERSION = '0.1.3' + VERSION = '0.1.4' end diff --git a/test/example.js b/test/example.js deleted file mode 100644 index b8ee69be..00000000 --- a/test/example.js +++ /dev/null @@ -1,3 +0,0 @@ -var languege = 'en' // typo -console.log(languege) // typo -console.log('welcom') // typo diff --git a/test/example.py b/test/example.py deleted file mode 100644 index a994b620..00000000 --- a/test/example.py +++ /dev/null @@ -1,26 +0,0 @@ -def greet(name): - print(f"Hello, {name}! Welcome to Python programming.") - -def factorial(n): - if n == 0 or n == 1: - return 1 - else: - result = 1 - for i in range(2, n + 1): - result *= i - return result - -numbers = [5, 3, 8, 10] - -for number in numbers: - print(f"Factorial of {number} is: {factorial(number)}") - -user_name = input("Enter your name: ") - -greet(user_name) - -age = int(input("Enter your age: ")) -if age >= 18: - print("You are elligible for an adult privilege.") # typo -else: - print("You are underage, so no adult privileges for you.") diff --git a/test/example.rb b/test/example.rb deleted file mode 100644 index d192aa55..00000000 --- a/test/example.rb +++ /dev/null @@ -1,9 +0,0 @@ -def greeting(name) - puts "Hello, #{name}! Welocome to the Ruby typos test." - puts 'languege' # typo - puts 'knowlege' # typo - puts 'knowlege: languege' # typo - puts 'welcom' -end - -greeting('Alice') diff --git a/typocop-approval.png b/typocop-approval.png new file mode 100644 index 00000000..106505cd Binary files /dev/null and b/typocop-approval.png differ diff --git a/typocop.gemspec b/typocop.gemspec index 2e11b548..057df003 100644 --- a/typocop.gemspec +++ b/typocop.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.add_dependency 'octokit', '9.2.0' s.add_dependency 'rugged', '~> 1.6.3' s.add_dependency 'thor', '~> 1.3.2' - s.add_dependency 'typo_checker', '0.1.7' + s.add_dependency 'typo_checker' s.executables = %w[typocop] s.files.each do |file| next unless file.start_with?('bin/')