flight_plan_cli 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d4b7888cb0ed509d5dc14e48c856f67c929c3a68
4
- data.tar.gz: af5a01246e6979e28eda904000280d796ecd9f6b
3
+ metadata.gz: 8dfec330bff6d5d502e675e8735f53583d62b52c
4
+ data.tar.gz: aef12432b2608aa887e15520e2272384c0246fac
5
5
  SHA512:
6
- metadata.gz: 12efa16013e4409e369a63a21049edd4298c74914ea3e7bff8b90eb918b1507ac1db407f80ffbd8ac9dc72a996a1cd9a9ab35b64d4b9253e6637e1c881f01d53
7
- data.tar.gz: c6e89e95945b59f862eae6c5f63db34405a88c450cd1b66c01a86031b207972f6ad716902e21d45a57e5cf58b9e467300ce91472f1efaa44016d15c462c87052
6
+ metadata.gz: bde04240bf206d50d1f5365e08a0ba95fbc4377959bcebf914c8b77b88c2e8288a97b4f4eef0d14a8d4c60a7733a8a0d69f6aa5e354d373c77221b506299be82
7
+ data.tar.gz: 3e44b780dc1035fe0e1217dae0fe7c29fc3300d8c967b6dd469a2dda997c234f6baa510414ecda1fe6312ffa1d2cba56672ffca002f92faa5e06c93b9c820046
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
@@ -0,0 +1,61 @@
1
+ module FlightPlanCli
2
+ module Commands
3
+ class Checkout
4
+ def process(issue_no)
5
+ puts "Checking out branch for #{issue_no}"
6
+ local_branch_for(issue_no) ||
7
+ remote_branch_for(issue_no) ||
8
+ new_branch_for(issue_no)
9
+ rescue Rugged::CheckoutError => e
10
+ puts "Unable to checkout: #{e.message}".red
11
+ end
12
+
13
+ def local_branch_for(issue)
14
+ issue_branches = local_branches.map(&:name).grep(/##{issue}[^0-9]/)
15
+ return false unless issue_branches.count == 1
16
+
17
+ branch = issue_branches.first
18
+ puts "Checking out local branch '#{branch}'".green
19
+ git.checkout(branch)
20
+ true
21
+ end
22
+
23
+ def remote_branch_for(issue)
24
+ fetch
25
+ issue_branches = remote_branches.map(&:name).grep(/##{issue}[^0-9]/)
26
+ return false unless issue_branches.count == 1
27
+
28
+ remote_branch_name = issue_branches.first
29
+ branch = remote_branches.find { |rb| rb.name == remote_branch_name }
30
+ local_name = branch.name[branch.remote_name.size + 1..-1]
31
+
32
+ puts "Checking out and tracking remote branch '#{local_name}'".green
33
+ new_branch = git.branches.create(local_name, branch.name)
34
+ new_branch.upstream = branch
35
+ git.checkout(local_name)
36
+ true
37
+ end
38
+
39
+ def local_branches
40
+ @local_branches ||= git.branches.each(:local)
41
+ end
42
+
43
+ def remote_branches
44
+ @remote_branches ||= git.branches.each(:remote)
45
+ end
46
+
47
+ def git
48
+ @git ||= Rugged::Repository.new(Dir.pwd)
49
+ end
50
+
51
+ def fetch
52
+ puts 'Fetching...'.green
53
+ git.remotes.each { |remote| remote.fetch(credentials: ssh_agent) }
54
+ end
55
+
56
+ def ssh_agent
57
+ @ssh_agent ||= Rugged::Credentials::SshKeyFromAgent.new(username: 'git')
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,50 @@
1
+ module FlightPlanCli
2
+ module Commands
3
+ class Ls
4
+ include FlightPlanCli::Config
5
+
6
+ def initialize
7
+ read_config
8
+ end
9
+
10
+ def process
11
+ swimlanes = tickets_by_swimlane
12
+ default_swimlane_ids.each do |swimlane_id|
13
+ next unless swimlanes.key?(swimlane_id)
14
+
15
+ print_swimlane(swimlanes[swimlane_id])
16
+ end
17
+ rescue ApiUnauthorized
18
+ puts 'Unauthorize. Please ensure your key and secret as setup correctly'.red
19
+ rescue Errno::ECONNREFUSED, SocketError => e
20
+ # TODO: caching - low timeout (5s) then fallback to cache
21
+ puts "Network error. #{e.message}".red
22
+ end
23
+
24
+ def tickets_by_swimlane
25
+ response = client.board_tickets(board_id: board_id, repo_id: repo_id)
26
+ puts 'ok2'
27
+ raise ApiUnauthorized if response.code == 401
28
+ raise ApiNotFound if response.code == 404
29
+
30
+ swimlanes = {}
31
+ response.each do |board_ticket|
32
+ swimlane = board_ticket['swimlane']
33
+ next unless default_swimlane_ids.include? swimlane['id']
34
+
35
+ swimlanes[swimlane['id']] ||= swimlane
36
+ swimlanes[swimlane['id']]['tickets'] ||= []
37
+ swimlanes[swimlane['id']]['tickets'] << board_ticket['ticket']
38
+ end
39
+ swimlanes
40
+ end
41
+
42
+ def print_swimlane(swimlane)
43
+ puts "#{swimlane['name']} (#{swimlane['tickets'].count})".green
44
+ swimlane['tickets'].each do |ticket|
45
+ puts "├── #{ticket['remote_number'].rjust(4)} : #{ticket['remote_title']}".yellow
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,37 @@
1
+ module FlightPlanCli
2
+ module Config
3
+ YAML_FILE = '.flight_plan_cli.yml'.freeze
4
+
5
+ attr_reader :board_id, :repo_id, :default_swimlane_ids
6
+ attr_reader :api_url, :api_key, :api_secret
7
+
8
+ def read_config
9
+ @board_id = config['board_id']
10
+ @repo_id = config['repo_id']
11
+ @default_swimlane_ids = config['ls']['default_swimlane_ids']
12
+
13
+ @api_url = config['api_url']
14
+ @api_key = ENV['FLIGHT_PLAN_API_KEY']
15
+ @api_secret = ENV['FLIGHT_PLAN_API_SECRET']
16
+ end
17
+
18
+ def client
19
+ @client ||= FlightPlanCli::Api.new(
20
+ url: api_url,
21
+ key: api_key,
22
+ secret: api_secret
23
+ )
24
+ end
25
+
26
+ def config
27
+ @config ||=
28
+ begin
29
+ unless File.exist?(YAML_FILE)
30
+ puts "Could not file #{YAML_FILE} file."
31
+ exit 1
32
+ end
33
+ YAML.load_file(YAML_FILE)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -7,7 +7,7 @@ module FlightPlanCli
7
7
  class ApiNotFound < StandardError; end
8
8
 
9
9
  class Initializer < Thor
10
- YAML_FILE = '.flight_plan_cli.yml'.freeze
10
+ include FlightPlanCli::Config
11
11
 
12
12
  def initialize(*args)
13
13
  read_config
@@ -16,128 +16,14 @@ module FlightPlanCli
16
16
 
17
17
  desc 'ls', 'List open issues'
18
18
  def ls
19
- swimlanes = tickets_by_swimlane
20
- default_swimlane_ids.each do |swimlane_id|
21
- next unless swimlanes.key?(swimlane_id)
22
-
23
- print_swimlane(swimlanes[swimlane_id])
24
- end
25
- rescue ApiUnauthorized
26
- puts 'Unauthorize. Please ensure your key and secret as setup correctly'.red
27
- rescue Errno::ECONNREFUSED, SocketError => e
28
- # TODO: caching - low timeout (5s) then fallback to cache
29
- puts "Network error. #{e.message}".red
19
+ Commands::Ls.new.process
30
20
  end
31
21
 
32
22
  desc 'checkout ISSUE_NO', 'checkout a branch for ISSUE_NO'
33
23
  def checkout(issue_no)
34
- puts "Checking out branch for #{issue_no}"
35
- local_branch_for(issue_no) || remote_branch_for(issue_no)
36
- rescue Rugged::CheckoutError => e
37
- puts "Unable to checkout: #{e.message}".red
24
+ Commands::Checkout.new.process(issue_no)
38
25
  end
39
26
 
40
27
  map co: :checkout
41
-
42
- private
43
-
44
- attr_reader :board_id, :repo_id, :default_swimlane_ids
45
- attr_reader :api_url, :api_key, :api_secret
46
-
47
- def read_config
48
- @board_id = config['board_id']
49
- @repo_id = config['repo_id']
50
- @default_swimlane_ids = config['ls']['default_swimlane_ids']
51
-
52
- @api_url = config['api_url']
53
- @api_key = ENV['FLIGHT_PLAN_API_KEY']
54
- @api_secret = ENV['FLIGHT_PLAN_API_SECRET']
55
- end
56
-
57
- def local_branch_for(issue)
58
- issue_branches = local_branches.map(&:name).grep(/##{issue}[^0-9]/)
59
- return false unless issue_branches.count == 1
60
-
61
- branch = issue_branches.first
62
- puts "Checking out local branch '#{branch}'".green
63
- git.checkout(branch)
64
- true
65
- end
66
-
67
- def remote_branch_for(issue)
68
- git.fetch('origin')
69
- issue_branches = remote_branches.map(&:name).grep(/##{issue}[^0-9]/)
70
- return false unless issue_branches.count == 1
71
-
72
- remote_branch_name = issue_branches.first
73
- branch = remote_branches.find { |rb| rb.name == remote_branch_name }
74
- local_name = branch.name[branch.remote_name.size + 1..-1]
75
-
76
- puts "Checking out and tracking remote branch '#{local_name}'".green
77
- new_branch = git.branches.create(local_name, branch.name)
78
- new_branch.upstream = branch
79
- git.checkout(local_name)
80
- true
81
- end
82
-
83
- def local_branches
84
- @local_branches ||= git.branches.each(:local)
85
- end
86
-
87
- def remote_branches
88
- @remote_branches ||= git.branches.each(:remote)
89
- end
90
-
91
- def git
92
- @git ||= Rugged::Repository.new(Dir.pwd)
93
- end
94
-
95
- def fetch
96
- puts 'fetching...'
97
- git.remotes.each(&:fetch)
98
- end
99
-
100
- def print_swimlane(swimlane)
101
- puts "#{swimlane['name']} (#{swimlane['tickets'].count})".green
102
- swimlane['tickets'].each do |ticket|
103
- puts "├── #{ticket['remote_number'].rjust(4)} : #{ticket['remote_title']}".yellow
104
- end
105
- end
106
-
107
- def tickets_by_swimlane
108
- response = client.board_tickets(board_id: board_id, repo_id: repo_id)
109
- raise ApiUnauthorized if response.code == 401
110
- raise ApiNotFound if response.code == 404
111
-
112
- swimlanes = {}
113
- response.each do |board_ticket|
114
- swimlane = board_ticket['swimlane']
115
- next unless default_swimlane_ids.include? swimlane['id']
116
-
117
- swimlanes[swimlane['id']] ||= swimlane
118
- swimlanes[swimlane['id']]['tickets'] ||= []
119
- swimlanes[swimlane['id']]['tickets'] << board_ticket['ticket']
120
- end
121
- swimlanes
122
- end
123
-
124
- def config
125
- @config ||=
126
- begin
127
- unless File.exist?(YAML_FILE)
128
- puts "Could not file #{YAML_FILE} file."
129
- exit 1
130
- end
131
- YAML.load_file(YAML_FILE)
132
- end
133
- end
134
-
135
- def client
136
- @client ||= FlightPlanCli::Api.new(
137
- url: api_url,
138
- key: api_key,
139
- secret: api_secret
140
- )
141
- end
142
28
  end
143
29
  end
@@ -1,4 +1,7 @@
1
1
  require 'dotenv/load'
2
+ require 'flight_plan_cli/config'
3
+ require 'flight_plan_cli/commands/ls'
4
+ require 'flight_plan_cli/commands/checkout'
2
5
  require 'flight_plan_cli/initializer'
3
6
  require 'flight_plan_cli/version'
4
7
  require 'flight_plan_cli/api'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flight_plan_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Cleary
@@ -190,6 +190,9 @@ files:
190
190
  - bin/fp
191
191
  - lib/flight_plan_cli.rb
192
192
  - lib/flight_plan_cli/api.rb
193
+ - lib/flight_plan_cli/commands/checkout.rb
194
+ - lib/flight_plan_cli/commands/ls.rb
195
+ - lib/flight_plan_cli/config.rb
193
196
  - lib/flight_plan_cli/initializer.rb
194
197
  - lib/flight_plan_cli/version.rb
195
198
  homepage: https://github.com/jcleary/flight_plan_cli