Skip to content

Commit

Permalink
Upgrade to work with latest slack-ruby-bot.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 15, 2020
1 parent 217a4c5 commit 7d2517d
Show file tree
Hide file tree
Showing 29 changed files with 86 additions and 49 deletions.
16 changes: 14 additions & 2 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-11-15 20:50:06 UTC using RuboCop version 1.3.0.
# on 2020-11-15 22:07:57 UTC using RuboCop version 1.3.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
Expand All @@ -23,6 +23,18 @@ Layout/EmptyLinesAroundAttributeAccessor:
- 'slack-shellbot/models/current_directory_entry.rb'
- 'slack-shellbot/models/parent_directory_entry.rb'

# Offense count: 8
Lint/MixedRegexpCaptureTypes:
Exclude:
- 'slack-shellbot/commands/cat.rb'
- 'slack-shellbot/commands/cd.rb'
- 'slack-shellbot/commands/echo.rb'
- 'slack-shellbot/commands/mkdir.rb'
- 'slack-shellbot/commands/rm.rb'
- 'slack-shellbot/commands/rmdir.rb'
- 'slack-shellbot/commands/touch.rb'
- 'slack-shellbot/commands/vi.rb'

# Offense count: 2
# Cop supports --auto-correct.
Lint/NonDeterministicRequireOrder:
Expand Down Expand Up @@ -51,7 +63,7 @@ Naming/HeredocDelimiterNaming:
- 'slack-shellbot/commands/help.rb'
- 'slack-shellbot/info.rb'

# Offense count: 43
# Offense count: 19
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -13,7 +13,7 @@ gem 'rack-robotz'
gem 'rack-server-pages'
gem 'slack-ruby-bot-server'
gem 'slack-ruby-bot-server-mailchimp'
gem 'slack-ruby-bot-server-rtm', github: 'slack-ruby/slack-ruby-bot-server-rtm', branch: 'main'
gem 'slack-ruby-bot-server-rtm'
gem 'unicorn'

group :development, :test do
Expand Down
16 changes: 5 additions & 11 deletions Gemfile.lock
@@ -1,13 +1,3 @@
GIT
remote: git://github.com/slack-ruby/slack-ruby-bot-server-rtm.git
revision: b6b030c89a303bee09cb060fe1118a61703fc20b
branch: main
specs:
slack-ruby-bot-server-rtm (0.1.1)
async-websocket (~> 0.8.0)
slack-ruby-bot (>= 0.12.0)
slack-ruby-bot-server (>= 1.0.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -256,6 +246,10 @@ GEM
slack-ruby-bot-server-mailchimp (0.2.0)
mailchimp_api_v3
slack-ruby-bot-server (>= 0.10.0)
slack-ruby-bot-server-rtm (0.1.1)
async-websocket (~> 0.8.0)
slack-ruby-bot (>= 0.12.0)
slack-ruby-bot-server (>= 1.0.0)
slack-ruby-client (0.14.6)
activesupport
faraday (>= 0.9)
Expand Down Expand Up @@ -314,7 +308,7 @@ DEPENDENCIES
selenium-webdriver
slack-ruby-bot-server
slack-ruby-bot-server-mailchimp
slack-ruby-bot-server-rtm!
slack-ruby-bot-server-rtm
unicorn
vcr
webmock
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/slack-ruby-bot/hooks/message.rb
Expand Up @@ -6,14 +6,14 @@ def call(client, data)
return if client.message_to_self?(data)

Thread.current[:stdout] = []
data = Hashie::Mash.new(data)
return if data.subtype

if data.key?(:text)
data.text = Slack::Messages::Formatting.unescape(data.text)
command, redirect_to = split_redirect(data.text)
data.text = command if command
end

fs = client.owner.fs[data.channel] if data.channel
if fs && fs.program
client.logger.info "PROGRAM: #{client.owner}, #{fs}, program=#{fs.program._type}, user=#{data.user}, message_ts=#{fs.program.message_ts}"
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/cat.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Cat < SlackRubyBot::Commands::Base
match(/^cat([\s])?(?<file>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
file = Shellwords.split(match['expression']).first if match.names.include?('expression')
file = Shellwords.split(match['file']).first if match.names.include?('file')
raise 'usage: cat <file> ...' unless file

file_entry = fs.current_directory_entry.find(file)
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/cd.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Cd < SlackRubyBot::Commands::Base
match(/^cd([\s])?(?<path>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
directory = Shellwords.split(match['expression']).first if match.names.include?('expression')
directory = Shellwords.split(match['path']).first if match.names.include?('path')
raise 'usage: cd directory ...' unless directory

directory_entry = fs.cd(directory)
Expand Down
9 changes: 6 additions & 3 deletions slack-shellbot/commands/echo.rb
@@ -1,11 +1,14 @@
module SlackShellbot
module Commands
class Echo < SlackRubyBot::Commands::Base
match(/^echo$/)
match(/^echo([\s])?(?<text>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
expression = match['expression'] if match.names.include?('expression')
client.say(channel: data.channel, text: expression || "\n")
logger.info "ECHO: #{client.owner}, #{fs}, expression=#{expression}, user=#{data.user}"
text = match['text'] if match.names.include?('text')
client.say(channel: data.channel, text: text || "\n")
logger.info "ECHO: #{client.owner}, #{fs}, text=#{text}, user=#{data.user}"
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions slack-shellbot/commands/help.rb
@@ -1,6 +1,8 @@
module SlackShellbot
module Commands
class Help < SlackRubyBot::Commands::Base
command 'help'

HELP = <<~EOS.freeze
I am your friendly Shellbot, here to help.
Expand Down
2 changes: 2 additions & 0 deletions slack-shellbot/commands/ls.rb
@@ -1,6 +1,8 @@
module SlackShellbot
module Commands
class Ls < SlackRubyBot::Commands::Base
match(/^ls$/)

def self.call(client, data, _match)
fs = client.owner.fs[data.channel]
dirs = fs.current_directory_entry.map(&:to_s).join("\n")
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/mkdir.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Mkdir < SlackRubyBot::Commands::Base
match(/^mkdir([\s])?(?<path>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
directory = Shellwords.split(match['expression']).first if match.names.include?('expression')
directory = Shellwords.split(match['path']).first if match.names.include?('path')
raise 'usage: mkdir <directory> ...' unless directory

directory_entry = fs.current_directory_entry.mkdir(directory)
Expand Down
2 changes: 2 additions & 0 deletions slack-shellbot/commands/pwd.rb
@@ -1,6 +1,8 @@
module SlackShellbot
module Commands
class Pwd < SlackRubyBot::Commands::Base
match(/^pwd$/)

def self.call(client, data, _match)
fs = client.owner.fs[data.channel]
client.say(channel: data.channel, text: fs.current_directory_entry.path)
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/rm.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Rm < SlackRubyBot::Commands::Base
match(/^rm([\s])?(?<file>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
file = Shellwords.split(match['expression']).first if match.names.include?('expression')
file = Shellwords.split(match['file']).first if match.names.include?('file')
raise 'usage: rm <file> ...' unless file

file_entry = fs.current_directory_entry.rm(file)
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/rmdir.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Rmdir < SlackRubyBot::Commands::Base
match(/^rmdir([\s])?(?<path>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
directory = Shellwords.split(match['expression']).first if match.names.include?('expression')
directory = Shellwords.split(match['path']).first if match.names.include?('path')
raise 'usage: rmdir <directory> ...' unless directory

directory_entry = fs.current_directory_entry.rmdir(directory)
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/touch.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Touch < SlackRubyBot::Commands::Base
match(/^touch([\s])?(?<file>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
file = Shellwords.split(match['expression']).first if match.names.include?('expression')
file = Shellwords.split(match['file']).first if match.names.include?('file')
raise 'usage: touch <file> ...' unless file

file_entry = fs.current_directory_entry.touch(file)
Expand Down
2 changes: 2 additions & 0 deletions slack-shellbot/commands/uname.rb
@@ -1,6 +1,8 @@
module SlackShellbot
module Commands
class Uname < SlackRubyBot::Commands::Base
match(/^uname$/)

def self.call(client, data, _match)
client.say(channel: data.channel, text: 'Shell')
logger.info "UNAME: #{client.owner}, user=#{data.user}"
Expand Down
4 changes: 3 additions & 1 deletion slack-shellbot/commands/vi.rb
@@ -1,9 +1,11 @@
module SlackShellbot
module Commands
class Vi < SlackRubyBot::Commands::Base
match(/^vi([\s])?(?<path>.*)$/)

def self.call(client, data, match)
fs = client.owner.fs[data.channel]
filename = Shellwords.split(match['expression']).first if match.names.include?('expression')
filename = Shellwords.split(match['path']).first if match.names.include?('path')
program = ViProgram.create!(filename: filename, file_system: fs)
sent = client.say(channel: data.channel, text: program.to_s)
program.update_attributes!(message_ts: sent.ts) if sent && sent.ts
Expand Down
2 changes: 2 additions & 0 deletions slack-shellbot/commands/whoami.rb
@@ -1,6 +1,8 @@
module SlackShellbot
module Commands
class Whoami < SlackRubyBot::Commands::Base
match(/^whoami$/)

def self.call(client, data, _match)
client.say(channel: data.channel, text: "<@#{data.user}>")
logger.info "UNAME: #{client.owner}, user=#{data.user}"
Expand Down
2 changes: 1 addition & 1 deletion slack-shellbot/models/vi_program.rb
Expand Up @@ -16,7 +16,7 @@ def to_s
def message(client, request)
ensure_data
text, = SlackRubyBot::Commands::Base.send(:parse, client, request)
match = text.match(/^(?<bot>\S*)[\s]*(?<expression>.*)$/)
match = text.match(/(?<expression>.*)$/)
expression = match['expression'] if match.names.include?('expression')
if expression
case expression
Expand Down
6 changes: 5 additions & 1 deletion spec/slack-shellbot/commands/cat_spec.rb
Expand Up @@ -10,7 +10,11 @@
let!(:file) { Fabricate(:file_entry, data: 'hello world', parent_directory_entry: fs.root_directory_entry) }
it 'pipe into a file' do
expect(client).to receive(:say).with(channel: 'channel', text: 'hello world')
message_hook.call(client, Hashie::Mash.new(text: "#{SlackRubyBot.config.user} cat #{file.name}", channel: 'channel', team: team))
message_hook.call(client, Hashie::Mash.new(text: "cat #{file.name}", channel: 'channel', team: team))
end
it 'provides syntax' do
expect(client).to receive(:say).with(channel: 'channel', text: 'usage: cat <file> ...')
message_hook.call(client, Hashie::Mash.new(text: "cat", channel: 'channel', team: team))
end
end
end
6 changes: 3 additions & 3 deletions spec/slack-shellbot/commands/cd_spec.rb
Expand Up @@ -8,13 +8,13 @@
context 'cd' do
it 'changes directory' do
expect(client).to receive(:say).with(channel: 'channel', text: '/test')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} mkdir test", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "mkdir test", channel: 'channel'))

expect(client).to receive(:say).with(channel: 'channel', text: '/test')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} cd test", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "cd test", channel: 'channel'))

expect(client).to receive(:say).with(channel: 'channel', text: '/test')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} pwd", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "pwd", channel: 'channel'))
end
end
end
6 changes: 3 additions & 3 deletions spec/slack-shellbot/commands/echo_spec.rb
Expand Up @@ -8,17 +8,17 @@
context 'echo' do
it 'no message' do
expect(client).to receive(:say).with(channel: 'channel', text: "\n")
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} echo", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "echo", channel: 'channel'))
end
it 'message' do
expect(client).to receive(:say).with(channel: 'channel', text: 'hi')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} echo hi", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "echo hi", channel: 'channel'))
end
it 'pipe into a file' do
expect do
expect(client.web_client).to receive(:chat_postMessage).with(channel: 'channel', text: '```hi```', as_user: true)
expect(client.web_client).to receive(:chat_postMessage).with(channel: 'channel', text: '```2 byte(s) written```', as_user: true)
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} echo hi > \"text file.txt\"", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "echo hi > \"text file.txt\"", channel: 'channel'))
end.to change(FileEntry, :count).by(1)
root = team.fs['channel'].root_directory_entry
expect(root.entries.count).to eq 1
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-shellbot/commands/ls_spec.rb
Expand Up @@ -8,7 +8,7 @@
context 'ls' do
it 'returns contents of the current directory' do
expect(client).to receive(:say).with(channel: 'channel', text: ".\n..")
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} ls", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "ls", channel: 'channel'))
end
end
end
4 changes: 2 additions & 2 deletions spec/slack-shellbot/commands/mkdir_spec.rb
Expand Up @@ -8,11 +8,11 @@
context 'mkdir' do
it 'returns current directory' do
expect(client).to receive(:say).with(channel: 'channel', text: '/test')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} mkdir test", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "mkdir test", channel: 'channel'))
end
it 'makes a directory with a space' do
expect(client).to receive(:say).with(channel: 'channel', text: '/foo bar')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} mkdir \"foo bar\"", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "mkdir \"foo bar\"", channel: 'channel'))
end
end
end
2 changes: 1 addition & 1 deletion spec/slack-shellbot/commands/pwd_spec.rb
Expand Up @@ -8,7 +8,7 @@
context 'pwd' do
it 'returns current directory' do
expect(client).to receive(:say).with(channel: 'channel', text: '/')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} pwd", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "pwd", channel: 'channel'))
end
end
end
4 changes: 2 additions & 2 deletions spec/slack-shellbot/commands/rm_spec.rb
Expand Up @@ -12,7 +12,7 @@
it 'returns removed file name' do
expect do
expect(client).to receive(:say).with(channel: 'channel', text: '/test.txt')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} rm test.txt", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "rm test.txt", channel: 'channel'))
end.to change(FileEntry, :count).by(-1)
end
end
Expand All @@ -21,7 +21,7 @@
it 'returns removed file name' do
expect do
expect(client).to receive(:say).with(channel: 'channel', text: '/> test.txt')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} rm \"> test.txt\"", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "rm \"> test.txt\"", channel: 'channel'))
end.to change(FileEntry, :count).by(-1)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-shellbot/commands/touch_spec.rb
Expand Up @@ -8,7 +8,7 @@
context 'touch' do
it 'returns new file name' do
expect(client).to receive(:say).with(channel: 'channel', text: '/test.txt')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} touch test.txt", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "touch test.txt", channel: 'channel'))
end
end
end
2 changes: 1 addition & 1 deletion spec/slack-shellbot/commands/uname_spec.rb
Expand Up @@ -8,7 +8,7 @@
context 'uname' do
it 'returns Shell' do
expect(client).to receive(:say).with(channel: 'channel', text: 'Shell')
message_hook.call(client, Hashie::Mash.new(team: team, text: "#{SlackRubyBot.config.user} uname", channel: 'channel'))
message_hook.call(client, Hashie::Mash.new(team: team, text: "uname", channel: 'channel'))
end
end
end

0 comments on commit 7d2517d

Please sign in to comment.