Skip to content

Add search issues command and refactoring #7

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

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2

updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "bundler"
directory: "/"
schedule:
interval: "weekly"
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Test

on:
pull_request:
push:
branches:
- master
workflow_dispatch:

permissions:
contents: read

jobs:
rspec:
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1"]
runs-on: "ubuntu-latest"
continue-on-error: false
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rspec

rubocop:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- run: bundle exec rubocop
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tmp
*.o
*.a
mkmf.log
vendor/bundle/
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
inherit_from: .rubocop_todo.yml

require:
- rubocop-performance
- rubocop-rspec
- rubocop-rake

AllCops:
NewCops: enable
84 changes: 84 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 99999`
# on 2022-10-12 04:57:08 UTC using RuboCop version 1.36.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'ruboty-github.gemspec'

# Offense count: 1
Lint/UnreachableCode:
Exclude:
- 'lib/ruboty/github/actions/close_issue.rb'

# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 24

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
Metrics/MethodLength:
Max: 14

# Offense count: 2
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.
# NamePrefix: is_, has_, have_
# ForbiddenPrefixes: is_, has_, have_
# AllowedMethods: is_a?
# MethodDefinitionMacros: define_method, define_singleton_method
Naming/PredicateName:
Exclude:
- 'spec/**/*'
- 'lib/ruboty/github/actions/base.rb'
- 'lib/ruboty/github/actions/close_issue.rb'

# Offense count: 3
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 2
RSpec/MultipleExpectations:
Max: 2

# Offense count: 11
# Configuration parameters: AllowSubject.
RSpec/MultipleMemoizedHelpers:
Max: 13

# Offense count: 1
RSpec/NoExpectationExample:
Exclude:
- 'spec/ruboty/handlers/github_spec.rb'

# Offense count: 9
# Configuration parameters: AllowedConstants.
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/ruboty/github/actions/base.rb'
- 'lib/ruboty/github/actions/close_issue.rb'
- 'lib/ruboty/github/actions/create_deploy_pull_request.rb'
- 'lib/ruboty/github/actions/create_issue.rb'
- 'lib/ruboty/github/actions/create_pull_request.rb'
- 'lib/ruboty/github/actions/merge_pull_request.rb'
- 'lib/ruboty/github/actions/remember.rb'
- 'lib/ruboty/github/actions/search_issues.rb'
- 'lib/ruboty/handlers/github.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 148
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased
- Add `search issues` command
- Support test on GitHub Actions
- Add rubocop, rubocop-rake, rubocop-performance, rubocop-rspec gems
- Use rubocop autocorrect and modify the code

## 0.2.3
- Fix typo

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in ruboty-github.gemspec
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'
27 changes: 15 additions & 12 deletions lib/ruboty/github.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
require "active_support/core_ext/string/strip"
require "octokit"
# frozen_string_literal: true

require "ruboty"
require "ruboty/github/actions/base"
require "ruboty/github/actions/close_issue"
require "ruboty/github/actions/create_issue"
require "ruboty/github/actions/create_pull_request"
require "ruboty/github/actions/create_deploy_pull_request"
require "ruboty/github/actions/merge_pull_request"
require "ruboty/github/actions/remember"
require "ruboty/github/version"
require "ruboty/handlers/github"
require 'active_support/core_ext/string/strip'
require 'octokit'

require 'ruboty'
require 'ruboty/github/actions/base'
require 'ruboty/github/actions/close_issue'
require 'ruboty/github/actions/create_issue'
require 'ruboty/github/actions/create_pull_request'
require 'ruboty/github/actions/create_deploy_pull_request'
require 'ruboty/github/actions/merge_pull_request'
require 'ruboty/github/actions/remember'
require 'ruboty/github/actions/search_issues'
require 'ruboty/github/version'
require 'ruboty/handlers/github'
19 changes: 10 additions & 9 deletions lib/ruboty/github/actions/base.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

module Ruboty
module Github
module Actions
class Base
NAMESPACE = "github"
NAMESPACE = 'github'

attr_reader :message

Expand All @@ -17,7 +19,7 @@ def access_tokens
end

def body
message[:description] || ""
message[:description] || ''
end

def sender_name
Expand Down Expand Up @@ -45,14 +47,14 @@ def repository
end

def client_options
client_options_with_nil_value.reject {|key, value| value.nil? }
client_options_with_nil_value.compact
end

def client_options_with_nil_value
{
access_token: access_token,
api_endpoint: api_endpoint,
web_endpoint: web_endpoint,
web_endpoint: web_endpoint
}
end

Expand All @@ -66,11 +68,10 @@ def api_endpoint

# @note GITHUB_HOST will be deprecated on the next major version
def github_base_url
case
when ENV["GITHUB_BASE_URL"]
ENV["GITHUB_BASE_URL"]
when ENV["GITHUB_HOST"]
"https://#{ENV['GITHUB_HOST']}"
if ENV.fetch('GITHUB_BASE_URL', nil)
ENV.fetch('GITHUB_BASE_URL', nil)
elsif ENV.fetch('GITHUB_HOST', nil)
"https://#{ENV.fetch('GITHUB_HOST', nil)}"
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions lib/ruboty/github/actions/close_issue.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# frozen_string_literal: true

module Ruboty
module Github
module Actions
class CloseIssue < Base
def call
case
when !has_access_token?
if !has_access_token?
require_access_token
when has_closed_issue_number?
elsif has_closed_issue_number?
reply_already_closed
else
close
end
rescue Octokit::Unauthorized
message.reply("Failed in authentication (401)")
message.reply('Failed in authentication (401)')
rescue Octokit::NotFound
message.reply("Could not find that issue")
rescue => exception
raise exception
message.reply("Failed by #{exception.class}")
message.reply('Could not find that issue')
rescue StandardError => e
raise e
message.reply("Failed by #{e.class}")
end

private
Expand All @@ -32,7 +33,7 @@ def request
end

def has_closed_issue_number?
issue.state == "closed"
issue.state == 'closed'
end

def reply_already_closed
Expand Down
20 changes: 10 additions & 10 deletions lib/ruboty/github/actions/create_deploy_pull_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruboty
module Github
module Actions
Expand All @@ -7,28 +9,26 @@ class CreateDeployPullRequest < CreatePullRequest
def create
super

if database_schema_changed?
message.reply("@here :warning: This deployment includes some database migrations")
end
message.reply('@here :warning: This deployment includes some database migrations') if database_schema_changed?
end

# e.g. master
def to_branch
to.split(":").last
to.split(':').last
end

def body
lines = included_pull_requests.map do |pr|
"- [##{pr[:number]}](#{pr[:html_url]}): #{pr[:title]} by @#{pr[:user][:login]}"
end
lines.unshift('## Pull Requests to deploy', '')
lines.join("\n")
lines = included_pull_requests.map do |pr|
"- [##{pr[:number]}](#{pr[:html_url]}): #{pr[:title]} by @#{pr[:user][:login]}"
end
lines.unshift('## Pull Requests to deploy', '')
lines.join("\n")
end

def included_pull_requests
numbers = comparison.commits.map do |commit|
/^Merge pull request #(\d+) from/ =~ commit[:commit][:message]
$1
::Regexp.last_match(1)
end
numbers.compact.map { |number| client.pull_request(repository, number.to_i) }
end
Expand Down
10 changes: 6 additions & 4 deletions lib/ruboty/github/actions/create_issue.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruboty
module Github
module Actions
Expand All @@ -15,11 +17,11 @@ def call
def create
message.reply("Created #{issue.html_url}")
rescue Octokit::Unauthorized
message.reply("Failed in authentication (401)")
message.reply('Failed in authentication (401)')
rescue Octokit::NotFound
message.reply("Could not find that repository")
rescue => exception
message.reply("Failed by #{exception.class}")
message.reply('Could not find that repository')
rescue StandardError => e
message.reply("Failed by #{e.class}")
end

def issue
Expand Down
Loading