Skip to content

Commit

Permalink
ci: improve GitHub Actions workflow and add code quality checks
Browse files Browse the repository at this point in the history
- Remove dependency on real API tokens in tests
- Add multi-Ruby version testing (3.0, 3.1, 3.2)
- Implement test coverage reporting with SimpleCov
- Add RuboCop for code style enforcement
- Add Brakeman for security analysis
- Add bundle-audit for dependency security checks
- Add Dependabot configuration for automated updates
- Configure caching for faster CI builds

Breaking Changes:
- Tests now use WebMock instead of real API calls
- Require Ruby >= 3.0

Testing:
- Use dummy API key for all test runs
- All API calls are now mocked with WebMock
- Coverage reporting through CodeClimate

Configuration:
- Add .rubocop.yml for code style
- Add dependabot.yml for dependency management
- Update gemspec with new development dependencies
  • Loading branch information
nagstler committed Oct 30, 2024
1 parent b0c796c commit e8ca99f
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 197 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "bundler"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
83 changes: 61 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,76 @@
name: CI
on: [push]
env:
CI: true
API_KEY: ${{ secrets.API_KEY }}
CHATGPT_API_KEY: ${{ secrets.CHATGPT_API_KEY }}

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [3.1.3]
ruby-version: ['3.0', '3.1', '3.2']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # This will cache installed gems

- name: Install dependencies
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run tests with code coverage
run: COVERAGE=true bundle exec rake test
- name: Install Code Climate Test Reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
working-directory: ${{ github.workspace }}
- name: Push coverage data to Code Climate
run: bundle install

- name: Run tests
run: bundle exec rake test
env:
COVERAGE: true
# Using a dummy API key since all API calls are mocked
CHATGPT_API_KEY: 'test-key'

- name: Publish code coverage
uses: paambaati/codeclimate-action@v3.2.0
if: github.ref == 'refs/heads/main' # Only run on main branch
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
run: |
./cc-test-reporter before-build
./cc-test-reporter after-build --exit-code $?
working-directory: ${{ github.workspace }}
with:
coverageCommand: bundle exec rake test
coverageLocations: ${{github.workspace}}/coverage/.resultset.json:simplecov

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true

- name: Run RuboCop
run: bundle exec rubocop

security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true

- name: Install Brakeman
run: gem install brakeman

- name: Run Brakeman
run: brakeman -A -q -w2

- name: Check for vulnerable dependencies
run: bundle exec bundle-audit check --update
24 changes: 17 additions & 7 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# .rubocop.yml
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 3.0
NewCops: enable
SuggestExtensions: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
EnforcedStyle: single_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes
Style/Documentation:
Enabled: false

Metrics/MethodLength:
Max: 20

Metrics/AbcSize:
Max: 30

Layout/LineLength:
Max: 120
Metrics/BlockLength:
Exclude:
- 'test/**/*'
- '*.gemspec'
16 changes: 8 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.org'

# Specify your gem's dependencies in chatgpt-ruby.gemspec
gemspec

gem "rake", "~> 13.0"
gem 'rake', '~> 13.0'

gem "minitest", "~> 5.0"
gem 'minitest', '~> 5.0'

gem "rubocop", "~> 1.21"
gem 'rubocop', '~> 1.21'

gem "rest-client"
gem 'rest-client'

# Gemfile
group :test do
gem "simplecov"
gem "simplecov_json_formatter"
gem "webmock"
gem 'simplecov'
gem 'simplecov_json_formatter'
gem 'webmock'
end
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ GEM
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
bigdecimal (3.1.8)
brakeman (5.4.1)
bundler-audit (0.9.2)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
crack (1.0.0)
bigdecimal
rexml
Expand Down Expand Up @@ -59,6 +63,7 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
thor (1.3.2)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.2)
Expand All @@ -73,6 +78,8 @@ PLATFORMS
arm64-darwin-22

DEPENDENCIES
brakeman (~> 5.2)
bundler-audit (~> 0.9)
chatgpt-ruby!
minitest (~> 5.0)
rake (~> 13.0)
Expand Down
14 changes: 7 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.pattern = "test/**/*_test.rb"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.libs << 'lib'
t.test_files = FileList['test/**/test_*.rb']
end

require "rubocop/rake_task"
require 'rubocop/rake_task'

RuboCop::RakeTask.new

Expand Down
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "chatgpt/ruby"
require 'bundler/setup'
require 'chatgpt/ruby'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -11,5 +11,5 @@ require "chatgpt/ruby"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start(__FILE__)
42 changes: 23 additions & 19 deletions chatgpt-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
# chatgpt-ruby.gemspec
# frozen_string_literal: true

require_relative "lib/chatgpt/version"
require_relative 'lib/chatgpt/version'

Gem::Specification.new do |spec|
spec.name = "chatgpt-ruby"
spec.name = 'chatgpt-ruby'
spec.version = Chatgpt::Ruby::VERSION
spec.authors = ["Nagendra Dhanakeerthi"]
spec.email = ["nagendra.dhanakeerthi@gmail.com"]
spec.authors = ['Nagendra Dhanakeerthi']
spec.email = ['nagendra.dhanakeerthi@gmail.com']

spec.summary = "Ruby client for OpenAI's ChatGPT API"
spec.description = "A Ruby SDK for OpenAI's ChatGPT API"
spec.homepage = "https://github.com/nagstler/chatgpt-ruby"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"
spec.homepage = 'https://github.com/nagstler/chatgpt-ruby'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.6.0'

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/nagstler/chatgpt-ruby"
spec.metadata["changelog_uri"] = "https://github.com/nagstler/chatgpt-ruby/blob/main/CHANGELOG.md"
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/nagstler/chatgpt-ruby'
spec.metadata['changelog_uri'] = 'https://github.com/nagstler/chatgpt-ruby/blob/main/CHANGELOG.md'

spec.files = Dir.glob("{lib,exe}/**/*") + %w[README.md LICENSE.txt]
spec.bindir = "exe"
spec.files = Dir.glob('{lib,exe}/**/*') + %w[README.md LICENSE.txt]
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency "rest-client", "~> 2.1"
spec.add_dependency 'rest-client', '~> 2.1'

spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "simplecov", "~> 0.21"
spec.add_development_dependency "simplecov_json_formatter", "~> 0.1"
spec.add_development_dependency "webmock", "~> 3.18"
spec.add_development_dependency 'brakeman', '~> 5.2'
spec.add_development_dependency 'bundler-audit', '~> 0.9'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rubocop', '~> 1.21'
spec.add_development_dependency 'simplecov', '~> 0.21'
spec.add_development_dependency 'simplecov_json_formatter', '~> 0.1'
spec.add_development_dependency 'webmock', '~> 3.18'
spec.metadata['rubygems_mfa_required'] = 'true'
end
8 changes: 4 additions & 4 deletions lib/chatgpt.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# lib/chatgpt.rb
# frozen_string_literal: true

require_relative "chatgpt/version"
require_relative "chatgpt/errors"
require_relative "chatgpt/configuration"
require_relative "chatgpt/client"
require_relative 'chatgpt/version'
require_relative 'chatgpt/errors'
require_relative 'chatgpt/configuration'
require_relative 'chatgpt/client'

module ChatGPT
class << self
Expand Down
Loading

0 comments on commit e8ca99f

Please sign in to comment.