Skip to content

Commit

Permalink
Merge pull request #7 from datpmt/test-rb
Browse files Browse the repository at this point in the history
Test rb
  • Loading branch information
datpmt authored Dec 7, 2024
2 parents ec4a2e2 + 710d795 commit 897dee2
Show file tree
Hide file tree
Showing 21 changed files with 514 additions and 177 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/typocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Check Typos in Pull Request

on: [pull_request]

jobs:
typocop:
permissions: write-all
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.14.0-alpha.1'

- name: Install typos
run: |
python3 -m pip install --upgrade pip
pip install typos
- name: Run Typocop Action
uses: ./
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pull_request_id: ${{ github.event.pull_request.number }}
github_base_ref: ${{ github.base_ref }}
67 changes: 0 additions & 67 deletions .github/workflows/typos-check.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typocop-*.gem
Empty file added CHANGELOG.md
Empty file.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Gemfile
source 'https://rubygems.org'

gem 'cli'
gem 'octokit', '>= 9.2.0'
gem 'rugged', '1.6.3'
37 changes: 37 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
cli (1.4.0)
faraday (2.12.1)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.4.0)
net-http (>= 0.5.0)
json (2.9.0)
logger (1.6.2)
net-http (0.6.0)
uri
octokit (9.2.0)
faraday (>= 1, < 3)
sawyer (~> 0.9)
public_suffix (6.0.1)
rugged (1.6.3)
sawyer (0.9.2)
addressable (>= 2.3.5)
faraday (>= 0.17.3, < 3)
uri (1.0.2)

PLATFORMS
arm64-darwin-22
x86_64-darwin-22

DEPENDENCIES
cli
octokit (>= 9.2.0)
rugged (= 1.6.3)

BUNDLED WITH
2.3.5
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Check Typos in Pull Request

![image](https://github.com/user-attachments/assets/bbcdcedb-cf0b-4cc0-959a-fe5caa71f833)
![image](typocop.png)

This GitHub Action automatically checks for typos in the files changed in a pull request. It comments on the pull request with any detected typos and provides suggestions for corrections.

Expand All @@ -9,11 +9,12 @@ This GitHub Action automatically checks for typos in the files changed in a pull
- Detects typos in files that are part of the pull request.
- Provides a brief output of typos found in each file.
- Comments on the pull request with the results.
- Approve the PR if no typos.
- Supports all programing languages.

## Usage

- Copy content `.github/workflows/typos-check.yml` and `typos-check.js` into your project.
- Copy file `.github/workflows/typocop.yml` into your project.
- Create new PR.

## Contributors
Expand Down
40 changes: 40 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: 'typocop'
description: 'Github action comments on PRs with typos or approvals'
author: "Datpmt <[email protected]>"
repository: "https://github.com/datpmt/typocop"
branding:
icon: 'check-circle'
color: 'green'

inputs:
github_token:
description: 'GitHub Token'
required: true
pull_request_id:
description: 'Pull Request ID'
required: true
github_base_ref:
description: 'GitHub Base Reference'
required: true

runs:
using: 'composite'
steps:
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.1'

- name: Install dependencies
run: |
gem install typocop
shell: bash

- name: Run Typocop
run: |
typocop execute
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
PULL_REQUEST_ID: ${{ inputs.pull_request_id }}
GITHUB_BASE_REF: ${{ inputs.github_base_ref }}
shell: bash
3 changes: 3 additions & 0 deletions bin/typocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
require 'typocop/cli'
Typocop::CLI.start(ARGV)
60 changes: 60 additions & 0 deletions lib/typocop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'rugged'
# require 'pry'
require 'octokit'

require 'typocop/cli'
require 'typocop/client'
require 'typocop/comment'
require 'typocop/cop'
require 'typocop/cops'
require 'typocop/patch'
require 'typocop/repo'

GITHUB_TOKEN = ENV['GITHUB_TOKEN'] || ''
PULL_ID = ENV['PULL_REQUEST_ID']
GITHUB_BASE_REF = ENV['GITHUB_BASE_REF'] || 'main'
BASE_BRANCH = GITHUB_BASE_REF.start_with?('origin/') ? GITHUB_BASE_REF : "origin/#{GITHUB_BASE_REF}"

module Typocop
def self.execute
typo_outputs = `typos --format brief`
typo_outputs = typo_outputs.split("\n")

if typo_outputs.empty?
puts 'No typo output.'
else
result = typo_outputs.each_with_object({}) do |output, hash|
path, line, _column, typo_detail = output.split(':')
typo_match = /`(.*?)` -> `(.*?)`/.match(typo_detail)
incorrect_word, correct_word = typo_match ? typo_match.captures : []

path = path.start_with?('./') ? path[2..] : path
line = line.to_i

hash[path] ||= {}

hash[path][:typos] ||= []

existing_entry = hash[path][:typos].find { |typo| typo[:line] == line }

if existing_entry
existing_entry[:typos] << { incorrect_word: incorrect_word, correct_word: correct_word }
else
hash[path][:typos] << { line: line, typos: [{ incorrect_word: incorrect_word, correct_word: correct_word }] }
end
end

result = result.map do |path, data|
data[:typos].map do |entry|
{ path: path, line: entry[:line], typos: entry[:typos] }
end
end.flatten

cops = Cops.new(result)
cops_data = cops.cops
repo = Repo.new
client = Client.new(repo)
client.execute(cops_data)
end
end
end
11 changes: 11 additions & 0 deletions lib/typocop/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'thor'

module Typocop
class CLI < Thor
require 'typocop'
desc 'execute', 'Run typocop'
def execute
Typocop.execute
end
end
end
Loading

0 comments on commit 897dee2

Please sign in to comment.