Skip to content

Commit a7d9e4a

Browse files
committed
Insert item to open PRs page
1 parent d03e768 commit a7d9e4a

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

app.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def user_repos
4747
end
4848

4949
def user_pulls
50-
Commands::UserPulls.new(pull_requests: pull_requests)
50+
Commands::UserPulls.new(
51+
pull_requests: pull_requests,
52+
web_host: ENV['GITHUB_HOST']
53+
)
5154
end
5255

5356
private

lib/commands/user_pulls.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# frozen_string_literal: true
22

33
require 'json'
4+
require 'ostruct'
45

56
module Commands
67
class UserPulls
78
def self.help
89
'Usage: cli user-pulls [query]'
910
end
1011

11-
def initialize(pull_requests:)
12+
def initialize(pull_requests:, web_host:)
1213
@pull_requests = pull_requests
14+
@web_host = web_host
1315
end
1416

1517
def call(args)
16-
results = filter_pulls(args.join(" "))
18+
results = filter_pulls(args.join(' '))
19+
results.unshift(open_pulls_page_item) if args.empty?
1720
serialize(results)
1821
end
1922

@@ -35,8 +38,22 @@ def user_pulls
3538
pull_requests.user_pulls
3639
end
3740

38-
def serialize(results)
39-
JSON.generate(items: results.map(&:as_alfred_item))
41+
def serialize(items)
42+
JSON.generate(items: items.map(&:as_alfred_item))
43+
end
44+
45+
def open_pulls_page_item
46+
url = "https://#{@web_host}/pulls"
47+
OpenStruct.new(
48+
as_alfred_item: {
49+
title: 'Open your Pull Requests page...',
50+
subtitle: url,
51+
arg: url,
52+
text: {
53+
copy: url
54+
}
55+
}
56+
)
4057
end
4158
end
4259
end

test/apps/cli_test.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# frozen_string_literal: true
22

33
require 'test_helper'
4+
require 'commands/help'
5+
require 'commands/search'
6+
require 'commands/user_repos'
7+
require 'commands/user_pulls'
48
load 'cli'
59

610
module Apps
@@ -15,19 +19,26 @@ def subject
1519
end
1620

1721
def help
18-
@help ||= stub('commands.help')
22+
@help ||= Commands::Help.new
1923
end
2024

2125
def search
22-
@search ||= stub('commands.search')
26+
@search ||= Commands::Search.new(
27+
repositories: stub('data_source.repositories')
28+
)
2329
end
2430

2531
def user_repos
26-
@user_repos ||= stub('commands.user_repos')
32+
@user_repos ||= Commands::UserRepos.new(
33+
repositories: stub('data_source.repositories')
34+
)
2735
end
2836

2937
def user_pulls
30-
@user_pulls ||= stub('commands.user_pulls')
38+
@user_pulls ||= Commands::UserPulls.new(
39+
pull_requests: stub('data_source.pull_requests'),
40+
web_host: 'www.example.com'
41+
)
3142
end
3243

3344
def test_empty_arguments_calls_help_command

test/lib/commands/user_pulls_test.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# frozen_string_literal: true
22

3+
require 'ostruct'
34
require 'test_helper'
45
require 'commands/user_pulls'
56
require 'entities/pull_request'
67

78
module Commands
89
class UserPullsTest < Minitest::Test
910
def subject
10-
@subject ||= UserPulls.new(pull_requests: pull_requests)
11+
@subject ||= UserPulls.new(
12+
pull_requests: pull_requests,
13+
web_host: 'www.example.com'
14+
)
1115
end
1216

1317
def pull_requests
@@ -18,6 +22,10 @@ def pull_entity
1822
@pull_entity ||= Entities::PullRequest.new
1923
end
2024

25+
def serialize_items(items)
26+
JSON.generate(items: items.map(&:as_alfred_item))
27+
end
28+
2129
def test_calls_pull_requests_data_source
2230
pull_requests.expects(:user_pulls).returns([])
2331
subject.call([])
@@ -28,7 +36,7 @@ def test_fuzzy_filters_pulls_title
2836
pull2 = Entities::PullRequest.new(title: 'hello-world')
2937
pull_requests.expects(:user_pulls).returns([pull1, pull2])
3038
actual = subject.call(%w[fobz])
31-
expected = JSON.generate(items: [pull1.as_alfred_item])
39+
expected = serialize_items([pull1])
3240
assert_equal expected, actual
3341
end
3442

@@ -37,14 +45,25 @@ def test_fuzzy_filters_pulls_html_url
3745
pull2 = Entities::PullRequest.new(html_url: 'hello-world')
3846
pull_requests.expects(:user_pulls).returns([pull1, pull2])
3947
actual = subject.call(%w[fobz])
40-
expected = JSON.generate(items: [pull1.as_alfred_item])
48+
expected = serialize_items([pull1])
4149
assert_equal expected, actual
4250
end
4351

44-
def test_returns_alfred_items_json
52+
def test_inserts_open_pulls_page_when_no_args
4553
pull_requests.expects(:user_pulls).returns([pull_entity])
4654
actual = subject.call([])
47-
expected = JSON.generate(items: [pull_entity.as_alfred_item])
55+
url = 'https://www.example.com/pulls'
56+
open_page_item = OpenStruct.new(
57+
as_alfred_item: {
58+
title: 'Open your Pull Requests page...',
59+
subtitle: url,
60+
arg: url,
61+
text: {
62+
copy: url
63+
}
64+
}
65+
)
66+
expected = serialize_items([open_page_item, pull_entity])
4867
assert_equal expected, actual
4968
end
5069
end

0 commit comments

Comments
 (0)