Skip to content

Commit

Permalink
Add Config Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
anarbayramov committed Dec 5, 2023
1 parent 125ee11 commit a5f7aae
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/smart_todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module SmartTodo
autoload :Todo, "smart_todo/todo"
autoload :CommentParser, "smart_todo/comment_parser"
autoload :HttpClientBuilder, "smart_todo/http_client_builder"
autoload :GitConfigParser, "smart_todo/git_config_parser"

module Dispatchers
autoload :Base, "smart_todo/dispatchers/base"
Expand Down
3 changes: 3 additions & 0 deletions lib/smart_todo/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def define_options
opts.on("--dispatcher DISPATCHER") do |dispatcher|
@options[:dispatcher] = dispatcher
end
opts.on("--read-repository-config") do |repository_config|
@options[:repository_config] = GitConfigParser.new
end
end
end

Expand Down
10 changes: 9 additions & 1 deletion lib/smart_todo/dispatchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def slack_message(user, assignee)
<<~EOM
#{header}
You have an assigned TODO in the `#{@file}` file.
You have an assigned TODO in the `#{filepath_or_url}` file.
#{@event_message}
Here is the associated comment on your TODO:
Expand All @@ -91,6 +91,14 @@ def unexisting_user(assignee)
def existing_user
"Hello :wave:,"
end

def filepath_or_url
if @options.has_key? :repository_config
return @options[:repository_config].github_repo_url + @file
end

@file
end
end
end
end
36 changes: 36 additions & 0 deletions lib/smart_todo/git_config_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class GitConfigParser
GIT_CONFIG_PATH = ".git/config"

def initialize
@config = {}
parse(GIT_CONFIG_PATH) if File.exist?(GIT_CONFIG_PATH)
end

def parse(file_path)
current_section = nil
File.readlines(file_path).each do |line|
line = line.strip

if line.start_with?("[") && line.end_with?("]")
current_section = line[1...-1]
@config[current_section] = {}
elsif current_section && line.include?("=")
key, value = line.split("=", 2).map(&:strip)
@config[current_section][key] = value
end
end
end

def read_value(section, key)
return @config[section][key] if @config.key?(section) && @config[section].key?(key)

nil
end

# Github defined as following in repository config
# git@git.domain.com:organization/repo.git
# TODO Gitlab and Bitbucket urls are slightly different so it wont work for them as of yet
def github_repo_url
"https://" + self.read_value('remote "origin"', 'url').split('@').last.gsub(':', '/').gsub(/\.git$/, "") + "/blob/HEAD/"
end
end
27 changes: 27 additions & 0 deletions test/smart_todo/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,32 @@ def hello
end
end
end

def test_if_repository_config_read_correctly
cli = CLI.new
ruby_code = <<~EOM
# TODO(on: date('2015-03-01'), to: 'john@example.com')
# Revisit the way we say hello.
# Please.
def hello
end
EOM

mock = Minitest::Mock.new
mock.expect(:dispatch, nil)

generate_ruby_file(ruby_code) do |file|
Dispatchers::Slack.stub(:new, mock) do
assert_output(".") do
assert_equal(
0,
cli.run([file.path, "--slack_token", "123", "--fallback_channel", '#general"', "--dispatcher", "slack", "--read-repository-config"]),
)
end
end
end

assert_mock(mock)
end
end
end
28 changes: 28 additions & 0 deletions test/smart_todo/dispatchers/output_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ module Dispatchers
class OutputTest < Minitest::Test
def setup
@options = { fallback_channel: "#general", slack_token: "123" }
@test_config_content = <<~HEREDOC
[remote "origin"]
url = git@github.com:org_name/repo_name.git
[user]
name = John Doe
email = john@example.com
HEREDOC
end

def test_dispatch
Expand All @@ -27,6 +34,27 @@ def test_dispatch
assert_output(expected_text) { dispatcher.dispatch }
end

def test_github_url
File.stub(:readlines, @test_config_content.lines) do
@options[:repository_config] = GitConfigParser.new
dispatcher = Output.new("Foo", todo_node, "file.rb", @options)
expected_text = <<~HEREDOC
Hello :wave:,
You have an assigned TODO in the `https://github.com/org_name/repo_name/blob/HEAD/file.rb` file.
Foo
Here is the associated comment on your TODO:
```
```
HEREDOC

assert_output(expected_text) { dispatcher.dispatch }
end
end

private

def todo_node(*assignees)
Expand Down
51 changes: 51 additions & 0 deletions test/smart_todo/git_config_parser_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "test_helper"

class GitConfigParserTest < Minitest::Test
def setup
@test_config_content = <<~HEREDOC
[remote "origin"]
url = git@github.com:org_name/repo_name.git
[user]
name = John Doe
email = john@example.com
HEREDOC
end

def test_initialize_parses_git_config_file
File.stub(:exist?, true) do
File.stub(:readlines, @test_config_content.lines) do
assert_equal({
'remote "origin"' => { 'url' => 'git@github.com:org_name/repo_name.git' },
'user' => { 'name' => 'John Doe', 'email' => 'john@example.com' }
}, GitConfigParser.new.instance_variable_get(:@config))
end
end
end

def test_read_value_returns_value_for_given_section_and_key
File.stub(:exist?, true) do
File.stub(:readlines, @test_config_content.lines) do
assert_equal('git@github.com:org_name/repo_name.git', GitConfigParser.new.read_value('remote "origin"', 'url'))
end
end

end

def test_read_value_returns_nil_for_nonexistent_section_or_key
File.stub(:exist?, true) do
File.stub(:readlines, @test_config_content.lines) do
assert_nil(GitConfigParser.new.read_value('nonexistent_section', 'nonexistent_key'))
end
end
end

def test_github_repo_url_returns_correct_github_repository_url
File.stub(:exist?, true) do
File.stub(:readlines, @test_config_content.lines) do
assert_equal('https://github.com/org_name/repo_name/blob/HEAD/', GitConfigParser.new.github_repo_url)
end
end
end
end

0 comments on commit a5f7aae

Please sign in to comment.