From 52f273d605d56720b471bbc5690f20739f5057a8 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Fri, 10 Apr 2015 20:39:06 -0500 Subject: [PATCH 01/65] Create PRs for master and development when publishing hotfix --- .gitignore | 1 + bin/code | 2 +- lib/code/git.rb | 33 +++++++++++++++++++-------------- 3 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/bin/code b/bin/code index 0910d54..b9b34c0 100755 --- a/bin/code +++ b/bin/code @@ -44,7 +44,7 @@ command :publish, :pr do |c| base_branch = nil base_branch = Code::Branch.master if options[:master] - $git.publish(branch: Code::Branch.current, base: base_branch, message: message) + $git.publish(base: base_branch, message: message) end end diff --git a/lib/code/git.rb b/lib/code/git.rb index 7436a3d..e57a537 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -75,27 +75,32 @@ def commit(message) System.call "commit -m \"#{message}\"" end - def publish(branch:, base: nil, message: '') + def publish(base: nil, message: '') raise NotOnFeatureBranchError, 'Must be on a feature branch to publish your code' unless current_branch.feature? raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes? - - base = infer_base_branch(branch) if base.nil? - - branch.push - - System.open_in_browser pull_request(branch: branch, base: base, message: message) + push + generate_prs_for(base, message) end def push current_branch.push end - def pull_request(branch:, base:, message: '') - message = branch.message if message.empty? - command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{branch}" + def pull_request(base:, message: '') + message = current_branch.message if message.empty? + command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{current_branch}" System.exec(command).strip end + def generate_prs_for(base, message) + # if a base branch is passed, we only want to generate a pr against that branch + return System.open_in_browser pull_request(branch: current_branch, base: base, message: message) if base + + infer_base_branches.each do |base_branch| + System.open_in_browser pull_request(base: base_branch, message: message) + end + end + def compare_in_browser(branch) System.exec "hub compare #{branch}" end @@ -134,11 +139,11 @@ def master_branch Branch.master end - def infer_base_branch(branch) - if branch.hotfix? - master_branch + def infer_base_branches + if current_branch.hotfix? + [master_branch, development_branch] else - development_branch + [development_branch] end end From dc8d0bb2124089b8f387c89c4585d3bba2def7b5 Mon Sep 17 00:00:00 2001 From: begedin Date: Mon, 13 Apr 2015 13:04:28 +0200 Subject: [PATCH 02/65] Expanded "code pr" to open pr if it already exists --- Gemfile | 1 + Gemfile.lock | 10 +++++++ bin/code | 25 ++++++++++++++++- lib/code/git.rb | 2 +- lib/code/github_api.rb | 62 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 lib/code/github_api.rb diff --git a/Gemfile b/Gemfile index a80c363..00f1c9f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' ruby '2.1.1' gemspec +gem "octokit" diff --git a/Gemfile.lock b/Gemfile.lock index 47ede46..8c1a6f4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,7 @@ PATH GEM remote: https://rubygems.org/ specs: + addressable (2.3.8) aruba (0.5.3) childprocess (>= 0.3.6) cucumber (>= 1.1.1) @@ -22,6 +23,8 @@ GEM multi_json (>= 1.7.5, < 2.0) multi_test (>= 0.0.2) diff-lcs (1.2.4) + faraday (0.9.1) + multipart-post (>= 1.2, < 3) ffi (1.9.0) gherkin (2.12.1) multi_json (~> 1.3) @@ -30,6 +33,9 @@ GEM json (1.8.0) multi_json (1.8.0) multi_test (0.0.2) + multipart-post (2.0.0) + octokit (3.8.0) + sawyer (~> 0.6.0, >= 0.5.3) rake (10.1.0) rdoc (4.0.1) json (~> 1.4) @@ -41,6 +47,9 @@ GEM rspec-expectations (2.14.2) diff-lcs (>= 1.1.3, < 2.0) rspec-mocks (2.14.3) + sawyer (0.6.0) + addressable (~> 2.3.5) + faraday (~> 0.8, < 0.10) PLATFORMS ruby @@ -48,6 +57,7 @@ PLATFORMS DEPENDENCIES aruba code! + octokit rake rdoc rspec diff --git a/bin/code b/bin/code index b9b34c0..05c1d01 100755 --- a/bin/code +++ b/bin/code @@ -13,6 +13,7 @@ rescue LoadError end require 'code/git' +require 'code/github_api' require 'code/branch' require 'code/file_list' require 'code/system' @@ -23,6 +24,22 @@ program_desc 'The Coderly Toolbelt' version Code::VERSION +desc 'Authorize with GitHub' +command :authorize, :login do |c| + c.flag 'username', desc: 'GitHub username' + c.flag 'password', desc: 'GitHub password' + c.action do |global_options, options, args| + puts "global_options: #{global_options}" + puts "options: #{options}" + puts "args: #{args}" + + username = options[:username] + password = options[:password] + + Code::GitHubAPI.authorize(username: username, password: password) + end +end + desc 'Describe some switch here' switch [:s,:switch] @@ -44,7 +61,13 @@ command :publish, :pr do |c| base_branch = nil base_branch = Code::Branch.master if options[:master] - $git.publish(base: base_branch, message: message) + github_api = Code::GitHubAPI.new + + if (github_api.current_branch_pr?) + Code::System.open_in_browser(github_api.current_branch_pr_url) + else + $git.publish(base: base_branch, message: message) + end end end diff --git a/lib/code/git.rb b/lib/code/git.rb index e57a537..6f7aa1c 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -95,7 +95,7 @@ def pull_request(base:, message: '') def generate_prs_for(base, message) # if a base branch is passed, we only want to generate a pr against that branch return System.open_in_browser pull_request(branch: current_branch, base: base, message: message) if base - + infer_base_branches.each do |base_branch| System.open_in_browser pull_request(base: base_branch, message: message) end diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb new file mode 100644 index 0000000..e14ab4f --- /dev/null +++ b/lib/code/github_api.rb @@ -0,0 +1,62 @@ +require "octokit" + +module Code + + class GitHubAPI + + AUTHORIZATION_NOTE = "Code gem authorization token" + + def self.authorization_token=(token) + System.call("config --global oauth.token #{token}") + end + + def self.octokit_client_instance_from_basic_auth(username:, password:) + client = Octokit::Client.new login: username, password: password + end + + def self.authorize(username:, password:) + client = self.octokit_client_instance_from_basic_auth(username: username, password: password) + + authorization = client.authorizations.detect { |auth| auth[:note] == AUTHORIZATION_NOTE} + if authorization + self.authorization_token = authorization[:token] + else + self.authorization_token = client.create_authorization(scopes: ["repo"], note: AUTHORIZATION_NOTE)[:token] + end + end + + attr_accessor :origin_url + attr_accessor :authorization_token + + def initialize + @origin_url = System.call("config --get remote.origin.url") + @authorization_token = System.call('config --get oauth.token') + end + + def current_branch_pr_url + client = octokit_client_instance_from_token + client.pull_requests(current_repo, head: "#{current_organization}:#{Branch.current}")[0][:html_url] + end + + def current_branch_pr? + client = self.octokit_client_instance_from_token + client.pull_requests(current_repo, head: "#{current_organization}:#{Branch.current}").any? + end + + def current_organization + @origin_url.split('/')[-2] + end + + def current_repo_name + @origin_url.split("/")[-1].split('.')[0] + end + + def current_repo + "#{current_organization}/#{current_repo_name}" + end + + def octokit_client_instance_from_token + client = Octokit::Client.new access_token: authorization_token + end + end +end \ No newline at end of file From 1a59801c04c87b05c3a74dabb6138e96d4d0bca1 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Mon, 13 Apr 2015 11:26:06 -0500 Subject: [PATCH 03/65] refactor pr creation --- lib/code/git.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index e57a537..654f869 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -87,18 +87,27 @@ def push end def pull_request(base:, message: '') + base = development_branch unless base message = current_branch.message if message.empty? command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{current_branch}" System.exec(command).strip end def generate_prs_for(base, message) - # if a base branch is passed, we only want to generate a pr against that branch - return System.open_in_browser pull_request(branch: current_branch, base: base, message: message) if base - - infer_base_branches.each do |base_branch| - System.open_in_browser pull_request(base: base_branch, message: message) - end + if current_branch.hotfix? + generate_hotfix_prs(message) + else + generate_feature_pr(base, message) + end + end + + def generate_feature_pr(base, message) + System.open_in_browser pull_request(base: base, message: message) + end + + def generate_hotfix_prs(message) + System.open_in_browser pull_request(base: master_branch, message: message) + System.open_in_browser pull_request(base: development_branch, message: message) end def compare_in_browser(branch) @@ -139,14 +148,6 @@ def master_branch Branch.master end - def infer_base_branches - if current_branch.hotfix? - [master_branch, development_branch] - else - [development_branch] - end - end - def prune_remote_branches System.call 'remote prune origin' end From 532ce67fd55b39f8251e41f82b957090cb173e3f Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Mon, 13 Apr 2015 17:07:17 -0500 Subject: [PATCH 04/65] replace generate with create --- lib/code/git.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index 654f869..fa5543e 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -79,7 +79,7 @@ def publish(base: nil, message: '') raise NotOnFeatureBranchError, 'Must be on a feature branch to publish your code' unless current_branch.feature? raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes? push - generate_prs_for(base, message) + create_prs_for(base, message) end def push @@ -93,19 +93,19 @@ def pull_request(base:, message: '') System.exec(command).strip end - def generate_prs_for(base, message) + def create_prs_for(base, message) if current_branch.hotfix? - generate_hotfix_prs(message) + create_hotfix_prs(message) else - generate_feature_pr(base, message) + create_feature_pr(base, message) end end - def generate_feature_pr(base, message) + def create_feature_pr(base, message) System.open_in_browser pull_request(base: base, message: message) end - def generate_hotfix_prs(message) + def create_hotfix_prs(message) System.open_in_browser pull_request(base: master_branch, message: message) System.open_in_browser pull_request(base: development_branch, message: message) end From 5658225d38eb8d127ae0da51f7ac9c6ecee2ca8f Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 14 Apr 2015 08:47:19 +0200 Subject: [PATCH 05/65] Clean up debug output --- bin/code | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bin/code b/bin/code index 05c1d01..eb2dc80 100755 --- a/bin/code +++ b/bin/code @@ -29,10 +29,6 @@ command :authorize, :login do |c| c.flag 'username', desc: 'GitHub username' c.flag 'password', desc: 'GitHub password' c.action do |global_options, options, args| - puts "global_options: #{global_options}" - puts "options: #{options}" - puts "args: #{args}" - username = options[:username] password = options[:password] From e17b45fd231c775366449b199ca6b76d5423969d Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 14 Apr 2015 08:48:23 +0200 Subject: [PATCH 06/65] Add rudimentary os-based check for proper open command --- lib/code/system.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/code/system.rb b/lib/code/system.rb index f107e5d..61720dd 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -20,7 +20,13 @@ def open_in_browser(url) end def open(item) - `open #{item}` + if (/darwin/ =~ RUBY_PLATFORM) != nil + command = "open" + elsif (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) == nil + command ="xdg-open" + end + + `#{command} #{item}` end def exec(script) From 28c7579c0a733d7017c449bb66cd4dfa290633d8 Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 14 Apr 2015 08:49:40 +0200 Subject: [PATCH 07/65] Added error handling for no access token --- lib/code/github_api.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index e14ab4f..8f310fd 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -4,6 +4,8 @@ module Code class GitHubAPI + NoAuthTokenError = Class.new(StandardError) + AUTHORIZATION_NOTE = "Code gem authorization token" def self.authorization_token=(token) @@ -30,17 +32,22 @@ def self.authorize(username:, password:) def initialize @origin_url = System.call("config --get remote.origin.url") - @authorization_token = System.call('config --get oauth.token') + + begin + @authorization_token = System.call('config --get oauth.token') + rescue + raise NoAuthTokenError, "Couldn't retrieve an authorization token. Make sure you authorize through 'code authorize --username {username} --password {password}' first" + end end def current_branch_pr_url client = octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{Branch.current}")[0][:html_url] + client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}")[0][:html_url] end def current_branch_pr? client = self.octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{Branch.current}").any? + client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}").any? end def current_organization @@ -55,6 +62,10 @@ def current_repo "#{current_organization}/#{current_repo_name}" end + def current_branch + Branch.current + end + def octokit_client_instance_from_token client = Octokit::Client.new access_token: authorization_token end From 65ba8966724cbd346826ee05d7769e3acb4bbb48 Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 14 Apr 2015 09:41:07 +0200 Subject: [PATCH 08/65] Refactoring --- bin/code | 2 +- lib/code/github_api.rb | 57 +++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/bin/code b/bin/code index eb2dc80..65c5ef7 100755 --- a/bin/code +++ b/bin/code @@ -32,7 +32,7 @@ command :authorize, :login do |c| username = options[:username] password = options[:password] - Code::GitHubAPI.authorize(username: username, password: password) + Code::GitHubAPI.new.authorize(username: username, password: password) end end diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 8f310fd..5afad15 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -8,36 +8,19 @@ class GitHubAPI AUTHORIZATION_NOTE = "Code gem authorization token" - def self.authorization_token=(token) - System.call("config --global oauth.token #{token}") - end - - def self.octokit_client_instance_from_basic_auth(username:, password:) - client = Octokit::Client.new login: username, password: password - end - - def self.authorize(username:, password:) + def authorize(username:, password:) client = self.octokit_client_instance_from_basic_auth(username: username, password: password) - authorization = client.authorizations.detect { |auth| auth[:note] == AUTHORIZATION_NOTE} + authorization = client.authorizations.detect { |auth| auth[:note] == AUTHORIZATION_NOTE } + if authorization - self.authorization_token = authorization[:token] + token = authorization[:token] else - self.authorization_token = client.create_authorization(scopes: ["repo"], note: AUTHORIZATION_NOTE)[:token] + token = client.create_authorization(scopes: ["repo"], note: AUTHORIZATION_NOTE)[:token] end - end - - attr_accessor :origin_url - attr_accessor :authorization_token - - def initialize - @origin_url = System.call("config --get remote.origin.url") - begin - @authorization_token = System.call('config --get oauth.token') - rescue - raise NoAuthTokenError, "Couldn't retrieve an authorization token. Make sure you authorize through 'code authorize --username {username} --password {password}' first" - end + puts "Successfully authorized with token #{token}" + self.authorization_token = token end def current_branch_pr_url @@ -50,12 +33,24 @@ def current_branch_pr? client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}").any? end + def octokit_client_instance_from_token + client = Octokit::Client.new access_token: authorization_token + end + + def octokit_client_instance_from_basic_auth(username:, password:) + client = Octokit::Client.new login: username, password: password + end + + def origin_url + System.result("git config --get remote.origin.url") + end + def current_organization - @origin_url.split('/')[-2] + origin_url.split('/')[-2] end def current_repo_name - @origin_url.split("/")[-1].split('.')[0] + origin_url.split("/")[-1].split('.')[0] end def current_repo @@ -66,8 +61,14 @@ def current_branch Branch.current end - def octokit_client_instance_from_token - client = Octokit::Client.new access_token: authorization_token + def authorization_token + token = System.result('git config --get oauth.token') + raise NoAuthTokenError, "Couldn't retrieve an authorization token. Make sure you authorize through 'code authorize --username {username} --password {password}' first" if System.command_failed? + token + end + + def authorization_token=(token) + System.result("git config --global oauth.token #{token}") end end end \ No newline at end of file From 54a858689ca0ac902b82edbce85cfefd09a12d79 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Tue, 14 Apr 2015 15:21:39 -0500 Subject: [PATCH 09/65] mark pr as awaiting review --- bin/code | 8 ++++++++ lib/code/github_api.rb | 28 +++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/bin/code b/bin/code index 65c5ef7..07d9bcb 100755 --- a/bin/code +++ b/bin/code @@ -74,6 +74,14 @@ command :push, :p do |c| end end +desc "mark the current branch's pr as awaiting review" +command :ready do |c| + c.action do |global_options, options, args| + github_api = Code::GitHubAPI.new + github_api.mark_current_branch_as_awaiting_review + end +end + desc "Quickly create a pull request from your changes on development." command :hotfix, :fix do |c| c.action do |global_options, options, args| diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 5afad15..b9ccbee 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -5,6 +5,8 @@ module Code class GitHubAPI NoAuthTokenError = Class.new(StandardError) + NoPRError = Class.new(StandardError) + AUTHORIZATION_NOTE = "Code gem authorization token" @@ -23,9 +25,13 @@ def authorize(username:, password:) self.authorization_token = token end - def current_branch_pr_url + def current_branch_pr client = octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}")[0][:html_url] + client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}")[0] + end + + def current_branch_pr_url + current_branch_pr[:html_url] end def current_branch_pr? @@ -33,6 +39,12 @@ def current_branch_pr? client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}").any? end + def mark_current_branch_as_awaiting_review + raise NoPRError, "There's no PR for the current branch" unless current_branch_pr? + # all PRs are issues, so we user the issue number. + add_label_to_issue(current_issue_number, "awaiting review") + end + def octokit_client_instance_from_token client = Octokit::Client.new access_token: authorization_token end @@ -41,12 +53,14 @@ def octokit_client_instance_from_basic_auth(username:, password:) client = Octokit::Client.new login: username, password: password end + private + def origin_url System.result("git config --get remote.origin.url") end def current_organization - origin_url.split('/')[-2] + origin_url.split('/')[-2].split(':')[1] end def current_repo_name @@ -61,6 +75,14 @@ def current_branch Branch.current end + def current_issue_number + current_branch_pr.number + end + + def add_label_to_issue(number, label) + octokit_client_instance_from_token.add_labels_to_an_issue(current_repo, number, [label]) + end + def authorization_token token = System.result('git config --get oauth.token') raise NoAuthTokenError, "Couldn't retrieve an authorization token. Make sure you authorize through 'code authorize --username {username} --password {password}' first" if System.command_failed? From 4813a7259c8d9510e4dae93dfe4304f2be87a960 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Tue, 14 Apr 2015 15:44:54 -0500 Subject: [PATCH 10/65] mark prs as hotfix --- bin/code | 2 +- lib/code/git.rb | 7 +++++++ lib/code/github_api.rb | 19 ++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/bin/code b/bin/code index 07d9bcb..ff5a2aa 100755 --- a/bin/code +++ b/bin/code @@ -78,7 +78,7 @@ desc "mark the current branch's pr as awaiting review" command :ready do |c| c.action do |global_options, options, args| github_api = Code::GitHubAPI.new - github_api.mark_current_branch_as_awaiting_review + github_api.mark_current_pr_as_awaiting_review end end diff --git a/lib/code/git.rb b/lib/code/git.rb index fa5543e..61b5964 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -2,6 +2,7 @@ require 'code/branch' require 'code/system' +require 'code/github_api' module Code @@ -80,6 +81,7 @@ def publish(base: nil, message: '') raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes? push create_prs_for(base, message) + end def push @@ -108,6 +110,7 @@ def create_feature_pr(base, message) def create_hotfix_prs(message) System.open_in_browser pull_request(base: master_branch, message: message) System.open_in_browser pull_request(base: development_branch, message: message) + github_api.mark_current_prs_as_hotfix end def compare_in_browser(branch) @@ -164,6 +167,10 @@ def repo_url(name) System.result("git ls-remote --get-url #{name}") end + def github_api + @github_api ||= GitHubAPI.new + end + end end diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index b9ccbee..aa48baa 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -25,9 +25,13 @@ def authorize(username:, password:) self.authorization_token = token end - def current_branch_pr + def current_branch_prs client = octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}")[0] + client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}") + end + + def current_branch_pr + current_branch_prs[0] end def current_branch_pr_url @@ -39,12 +43,21 @@ def current_branch_pr? client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}").any? end - def mark_current_branch_as_awaiting_review + def mark_current_pr_as_awaiting_review raise NoPRError, "There's no PR for the current branch" unless current_branch_pr? # all PRs are issues, so we user the issue number. add_label_to_issue(current_issue_number, "awaiting review") end + def mark_current_prs_as_hotfix + raise NoPRError, "There's no PR for the current branch" unless current_branch_pr? + + current_branch_prs.each do |pr| + add_label_to_issue(pr.number, "hotfix") + end + end + + def octokit_client_instance_from_token client = Octokit::Client.new access_token: authorization_token end From 6a39c90bc13150628e13679c5a793a72f8ce19d1 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Tue, 14 Apr 2015 16:35:36 -0500 Subject: [PATCH 11/65] open search interface --- bin/code | 7 +++++++ lib/code/git.rb | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/bin/code b/bin/code index b9b34c0..3581607 100755 --- a/bin/code +++ b/bin/code @@ -84,6 +84,13 @@ command :switch, :s do |c| end end +desc "Opens the repository search interface on github" +command :search do |c| + c.action do |global_options, options, args| + $git.search + end +end + desc 'Deploy to production' command :production do |c| c.action do |global_options, options, args| diff --git a/lib/code/git.rb b/lib/code/git.rb index fa5543e..894131a 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -101,6 +101,10 @@ def create_prs_for(base, message) end end + def search + System.open_in_browser "https://github.com/#{current_repo}/find/development" + end + def create_feature_pr(base, message) System.open_in_browser pull_request(base: base, message: message) end @@ -164,6 +168,22 @@ def repo_url(name) System.result("git ls-remote --get-url #{name}") end + def origin_url + System.result("git config --get remote.origin.url") + end + + def current_organization + origin_url.split('/')[-2].split(':')[1] + end + + def current_repo_name + origin_url.split("/")[-1].split('.')[0] + end + + def current_repo + "#{current_organization}/#{current_repo_name}" + end + end end From 5d0b554e4001c36e39862c681d5561deaf67c034 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Tue, 14 Apr 2015 17:32:31 -0500 Subject: [PATCH 12/65] typo --- bin/code | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/code b/bin/code index ff5a2aa..211d328 100755 --- a/bin/code +++ b/bin/code @@ -74,7 +74,7 @@ command :push, :p do |c| end end -desc "mark the current branch's pr as awaiting review" +desc "Marks the current branch's pr as awaiting review" command :ready do |c| c.action do |global_options, options, args| github_api = Code::GitHubAPI.new From 54150fa286ecdb0d44be7c0f91b70ebe151bc8de Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Wed, 15 Apr 2015 09:55:35 -0500 Subject: [PATCH 13/65] fix sloppy text --- lib/code/github_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index aa48baa..9ba7957 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -45,7 +45,7 @@ def current_branch_pr? def mark_current_pr_as_awaiting_review raise NoPRError, "There's no PR for the current branch" unless current_branch_pr? - # all PRs are issues, so we user the issue number. + # All PRs are issues, so we use the issue number. add_label_to_issue(current_issue_number, "awaiting review") end From 5cabb40661ce85f9b3958774e01bc405b7e19c87 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Wed, 15 Apr 2015 18:04:43 -0500 Subject: [PATCH 14/65] big refactor to PR management --- bin/code | 12 ++++------ lib/code/branch.rb | 41 +++++++++++++++++++++++++++++++++ lib/code/git.rb | 2 +- lib/code/github_api.rb | 44 ++++++------------------------------ lib/code/pull_request.rb | 49 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 45 deletions(-) create mode 100644 lib/code/pull_request.rb diff --git a/bin/code b/bin/code index 211d328..6780c27 100755 --- a/bin/code +++ b/bin/code @@ -56,11 +56,10 @@ command :publish, :pr do |c| base_branch = nil base_branch = Code::Branch.master if options[:master] + current_branch = Code::Branch.current - github_api = Code::GitHubAPI.new - - if (github_api.current_branch_pr?) - Code::System.open_in_browser(github_api.current_branch_pr_url) + if (current_branch.has_pr?) + Code::System.open_in_browser(current_branch.pull_request_url) else $git.publish(base: base_branch, message: message) end @@ -77,9 +76,8 @@ end desc "Marks the current branch's pr as awaiting review" command :ready do |c| c.action do |global_options, options, args| - github_api = Code::GitHubAPI.new - github_api.mark_current_pr_as_awaiting_review - end + Code::Branch.current.mark_prs_as_awaiting_review + end end desc "Quickly create a pull request from your changes on development." diff --git a/lib/code/branch.rb b/lib/code/branch.rb index 8d13ab9..b5662ad 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -1,14 +1,19 @@ +require 'code/pull_request' + module Code class Branch ProtectedBranchError = Class.new(StandardError) PrivateBranchError = Class.new(StandardError) + NoPRError = Class.new(StandardError) + MASTER_BRANCH_NAME = 'master' DEVELOPMENT_BRANCH_NAME = 'development' PROTECTED_BRANCH_NAMES = %w{master development} + def self.all_names System.result('git branch').strip.lines.map { |line| line.gsub(/\s|\*/, '') } end @@ -135,6 +140,32 @@ def hotfix? !! name.match(/^hotfix-/) end + def has_pr? + !pull_request.nil? + end + + def mark_prs_as_awaiting_review + label_prs("awaiting review") + end + + def mark_prs_as_hotfix + label_prs('hotfix') + end + + def pull_request + pull_requests.first + end + + def pull_requests + @pull_request ||= PullRequest.for_branch(self) + end + + def pull_request_url + # This could be a delegate, but it seems easier to read just by + # calling it. + pull_request.url + end + def ensure_public! raise PrivateBranchError, "#{name} is a private branch" if private? end @@ -142,5 +173,15 @@ def ensure_public! attr_reader :name alias_method :to_s, :name + private + + def label_prs(label) + raise NoPRError, "There is no PR for the this branch" unless has_pr? + + pull_requests.each do |pr| + pr.add_label(label) + end + end + end end diff --git a/lib/code/git.rb b/lib/code/git.rb index 61b5964..0b37d52 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -110,7 +110,7 @@ def create_feature_pr(base, message) def create_hotfix_prs(message) System.open_in_browser pull_request(base: master_branch, message: message) System.open_in_browser pull_request(base: development_branch, message: message) - github_api.mark_current_prs_as_hotfix + current_branch.mark_prs_as_hotfix end def compare_in_browser(branch) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 9ba7957..a75b6d8 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -5,8 +5,6 @@ module Code class GitHubAPI NoAuthTokenError = Class.new(StandardError) - NoPRError = Class.new(StandardError) - AUTHORIZATION_NOTE = "Code gem authorization token" @@ -25,39 +23,11 @@ def authorize(username:, password:) self.authorization_token = token end - def current_branch_prs - client = octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}") - end - - def current_branch_pr - current_branch_prs[0] - end - - def current_branch_pr_url - current_branch_pr[:html_url] - end - - def current_branch_pr? + def prs_for_branch(branch) client = self.octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{current_branch}").any? - end - - def mark_current_pr_as_awaiting_review - raise NoPRError, "There's no PR for the current branch" unless current_branch_pr? - # All PRs are issues, so we use the issue number. - add_label_to_issue(current_issue_number, "awaiting review") + client.pull_requests(current_repo, head: "#{current_organization}:#{branch.name}") end - def mark_current_prs_as_hotfix - raise NoPRError, "There's no PR for the current branch" unless current_branch_pr? - - current_branch_prs.each do |pr| - add_label_to_issue(pr.number, "hotfix") - end - end - - def octokit_client_instance_from_token client = Octokit::Client.new access_token: authorization_token end @@ -66,6 +36,10 @@ def octokit_client_instance_from_basic_auth(username:, password:) client = Octokit::Client.new login: username, password: password end + def label_pr(pull_request, label) + add_label_to_pr(pull_request.number, label) + end + private def origin_url @@ -88,11 +62,7 @@ def current_branch Branch.current end - def current_issue_number - current_branch_pr.number - end - - def add_label_to_issue(number, label) + def add_label_to_pr(number, label) octokit_client_instance_from_token.add_labels_to_an_issue(current_repo, number, [label]) end diff --git a/lib/code/pull_request.rb b/lib/code/pull_request.rb new file mode 100644 index 0000000..852c01f --- /dev/null +++ b/lib/code/pull_request.rb @@ -0,0 +1,49 @@ +require 'code/github_api' + +module Code + class PullRequest + + def initialize(pull_request_info) + @pull_request_info = pull_request_info + end + + def self.for_branch(branch) + fetch_prs_for_branch(branch).map do |pull_request_info| + new(pull_request_info) + end + end + + def url + pull_request_info[:html_url] + end + + def number + pull_request_info[:number] + end + + def add_label(label) + github_api.label_pr(self, label) + end + + private + + attr_reader :pull_request_info + + def github_api + @github_api ||= GitHubAPI.new + end + + def self.fetch_prs_for_branch(branch) + github_api.prs_for_branch(branch) + end + + # I need a github api instance to create instances + # and also need an api instance once the api is created + # I tried to solve this problem with modules and complicated + # solutions, but I'll duplicate it for now. + def self.github_api + @github_api ||= GitHubAPI.new + end + + end +end \ No newline at end of file From 857ef67345ad4c76f96aebcccb853d9e3f0bb360 Mon Sep 17 00:00:00 2001 From: israel de la hoz Date: Wed, 15 Apr 2015 18:36:22 -0500 Subject: [PATCH 15/65] fix writing for explanation comment --- lib/code/pull_request.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/code/pull_request.rb b/lib/code/pull_request.rb index 852c01f..7bbf527 100644 --- a/lib/code/pull_request.rb +++ b/lib/code/pull_request.rb @@ -38,9 +38,9 @@ def self.fetch_prs_for_branch(branch) end # I need a github api instance to create instances - # and also need an api instance once the api is created - # I tried to solve this problem with modules and complicated - # solutions, but I'll duplicate it for now. + # and also need an api instance once the PullRequest instance is created + # I tried to solve this problem with modules by extending on the two places + # but it seems like overkilling it, I'll duplicate it for now. def self.github_api @github_api ||= GitHubAPI.new end From 0f9ca8bd9612be85142333a3163f4e747a488aa2 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 12:26:51 +0200 Subject: [PATCH 16/65] renamed `has_pr?` to `has_pull_request?` --- bin/code | 2 +- lib/code/branch.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/code b/bin/code index 6780c27..813ee57 100755 --- a/bin/code +++ b/bin/code @@ -58,7 +58,7 @@ command :publish, :pr do |c| base_branch = Code::Branch.master if options[:master] current_branch = Code::Branch.current - if (current_branch.has_pr?) + if (current_branch.has_pull_request?) Code::System.open_in_browser(current_branch.pull_request_url) else $git.publish(base: base_branch, message: message) diff --git a/lib/code/branch.rb b/lib/code/branch.rb index b5662ad..f599364 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -140,7 +140,7 @@ def hotfix? !! name.match(/^hotfix-/) end - def has_pr? + def has_pull_request? !pull_request.nil? end @@ -161,7 +161,7 @@ def pull_requests end def pull_request_url - # This could be a delegate, but it seems easier to read just by + # This could be a delegate, but it seems easier to read just by # calling it. pull_request.url end From 4875bf243b803082abef70210dda2f3aa207f094 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 12:50:07 +0200 Subject: [PATCH 17/65] Additional method rename --- lib/code/pull_request.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/code/pull_request.rb b/lib/code/pull_request.rb index 7bbf527..feec3aa 100644 --- a/lib/code/pull_request.rb +++ b/lib/code/pull_request.rb @@ -8,7 +8,7 @@ def initialize(pull_request_info) end def self.for_branch(branch) - fetch_prs_for_branch(branch).map do |pull_request_info| + fetch_pull_requests_for_branch(branch).map do |pull_request_info| new(pull_request_info) end end @@ -33,10 +33,10 @@ def github_api @github_api ||= GitHubAPI.new end - def self.fetch_prs_for_branch(branch) - github_api.prs_for_branch(branch) + def self.fetch_pull_requests_for_branch(branch) + github_api.pull_requests_for_branch(branch) end - + # I need a github api instance to create instances # and also need an api instance once the PullRequest instance is created # I tried to solve this problem with modules by extending on the two places From 50f862b23e3ebcdcf8652cfdad641689db769578 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 12:56:49 +0200 Subject: [PATCH 18/65] Applied updates from pr#9 --- lib/code/github_api.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index a75b6d8..d79b533 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -23,7 +23,7 @@ def authorize(username:, password:) self.authorization_token = token end - def prs_for_branch(branch) + def pull_requests_for_branch(branch) client = self.octokit_client_instance_from_token client.pull_requests(current_repo, head: "#{current_organization}:#{branch.name}") end @@ -37,7 +37,7 @@ def octokit_client_instance_from_basic_auth(username:, password:) end def label_pr(pull_request, label) - add_label_to_pr(pull_request.number, label) + add_label_to_pr(pull_request.number, label) end private @@ -47,11 +47,17 @@ def origin_url end def current_organization - origin_url.split('/')[-2].split(':')[1] + organization_name = origin_url.split("/")[-2] + + possible_prefix = "git@github.com:" + organization_name.sub!(possible_prefix,"") + + organization_name end def current_repo_name - origin_url.split("/")[-1].split('.')[0] + repo_name_with_extension = origin_url.split("/").last + repo_name_without_extension = repo_name_with_extension.sub(".git", "") end def current_repo From c2948d9b0ebcd6c1e67d86c079474e8710fd6965 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 13:38:18 +0200 Subject: [PATCH 19/65] Created OS class for OS detection and OS-baded commands --- lib/code/os.rb | 27 +++++++++++++++++++++++++++ lib/code/system.rb | 8 +++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 lib/code/os.rb diff --git a/lib/code/os.rb b/lib/code/os.rb new file mode 100644 index 0000000..e0b5896 --- /dev/null +++ b/lib/code/os.rb @@ -0,0 +1,27 @@ +module Code + module System + class OS + + def self.windows? + (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil + end + + def self.unix? + !self.windows? + end + + def self.osx? + (/darwin/ =~ RUBY_PLATFORM) != nil + end + + def self.linux? + self.unix? and not self.osx? + end + + def self.open_command + "open" if self.osx? + "xdg-open" if self.linux? + end + end + end +end diff --git a/lib/code/system.rb b/lib/code/system.rb index 61720dd..d307eff 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -1,3 +1,5 @@ +require 'code/os' + module Code module System extend self @@ -20,11 +22,7 @@ def open_in_browser(url) end def open(item) - if (/darwin/ =~ RUBY_PLATFORM) != nil - command = "open" - elsif (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) == nil - command ="xdg-open" - end + command = OS.open_command `#{command} #{item}` end From fc36bf275ad47b1581cbd9cff375dc59d16dc57f Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 14:49:49 +0200 Subject: [PATCH 20/65] Refactored repository property parsing. Added specs --- lib/code/github_api.rb | 29 +++++++++--------------- lib/code/repository.rb | 39 ++++++++++++++++++++++++++++++++ spec/code/repository_spec.rb | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 lib/code/repository.rb create mode 100644 spec/code/repository_spec.rb diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index d79b533..0981939 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -1,4 +1,5 @@ require "octokit" +require "code/repository" module Code @@ -8,6 +9,10 @@ class GitHubAPI AUTHORIZATION_NOTE = "Code gem authorization token" + def initialize(repository: Repository.from_current_repository_url) + @repository = repository + end + def authorize(username:, password:) client = self.octokit_client_instance_from_basic_auth(username: username, password: password) @@ -25,7 +30,7 @@ def authorize(username:, password:) def pull_requests_for_branch(branch) client = self.octokit_client_instance_from_token - client.pull_requests(current_repo, head: "#{current_organization}:#{branch.name}") + client.pull_requests(current_repo_slug, head: "#{current_organization}:#{branch.name}") end def octokit_client_instance_from_token @@ -42,26 +47,12 @@ def label_pr(pull_request, label) private - def origin_url - System.result("git config --get remote.origin.url") - end - def current_organization - organization_name = origin_url.split("/")[-2] - - possible_prefix = "git@github.com:" - organization_name.sub!(possible_prefix,"") - - organization_name - end - - def current_repo_name - repo_name_with_extension = origin_url.split("/").last - repo_name_without_extension = repo_name_with_extension.sub(".git", "") + @repository.organization end - def current_repo - "#{current_organization}/#{current_repo_name}" + def current_repo_slug + @repository.slug end def current_branch @@ -69,7 +60,7 @@ def current_branch end def add_label_to_pr(number, label) - octokit_client_instance_from_token.add_labels_to_an_issue(current_repo, number, [label]) + octokit_client_instance_from_token.add_labels_to_an_issue(current_repo_slug, number, [label]) end def authorization_token diff --git a/lib/code/repository.rb b/lib/code/repository.rb new file mode 100644 index 0000000..92e21bc --- /dev/null +++ b/lib/code/repository.rb @@ -0,0 +1,39 @@ +module Code + + class Repository + + def self.current_repository_url + System.result("git config --get remote.origin.url") + end + + def self.from_current_repository_url + new(url: Repository.current_repository_url) + end + + def initialize(url:) + @url = url + end + + def organization + organization_name = @url.split("/")[-2] + + possible_prefix = "git@github.com:" + organization_name.sub!(possible_prefix,"") + + organization_name + end + + def name + repo_name_with_extension = @url.split("/").last + repo_name_without_extension = repo_name_with_extension.sub(".git", "") + + repo_name_without_extension + end + + def slug + "#{organization}/#{name}" + end + + end + +end \ No newline at end of file diff --git a/spec/code/repository_spec.rb b/spec/code/repository_spec.rb new file mode 100644 index 0000000..87598e9 --- /dev/null +++ b/spec/code/repository_spec.rb @@ -0,0 +1,43 @@ +require 'code/repository' + +module Code + describe Repository do + + describe '#organization' do + it "works with 'https://' format repository urls" do + repo = Repository.new(url: "https:/github.com/test_org/test_name.git") + expect(repo.organization).to eq "test_org" + end + + it "works with 'git@github.com:' format repository urls" do + repo = Repository.new(url: "git@github.com:test_org/test_name.git") + expect(repo.organization).to eq "test_org" + end + end + + describe '#name' do + it "works with 'https://' format repository urls" do + repo = Repository.new(url: "https:/github.com/test_org/test_name.git") + expect(repo.name).to eq "test_name" + end + + it "works with 'git@github.com:' format repository urls" do + repo = Repository.new(url: "git@github.com:test_org/test_name.git") + expect(repo.name).to eq "test_name" + end + end + + describe '#slug' do + it "works with 'https://' format repository urls" do + repo = Repository.new(url: "https:/github.com/test_org/test_name.git") + expect(repo.slug).to eq "test_org/test_name" + end + + it "works with 'git@github.com:' format repository urls" do + repo = Repository.new(url: "git@github.com:test_org/test_name.git") + expect(repo.slug).to eq "test_org/test_name" + end + end + + end +end \ No newline at end of file From 663e842dd4e54c0be34fb9819e9b6f57b8f5619d Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 15:02:04 +0200 Subject: [PATCH 21/65] Add specs for file system path --- spec/code/repository_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/code/repository_spec.rb b/spec/code/repository_spec.rb index 87598e9..457ea42 100644 --- a/spec/code/repository_spec.rb +++ b/spec/code/repository_spec.rb @@ -13,6 +13,11 @@ module Code repo = Repository.new(url: "git@github.com:test_org/test_name.git") expect(repo.organization).to eq "test_org" end + + it "works with file system paths" do + repo = Repository.new(url: "/home/random/path/to/git/test_org/test_name") + expect(repo.organization).to eq "test_org" + end end describe '#name' do @@ -25,6 +30,11 @@ module Code repo = Repository.new(url: "git@github.com:test_org/test_name.git") expect(repo.name).to eq "test_name" end + + it "works with file system paths" do + repo = Repository.new(url: "/home/random/path/to/git/test_org/test_name") + expect(repo.name).to eq "test_name" + end end describe '#slug' do @@ -37,6 +47,11 @@ module Code repo = Repository.new(url: "git@github.com:test_org/test_name.git") expect(repo.slug).to eq "test_org/test_name" end + + it "works with file system paths" do + repo = Repository.new(url: "/home/random/path/to/git/test_org/test_name") + expect(repo.slug).to eq "test_org/test_name" + end end end From 3916bd2b76dc1f3bc8dfa3a25dde44999af905e2 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 15:08:16 +0200 Subject: [PATCH 22/65] Renamed `Repository.from_current_url` to `Repository.current` --- lib/code/github_api.rb | 2 +- lib/code/repository.rb | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 0981939..9e10a19 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -9,7 +9,7 @@ class GitHubAPI AUTHORIZATION_NOTE = "Code gem authorization token" - def initialize(repository: Repository.from_current_repository_url) + def initialize(repository: Repository.current) @repository = repository end diff --git a/lib/code/repository.rb b/lib/code/repository.rb index 92e21bc..fa8ed5d 100644 --- a/lib/code/repository.rb +++ b/lib/code/repository.rb @@ -2,12 +2,8 @@ module Code class Repository - def self.current_repository_url - System.result("git config --get remote.origin.url") - end - - def self.from_current_repository_url - new(url: Repository.current_repository_url) + def self.current + new(url: System.result("git config --get remote.origin.url")) end def initialize(url:) From 5d087f051700dd0f5eba9860dcac4c4303571e84 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 16 Apr 2015 16:21:25 +0200 Subject: [PATCH 23/65] Refactored OS code a bit. Added specs. --- lib/code/os.rb | 32 +++++++++++++++++++---------- lib/code/system.rb | 2 +- spec/code/os_spec.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 spec/code/os_spec.rb diff --git a/lib/code/os.rb b/lib/code/os.rb index e0b5896..80b4c7e 100644 --- a/lib/code/os.rb +++ b/lib/code/os.rb @@ -2,25 +2,35 @@ module Code module System class OS - def self.windows? - (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil + def self.current + new(platform_string: RUBY_PLATFORM) end - def self.unix? - !self.windows? + def initialize(platform_string:) + @platform_string = platform_string end - def self.osx? - (/darwin/ =~ RUBY_PLATFORM) != nil + def windows? + (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ @platform_string) != nil end - def self.linux? - self.unix? and not self.osx? + def unix? + !windows? end - def self.open_command - "open" if self.osx? - "xdg-open" if self.linux? + def osx? + (/darwin/ =~ @platform_string) != nil + end + + def linux? + unix? and not osx? + end + + def open_command + command = "open" if osx? + command = "xdg-open" if linux? + + command end end end diff --git a/lib/code/system.rb b/lib/code/system.rb index d307eff..052158e 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -22,7 +22,7 @@ def open_in_browser(url) end def open(item) - command = OS.open_command + command = OS.current.open_command `#{command} #{item}` end diff --git a/spec/code/os_spec.rb b/spec/code/os_spec.rb new file mode 100644 index 0000000..6d41802 --- /dev/null +++ b/spec/code/os_spec.rb @@ -0,0 +1,49 @@ +require 'code/os' + +module Code + describe "OS" do + it "detects linux" do + platform_string ="x86_64-linux" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.linux?).to be true + end + + it "detects windows" do + platform_string="i386-mingw32" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.windows?).to be true + end + + it "detects unix" do + platform_string ="x86_64-linux" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.unix?).to be true + end + + it "detects osx" do + platform_string = "x86_64-darwin14" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.osx?).to be true + end + + describe "#open_command" do + it "returns 'open' for osx" do + platform_string ="x86_64-darwin14" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.open_command).to eq "open" + end + + it "returns 'xdg-open' for linux" do + platform_string ="x86_64-linux" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.open_command).to eq "xdg-open" + end + end + end +end \ No newline at end of file From c13c74129615e56c3834fd67185db2d72c6ff54e Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 17 Apr 2015 17:22:32 +0200 Subject: [PATCH 24/65] Moved os spec to proper module --- spec/code/os_spec.rb | 64 +++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/spec/code/os_spec.rb b/spec/code/os_spec.rb index 6d41802..7b8472c 100644 --- a/spec/code/os_spec.rb +++ b/spec/code/os_spec.rb @@ -1,48 +1,50 @@ require 'code/os' module Code - describe "OS" do - it "detects linux" do - platform_string ="x86_64-linux" - os_detector = Code::System::OS.new(platform_string: platform_string) - - expect(os_detector.linux?).to be true - end - - it "detects windows" do - platform_string="i386-mingw32" - os_detector = Code::System::OS.new(platform_string: platform_string) + module System + describe OS do + it "detects linux" do + platform_string ="x86_64-linux" + os_detector = Code::System::OS.new(platform_string: platform_string) - expect(os_detector.windows?).to be true - end + expect(os_detector.linux?).to be true + end - it "detects unix" do - platform_string ="x86_64-linux" - os_detector = Code::System::OS.new(platform_string: platform_string) + it "detects windows" do + platform_string="i386-mingw32" + os_detector = Code::System::OS.new(platform_string: platform_string) - expect(os_detector.unix?).to be true - end + expect(os_detector.windows?).to be true + end - it "detects osx" do - platform_string = "x86_64-darwin14" - os_detector = Code::System::OS.new(platform_string: platform_string) + it "detects unix" do + platform_string ="x86_64-linux" + os_detector = Code::System::OS.new(platform_string: platform_string) - expect(os_detector.osx?).to be true - end + expect(os_detector.unix?).to be true + end - describe "#open_command" do - it "returns 'open' for osx" do - platform_string ="x86_64-darwin14" + it "detects osx" do + platform_string = "x86_64-darwin14" os_detector = Code::System::OS.new(platform_string: platform_string) - expect(os_detector.open_command).to eq "open" + expect(os_detector.osx?).to be true end - it "returns 'xdg-open' for linux" do - platform_string ="x86_64-linux" - os_detector = Code::System::OS.new(platform_string: platform_string) + describe "#open_command" do + it "returns 'open' for osx" do + platform_string ="x86_64-darwin14" + os_detector = Code::System::OS.new(platform_string: platform_string) + + expect(os_detector.open_command).to eq "open" + end + + it "returns 'xdg-open' for linux" do + platform_string ="x86_64-linux" + os_detector = Code::System::OS.new(platform_string: platform_string) - expect(os_detector.open_command).to eq "xdg-open" + expect(os_detector.open_command).to eq "xdg-open" + end end end end From 931def8ba6acbbe309b2e9cbeba3fddd3a2c0a19 Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 17 Apr 2015 17:28:09 +0200 Subject: [PATCH 25/65] Changed file/folder structure to reflect module/class structure --- lib/code/system.rb | 2 +- lib/code/{ => system}/os.rb | 0 spec/code/{ => system}/os_spec.rb | 8 ++++---- 3 files changed, 5 insertions(+), 5 deletions(-) rename lib/code/{ => system}/os.rb (100%) rename spec/code/{ => system}/os_spec.rb (82%) diff --git a/lib/code/system.rb b/lib/code/system.rb index 052158e..59eed92 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -1,4 +1,4 @@ -require 'code/os' +require 'code/system/os' module Code module System diff --git a/lib/code/os.rb b/lib/code/system/os.rb similarity index 100% rename from lib/code/os.rb rename to lib/code/system/os.rb diff --git a/spec/code/os_spec.rb b/spec/code/system/os_spec.rb similarity index 82% rename from spec/code/os_spec.rb rename to spec/code/system/os_spec.rb index 7b8472c..e606ed9 100644 --- a/spec/code/os_spec.rb +++ b/spec/code/system/os_spec.rb @@ -1,25 +1,25 @@ -require 'code/os' +require 'code/system/os' module Code module System describe OS do it "detects linux" do platform_string ="x86_64-linux" - os_detector = Code::System::OS.new(platform_string: platform_string) + os_detector = OS.new(platform_string: platform_string) expect(os_detector.linux?).to be true end it "detects windows" do platform_string="i386-mingw32" - os_detector = Code::System::OS.new(platform_string: platform_string) + os_detector = OS.new(platform_string: platform_string) expect(os_detector.windows?).to be true end it "detects unix" do platform_string ="x86_64-linux" - os_detector = Code::System::OS.new(platform_string: platform_string) + os_detector = OS.new(platform_string: platform_string) expect(os_detector.unix?).to be true end From e549fd0c3e4c4af77de4ebc9d5c06e62060f51f4 Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 17 Apr 2015 17:33:35 +0200 Subject: [PATCH 26/65] Apparently, I'm blind now - removed remaining namespaces --- spec/code/system/os_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/code/system/os_spec.rb b/spec/code/system/os_spec.rb index e606ed9..644d069 100644 --- a/spec/code/system/os_spec.rb +++ b/spec/code/system/os_spec.rb @@ -34,14 +34,14 @@ module System describe "#open_command" do it "returns 'open' for osx" do platform_string ="x86_64-darwin14" - os_detector = Code::System::OS.new(platform_string: platform_string) + os_detector = OS.new(platform_string: platform_string) expect(os_detector.open_command).to eq "open" end it "returns 'xdg-open' for linux" do platform_string ="x86_64-linux" - os_detector = Code::System::OS.new(platform_string: platform_string) + os_detector = OS.new(platform_string: platform_string) expect(os_detector.open_command).to eq "xdg-open" end From 0e78891563a68f622df2722e1d79105d62191788 Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 17 Apr 2015 17:36:25 +0200 Subject: [PATCH 27/65] I missed one :( --- spec/code/system/os_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/code/system/os_spec.rb b/spec/code/system/os_spec.rb index 644d069..220e4f5 100644 --- a/spec/code/system/os_spec.rb +++ b/spec/code/system/os_spec.rb @@ -26,7 +26,7 @@ module System it "detects osx" do platform_string = "x86_64-darwin14" - os_detector = Code::System::OS.new(platform_string: platform_string) + os_detector = OS.new(platform_string: platform_string) expect(os_detector.osx?).to be true end From ce54dd769b471a57055588cd1c6bfacb3fc6827c Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 20 Jul 2015 09:33:38 +0200 Subject: [PATCH 28/65] Fix search command broken by merge --- lib/code/git.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index 9aa0f27..6826f90 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -104,7 +104,7 @@ def create_prs_for(base, message) end def search - System.open_in_browser "https://github.com/#{current_repo}/find/development" + System.open_in_browser "https://github.com/#current_repo_slug/find/development" end def create_feature_pr(base, message) @@ -175,6 +175,10 @@ def github_api @github_api ||= GitHubAPI.new end + def current_repo_slug + Repository.current.slug + end + end end From 57126835a3aae0f7d612c1b4e87a11ff5dd90617 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 20 Jul 2015 09:49:08 +0200 Subject: [PATCH 29/65] Actually fixed the search command. --- lib/code/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index 6826f90..927b894 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -104,7 +104,7 @@ def create_prs_for(base, message) end def search - System.open_in_browser "https://github.com/#current_repo_slug/find/development" + System.open_in_browser "https://github.com/#{current_repo_slug}/find/development" end def create_feature_pr(base, message) From ffac2e9a916cc39894b5cb9b2f321085eb6df38d Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 20 Jul 2015 09:49:38 +0200 Subject: [PATCH 30/65] Added spec for search command --- spec/code/git_spec.rb | 17 ++++++++++++++++- spec/support/git_extras.rb | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 170fe6c..acf6582 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -84,5 +84,20 @@ module Code end + describe 'search' do + + before do + allow(System).to receive(:open_in_browser) + end + + it 'should call System.open_in_browser with the proper url' do + expect(System).to receive(:open_in_browser).with('https://github.com/testuser/codegit/find/development') + + git = Git.new + + git.search + end + end + end -end \ No newline at end of file +end diff --git a/spec/support/git_extras.rb b/spec/support/git_extras.rb index c83eac5..01b51e2 100644 --- a/spec/support/git_extras.rb +++ b/spec/support/git_extras.rb @@ -14,6 +14,7 @@ def setup_test_repo System.exec 'touch README' System.call 'add -A' System.call 'commit -m "initial commit"' + System.call 'remote add origin https:/github.com/testuser/codegit' end end From ceab354c2be48d3884d34e9d230404e4110b2398 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 20 Jul 2015 09:53:31 +0200 Subject: [PATCH 31/65] Cleanup search spec --- spec/code/git_spec.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index acf6582..131a46a 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -92,9 +92,6 @@ module Code it 'should call System.open_in_browser with the proper url' do expect(System).to receive(:open_in_browser).with('https://github.com/testuser/codegit/find/development') - - git = Git.new - git.search end end From 43db815322bc24839c680f8fff89c7c12c6c1171 Mon Sep 17 00:00:00 2001 From: begedin Date: Mon, 20 Jul 2015 15:25:35 +0200 Subject: [PATCH 32/65] Switched login to username and password - made the switch - the following commands do the prompt: publish, ready, hotfix - specs are basic for now, will add more - after initial prompt, oauth token is stored on the machine, so user is not prompted again ToDo - Mask the password input - Find a nicer way to wrap only specified commands into the credential prompt --- bin/code | 18 ++++++------------ lib/code/github_api.rb | 35 ++++++++++++++++++++++++----------- spec/code/github_api_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 spec/code/github_api_spec.rb diff --git a/bin/code b/bin/code index 813ee57..5b9b9b5 100755 --- a/bin/code +++ b/bin/code @@ -24,18 +24,6 @@ program_desc 'The Coderly Toolbelt' version Code::VERSION -desc 'Authorize with GitHub' -command :authorize, :login do |c| - c.flag 'username', desc: 'GitHub username' - c.flag 'password', desc: 'GitHub password' - c.action do |global_options, options, args| - username = options[:username] - password = options[:password] - - Code::GitHubAPI.new.authorize(username: username, password: password) - end -end - desc 'Describe some switch here' switch [:s,:switch] @@ -52,6 +40,9 @@ desc 'Upload your feature branch to GitHub' command :publish, :pr do |c| c.switch [:master], desc: 'Make the pull request based off the master branch' c.action do |global_options, options, args| + + $gitHubAPI.ensure_authorized + message = args.join(' ') base_branch = nil @@ -76,6 +67,7 @@ end desc "Marks the current branch's pr as awaiting review" command :ready do |c| c.action do |global_options, options, args| + $gitHubAPI.ensure_authorized Code::Branch.current.mark_prs_as_awaiting_review end end @@ -83,6 +75,7 @@ end desc "Quickly create a pull request from your changes on development." command :hotfix, :fix do |c| c.action do |global_options, options, args| + $gitHubAPI.ensure_authorized hotfix_name = args.join('-') $git.hotfix hotfix_name end @@ -146,6 +139,7 @@ end pre do |global, command, options, args| $git = Code::Git.new + $gitHubAPI = Code::GitHubAPI.new true end diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 9e10a19..525f42c 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -1,28 +1,32 @@ require "octokit" require "code/repository" +require "securerandom" module Code class GitHubAPI - NoAuthTokenError = Class.new(StandardError) - - AUTHORIZATION_NOTE = "Code gem authorization token" + def authorization_note + "CoderlyCode-" + SecureRandom.uuid + end def initialize(repository: Repository.current) @repository = repository end + def ensure_authorized + if not authorized? + username = prompt 'GitHub username' + password = prompt 'GitHub password' + + authorize(username: username, password: password) + end + end + def authorize(username:, password:) client = self.octokit_client_instance_from_basic_auth(username: username, password: password) - authorization = client.authorizations.detect { |auth| auth[:note] == AUTHORIZATION_NOTE } - - if authorization - token = authorization[:token] - else - token = client.create_authorization(scopes: ["repo"], note: AUTHORIZATION_NOTE)[:token] - end + token = client.create_authorization(scopes: ["repo"], note: authorization_note)[:token] puts "Successfully authorized with token #{token}" self.authorization_token = token @@ -45,6 +49,10 @@ def label_pr(pull_request, label) add_label_to_pr(pull_request.number, label) end + def authorized? + !authorization_token.empty? + end + private def current_organization @@ -65,12 +73,17 @@ def add_label_to_pr(number, label) def authorization_token token = System.result('git config --get oauth.token') - raise NoAuthTokenError, "Couldn't retrieve an authorization token. Make sure you authorize through 'code authorize --username {username} --password {password}' first" if System.command_failed? token end def authorization_token=(token) System.result("git config --global oauth.token #{token}") end + + def prompt(prompt_text) + print prompt_text + ':' + input = gets + input.strip + end end end \ No newline at end of file diff --git a/spec/code/github_api_spec.rb b/spec/code/github_api_spec.rb new file mode 100644 index 0000000..c2d7d21 --- /dev/null +++ b/spec/code/github_api_spec.rb @@ -0,0 +1,36 @@ +require 'code/github_api' + +module Code + describe GitHubAPI do + let(:api) { GitHubAPI.new } + + describe '#ensure_authorized' do + + context 'when system is authorized' do + before do + allow(api).to receive(:authorized?).and_return(true) + end + + it 'should not call #authorize' do + expect(api).not_to receive(:authorize) + expect(api).not_to receive(:prompt) + + api.ensure_authorized + end + end + + context 'when system is not authorized' do + before do + allow(api).to receive(:authorized?).and_return(false) + end + + it 'should call #authorize' do + expect(api).to receive(:authorize) + expect(api).to receive(:prompt).twice + + api.ensure_authorized + end + end + end + end +end \ No newline at end of file From c3bae048c9ec09535b10df2dd370b32feb7dce02 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Tue, 21 Jul 2015 08:49:38 +0200 Subject: [PATCH 33/65] Added masking for password input --- lib/code/github_api.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 525f42c..2155f8c 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -1,6 +1,7 @@ require "octokit" require "code/repository" require "securerandom" +require "io/console" module Code @@ -17,7 +18,7 @@ def initialize(repository: Repository.current) def ensure_authorized if not authorized? username = prompt 'GitHub username' - password = prompt 'GitHub password' + password = prompt_password 'GitHub password' authorize(username: username, password: password) end @@ -85,5 +86,12 @@ def prompt(prompt_text) input = gets input.strip end + + def prompt_password(prompt_text) + print prompt_text + ':' + input = STDIN.noecho(&:gets) + puts + input.strip + end end -end \ No newline at end of file +end From dba591a211e379b40061af7433690e229b4ad436 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Tue, 21 Jul 2015 09:03:01 +0200 Subject: [PATCH 34/65] Moved prompt methods to system module --- lib/code/git.rb | 2 +- lib/code/github_api.rb | 21 +++++---------------- lib/code/system.rb | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index 0b37d52..18208a8 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -100,7 +100,7 @@ def create_prs_for(base, message) create_hotfix_prs(message) else create_feature_pr(base, message) - end + end end def create_feature_pr(base, message) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 2155f8c..4cdaf93 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -1,7 +1,8 @@ -require "octokit" require "code/repository" +require "code/system" + +require "octokit" require "securerandom" -require "io/console" module Code @@ -17,8 +18,8 @@ def initialize(repository: Repository.current) def ensure_authorized if not authorized? - username = prompt 'GitHub username' - password = prompt_password 'GitHub password' + username = System.prompt 'GitHub username' + password = System.prompt_hidden 'GitHub password' authorize(username: username, password: password) end @@ -81,17 +82,5 @@ def authorization_token=(token) System.result("git config --global oauth.token #{token}") end - def prompt(prompt_text) - print prompt_text + ':' - input = gets - input.strip - end - - def prompt_password(prompt_text) - print prompt_text + ':' - input = STDIN.noecho(&:gets) - puts - input.strip - end end end diff --git a/lib/code/system.rb b/lib/code/system.rb index 59eed92..580704a 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -1,3 +1,5 @@ +require "io/console" + require 'code/system/os' module Code @@ -8,9 +10,17 @@ module System COLORS = {black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, teal: 36} - def prompt(*args) - print(*args) - gets + def prompt(prompt_text) + print prompt_text + ': ' + input = gets + input.strip + end + + def prompt_hidden(prompt_text) + print prompt_text + ': ' + input = STDIN.noecho(&:gets) + puts '' + input.strip end def error(message) From 2c45a8a0360c86d3c25561d187c01a881a8a5c52 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Tue, 21 Jul 2015 09:06:19 +0200 Subject: [PATCH 35/65] Fixed specs broken by password masking --- spec/code/github_api_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/code/github_api_spec.rb b/spec/code/github_api_spec.rb index c2d7d21..ec8a4a0 100644 --- a/spec/code/github_api_spec.rb +++ b/spec/code/github_api_spec.rb @@ -13,7 +13,8 @@ module Code it 'should not call #authorize' do expect(api).not_to receive(:authorize) - expect(api).not_to receive(:prompt) + expect(System).not_to receive(:prompt) + expect(System).not_to receive(:prompt_text) api.ensure_authorized end @@ -26,11 +27,12 @@ module Code it 'should call #authorize' do expect(api).to receive(:authorize) - expect(api).to receive(:prompt).twice + expect(System).to receive(:prompt).once + expect(System).to receive(:prompt_hidden).once api.ensure_authorized end end end end -end \ No newline at end of file +end From 9efe82120ae85d100117dcd253cf81babc81f3c3 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Tue, 21 Jul 2015 10:01:11 +0200 Subject: [PATCH 36/65] Specs for System.prompt and System.prompt_hidden --- lib/code/system.rb | 12 ++++++++---- spec/code/system_spec.rb | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 spec/code/system_spec.rb diff --git a/lib/code/system.rb b/lib/code/system.rb index 580704a..c3804cb 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -11,15 +11,15 @@ module System COLORS = {black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, teal: 36} def prompt(prompt_text) - print prompt_text + ': ' + print prompt_text + ": " input = gets input.strip end def prompt_hidden(prompt_text) - print prompt_text + ': ' - input = STDIN.noecho(&:gets) - puts '' + print prompt_text + ": " + input = noecho_gets + puts "" input.strip end @@ -52,6 +52,10 @@ def puts(text) Kernel.puts text end + def noecho_gets + STDIN.noecho(&:gets) + end + def call(params) exec "git #{params}" end diff --git a/spec/code/system_spec.rb b/spec/code/system_spec.rb new file mode 100644 index 0000000..63996e1 --- /dev/null +++ b/spec/code/system_spec.rb @@ -0,0 +1,41 @@ +require "code/system" +require "io/console" + +module Code + module System + describe "#prompt" do + it "should print the prompt, then ask for and return the input" do + expect(System).to receive(:print).with("Test: ").and_return("") + expect(System).to receive(:gets).and_return("test_user") + input = System.prompt "Test" + expect(input).to eq "test_user" + end + + it "should strip special characters from the input" do + allow(System).to receive(:print).and_return("") + allow(System).to receive(:gets).and_return("test\n") + + input = System.prompt "" + expect(input).to eq "test" + end + end + + describe "#prompt_hidden" do + it "should print the prompt, ask for input without echo, print a new line and return the input" do + expect(System).to receive(:print).with("Test: ").and_return("") + expect(System).to receive(:noecho_gets).and_return("test_pass") + expect(System).to receive(:puts).and_return("") + input = System.prompt_hidden "Test" + expect(input).to eq "test_pass" + end + + it "should strip special characters from the input" do + allow(System).to receive(:print).and_return("") + allow(System).to receive(:noecho_gets).and_return("test\n") + + input = System.prompt_hidden "" + expect(input).to eq "test" + end + end + end +end From 9bf7b9bc3b4f9156119fa8746cbfc360b1487292 Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 21 Jul 2015 11:19:12 +0200 Subject: [PATCH 37/65] Removed ensure_authorized call from hotfix command, since it doesn't use the API --- bin/code | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/code b/bin/code index e9d055b..9dc501f 100755 --- a/bin/code +++ b/bin/code @@ -75,7 +75,6 @@ end desc "Quickly create a pull request from your changes on development." command :hotfix, :fix do |c| c.action do |global_options, options, args| - $gitHubAPI.ensure_authorized hotfix_name = args.join('-') $git.hotfix hotfix_name end From 003407d7b63a33b701d0c538ef8313fe3e59f862 Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 21 Jul 2015 12:23:05 +0200 Subject: [PATCH 38/65] Switched ruby version to 2.2.2 Specs pass --- Gemfile | 2 +- Gemfile.lock | 65 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/Gemfile b/Gemfile index 00f1c9f..85178e4 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' -ruby '2.1.1' +ruby '2.2.2' gemspec gem "octokit" diff --git a/Gemfile.lock b/Gemfile.lock index 8c1a6f4..7466bd5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,44 +9,52 @@ GEM remote: https://rubygems.org/ specs: addressable (2.3.8) - aruba (0.5.3) - childprocess (>= 0.3.6) - cucumber (>= 1.1.1) - rspec-expectations (>= 2.7.0) + aruba (0.8.1) + childprocess (~> 0.5.6) + contracts (~> 0.9) + cucumber (>= 1.3.19) + rspec-expectations (>= 2.99) builder (3.2.2) - childprocess (0.3.9) + childprocess (0.5.6) ffi (~> 1.0, >= 1.0.11) - cucumber (1.3.8) + contracts (0.10.1) + cucumber (2.0.2) builder (>= 2.1.2) + cucumber-core (~> 1.2.0) diff-lcs (>= 1.1.3) - gherkin (~> 2.12.1) + gherkin (~> 2.12) multi_json (>= 1.7.5, < 2.0) - multi_test (>= 0.0.2) - diff-lcs (1.2.4) + multi_test (>= 0.1.2) + cucumber-core (1.2.0) + gherkin (~> 2.12.0) + diff-lcs (1.2.5) faraday (0.9.1) multipart-post (>= 1.2, < 3) - ffi (1.9.0) - gherkin (2.12.1) + ffi (1.9.10) + gherkin (2.12.2) multi_json (~> 1.3) gli (2.8.0) - hub (1.10.6) - json (1.8.0) - multi_json (1.8.0) - multi_test (0.0.2) + hub (1.12.4) + multi_json (1.11.2) + multi_test (0.1.2) multipart-post (2.0.0) - octokit (3.8.0) + octokit (4.0.1) sawyer (~> 0.6.0, >= 0.5.3) - rake (10.1.0) - rdoc (4.0.1) - json (~> 1.4) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.5) - rspec-expectations (2.14.2) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.3) + rake (10.4.2) + rdoc (4.2.0) + rspec (3.3.0) + rspec-core (~> 3.3.0) + rspec-expectations (~> 3.3.0) + rspec-mocks (~> 3.3.0) + rspec-core (3.3.2) + rspec-support (~> 3.3.0) + rspec-expectations (3.3.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-mocks (3.3.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-support (3.3.0) sawyer (0.6.0) addressable (~> 2.3.5) faraday (~> 0.8, < 0.10) @@ -61,3 +69,6 @@ DEPENDENCIES rake rdoc rspec + +BUNDLED WITH + 1.10.5 From 4094963dd3362830ea974eaaacc72dc32d8259af Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 21 Jul 2015 12:27:46 +0200 Subject: [PATCH 39/65] Update ruby version in travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ec60a07..a97a36f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: ruby rvm: - - 2.1.1 \ No newline at end of file + - 2.2.2 \ No newline at end of file From faeba8466d8db731976a01c6beabf66db215d72e Mon Sep 17 00:00:00 2001 From: begedin Date: Tue, 21 Jul 2015 15:21:19 +0200 Subject: [PATCH 40/65] Some coverage of System and Branch --- spec/code/branch_spec.rb | 102 +++++++++++++++++++++++++++++++++++++++ spec/code/system_spec.rb | 26 ++++++++++ 2 files changed, 128 insertions(+) create mode 100644 spec/code/branch_spec.rb create mode 100644 spec/code/system_spec.rb diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb new file mode 100644 index 0000000..8de3c9d --- /dev/null +++ b/spec/code/branch_spec.rb @@ -0,0 +1,102 @@ +require "code/branch" +require "code/system" + +module Code + + + describe Branch do + + def setup_test_repo(options = {}) + + defaults = { + name: "test_repo", + origins: [], + branches: [] + } + + options = defaults.merge(options) + + repo_path = Dir.mktmpdir(options[:name]) + Dir.chdir repo_path + + System.call "init" + System.call "config user.email \"test@hotmail.com\"" + System.call "config user.name \"Chuck Norris\"" + System.exec "touch README" + System.call "add -A" + System.call "commit -m \"initial commit\"" + + options[:origins].each do |origin| + System.call "remote add #{origin.name} #{origin.url}" + end + + options[:branches].each do |branch| + System.call "branch #{branch}" + end + end + + def checkout_branch(name) + System.call("checkout #{name}") + end + + before do + allow(System).to receive(:puts) + end + + describe "self.all_names" do + it "returns names of all branches in the repo as strings" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + + branch_names = Branch.all_names + + expect(branch_names.length).to eq 3 + expect(branch_names[0]).to eq "master" + expect(branch_names[1]).to eq "test-branch-1" + expect(branch_names[2]).to eq "test-branch-2" + end + end + + describe "self.all" do + it "returns Branch instances for all branches in the repo" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + + branches = Branch.all + + expect(branches.length).to eq 3 + expect(branches[0]).to be_a(Branch) + expect(branches[1]).to be_a(Branch) + expect(branches[2]).to be_a(Branch) + expect(branches[0].name).to eq "master" + expect(branches[1].name).to eq "test-branch-1" + expect(branches[2].name).to eq "test-branch-2" + end + end + + describe "self.exists?" do + it "works" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + + expect(Branch.exists? "master").to eq true + expect(Branch.exists? "test-branch-1").to eq true + expect(Branch.exists? "test-branch-2").to eq true + expect(Branch.exists? "test-branch-3").to eq false + end + end + + describe "self.current" do + it "returns a Branch instance for the current git branch" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + + current_branch = Branch.current + expect(current_branch).to be_a Branch + expect(current_branch.name).to eq "master" + + checkout_branch "test-branch-1" + + current_branch = Branch.current + expect(current_branch).to be_a Branch + expect(current_branch.name).to eq "test-branch-1" + end + end + end +end \ No newline at end of file diff --git a/spec/code/system_spec.rb b/spec/code/system_spec.rb new file mode 100644 index 0000000..de0fb45 --- /dev/null +++ b/spec/code/system_spec.rb @@ -0,0 +1,26 @@ +require "code/system" + +module Code + module System + + describe "#open_in_browser" do + it "calls #open for a fully defined URL" do + expect(System).to receive(:open) + + System.open_in_browser("https://www.example.com") + end + + it "doesn't call #open for an incomplete URL" do + expect(System).not_to receive(:open) + + System.open_in_browser("www.example.com") + end + + it "doesn't call #open for an invalid URL" do + expect(System).not_to receive(:open) + + System.open_in_browser("000") + end + end + end +end \ No newline at end of file From 8516b693ef99e2ad8f64750d77dec92ce18e2129 Mon Sep 17 00:00:00 2001 From: begedin Date: Wed, 22 Jul 2015 08:30:10 +0200 Subject: [PATCH 41/65] Renamed $gitHubAPI to $github_api --- bin/code | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/code b/bin/code index 9dc501f..43b6d25 100755 --- a/bin/code +++ b/bin/code @@ -41,7 +41,7 @@ command :publish, :pr do |c| c.switch [:master], desc: 'Make the pull request based off the master branch' c.action do |global_options, options, args| - $gitHubAPI.ensure_authorized + $github_api.ensure_authorized message = args.join(' ') @@ -67,7 +67,7 @@ end desc "Marks the current branch's pr as awaiting review" command :ready do |c| c.action do |global_options, options, args| - $gitHubAPI.ensure_authorized + $github_api.ensure_authorized Code::Branch.current.mark_prs_as_awaiting_review end end @@ -145,7 +145,7 @@ end pre do |global, command, options, args| $git = Code::Git.new - $gitHubAPI = Code::GitHubAPI.new + $github_api = Code::GitHubAPI.new true end From b61ea59a552ce4ff6590ada8ed5adbaf4274478f Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Wed, 22 Jul 2015 10:20:29 +0200 Subject: [PATCH 42/65] Partial coverage of Branch with specs --- spec/code/branch_spec.rb | 242 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 2 deletions(-) diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index 8de3c9d..f030e79 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -72,8 +72,33 @@ def checkout_branch(name) end end + describe "self.coerce_patterns" do + it "returns an array consisting of provided patterns" do + patterns = Branch.coerce_patterns 'abc', 'def', '123' + + expect(patterns.length).to eq 3 + expect(patterns[0]).to eq 'abc' + expect(patterns[1]).to eq 'def' + expect(patterns[2]).to eq '123' + end + end + + describe "self.matching" do + it "finds the branch matching the patterns" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1"]) + + expect(Branch.matching('test', 'branch', '1').name).to eq 'test-branch-1' + end + + it "returns the first branch that matches the patterns" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + + expect(Branch.matching('test', 'branch').name).to eq 'test-branch-1' + end + end + describe "self.exists?" do - it "works" do + it "returns true for a localy known branch, false otherwise" do setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) expect(Branch.exists? "master").to eq true @@ -83,6 +108,8 @@ def checkout_branch(name) end end + #TODO: "self.merged" + describe "self.current" do it "returns a Branch instance for the current git branch" do setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) @@ -98,5 +125,216 @@ def checkout_branch(name) expect(current_branch.name).to eq "test-branch-1" end end + + describe "self.master" do + it "returns a Branch instance named 'master'" do + master_branch = Branch.master + + expect(master_branch).to be_a Branch + expect(master_branch.name).to eq 'master' + end + end + + describe "self.development" do + it "returns a Branch instance named 'development'" do + development_branch = Branch.development + + expect(development_branch).to be_a Branch + expect(development_branch.name).to eq 'development' + end + end + + describe "self.create" do + it "makes a system call to create a branch and returns a branch instance" do + expect(System).to receive(:call).with("branch test_branch") + + branch = Branch.create("test_branch") + + expect(branch).to be_a Branch + expect(branch.name).to eq "test_branch" + end + end + + describe "self.find" do + it "creates a new Branch instance with specified name, but makes no System.call" do + expect(System).not_to receive(:call) + + branch = Branch.find("test_branch") + + expect(branch).to be_a Branch + expect(branch.name).to eq "test_branch" + end + end + + describe "#==" do + it "compares by branch name" do + branch_a = Branch.find "branch-a" + branch_b = Branch.find "branch-b" + other_branch_a = Branch.find "branch-a" + + expect(branch_a == branch_b).to eq false + expect(branch_a == other_branch_a).to eq true + expect(branch_b == other_branch_a).to eq false + end + end + + describe "#matches?" do + it "returns true if the branch name matches all the patterns" do + branch = Branch.find "test-branch" + + expect(branch.matches? "test").to eq true + expect(branch.matches? "test", "branch").to eq true + expect(branch.matches? "branch").to eq true + expect(branch.matches? "test-branch").to eq true + end + + it "returns false if the branch name matches some or none of the patterns" do + branch = Branch.find "test-branch" + + expect(branch.matches? "test_branch").to eq false + expect(branch.matches? "tst").to eq false + expect(branch.matches? "tst", "test").to eq false + end + end + + describe "#exists" do + + it "calls System.result and returns the result" do + test_branch_1 = Branch.find("test-branch-1") + + expect(System).to receive(:result).with("git show-ref refs/heads/test-branch-1").and_return("something") + expect(test_branch_1.exists?).to eq true + + + expect(System).to receive(:result).with("git show-ref refs/heads/test-branch-1").and_return("") + expect(test_branch_1.exists?).to eq false + end + + it "returns true if the branch with the provided instance name exists in the git repo" do + setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + + test_branch_1 = Branch.find("test-branch-1") + test_branch_2 = Branch.find("test-branch-2") + test_branch_3 = Branch.find("test-branch-3") + + expect(test_branch_1.exists?).to eq true + expect(test_branch_2.exists?).to eq true + expect(test_branch_3.exists?).to eq false + end + end + + describe "#master?" do + it "returns true if branch name is 'master', false otherwise" do + master_branch = Branch.find("master") + expect(master_branch.master?).to eq true + + other_branch = Branch.find("other") + expect(other_branch.master?).to eq false + end + end + + describe "#development?" do + it "returns true if branch name is 'development', false otherwise" do + development_branch = Branch.find("development") + expect(development_branch.development?).to eq true + + other_branch = Branch.find("other") + expect(other_branch.development?).to eq false + end + end + + describe "#protected?" do + it "returns true if branch name is in the protected list, false otherwise" do + development_branch = Branch.find("development") + expect(development_branch.protected?).to eq true + + master_branch = Branch.find("master") + expect(master_branch.protected?).to eq true + + other_branch = Branch.find("other") + expect(other_branch.protected?).to eq false + end + end + + describe "#feature?" do + it "returns the opposite of #protected?" do + random_branch = Branch.find("random") + expect(random_branch).to receive(:protected?).and_return(true) + expect(random_branch.feature?).to eq false + expect(random_branch).to receive(:protected?).and_return(false) + expect(random_branch.feature?).to eq true + end + end + + describe "#delete!" do + it "checks if branch is allowed to be deleted" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:authorize_delete!).and_return(true) + allow(System).to receive(:call) + + random_branch.delete! + end + + it "makes a system call for 'branch -d branch-name" do + random_branch = Branch.find("random") + allow(random_branch).to receive(:authorize_delete!).and_return(true) + expect(System).to receive(:call).with("branch -d random") + + random_branch.delete! + end + + it "allows to change the deletion flag to -D instead of -d" do + random_branch = Branch.find("random") + allow(random_branch).to receive(:authorize_delete!).and_return(true) + + expect(System).to receive(:call).with("branch -D random") + + random_branch.delete! force: true + + end + + it "allows deletion of a feature branch" do + random_branch = Branch.find("random") + allow(System).to receive(:call) + expect { random_branch.delete! }.not_to raise_error + end + + it "doesn't allow deletion of a protected branch" do + master_branch = Branch.find("master") + expect { master_branch.delete! }.to raise_error Branch::ProtectedBranchError + end + end + + describe "#delete_remote!" do + it "checks if branch is allowed to be deleted" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:authorize_delete!).and_return(true) + allow(System).to receive(:call) + + random_branch.delete_remote! + end + + it "makes a system call for 'push origin :branch-name" do + random_branch = Branch.find("random") + allow(random_branch).to receive(:authorize_delete!).and_return(true) + expect(System).to receive(:call).with("push origin :random") + + random_branch.delete_remote! + end + + it "allows deletion of a feature branch" do + random_branch = Branch.find("random") + allow(System).to receive(:call) + expect { random_branch.delete_remote! }.not_to raise_error + end + + it "doesn't allow deletion of a protected branch" do + master_branch = Branch.find("master") + expect { master_branch.delete_remote! }.to raise_error Branch::ProtectedBranchError + end + end + end -end \ No newline at end of file +end From 8fe03675e8e9553647254a5eaeb2a5635470bf48 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Wed, 22 Jul 2015 10:42:17 +0200 Subject: [PATCH 43/65] Partial coverage of Branch with specs, part 2 --- spec/code/branch_spec.rb | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index f030e79..dd079d1 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -336,5 +336,99 @@ def checkout_branch(name) end end + describe "#push" do + it "checks if the branch is public before pushing it" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:ensure_public!).and_return(true) + allow(System).to receive(:call) + + random_branch.push + end + + it "makes a system call for 'push origin branch-name:branch-name'" do + random_branch = Branch.find("random") + allow(random_branch).to receive(:ensure_public!).and_return(true) + expect(System).to receive(:call).with("push origin random:random") + + random_branch.push + end + + it "allows pushing of a public branch" do + random_branch = Branch.find("random") + allow(System).to receive(:call) + expect { random_branch.push }.not_to raise_error + end + + it "doesn't allow pushing of a private branch" do + master_branch = Branch.find("random-local") + expect { master_branch.push }.to raise_error Branch::PrivateBranchError + end + end + + describe "#pull" do + it "makes a system call for 'pull origin branch-name:branch-name'" do + random_branch = Branch.find("random") + expect(System).to receive(:call).with("pull origin random:random") + random_branch.pull + end + end + + describe "#checkout" do + it "makes a system call for 'checkout branch-name'" do + random_branch = Branch.find("random") + expect(System).to receive(:call).with("checkout random") + random_branch.checkout + end + + it "returns its own instance" do + random_branch = Branch.find("random") + allow(System).to receive(:call) + expect(random_branch.checkout).to eq random_branch + end + end + + describe "#authorize_delete!" do + it "raises a ProtectedBranchError for protected branches" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:protected?).and_return(true) + expect { random_branch.authorize_delete!}.to raise_error Branch::ProtectedBranchError + end + + it "doesn't raise an error for unprotected branches" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:protected?).and_return(false) + expect {random_branch.authorize_delete!}.not_to raise_error + end + end + + describe "#message" do + it "returns the capitalized branch name, where dashes are replaced with spaces" do + random_branch = Branch.find("random") + expect(random_branch.message).to eq "Random" + random_branch = Branch.find("random-branch") + expect(random_branch.message).to eq "Random branch" + end + end + + describe "#private?" do + it "returns true if branch name ends with '-local'" do + random_branch = Branch.find("random") + expect(random_branch.private?).to eq false + random_branch = Branch.find("random-local") + expect(random_branch.private?).to eq true + end + end + + describe "#hotfix?" do + it "returns true if branch name starts with 'hotfix-'" do + random_branch = Branch.find("random") + expect(random_branch.hotfix?).to eq false + random_branch = Branch.find("hotfix-random") + expect(random_branch.hotfix?).to eq true + end + end end end From 167cd4b8541dc7c48b20b39d52d6bdf1b5de033b Mon Sep 17 00:00:00 2001 From: begedin Date: Wed, 22 Jul 2015 12:30:02 +0200 Subject: [PATCH 44/65] Full coverage of Branch with specs + bug fix --- lib/code/branch.rb | 2 +- spec/code/branch_spec.rb | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/lib/code/branch.rb b/lib/code/branch.rb index f599364..85d2d26 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -176,7 +176,7 @@ def ensure_public! private def label_prs(label) - raise NoPRError, "There is no PR for the this branch" unless has_pr? + raise NoPRError, "There is no PR for the this branch" unless has_pull_request? pull_requests.each do |pr| pr.add_label(label) diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index dd079d1..490fbb7 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -1,5 +1,6 @@ require "code/branch" require "code/system" +require "code/pull_request" module Code @@ -430,5 +431,117 @@ def checkout_branch(name) expect(random_branch.hotfix?).to eq true end end + + describe "#has_pull_request?" do + it "returns true if #pull_request is not nil, false otherwise" do + random_branch = Branch.find("random") + expect(random_branch).to receive(:pull_request).and_return("something") + expect(random_branch.has_pull_request?).to eq true + expect(random_branch).to receive(:pull_request).and_return(nil) + expect(random_branch.has_pull_request?).to eq false + end + end + + describe "#mark_prs_as_awaiting_review" do + it "calls #label_prs with 'awaiting review'" do + random_branch = Branch.find("random") + expect(random_branch).to receive(:label_prs).with("awaiting review") + random_branch.mark_prs_as_awaiting_review + end + + it "throws NoPRError if branch has no pull requests" do + random_branch = Branch.find("random") + expect(random_branch).to receive(:has_pull_request?).and_return(false) + expect { random_branch.mark_prs_as_awaiting_review }.to raise_error Branch::NoPRError + end + end + + describe "#mark_prs_as_hotfix" do + it "calls #label_prs with 'hotfix'" do + random_branch = Branch.find("random") + expect(random_branch).to receive(:label_prs).with("hotfix") + random_branch.mark_prs_as_hotfix + end + + it "throws NoPRError if branch has no pull requests" do + random_branch = Branch.find("random") + expect(random_branch).to receive(:has_pull_request?).and_return(false) + expect { random_branch.mark_prs_as_hotfix }.to raise_error Branch::NoPRError + end + end + + describe "#pull_request" do + it "returns the first item in #pull_requests" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:pull_requests).and_return(["first", "second"]) + expect(random_branch.pull_request).to eq "first" + end + + it "returns nil if #pull_requests is empty" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:pull_requests).and_return([]) + expect(random_branch.pull_request).to eq nil + end + end + + describe "#pull_requests" do + it "calls 'PullRequest.for_branch'" do + random_branch = Branch.find("random") + pull_requests = [ Code::PullRequest.new({}) ] + + expect(PullRequest).to receive(:for_branch).with(random_branch).and_return(pull_requests) + expect(random_branch.pull_requests).to eq pull_requests + end + end + + describe "#pull_request_url" do + it "returns #pull_request.url" do + random_branch = Branch.find("random") + pull_request = Code::PullRequest.new({ html_url: "example.com" }) + + expect(random_branch).to receive(:pull_request).and_return(pull_request) + expect(random_branch.pull_request_url).to eq "example.com" + end + end + + describe "#ensure_public!" do + it "raises error if branch is private" do + random_branch = Branch.find("random-local") + + expect { random_branch.ensure_public! }.to raise_error Branch::PrivateBranchError + end + + it "raises no error if branch is public" do + random_branch = Branch.find("random") + + expect { random_branch.ensure_public! }.not_to raise_error + end + end + + describe "#label_prs" do + it "rases a NoPRError if there are no PRs" do + random_branch = Branch.find("random") + + expect(random_branch).to receive(:has_pull_request?).and_return(false) + expect { random_branch.send(:label_prs, "label") }.to raise_error Branch::NoPRError + end + + it "calls PullRequest#add_label for each pull_request" do + random_branch = Branch.find("random") + + pull_request_a = Code::PullRequest.new({}) + pull_request_b = Code::PullRequest.new({}) + + expect(random_branch).to receive(:pull_requests).and_return([pull_request_a, pull_request_b]).twice + + expect(pull_request_a).to receive(:add_label).with("label") + expect(pull_request_b).to receive(:add_label).with("label") + + expect { random_branch.send(:label_prs, "label") }.not_to raise_error + + end + end end end From 0c8162d0e6d7a48d8a54c02b674e3b9ec19c2110 Mon Sep 17 00:00:00 2001 From: begedin Date: Wed, 22 Jul 2015 12:55:03 +0200 Subject: [PATCH 45/65] System coverage with specs --- spec/code/system_spec.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spec/code/system_spec.rb b/spec/code/system_spec.rb index a903b2f..37926e8 100644 --- a/spec/code/system_spec.rb +++ b/spec/code/system_spec.rb @@ -57,5 +57,40 @@ module System System.open_in_browser("000") end end + + describe "#error" do + it "calls abort with a red message" do + expect(System).to receive(:abort) + expect(System).to receive(:red).with("message") + + System.error "message" + end + end + + describe "#exec" do + it "calls #puts with a green message, then executes the command using Kernel backtick" do + expect(System).to receive(:puts) + expect(System).to receive(:green).with("message") + expect(System).to receive(:command_failed?).and_return(false) + expect(System).to receive(:`) + System.exec "message" + end + + it "raises a CommandFailedError if command has failed, with red text" do + allow(System).to receive(:puts) + allow(System).to receive(:green) + expect(System).to receive(:red) + expect(System).to receive(:command_failed?).and_return(true) + expect(System).to receive(:`) + expect { System.exec "message" }.to raise_error System::CommandFailedError + end + end + + describe "#call" do + it "calls #exec with git prepended to the command script" do + expect(System).to receive(:exec).with("git some command") + System.call "some command" + end + end end end \ No newline at end of file From e544ac5261991ba43dcedc33657d7d445554b819 Mon Sep 17 00:00:00 2001 From: begedin Date: Wed, 22 Jul 2015 13:07:00 +0200 Subject: [PATCH 46/65] Repository spec coverage --- spec/code/repository_spec.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/spec/code/repository_spec.rb b/spec/code/repository_spec.rb index 457ea42..7c8a3ca 100644 --- a/spec/code/repository_spec.rb +++ b/spec/code/repository_spec.rb @@ -1,9 +1,26 @@ require 'code/repository' +require_relative '../support/git_extras' module Code describe Repository do - describe '#organization' do + describe "self.current" do + before(:all) do + Repository.send :include, GitExtras + end + + before do + allow(System).to receive(:puts) + Repository.setup_test_repo + end + + it "returns a Repository instance from the URL of the current origin remote" do + current_repo = Repository.current + expect(current_repo.slug).to eq "testuser/codegit" + end + end + + describe "#organization" do it "works with 'https://' format repository urls" do repo = Repository.new(url: "https:/github.com/test_org/test_name.git") expect(repo.organization).to eq "test_org" @@ -20,7 +37,7 @@ module Code end end - describe '#name' do + describe "#name" do it "works with 'https://' format repository urls" do repo = Repository.new(url: "https:/github.com/test_org/test_name.git") expect(repo.name).to eq "test_name" @@ -37,7 +54,7 @@ module Code end end - describe '#slug' do + describe "#slug" do it "works with 'https://' format repository urls" do repo = Repository.new(url: "https:/github.com/test_org/test_name.git") expect(repo.slug).to eq "test_org/test_name" From d329ebd8a72d6883c21b08c50292dd539479165c Mon Sep 17 00:00:00 2001 From: begedin Date: Wed, 22 Jul 2015 13:53:48 +0200 Subject: [PATCH 47/65] Covered PullRequest with specs --- lib/code/pull_request.rb | 9 +-- spec/code/branch_spec.rb | 120 ++++++++++++++++----------------- spec/code/pull_request_spec.rb | 25 +++++++ 3 files changed, 90 insertions(+), 64 deletions(-) create mode 100644 spec/code/pull_request_spec.rb diff --git a/lib/code/pull_request.rb b/lib/code/pull_request.rb index feec3aa..1111085 100644 --- a/lib/code/pull_request.rb +++ b/lib/code/pull_request.rb @@ -3,13 +3,14 @@ module Code class PullRequest - def initialize(pull_request_info) + def initialize(pull_request_info:, github_api: GitHubAPI.new) @pull_request_info = pull_request_info + @github_api = github_api end def self.for_branch(branch) fetch_pull_requests_for_branch(branch).map do |pull_request_info| - new(pull_request_info) + new(pull_request_info: pull_request_info) end end @@ -27,12 +28,12 @@ def add_label(label) private - attr_reader :pull_request_info - def github_api @github_api ||= GitHubAPI.new end + attr_reader :pull_request_info + def self.fetch_pull_requests_for_branch(branch) github_api.pull_requests_for_branch(branch) end diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index 490fbb7..3f9f8cd 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -149,7 +149,7 @@ def checkout_branch(name) it "makes a system call to create a branch and returns a branch instance" do expect(System).to receive(:call).with("branch test_branch") - branch = Branch.create("test_branch") + branch = Branch.create "test_branch" expect(branch).to be_a Branch expect(branch.name).to eq "test_branch" @@ -160,7 +160,7 @@ def checkout_branch(name) it "creates a new Branch instance with specified name, but makes no System.call" do expect(System).not_to receive(:call) - branch = Branch.find("test_branch") + branch = Branch.find "test_branch" expect(branch).to be_a Branch expect(branch.name).to eq "test_branch" @@ -169,9 +169,9 @@ def checkout_branch(name) describe "#==" do it "compares by branch name" do - branch_a = Branch.find "branch-a" - branch_b = Branch.find "branch-b" - other_branch_a = Branch.find "branch-a" + branch_a = Branch.new "branch-a" + branch_b = Branch.new "branch-b" + other_branch_a = Branch.new "branch-a" expect(branch_a == branch_b).to eq false expect(branch_a == other_branch_a).to eq true @@ -181,7 +181,7 @@ def checkout_branch(name) describe "#matches?" do it "returns true if the branch name matches all the patterns" do - branch = Branch.find "test-branch" + branch = Branch.new "test-branch" expect(branch.matches? "test").to eq true expect(branch.matches? "test", "branch").to eq true @@ -190,7 +190,7 @@ def checkout_branch(name) end it "returns false if the branch name matches some or none of the patterns" do - branch = Branch.find "test-branch" + branch = Branch.new "test-branch" expect(branch.matches? "test_branch").to eq false expect(branch.matches? "tst").to eq false @@ -201,7 +201,7 @@ def checkout_branch(name) describe "#exists" do it "calls System.result and returns the result" do - test_branch_1 = Branch.find("test-branch-1") + test_branch_1 = Branch.new "test-branch-1" expect(System).to receive(:result).with("git show-ref refs/heads/test-branch-1").and_return("something") expect(test_branch_1.exists?).to eq true @@ -214,9 +214,9 @@ def checkout_branch(name) it "returns true if the branch with the provided instance name exists in the git repo" do setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) - test_branch_1 = Branch.find("test-branch-1") - test_branch_2 = Branch.find("test-branch-2") - test_branch_3 = Branch.find("test-branch-3") + test_branch_1 = Branch.new "test-branch-1" + test_branch_2 = Branch.new "test-branch-2" + test_branch_3 = Branch.new "test-branch-3" expect(test_branch_1.exists?).to eq true expect(test_branch_2.exists?).to eq true @@ -226,40 +226,40 @@ def checkout_branch(name) describe "#master?" do it "returns true if branch name is 'master', false otherwise" do - master_branch = Branch.find("master") + master_branch = Branch.new "master" expect(master_branch.master?).to eq true - other_branch = Branch.find("other") + other_branch = Branch.new "other" expect(other_branch.master?).to eq false end end describe "#development?" do it "returns true if branch name is 'development', false otherwise" do - development_branch = Branch.find("development") + development_branch = Branch.new "development" expect(development_branch.development?).to eq true - other_branch = Branch.find("other") + other_branch = Branch.new "other" expect(other_branch.development?).to eq false end end describe "#protected?" do it "returns true if branch name is in the protected list, false otherwise" do - development_branch = Branch.find("development") + development_branch = Branch.new "development" expect(development_branch.protected?).to eq true - master_branch = Branch.find("master") + master_branch = Branch.new "master" expect(master_branch.protected?).to eq true - other_branch = Branch.find("other") + other_branch = Branch.new "other" expect(other_branch.protected?).to eq false end end describe "#feature?" do it "returns the opposite of #protected?" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:protected?).and_return(true) expect(random_branch.feature?).to eq false expect(random_branch).to receive(:protected?).and_return(false) @@ -269,7 +269,7 @@ def checkout_branch(name) describe "#delete!" do it "checks if branch is allowed to be deleted" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:authorize_delete!).and_return(true) allow(System).to receive(:call) @@ -278,7 +278,7 @@ def checkout_branch(name) end it "makes a system call for 'branch -d branch-name" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(random_branch).to receive(:authorize_delete!).and_return(true) expect(System).to receive(:call).with("branch -d random") @@ -286,7 +286,7 @@ def checkout_branch(name) end it "allows to change the deletion flag to -D instead of -d" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(random_branch).to receive(:authorize_delete!).and_return(true) expect(System).to receive(:call).with("branch -D random") @@ -296,20 +296,20 @@ def checkout_branch(name) end it "allows deletion of a feature branch" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(System).to receive(:call) expect { random_branch.delete! }.not_to raise_error end it "doesn't allow deletion of a protected branch" do - master_branch = Branch.find("master") + master_branch = Branch.new "master" expect { master_branch.delete! }.to raise_error Branch::ProtectedBranchError end end describe "#delete_remote!" do it "checks if branch is allowed to be deleted" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:authorize_delete!).and_return(true) allow(System).to receive(:call) @@ -318,7 +318,7 @@ def checkout_branch(name) end it "makes a system call for 'push origin :branch-name" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(random_branch).to receive(:authorize_delete!).and_return(true) expect(System).to receive(:call).with("push origin :random") @@ -326,20 +326,20 @@ def checkout_branch(name) end it "allows deletion of a feature branch" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(System).to receive(:call) expect { random_branch.delete_remote! }.not_to raise_error end it "doesn't allow deletion of a protected branch" do - master_branch = Branch.find("master") + master_branch = Branch.new "master" expect { master_branch.delete_remote! }.to raise_error Branch::ProtectedBranchError end end describe "#push" do it "checks if the branch is public before pushing it" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:ensure_public!).and_return(true) allow(System).to receive(:call) @@ -348,7 +348,7 @@ def checkout_branch(name) end it "makes a system call for 'push origin branch-name:branch-name'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(random_branch).to receive(:ensure_public!).and_return(true) expect(System).to receive(:call).with("push origin random:random") @@ -356,20 +356,20 @@ def checkout_branch(name) end it "allows pushing of a public branch" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(System).to receive(:call) expect { random_branch.push }.not_to raise_error end it "doesn't allow pushing of a private branch" do - master_branch = Branch.find("random-local") + master_branch = Branch.new "random-local" expect { master_branch.push }.to raise_error Branch::PrivateBranchError end end describe "#pull" do it "makes a system call for 'pull origin branch-name:branch-name'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(System).to receive(:call).with("pull origin random:random") random_branch.pull end @@ -377,13 +377,13 @@ def checkout_branch(name) describe "#checkout" do it "makes a system call for 'checkout branch-name'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(System).to receive(:call).with("checkout random") random_branch.checkout end it "returns its own instance" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" allow(System).to receive(:call) expect(random_branch.checkout).to eq random_branch end @@ -391,14 +391,14 @@ def checkout_branch(name) describe "#authorize_delete!" do it "raises a ProtectedBranchError for protected branches" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:protected?).and_return(true) expect { random_branch.authorize_delete!}.to raise_error Branch::ProtectedBranchError end it "doesn't raise an error for unprotected branches" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:protected?).and_return(false) expect {random_branch.authorize_delete!}.not_to raise_error @@ -407,34 +407,34 @@ def checkout_branch(name) describe "#message" do it "returns the capitalized branch name, where dashes are replaced with spaces" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch.message).to eq "Random" - random_branch = Branch.find("random-branch") + random_branch = Branch.new "random-branch" expect(random_branch.message).to eq "Random branch" end end describe "#private?" do it "returns true if branch name ends with '-local'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch.private?).to eq false - random_branch = Branch.find("random-local") + random_branch = Branch.new "random-local" expect(random_branch.private?).to eq true end end describe "#hotfix?" do it "returns true if branch name starts with 'hotfix-'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch.hotfix?).to eq false - random_branch = Branch.find("hotfix-random") + random_branch = Branch.new "hotfix-random" expect(random_branch.hotfix?).to eq true end end describe "#has_pull_request?" do it "returns true if #pull_request is not nil, false otherwise" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:pull_request).and_return("something") expect(random_branch.has_pull_request?).to eq true expect(random_branch).to receive(:pull_request).and_return(nil) @@ -444,13 +444,13 @@ def checkout_branch(name) describe "#mark_prs_as_awaiting_review" do it "calls #label_prs with 'awaiting review'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:label_prs).with("awaiting review") random_branch.mark_prs_as_awaiting_review end it "throws NoPRError if branch has no pull requests" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:has_pull_request?).and_return(false) expect { random_branch.mark_prs_as_awaiting_review }.to raise_error Branch::NoPRError end @@ -458,13 +458,13 @@ def checkout_branch(name) describe "#mark_prs_as_hotfix" do it "calls #label_prs with 'hotfix'" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:label_prs).with("hotfix") random_branch.mark_prs_as_hotfix end it "throws NoPRError if branch has no pull requests" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:has_pull_request?).and_return(false) expect { random_branch.mark_prs_as_hotfix }.to raise_error Branch::NoPRError end @@ -472,14 +472,14 @@ def checkout_branch(name) describe "#pull_request" do it "returns the first item in #pull_requests" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:pull_requests).and_return(["first", "second"]) expect(random_branch.pull_request).to eq "first" end it "returns nil if #pull_requests is empty" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:pull_requests).and_return([]) expect(random_branch.pull_request).to eq nil @@ -488,8 +488,8 @@ def checkout_branch(name) describe "#pull_requests" do it "calls 'PullRequest.for_branch'" do - random_branch = Branch.find("random") - pull_requests = [ Code::PullRequest.new({}) ] + random_branch = Branch.new "random" + pull_requests = [ Code::PullRequest.new(pull_request_info: {}) ] expect(PullRequest).to receive(:for_branch).with(random_branch).and_return(pull_requests) expect(random_branch.pull_requests).to eq pull_requests @@ -498,8 +498,8 @@ def checkout_branch(name) describe "#pull_request_url" do it "returns #pull_request.url" do - random_branch = Branch.find("random") - pull_request = Code::PullRequest.new({ html_url: "example.com" }) + random_branch = Branch.new "random" + pull_request = Code::PullRequest.new(pull_request_info: { html_url: "example.com" }) expect(random_branch).to receive(:pull_request).and_return(pull_request) expect(random_branch.pull_request_url).to eq "example.com" @@ -508,13 +508,13 @@ def checkout_branch(name) describe "#ensure_public!" do it "raises error if branch is private" do - random_branch = Branch.find("random-local") + random_branch = Branch.new "random-local" expect { random_branch.ensure_public! }.to raise_error Branch::PrivateBranchError end it "raises no error if branch is public" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect { random_branch.ensure_public! }.not_to raise_error end @@ -522,17 +522,17 @@ def checkout_branch(name) describe "#label_prs" do it "rases a NoPRError if there are no PRs" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" expect(random_branch).to receive(:has_pull_request?).and_return(false) expect { random_branch.send(:label_prs, "label") }.to raise_error Branch::NoPRError end it "calls PullRequest#add_label for each pull_request" do - random_branch = Branch.find("random") + random_branch = Branch.new "random" - pull_request_a = Code::PullRequest.new({}) - pull_request_b = Code::PullRequest.new({}) + pull_request_a = Code::PullRequest.new(pull_request_info: {}) + pull_request_b = Code::PullRequest.new(pull_request_info: {}) expect(random_branch).to receive(:pull_requests).and_return([pull_request_a, pull_request_b]).twice diff --git a/spec/code/pull_request_spec.rb b/spec/code/pull_request_spec.rb new file mode 100644 index 0000000..dac489e --- /dev/null +++ b/spec/code/pull_request_spec.rb @@ -0,0 +1,25 @@ +require "code/pull_request" +require "code/branch" + +module Code + describe PullRequest do + # ToDO self.for_branch uses a GitHubAPI instance, which we can't inject + # Not sure how to test that part + + it "properly maps the url and number properties" do + pr = PullRequest.new(pull_request_info: { html_url: "example.com", number: 1 }) + expect(pr.url).to eq "example.com" + expect(pr.number).to eq 1 + end + + describe "#add_label" do + it "calls GitHubAPI#label_pr" do + api = GitHubAPI.new + pr = PullRequest.new(pull_request_info: { html_url: "example.com", number: 1 }, github_api: api) + + expect(api).to receive(:label_pr).with(pr, "label") + pr.add_label("label") + end + end + end +end From 5b841e7a5c7a0fb89bf175696fa7d88ff7643731 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 23 Jul 2015 14:32:47 +0200 Subject: [PATCH 48/65] Covered githug_api with specs --- lib/code/github_api.rb | 43 ++++++++++++++++++------------------ spec/code/github_api_spec.rb | 11 ++++++++- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index 4cdaf93..ef4f3cb 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -8,10 +8,6 @@ module Code class GitHubAPI - def authorization_note - "CoderlyCode-" + SecureRandom.uuid - end - def initialize(repository: Repository.current) @repository = repository end @@ -25,20 +21,17 @@ def ensure_authorized end end - def authorize(username:, password:) - client = self.octokit_client_instance_from_basic_auth(username: username, password: password) - - token = client.create_authorization(scopes: ["repo"], note: authorization_note)[:token] - - puts "Successfully authorized with token #{token}" - self.authorization_token = token - end - def pull_requests_for_branch(branch) client = self.octokit_client_instance_from_token client.pull_requests(current_repo_slug, head: "#{current_organization}:#{branch.name}") end + def label_pr(pull_request, label) + add_label_to_pr(pull_request.number, label) + end + + private + def octokit_client_instance_from_token client = Octokit::Client.new access_token: authorization_token end @@ -47,16 +40,10 @@ def octokit_client_instance_from_basic_auth(username:, password:) client = Octokit::Client.new login: username, password: password end - def label_pr(pull_request, label) - add_label_to_pr(pull_request.number, label) - end - - def authorized? - !authorization_token.empty? + def create_token(client) + client.create_authorization(scopes: ["repo"], note: authorization_note)[:token] end - private - def current_organization @repository.organization end @@ -73,6 +60,16 @@ def add_label_to_pr(number, label) octokit_client_instance_from_token.add_labels_to_an_issue(current_repo_slug, number, [label]) end + def authorize(username:, password:) + client = octokit_client_instance_from_basic_auth(username: username, password: password) + token = create_token(client) + self.authorization_token = token + end + + def authorized? + !authorization_token.empty? + end + def authorization_token token = System.result('git config --get oauth.token') token @@ -82,5 +79,9 @@ def authorization_token=(token) System.result("git config --global oauth.token #{token}") end + def authorization_note + "CoderlyCode-" + SecureRandom.uuid + end + end end diff --git a/spec/code/github_api_spec.rb b/spec/code/github_api_spec.rb index ec8a4a0..cf08c56 100644 --- a/spec/code/github_api_spec.rb +++ b/spec/code/github_api_spec.rb @@ -4,7 +4,7 @@ module Code describe GitHubAPI do let(:api) { GitHubAPI.new } - describe '#ensure_authorized' do + describe "#ensure_authorized" do context 'when system is authorized' do before do @@ -34,5 +34,14 @@ module Code end end end + + describe "#authorize" do + it "creates a client instance, uses it to create a token and stores it" do + expect(api).to receive(:octokit_client_instance_from_basic_auth).with(username: "test_user", password: "test_pass").and_return("something") + expect(api).to receive(:create_token).with("something").and_return("token") + expect(System).to receive(:result).with("git config --global oauth.token token") + api.send(:authorize, username: "test_user", password: "test_pass") + end + end end end From b4f5dcf2e479c351df785c517f84789c7f3c4440 Mon Sep 17 00:00:00 2001 From: begedin Date: Thu, 23 Jul 2015 16:01:48 +0200 Subject: [PATCH 49/65] Further coverage of git.rb with specs --- lib/code/git.rb | 5 +++ lib/code/github_api.rb | 2 +- spec/code/git_spec.rb | 83 ++++++++++++++++++++++++++++++++++++-- spec/support/git_extras.rb | 7 +++- 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index 927b894..ca3259f 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -12,6 +12,10 @@ class Git UncommittedChangesError = Class.new(StandardError) FeatureExistsError = Class.new(StandardError) + def initialize(github_api: GitHubAPI.new) + @github_api = github_api + end + def path Dir.pwd end @@ -80,6 +84,7 @@ def publish(base: nil, message: '') raise NotOnFeatureBranchError, 'Must be on a feature branch to publish your code' unless current_branch.feature? raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes? push + create_prs_for(base, message) end diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb index ef4f3cb..db13fe9 100644 --- a/lib/code/github_api.rb +++ b/lib/code/github_api.rb @@ -22,7 +22,7 @@ def ensure_authorized end def pull_requests_for_branch(branch) - client = self.octokit_client_instance_from_token + client = octokit_client_instance_from_token client.pull_requests(current_repo_slug, head: "#{current_organization}:#{branch.name}") end diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 131a46a..2f8771a 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -9,7 +9,8 @@ module Code describe Git do - let(:git) { Git.new } + let(:github_api) { GitHubAPI.new(repository: Git.test_repo_origin) } + let(:git) { Git.new(github_api: github_api) } let(:repo_path) { @repo_path } before(:all) do @@ -21,7 +22,7 @@ module Code Git.setup_test_repo end - describe 'current_branch' do + describe "current_branch" do let(:branch) { git.current_branch } subject { branch } @@ -84,7 +85,7 @@ module Code end - describe 'search' do + describe "search" do before do allow(System).to receive(:open_in_browser) @@ -96,5 +97,81 @@ module Code end end + describe "#repo_url" do + it "should use the 'git ls-remote' subcommand to get the origin url" do + expect(System).to receive(:result).with("git ls-remote --get-url origin").and_call_original + expect(git.repo_url "origin").to eq "https:/github.com/testuser/codegit" + end + end + + describe "#finish" do + it "should switch to development, perform a pull and delete the previous branch" do + branch = Branch.create("test-branch") + development_branch = Branch.create("development") + branch.checkout + + expect(System).to receive(:call).with("checkout development").and_call_original + expect(System).to receive(:call).with("pull origin development:development") + expect(System).to receive(:call).with("branch -d test-branch").and_call_original + + allow(git).to receive(:fetch) + + git.finish + + expect(Branch.exists? "test-branch").to eq false + end + end + + describe "#publish" do + it "should raise an error on a protected branch" do + branch = Branch.create("protected") + allow(branch).to receive(:protected?).and_return(true) + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + + expect{ git.publish }.to raise_error Git::NotOnFeatureBranchError + end + + it "should raise an error if there are uncommited_changes" do + branch = Branch.create("test-branch") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(true) + + expect{ git.publish }.to raise_error Git::UncommittedChangesError + end + + it "should create a PRs against both development and master for a hotfix branch, then label them" do + branch = Branch.create("hotfix-test") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + allow(git).to receive(:main_repo).and_return("test_org/test_repo") + allow(git).to receive(:push) + + allow(System).to receive(:opent_in_browser).twice + + expect(System).to receive(:exec).with("hub pull-request -f \"Hotfix test\" -b test_org/test_repo:development -h test_org/test_repo:hotfix-test").and_return("something") + expect(System).to receive(:exec).with("hub pull-request -f \"Hotfix test\" -b test_org/test_repo:master -h test_org/test_repo:hotfix-test").and_return("something") + expect(branch).to receive(:mark_prs_as_hotfix) + + git.publish + end + + it "should create a PR against development for any other feature branch" do + branch = Branch.create("test-feature") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + allow(git).to receive(:main_repo).and_return("test_org/test_repo") + allow(git).to receive(:push) + + allow(System).to receive(:opent_in_browser).twice + + allow(System).to receive(:exec).with("git ls-remote --get-url origin").and_call_original + expect(System).to receive(:exec).with("hub pull-request -f \"Test feature\" -b test_org/test_repo:development -h test_org/test_repo:test-feature").and_return("something") + + git.publish + + end + end + end end diff --git a/spec/support/git_extras.rb b/spec/support/git_extras.rb index 01b51e2..6269c57 100644 --- a/spec/support/git_extras.rb +++ b/spec/support/git_extras.rb @@ -4,6 +4,11 @@ module Code module GitExtras module ClassMethods + + def test_repo_origin + "https:/github.com/testuser/codegit" + end + def setup_test_repo repo_path = Dir.mktmpdir('codegit') Dir.chdir repo_path @@ -14,7 +19,7 @@ def setup_test_repo System.exec 'touch README' System.call 'add -A' System.call 'commit -m "initial commit"' - System.call 'remote add origin https:/github.com/testuser/codegit' + System.call "remote add origin #{test_repo_origin}" end end From 5d5b6fd725bffcb3f0ab09c30beffd975ec9a406 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Fri, 24 Jul 2015 08:54:17 +0200 Subject: [PATCH 50/65] Speced git#start and git#hotfix --- spec/code/git_spec.rb | 57 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 2f8771a..8759fe8 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -104,6 +104,59 @@ module Code end end + describe "#start" do + it "should raise an error if branch already exists" do + existing_branch = Branch.create "existing-branch" + + expect{ git.start "existing-branch" }.to raise_error Git::FeatureExistsError + end + + it "should checkout the development branch, pull it, then create and switch to a new branch" do + expect(System).to receive(:call).with("checkout development") + expect(System).to receive(:call).with("pull origin development:development") + expect(System).to receive(:call).with("branch new-feature") + expect(System).to receive(:call).with("checkout new-feature") + + git.start "new-feature" + end + + it "should not checkout the development branch if already on it" do + dev_branch = Branch.create "development" + allow(git).to receive(:current_branch).and_return(dev_branch) + expect(System).not_to receive(:call).with("checkout development") + + git.start "new-feature" + end + end + + describe "#hotfix" do + it "should raise an error if branch already exists" do + existing_branch = Branch.create "hotfix-branch" + + expect{ git.hotfix "branch" }.to raise_error Git::FeatureExistsError + end + + it "should checkout the master branch, pull it, then create and switch to a new branch" do + some_other_branch = Branch.create "random-branch" + + allow(git).to receive(:current_branch).and_return(some_other_branch) + + expect(System).to receive(:call).with("checkout master") + expect(System).to receive(:call).with("pull origin master:master") + expect(System).to receive(:call).with("branch hotfix-branch") + expect(System).to receive(:call).with("checkout hotfix-branch") + + git.hotfix "branch" + end + + it "should not checkout the master branch if already on it" do + # NOTE: the default git repo is on "master" branch to begin + expect(System).not_to receive(:call).with("checkout master") + + git.hotfix "branch" + end + end + describe "#finish" do it "should switch to development, perform a pull and delete the previous branch" do branch = Branch.create("test-branch") @@ -112,13 +165,11 @@ module Code expect(System).to receive(:call).with("checkout development").and_call_original expect(System).to receive(:call).with("pull origin development:development") - expect(System).to receive(:call).with("branch -d test-branch").and_call_original + expect(System).to receive(:call).with("branch -d test-branch") allow(git).to receive(:fetch) git.finish - - expect(Branch.exists? "test-branch").to eq false end end From 3ffdb8e53fa491757110be13f283dcc0f949d655 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Fri, 24 Jul 2015 09:57:15 +0200 Subject: [PATCH 51/65] Cleaned up Git class and specs --- lib/code/git.rb | 102 ++++++++++++++++------------------ spec/code/git_spec.rb | 126 ++++++++++++++++++++++++++++++++---------- 2 files changed, 143 insertions(+), 85 deletions(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index ca3259f..701bf67 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -2,7 +2,6 @@ require 'code/branch' require 'code/system' -require 'code/github_api' module Code @@ -12,12 +11,7 @@ class Git UncommittedChangesError = Class.new(StandardError) FeatureExistsError = Class.new(StandardError) - def initialize(github_api: GitHubAPI.new) - @github_api = github_api - end - - def path - Dir.pwd + def initialize() end def start(feature) @@ -31,24 +25,6 @@ def start(feature) end end - def stash - System.call('stash') - end - - def unstash - System.call('stash pop') - end - - def with_stash - if uncommitted_changes? - stash - yield - unstash - else - yield - end - end - def hotfix(name) name = "hotfix-#{name}" raise FeatureExistsError, "Branch #{name} already exists" if Branch.exists?(name) @@ -75,6 +51,7 @@ def cancel end end + # NOT used anywhere at the moment. Did we loose track of a supported command? def commit(message) System.call 'add -A' System.call "commit -m \"#{message}\"" @@ -84,20 +61,54 @@ def publish(base: nil, message: '') raise NotOnFeatureBranchError, 'Must be on a feature branch to publish your code' unless current_branch.feature? raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes? push - create_prs_for(base, message) + end + + def finish + branch = current_branch + + development_branch.checkout + fetch + development_branch.pull + branch.delete! end def push current_branch.push end - def pull_request(base:, message: '') - base = development_branch unless base - message = current_branch.message if message.empty? - command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{current_branch}" - System.exec(command).strip + def search + System.open_in_browser "https://github.com/#{current_repo_slug}/find/development" + end + + # NOT used anywhere at the moment. Did we loose track of a supported command? + def compare_in_browser(branch) + System.exec "hub compare #{branch}" + end + + def prune_remote_branches + System.call 'remote prune origin' + end + + private + + def stash + System.call('stash') + end + + def unstash + System.call('stash pop') + end + + def with_stash + if uncommitted_changes? + stash + yield + unstash + else + yield + end end def create_prs_for(base, message) @@ -108,8 +119,11 @@ def create_prs_for(base, message) end end - def search - System.open_in_browser "https://github.com/#{current_repo_slug}/find/development" + def pull_request(base:, message: '') + base = development_branch unless base + message = current_branch.message if message.empty? + command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{current_branch}" + System.exec(command).strip end def create_feature_pr(base, message) @@ -122,20 +136,6 @@ def create_hotfix_prs(message) current_branch.mark_prs_as_hotfix end - def compare_in_browser(branch) - System.exec "hub compare #{branch}" - end - - def finish - branch = current_branch - - development_branch.checkout - fetch - development_branch.pull - - branch.delete! - end - def uncommitted_changes? System.result('git status --porcelain') != '' end @@ -160,10 +160,6 @@ def master_branch Branch.master end - def prune_remote_branches - System.call 'remote prune origin' - end - def main_repo main_repo_url[/:(\w+)\//,1] end @@ -176,10 +172,6 @@ def repo_url(name) System.result("git ls-remote --get-url #{name}") end - def github_api - @github_api ||= GitHubAPI.new - end - def current_repo_slug Repository.current.slug end diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 8759fe8..b56340a 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -9,8 +9,7 @@ module Code describe Git do - let(:github_api) { GitHubAPI.new(repository: Git.test_repo_origin) } - let(:git) { Git.new(github_api: github_api) } + let(:git) { Git.new } let(:repo_path) { @repo_path } before(:all) do @@ -22,70 +21,70 @@ module Code Git.setup_test_repo end - describe "current_branch" do - let(:branch) { git.current_branch } + describe "#current_branch" do + let(:branch) { Branch.current } subject { branch } - it 'should currently be on the master branch' do + it "should currently be on the master branch" do expect(branch.name).to eq 'master' end - context 'when creating a new branch' do + context "when creating a new branch" do before do - Branch.create 'test_branch' + Branch.create "test_branch" end - it 'should have the right branches' do + it "should have the right branches" do expect(Branch.all.count).to eq 2 end - it 'should have created the branch' do - expect(Branch.new('test_branch')).to exist + it "should have created the branch" do + expect(Branch.new("test_branch")).to exist end - context 'when deleting a branch' do + context "when deleting a branch" do before do - Branch.matching('test_branch').delete! + Branch.matching("test_branch").delete! end - it 'should only have one branch left' do + it "should only have one branch left" do expect(Branch.all.count).to eq 1 end - it 'should have deleted the branch' do - expect(Branch.new('test_branch')).to_not exist + it "should have deleted the branch" do + expect(Branch.new("test_branch")).to_not exist end end - context 'when checking out a branch' do + context "when checking out a branch" do before do Branch.matching('test_branch').checkout end - it 'should have changed the branch' do + it "should have changed the branch" do expect(Branch.current.name).to eq 'test_branch' end end end - context 'when trying to delete a protected branch' do + context "when trying to delete a protected branch" do before do - Branch.create 'development' + Branch.create "development" end - it 'should now be allowed' do - expect { Branch.matching('development').delete! }.to raise_error Branch::ProtectedBranchError + it "should now be allowed" do + expect { Branch.matching("development").delete! }.to raise_error Branch::ProtectedBranchError end end end - describe "search" do + describe "#search" do before do allow(System).to receive(:open_in_browser) @@ -97,13 +96,6 @@ module Code end end - describe "#repo_url" do - it "should use the 'git ls-remote' subcommand to get the origin url" do - expect(System).to receive(:result).with("git ls-remote --get-url origin").and_call_original - expect(git.repo_url "origin").to eq "https:/github.com/testuser/codegit" - end - end - describe "#start" do it "should raise an error if branch already exists" do existing_branch = Branch.create "existing-branch" @@ -127,6 +119,20 @@ module Code git.start "new-feature" end + + it "should stash and unstash if there are uncommitted changes" do + allow(git).to receive(:uncommitted_changes?).and_return true + + allow(System).to receive(:call).with("checkout development") + allow(System).to receive(:call).with("pull origin development:development") + allow(System).to receive(:call).with("branch new-feature") + allow(System).to receive(:call).with("checkout new-feature") + + expect(System).to receive(:call).with("stash") + expect(System).to receive(:call).with("stash pop") + + git.start "new-feature" + end end describe "#hotfix" do @@ -150,11 +156,24 @@ module Code end it "should not checkout the master branch if already on it" do - # NOTE: the default git repo is on "master" branch to begin + # NOTE: the test git repo is on "master" branch to begin expect(System).not_to receive(:call).with("checkout master") git.hotfix "branch" end + + it "should stash and unstash if there are uncommitted changes" do + allow(git).to receive(:uncommitted_changes?).and_return true + + allow(System).to receive(:call).with("pull origin master:master") + allow(System).to receive(:call).with("branch hotfix-new-feature") + allow(System).to receive(:call).with("checkout hotfix-new-feature") + + expect(System).to receive(:call).with("stash") + expect(System).to receive(:call).with("stash pop") + + git.hotfix "new-feature" + end end describe "#finish" do @@ -224,5 +243,52 @@ module Code end end + describe "#cancel" do + it "should not do anything if already on development branch" do + dev_branch = Branch.create "development" + + allow(git).to receive(:current_branch).and_return(dev_branch) + + expect(STDOUT).to receive(:puts).with("Nothing to cancel (already on development)") + + git.cancel + end + + it "should switch to development, then delete previous branch, with force flag" do + some_branch = Branch.create "some-branch" + Branch.create "development" + + allow(git).to receive(:current_branch).and_return(some_branch) + + expect(System).to receive(:call).with("checkout development") + expect(some_branch).to receive(:delete!).with(force: true) + + git.cancel + end + end + + describe "#commit" do + it "should add all files to the list and commit with the specified message" do + expect(System).to receive(:call).with("add -A") + expect(System).to receive(:call).with("commit -m \"test message\"") + + git.commit "test message" + end + end + + describe "#push" do + it "should push to origin" do + expect(System).to receive(:call).with("push origin master:master") + git.push + end + end + + describe "#prune_remote_branches" do + it "should call the appropriate git command" do + expect(System).to receive(:call).with("remote prune origin") + + git.prune_remote_branches + end + end end end From 36b1a02856e26a7398deae3e473a39eb7dc95b5f Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Fri, 24 Jul 2015 10:26:56 +0200 Subject: [PATCH 52/65] Further cleanup of git specs. Moved branch related tests to branch_spec.rb --- spec/code/branch_spec.rb | 133 ++++++++++++++++++++++--------------- spec/code/git_spec.rb | 86 +++++++----------------- spec/support/git_extras.rb | 10 +++ 3 files changed, 111 insertions(+), 118 deletions(-) diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index 3f9f8cd..74c80c5 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -7,47 +7,22 @@ module Code describe Branch do - def setup_test_repo(options = {}) - - defaults = { - name: "test_repo", - origins: [], - branches: [] - } - - options = defaults.merge(options) - - repo_path = Dir.mktmpdir(options[:name]) - Dir.chdir repo_path - - System.call "init" - System.call "config user.email \"test@hotmail.com\"" - System.call "config user.name \"Chuck Norris\"" - System.exec "touch README" - System.call "add -A" - System.call "commit -m \"initial commit\"" - - options[:origins].each do |origin| - System.call "remote add #{origin.name} #{origin.url}" - end - - options[:branches].each do |branch| - System.call "branch #{branch}" - end - end - - def checkout_branch(name) - System.call("checkout #{name}") + before(:all) do + Branch.send :include, GitExtras end before do allow(System).to receive(:puts) + Branch.setup_test_repo end describe "self.all_names" do - it "returns names of all branches in the repo as strings" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "returns names of all branches in the repo as strings" do branch_names = Branch.all_names expect(branch_names.length).to eq 3 @@ -58,8 +33,12 @@ def checkout_branch(name) end describe "self.all" do + + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + it "returns Branch instances for all branches in the repo" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) branches = Branch.all @@ -85,23 +64,24 @@ def checkout_branch(name) end describe "self.matching" do - it "finds the branch matching the patterns" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1"]) - expect(Branch.matching('test', 'branch', '1').name).to eq 'test-branch-1' + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) end it "returns the first branch that matches the patterns" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) - expect(Branch.matching('test', 'branch').name).to eq 'test-branch-1' + expect(Branch.matching('test', '2').name).to eq 'test-branch-2' end end describe "self.exists?" do - it "returns true for a localy known branch, false otherwise" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "returns true for a localy known branch, false otherwise" do expect(Branch.exists? "master").to eq true expect(Branch.exists? "test-branch-1").to eq true expect(Branch.exists? "test-branch-2").to eq true @@ -109,21 +89,63 @@ def checkout_branch(name) end end - #TODO: "self.merged" - describe "self.current" do - it "returns a Branch instance for the current git branch" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) + let(:branch) { Branch.current } + + subject { branch } + + it "should currently be on the master branch" do + expect(branch.name).to eq 'master' + end + + context "when creating a new branch" do + + before do + Branch.create "test_branch" + end + + it "should have the right branches" do + expect(Branch.all.count).to eq 2 + end + + it "should have created the branch" do + expect(Branch.new("test_branch")).to exist + end - current_branch = Branch.current - expect(current_branch).to be_a Branch - expect(current_branch.name).to eq "master" + context "when deleting a branch" do + before do + Branch.matching("test_branch").delete! + end - checkout_branch "test-branch-1" + it "should only have one branch left" do + expect(Branch.all.count).to eq 1 + end - current_branch = Branch.current - expect(current_branch).to be_a Branch - expect(current_branch.name).to eq "test-branch-1" + it "should have deleted the branch" do + expect(Branch.new("test_branch")).to_not exist + end + end + + context "when checking out a branch" do + before do + Branch.matching('test_branch').checkout + end + + it "should have changed the branch" do + expect(Branch.current.name).to eq 'test_branch' + end + end + end + + context "when trying to delete a protected branch" do + + before do + Branch.create "development" + end + + it "should now be allowed" do + expect { Branch.matching("development").delete! }.to raise_error Branch::ProtectedBranchError + end end end @@ -200,6 +222,10 @@ def checkout_branch(name) describe "#exists" do + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + it "calls System.result and returns the result" do test_branch_1 = Branch.new "test-branch-1" @@ -212,8 +238,6 @@ def checkout_branch(name) end it "returns true if the branch with the provided instance name exists in the git repo" do - setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"]) - test_branch_1 = Branch.new "test-branch-1" test_branch_2 = Branch.new "test-branch-2" test_branch_3 = Branch.new "test-branch-3" @@ -540,7 +564,6 @@ def checkout_branch(name) expect(pull_request_b).to receive(:add_label).with("label") expect { random_branch.send(:label_prs, "label") }.not_to raise_error - end end end diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index b56340a..ea6425c 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -21,69 +21,6 @@ module Code Git.setup_test_repo end - describe "#current_branch" do - let(:branch) { Branch.current } - - subject { branch } - - it "should currently be on the master branch" do - expect(branch.name).to eq 'master' - end - - context "when creating a new branch" do - - before do - Branch.create "test_branch" - end - - it "should have the right branches" do - expect(Branch.all.count).to eq 2 - end - - it "should have created the branch" do - expect(Branch.new("test_branch")).to exist - end - - context "when deleting a branch" do - before do - Branch.matching("test_branch").delete! - end - - it "should only have one branch left" do - expect(Branch.all.count).to eq 1 - end - - it "should have deleted the branch" do - expect(Branch.new("test_branch")).to_not exist - end - end - - context "when checking out a branch" do - before do - Branch.matching('test_branch').checkout - end - - it "should have changed the branch" do - expect(Branch.current.name).to eq 'test_branch' - end - end - - end - - context "when trying to delete a protected branch" do - - before do - Branch.create "development" - end - - it "should now be allowed" do - expect { Branch.matching("development").delete! }.to raise_error Branch::ProtectedBranchError - end - - end - - end - describe "#search" do before do @@ -176,6 +113,29 @@ module Code end end + describe "#switch" do + it "should switch to the first branch (alphabetically) matching the provided pattern" do + Branch.create("some-other-branch") + Branch.create("some-random-branch") + + expect(System).to receive(:call).with("checkout some-other-branch") + git.switch("some") + + expect(System).to receive(:call).with("checkout some-random-branch") + git.switch("some", "random") + + expect(System).to receive(:call).with("checkout some-other-branch") + git.switch("some", "other") + + expect(System).to receive(:call).with("checkout some-other-branch") + git.switch("some", "branch") + end + + it "should raise error if no matching branch was found" do + expect{ git.switch("some") }.to raise_error + end + end + describe "#finish" do it "should switch to development, perform a pull and delete the previous branch" do branch = Branch.create("test-branch") diff --git a/spec/support/git_extras.rb b/spec/support/git_extras.rb index 6269c57..eeabca1 100644 --- a/spec/support/git_extras.rb +++ b/spec/support/git_extras.rb @@ -21,6 +21,16 @@ def setup_test_repo System.call 'commit -m "initial commit"' System.call "remote add origin #{test_repo_origin}" end + + def checkout_branch(name) + System.call "checkout #{name}" + end + + def add_branches(names) + names.each do |name| + System.call "branch #{name}" + end + end end def self.included(base) From f8940896296c29806edd307a2a28d93ed929d752 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Fri, 24 Jul 2015 11:00:55 +0200 Subject: [PATCH 53/65] Cleanup Branch specs --- lib/code/branch.rb | 37 ++++++----- spec/code/branch_spec.rb | 130 ++++++--------------------------------- 2 files changed, 39 insertions(+), 128 deletions(-) diff --git a/lib/code/branch.rb b/lib/code/branch.rb index 85d2d26..0771340 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -13,7 +13,6 @@ class Branch DEVELOPMENT_BRANCH_NAME = 'development' PROTECTED_BRANCH_NAMES = %w{master development} - def self.all_names System.result('git branch').strip.lines.map { |line| line.gsub(/\s|\*/, '') } end @@ -22,10 +21,15 @@ def self.all all_names.map { |name| new(name) } end + + # TODO: This should probably be moved to another class or module. + # An identical method is used in match_scorer/base def self.coerce_patterns(*patterns) patterns.flatten.join(' ').split(' ') end + private_class_method :coerce_patterns + def self.matching(*patterns) patterns = coerce_patterns(patterns) all.find { |b| b.matches? *patterns } @@ -124,10 +128,6 @@ def checkout self end - def authorize_delete! - raise ProtectedBranchError, "The #{name} branch is protected" if protected? - end - def message name.gsub('-', ' ').capitalize end @@ -152,24 +152,12 @@ def mark_prs_as_hotfix label_prs('hotfix') end - def pull_request - pull_requests.first - end - - def pull_requests - @pull_request ||= PullRequest.for_branch(self) - end - def pull_request_url # This could be a delegate, but it seems easier to read just by # calling it. pull_request.url end - def ensure_public! - raise PrivateBranchError, "#{name} is a private branch" if private? - end - attr_reader :name alias_method :to_s, :name @@ -183,5 +171,20 @@ def label_prs(label) end end + def authorize_delete! + raise ProtectedBranchError, "The #{name} branch is protected" if protected? + end + + def pull_request + pull_requests.first + end + + def pull_requests + @pull_request ||= PullRequest.for_branch(self) + end + + def ensure_public! + raise PrivateBranchError, "#{name} is a private branch" if private? + end end end diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index 74c80c5..5bfc8d0 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -52,17 +52,6 @@ module Code end end - describe "self.coerce_patterns" do - it "returns an array consisting of provided patterns" do - patterns = Branch.coerce_patterns 'abc', 'def', '123' - - expect(patterns.length).to eq 3 - expect(patterns[0]).to eq 'abc' - expect(patterns[1]).to eq 'def' - expect(patterns[2]).to eq '123' - end - end - describe "self.matching" do before do @@ -180,7 +169,7 @@ module Code describe "self.find" do it "creates a new Branch instance with specified name, but makes no System.call" do - expect(System).not_to receive(:call) + expect(System).not_to receive(:call).with("branch test_branch") branch = Branch.find "test_branch" @@ -189,7 +178,7 @@ module Code end end - describe "#==" do + describe "equality comparer" do it "compares by branch name" do branch_a = Branch.new "branch-a" branch_b = Branch.new "branch-b" @@ -362,15 +351,6 @@ module Code end describe "#push" do - it "checks if the branch is public before pushing it" do - random_branch = Branch.new "random" - - expect(random_branch).to receive(:ensure_public!).and_return(true) - allow(System).to receive(:call) - - random_branch.push - end - it "makes a system call for 'push origin branch-name:branch-name'" do random_branch = Branch.new "random" allow(random_branch).to receive(:ensure_public!).and_return(true) @@ -381,7 +361,7 @@ module Code it "allows pushing of a public branch" do random_branch = Branch.new "random" - allow(System).to receive(:call) + allow(System).to receive(:call).with("push origin random:random") expect { random_branch.push }.not_to raise_error end @@ -413,22 +393,6 @@ module Code end end - describe "#authorize_delete!" do - it "raises a ProtectedBranchError for protected branches" do - random_branch = Branch.new "random" - - expect(random_branch).to receive(:protected?).and_return(true) - expect { random_branch.authorize_delete!}.to raise_error Branch::ProtectedBranchError - end - - it "doesn't raise an error for unprotected branches" do - random_branch = Branch.new "random" - - expect(random_branch).to receive(:protected?).and_return(false) - expect {random_branch.authorize_delete!}.not_to raise_error - end - end - describe "#message" do it "returns the capitalized branch name, where dashes are replaced with spaces" do random_branch = Branch.new "random" @@ -457,12 +421,17 @@ module Code end describe "#has_pull_request?" do - it "returns true if #pull_request is not nil, false otherwise" do - random_branch = Branch.new "random" - expect(random_branch).to receive(:pull_request).and_return("something") - expect(random_branch.has_pull_request?).to eq true - expect(random_branch).to receive(:pull_request).and_return(nil) - expect(random_branch.has_pull_request?).to eq false + it "returns true if the branch has any pull requests, false otherwise" do + random_branch_1 = Branch.new "random_1" + pull_request_1 = PullRequest.new(pull_request_info: { html_url: "example.com" }) + pull_request_2 = PullRequest.new(pull_request_info: { html_url: "example2.com" }) + + expect(PullRequest).to receive(:for_branch).with(random_branch_1).and_return([pull_request_1, pull_request_2]) + expect(random_branch_1.has_pull_request?).to eq true + + random_branch_2 = Branch.new "random_2" + expect(PullRequest).to receive(:for_branch).with(random_branch_2).and_return([]) + expect(random_branch_2.has_pull_request?).to eq false end end @@ -494,76 +463,15 @@ module Code end end - describe "#pull_request" do - it "returns the first item in #pull_requests" do - random_branch = Branch.new "random" - - expect(random_branch).to receive(:pull_requests).and_return(["first", "second"]) - expect(random_branch.pull_request).to eq "first" - end - - it "returns nil if #pull_requests is empty" do - random_branch = Branch.new "random" - - expect(random_branch).to receive(:pull_requests).and_return([]) - expect(random_branch.pull_request).to eq nil - end - end - - describe "#pull_requests" do - it "calls 'PullRequest.for_branch'" do - random_branch = Branch.new "random" - pull_requests = [ Code::PullRequest.new(pull_request_info: {}) ] - - expect(PullRequest).to receive(:for_branch).with(random_branch).and_return(pull_requests) - expect(random_branch.pull_requests).to eq pull_requests - end - end - describe "#pull_request_url" do - it "returns #pull_request.url" do + it "returns the URL of the first pull request" do random_branch = Branch.new "random" - pull_request = Code::PullRequest.new(pull_request_info: { html_url: "example.com" }) + pull_request_1 = PullRequest.new(pull_request_info: { html_url: "example.com" }) + pull_request_2 = PullRequest.new(pull_request_info: { html_url: "example2.com" }) - expect(random_branch).to receive(:pull_request).and_return(pull_request) - expect(random_branch.pull_request_url).to eq "example.com" - end - end - - describe "#ensure_public!" do - it "raises error if branch is private" do - random_branch = Branch.new "random-local" - - expect { random_branch.ensure_public! }.to raise_error Branch::PrivateBranchError - end + expect(PullRequest).to receive(:for_branch).and_return([pull_request_1, pull_request_2]) - it "raises no error if branch is public" do - random_branch = Branch.new "random" - - expect { random_branch.ensure_public! }.not_to raise_error - end - end - - describe "#label_prs" do - it "rases a NoPRError if there are no PRs" do - random_branch = Branch.new "random" - - expect(random_branch).to receive(:has_pull_request?).and_return(false) - expect { random_branch.send(:label_prs, "label") }.to raise_error Branch::NoPRError - end - - it "calls PullRequest#add_label for each pull_request" do - random_branch = Branch.new "random" - - pull_request_a = Code::PullRequest.new(pull_request_info: {}) - pull_request_b = Code::PullRequest.new(pull_request_info: {}) - - expect(random_branch).to receive(:pull_requests).and_return([pull_request_a, pull_request_b]).twice - - expect(pull_request_a).to receive(:add_label).with("label") - expect(pull_request_b).to receive(:add_label).with("label") - - expect { random_branch.send(:label_prs, "label") }.not_to raise_error + expect(random_branch.pull_request_url).to eq "example.com" end end end From 310e0e0a949f6c1d9e1d968131e4178639db8fb0 Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 24 Jul 2015 11:43:53 +0200 Subject: [PATCH 54/65] Added error to switch method in case when branch doesn't exist --- lib/code/git.rb | 5 ++++- spec/code/git_spec.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/code/git.rb b/lib/code/git.rb index 701bf67..d21245f 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -10,6 +10,7 @@ class Git NotOnFeatureBranchError = Class.new(StandardError) UncommittedChangesError = Class.new(StandardError) FeatureExistsError = Class.new(StandardError) + BranchDoesntExistError = Class.new(StandardError) def initialize() end @@ -38,7 +39,9 @@ def hotfix(name) end def switch(*patterns) - Branch.matching(*patterns).checkout + branch = Branch.matching(*patterns) + raise BranchDoesntExistError if !branch + branch.checkout end def cancel diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index ea6425c..1651b50 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -132,7 +132,7 @@ module Code end it "should raise error if no matching branch was found" do - expect{ git.switch("some") }.to raise_error + expect{ git.switch("some") }.to raise_error Git::BranchDoesntExistError end end From 8e2e7eebc1294b6bb364aa00c82ec8691c4a3bea Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 24 Jul 2015 12:28:03 +0200 Subject: [PATCH 55/65] Added spec for file_list --- spec/code/branch_spec.rb | 2 -- spec/code/file_list_spec.rb | 29 +++++++++++++++++++++++++++++ spec/support/uses_files.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 spec/code/file_list_spec.rb create mode 100644 spec/support/uses_files.rb diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index 5bfc8d0..a82c264 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -3,8 +3,6 @@ require "code/pull_request" module Code - - describe Branch do before(:all) do diff --git a/spec/code/file_list_spec.rb b/spec/code/file_list_spec.rb new file mode 100644 index 0000000..fe61cdb --- /dev/null +++ b/spec/code/file_list_spec.rb @@ -0,0 +1,29 @@ +require "code/file_list" +require_relative '../support/uses_files' + +module Code + + describe FileList do + include UsesFiles + + + describe "#matching" do + with_files("random_file", "non_random_file", "other_random_file", "last/random") + + it "returns a list of files matching the patterns" do + list = FileList.new.matching("random") + expect(list.length).to eq 4 + + list = FileList.new.matching("random", "file") + expect(list.length).to eq 3 + + list = FileList.new.matching("file") + expect(list.length).to eq 3 + + list = FileList.new.matching("non", "random") + expect(list.length).to eq 1 + end + + end + end +end \ No newline at end of file diff --git a/spec/support/uses_files.rb b/spec/support/uses_files.rb new file mode 100644 index 0000000..6f33114 --- /dev/null +++ b/spec/support/uses_files.rb @@ -0,0 +1,32 @@ +module Code + module UsesFiles + def self.included(group) + group.extend(self) + end + + def with_files(*files) + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + + files.each do |file| + create_file(file) + end + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + def create_file(file) + base_name = File.basename(file) + dir_name = File.dirname(file) + FileUtils.mkdir_p(dir_name) if dir_name + FileUtils.touch(file) + end + end +end \ No newline at end of file From c8734e6db102fbd7cb00e153842617400745b8ff Mon Sep 17 00:00:00 2001 From: begedin Date: Fri, 24 Jul 2015 14:51:28 +0200 Subject: [PATCH 56/65] Added basic config module, with specs --- Gemfile | 3 +- Gemfile.lock | 67 ++++++++++++++++++------------- lib/code/config.rb | 33 +++++++++++++++ spec/code/config_spec.rb | 60 +++++++++++++++++++++++++++ spec/code/file_list_spec.rb | 4 +- spec/support/uses_file_system.rb | 69 ++++++++++++++++++++++++++++++++ spec/support/uses_files.rb | 32 --------------- 7 files changed, 206 insertions(+), 62 deletions(-) create mode 100644 lib/code/config.rb create mode 100644 spec/code/config_spec.rb create mode 100644 spec/support/uses_file_system.rb delete mode 100644 spec/support/uses_files.rb diff --git a/Gemfile b/Gemfile index 00f1c9f..0d39d15 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,5 @@ source 'https://rubygems.org' -ruby '2.1.1' +ruby '2.2.2' gemspec gem "octokit" +gem "parseconfig" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 8c1a6f4..5030bfa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,44 +9,53 @@ GEM remote: https://rubygems.org/ specs: addressable (2.3.8) - aruba (0.5.3) - childprocess (>= 0.3.6) - cucumber (>= 1.1.1) - rspec-expectations (>= 2.7.0) + aruba (0.8.1) + childprocess (~> 0.5.6) + contracts (~> 0.9) + cucumber (>= 1.3.19) + rspec-expectations (>= 2.99) builder (3.2.2) - childprocess (0.3.9) + childprocess (0.5.6) ffi (~> 1.0, >= 1.0.11) - cucumber (1.3.8) + contracts (0.10.1) + cucumber (2.0.2) builder (>= 2.1.2) + cucumber-core (~> 1.2.0) diff-lcs (>= 1.1.3) - gherkin (~> 2.12.1) + gherkin (~> 2.12) multi_json (>= 1.7.5, < 2.0) - multi_test (>= 0.0.2) - diff-lcs (1.2.4) + multi_test (>= 0.1.2) + cucumber-core (1.2.0) + gherkin (~> 2.12.0) + diff-lcs (1.2.5) faraday (0.9.1) multipart-post (>= 1.2, < 3) - ffi (1.9.0) - gherkin (2.12.1) + ffi (1.9.10) + gherkin (2.12.2) multi_json (~> 1.3) gli (2.8.0) - hub (1.10.6) - json (1.8.0) - multi_json (1.8.0) - multi_test (0.0.2) + hub (1.12.4) + multi_json (1.11.2) + multi_test (0.1.2) multipart-post (2.0.0) - octokit (3.8.0) + octokit (4.0.1) sawyer (~> 0.6.0, >= 0.5.3) - rake (10.1.0) - rdoc (4.0.1) - json (~> 1.4) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.5) - rspec-expectations (2.14.2) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.3) + parseconfig (1.0.6) + rake (10.4.2) + rdoc (4.2.0) + rspec (3.3.0) + rspec-core (~> 3.3.0) + rspec-expectations (~> 3.3.0) + rspec-mocks (~> 3.3.0) + rspec-core (3.3.2) + rspec-support (~> 3.3.0) + rspec-expectations (3.3.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-mocks (3.3.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-support (3.3.0) sawyer (0.6.0) addressable (~> 2.3.5) faraday (~> 0.8, < 0.10) @@ -58,6 +67,10 @@ DEPENDENCIES aruba code! octokit + parseconfig rake rdoc rspec + +BUNDLED WITH + 1.10.6 diff --git a/lib/code/config.rb b/lib/code/config.rb new file mode 100644 index 0000000..41a814f --- /dev/null +++ b/lib/code/config.rb @@ -0,0 +1,33 @@ +require "parseconfig" + +module Code + + PROMPT_TEXTS = { + + } + + module Config + extend self + + def self.get(property) + ensure_file_exists + + config = ParseConfig.new ".codeconfig" + + if config[property] + property_value = config[property] if config[property] + else + property_value = System.prompt(PROMPT_TEXTS[property] || "Unknown") unless config[property] + config.add(property, property_value) + file = File.open ".codeconfig", "w" + config.write file + file.close + end + property_value + end + + def self.ensure_file_exists + FileUtils.touch ".codeconfig" unless File.exists? ".codeconfig" + end + end +end \ No newline at end of file diff --git a/spec/code/config_spec.rb b/spec/code/config_spec.rb new file mode 100644 index 0000000..e6580d5 --- /dev/null +++ b/spec/code/config_spec.rb @@ -0,0 +1,60 @@ +require "code/config" +require_relative '../support/uses_file_system' + +module Code + describe Config do + include UsesFileSystem + + describe "#get" do + + context "when .codeconfig doesn't exist" do + with_empty_tmp_folder + + it "creates a .codeconfig file" do + expect(File.exists? ".codeconfig").to be false + allow(System).to receive (:prompt) + Config.get "random-property" + expect(File.exists? ".codeconfig").to be true + end + end + + context "with an existing .codeconfig file" do + + context "when config property doesn't exist in the file" do + with_file_and_content(".codeconfig", "") + + it "should prompt the user for the property value" do + expect(System).to receive(:prompt).with("Unknown") + Config.get "property" + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with("Unknown").and_return("test-value") + expect(Config.get "property").to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with("Unknown").and_return("test-value") + Config.get "property" + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "property = \"test-value\"" + end + end + + context "when config property does exist" do + with_file_and_content(".codeconfig", "property = value") + + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.get "property" + end + + it "should return the stored property value" do + expect(Config.get "property").to eq "value" + end + end + end + + end + end +end \ No newline at end of file diff --git a/spec/code/file_list_spec.rb b/spec/code/file_list_spec.rb index fe61cdb..2809dab 100644 --- a/spec/code/file_list_spec.rb +++ b/spec/code/file_list_spec.rb @@ -1,10 +1,10 @@ require "code/file_list" -require_relative '../support/uses_files' +require_relative '../support/uses_file_system' module Code describe FileList do - include UsesFiles + include UsesFileSystem describe "#matching" do diff --git a/spec/support/uses_file_system.rb b/spec/support/uses_file_system.rb new file mode 100644 index 0000000..34e5d9e --- /dev/null +++ b/spec/support/uses_file_system.rb @@ -0,0 +1,69 @@ +module Code + module UsesFileSystem + def self.included(group) + group.extend(self) + end + + def with_files(*files) + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + + files.each do |file| + create_file(file) + end + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + def with_empty_tmp_folder + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + def with_file_and_content(file, content) + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + + create_file(file) + set_file_content(file, content) + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + private + + def create_file(file) + base_name = File.basename(file) + dir_name = File.dirname(file) + FileUtils.mkdir_p(dir_name) if dir_name + FileUtils.touch(file) + end + + def set_file_content(file, content) + File.write(file, content) + end + end +end \ No newline at end of file diff --git a/spec/support/uses_files.rb b/spec/support/uses_files.rb deleted file mode 100644 index 6f33114..0000000 --- a/spec/support/uses_files.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Code - module UsesFiles - def self.included(group) - group.extend(self) - end - - def with_files(*files) - before do - @pwd = Dir.pwd - @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') - FileUtils.mkdir_p(@tmp_dir) - Dir.chdir(@tmp_dir) - - files.each do |file| - create_file(file) - end - end - - after do - Dir.chdir(@pwd) - FileUtils.rm_rf(@tmp_dir) - end - end - - def create_file(file) - base_name = File.basename(file) - dir_name = File.dirname(file) - FileUtils.mkdir_p(dir_name) if dir_name - FileUtils.touch(file) - end - end -end \ No newline at end of file From 7bdf6900aebb19ddd4817b7f6d04c5a9938e1c92 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Fri, 24 Jul 2015 15:16:38 +0200 Subject: [PATCH 57/65] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ec60a07..23a0abe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: ruby rvm: - - 2.1.1 \ No newline at end of file + - 2.2.2 From 69bc87a30ea13aba7a0587bc0d02d185f7b3c6a3 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 27 Jul 2015 08:50:34 +0200 Subject: [PATCH 58/65] Update Gemfile.lock --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7466bd5..c12e835 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,4 +71,4 @@ DEPENDENCIES rspec BUNDLED WITH - 1.10.5 + 1.10.6 From 58ce4a24ebe2fe29465ad42ffcfca4808f41e462 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 27 Jul 2015 10:00:46 +0200 Subject: [PATCH 59/65] Added master and development branch name properties. Made specs run --- lib/code/branch.rb | 20 +++++------ lib/code/config.rb | 45 ++++++++++++++++++----- spec/code/branch_spec.rb | 3 ++ spec/code/config_spec.rb | 77 ++++++++++++++++++++++++++++++++++++++-- spec/code/git_spec.rb | 4 +++ 5 files changed, 127 insertions(+), 22 deletions(-) diff --git a/lib/code/branch.rb b/lib/code/branch.rb index 0771340..0386ecb 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -8,11 +8,6 @@ class Branch PrivateBranchError = Class.new(StandardError) NoPRError = Class.new(StandardError) - - MASTER_BRANCH_NAME = 'master' - DEVELOPMENT_BRANCH_NAME = 'development' - PROTECTED_BRANCH_NAMES = %w{master development} - def self.all_names System.result('git branch').strip.lines.map { |line| line.gsub(/\s|\*/, '') } end @@ -53,11 +48,11 @@ def self.current end def self.development - new DEVELOPMENT_BRANCH_NAME + new Config.get_development_branch_name end def self.master - new MASTER_BRANCH_NAME + new Config.get_master_branch_name end def self.create(branch_name) @@ -86,15 +81,18 @@ def exists? end def development? - name == DEVELOPMENT_BRANCH_NAME + name == Config.get_development_branch_name end def master? - name == MASTER_BRANCH_NAME + name == Config.get_master_branch_name end def protected? - PROTECTED_BRANCH_NAMES.include? name + master_branch_name = Config.get_master_branch_name + development_branch_name = Config.get_development_branch_name + protected_branch_names = [ master_branch_name, development_branch_name ] + protected_branch_names.include? name end def feature? @@ -149,7 +147,7 @@ def mark_prs_as_awaiting_review end def mark_prs_as_hotfix - label_prs('hotfix') + label_prs("hotfix") end def pull_request_url diff --git a/lib/code/config.rb b/lib/code/config.rb index 41a814f..f829436 100644 --- a/lib/code/config.rb +++ b/lib/code/config.rb @@ -3,7 +3,13 @@ module Code PROMPT_TEXTS = { + master_branch: "You didn't set the name of the master branch. What is it ('master')?", + development_branch: "You didn't set the name of the development branch. What is it ('development')?" + } + DEFAULTS = { + master_branch: "master", + development_branch: "development" } module Config @@ -11,23 +17,44 @@ module Config def self.get(property) ensure_file_exists + get_property_value(property) + end + + def self.get_master_branch_name + self.get(:master_branch) + end + + def self.get_development_branch_name + self.get(:development_branch) + end + + private + def ensure_file_exists + FileUtils.touch ".codeconfig" unless File.exists? ".codeconfig" + end + + def get_property_value(property) config = ParseConfig.new ".codeconfig" - if config[property] - property_value = config[property] if config[property] + if config[property.to_s] + property_value = config[property.to_s] else property_value = System.prompt(PROMPT_TEXTS[property] || "Unknown") unless config[property] - config.add(property, property_value) - file = File.open ".codeconfig", "w" - config.write file - file.close + + store_property_to_config(property, property_value, config) end + property_value end - def self.ensure_file_exists - FileUtils.touch ".codeconfig" unless File.exists? ".codeconfig" + def store_property_to_config(property, property_value, config) + config.add(property, property_value) if property_value + config.add(property, DEFAULTS[property]) unless property_value + + file = File.open ".codeconfig", "w" + config.write file + file.close end end -end \ No newline at end of file +end diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index a82c264..9ae4761 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -1,6 +1,7 @@ require "code/branch" require "code/system" require "code/pull_request" +require "code/config" module Code describe Branch do @@ -10,6 +11,8 @@ module Code end before do + allow(Config).to receive(:get_master_branch_name).and_return "master" + allow(Config).to receive(:get_development_branch_name).and_return "development" allow(System).to receive(:puts) Branch.setup_test_repo end diff --git a/spec/code/config_spec.rb b/spec/code/config_spec.rb index e6580d5..30e3cd3 100644 --- a/spec/code/config_spec.rb +++ b/spec/code/config_spec.rb @@ -42,7 +42,7 @@ module Code end context "when config property does exist" do - with_file_and_content(".codeconfig", "property = value") + with_file_and_content(".codeconfig", "property = value\n") it "should not prompt the user for the property value" do expect(System).not_to receive(:prompt) @@ -54,7 +54,80 @@ module Code end end end + end + + describe "get_master_branch_name" do + context "when there is no previously stored property value" do + with_file_and_content(".codeconfig", "") + let(:prompt_text) { "You didn't set the name of the master branch. What is it ('master')?" } + + it "asks the user for the property value" do + expect(System).to receive(:prompt).with prompt_text + Config.get_master_branch_name + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + + expect(Config.get_master_branch_name).to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + Config.get_master_branch_name + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "master_branch = \"test-value\"" + end + end + + context "when there is a previously stored property value" do + with_file_and_content(".codeconfig", "master_branch = \"value\"\n") + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.get_master_branch_name + end + it "should return the stored property value" do + expect(Config.get_master_branch_name).to eq "value" + end + end + end + + describe "get_development_branch_name" do + context "when there is no previously stored property value" do + with_file_and_content(".codeconfig", "") + let(:prompt_text) { "You didn't set the name of the development branch. What is it ('development')?" } + + it "asks the user for the property value" do + expect(System).to receive(:prompt).with prompt_text + Config.get_development_branch_name + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + + expect(Config.get_development_branch_name).to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + Config.get_development_branch_name + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "development_branch = \"test-value\"" + end + end + + context "when there is a previously stored property value" do + with_file_and_content(".codeconfig", "development_branch = \"value\"\n") + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.get_development_branch_name + end + + it "should return the stored property value" do + expect(Config.get_development_branch_name).to eq "value" + end + end end end -end \ No newline at end of file +end diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 1651b50..b6f8bdd 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -1,6 +1,7 @@ require 'code/git' require 'code/branch' require 'code/system' +require "code/config" require_relative '../support/git_extras' @@ -17,6 +18,9 @@ module Code end before do + allow(Config).to receive(:get_master_branch_name).and_return "master" + allow(Config).to receive(:get_development_branch_name).and_return "development" + allow(System).to receive(:puts) Git.setup_test_repo end From ab628251420631d5b13382da382dea9dac357c75 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 27 Jul 2015 10:50:17 +0200 Subject: [PATCH 60/65] Partial coverage of prompt specs --- spec/code/git_spec.rb | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index b6f8bdd..8a2a761 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -255,4 +255,84 @@ module Code end end end + + describe "user prompts" do + let(:git) { Git.new } + + before(:all) do + Git.send :include, GitExtras + end + + before do + allow(System).to receive(:puts) + Git.setup_test_repo + end + + it "should be made during call to #start to get the name of the development branch" do + expect(Config).to receive(:get_development_branch_name).exactly(3).times.and_return "test" + + allow(System).to receive(:call).with("checkout test") + allow(System).to receive(:call).with("pull origin test:test") + allow(System).to receive(:call).with("branch new-feature") + allow(System).to receive(:call).with("checkout new-feature") + + git.start "new-feature" + end + + it "should be made during call to #hotfix to get the name of the master branch" do + expect(Config).to receive(:get_master_branch_name).exactly(3).times.and_return "test" + + allow(System).to receive(:call).with("checkout test") + allow(System).to receive(:call).with("pull origin test:test") + allow(System).to receive(:call).with("branch hotfix-branch") + allow(System).to receive(:call).with("checkout hotfix-branch") + + git.hotfix "branch" + end + + it "should be made during call to #cancel to get the name of the development_branch" do + expect(Config).to receive(:get_development_branch_name).exactly(2).times.and_return "test" + + dev_branch = Branch.create "test" + allow(git).to receive(:current_branch).and_return(dev_branch) + allow(STDOUT).to receive(:puts).with("Nothing to cancel (already on test)") + + git.cancel + end + + it "should be made during call to #publish to get the names of master and development branches" do + expect(Config).to receive(:get_development_branch_name).exactly(2).times.and_return "test" + expect(Config).to receive(:get_master_branch_name).times.and_return "test" + + branch = Branch.create("test-feature") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + allow(git).to receive(:main_repo).and_return("test_org/test_repo") + allow(git).to receive(:push) + + allow(System).to receive(:open_in_browser).twice + + allow(System).to receive(:exec).with("git ls-remote --get-url origin").and_call_original + allow(System).to receive(:exec).with("hub pull-request -f \"Test feature\" -b test_org/test_repo:test -h test_org/test_repo:test-feature").and_return("something") + + git.publish + end + + it "should be made during call to #finish, to get the name of the development_branch" do + expect(Config).to receive(:get_development_branch_name).exactly(3).times.and_return "dev-test" + expect(Config).to receive(:get_master_branch_name).times.and_return "master-test" + + branch = Branch.new("test-branch") + allow(git).to receive(:current_branch).and_return(branch) + + allow(System).to receive(:call).with("checkout dev-test") + allow(System).to receive(:call).with("pull origin dev-test:dev-test") + allow(System).to receive(:call).with("branch -d test-branch") + + allow(git).to receive(:fetch) + + git.finish + end + + end end From f488f7730735224031dc17a4f7db5cd2e80a1741 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 27 Jul 2015 11:39:01 +0200 Subject: [PATCH 61/65] Add missing "require" statement to code/branch --- lib/code/branch.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/code/branch.rb b/lib/code/branch.rb index 0386ecb..41be165 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -1,4 +1,5 @@ -require 'code/pull_request' +require "code/pull_request" +require "code/config" module Code From 24bd61dbf1bf9aa7786db913a896a3597f0676ac Mon Sep 17 00:00:00 2001 From: begedin Date: Mon, 27 Jul 2015 12:12:09 +0200 Subject: [PATCH 62/65] Spec cleanup --- spec/code/git_spec.rb | 7 ------- spec/code/system_spec.rb | 1 - 2 files changed, 8 deletions(-) diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 8a2a761..b71709f 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -181,8 +181,6 @@ module Code allow(git).to receive(:main_repo).and_return("test_org/test_repo") allow(git).to receive(:push) - allow(System).to receive(:opent_in_browser).twice - expect(System).to receive(:exec).with("hub pull-request -f \"Hotfix test\" -b test_org/test_repo:development -h test_org/test_repo:hotfix-test").and_return("something") expect(System).to receive(:exec).with("hub pull-request -f \"Hotfix test\" -b test_org/test_repo:master -h test_org/test_repo:hotfix-test").and_return("something") expect(branch).to receive(:mark_prs_as_hotfix) @@ -197,8 +195,6 @@ module Code allow(git).to receive(:main_repo).and_return("test_org/test_repo") allow(git).to receive(:push) - allow(System).to receive(:opent_in_browser).twice - allow(System).to receive(:exec).with("git ls-remote --get-url origin").and_call_original expect(System).to receive(:exec).with("hub pull-request -f \"Test feature\" -b test_org/test_repo:development -h test_org/test_repo:test-feature").and_return("something") @@ -310,8 +306,6 @@ module Code allow(git).to receive(:main_repo).and_return("test_org/test_repo") allow(git).to receive(:push) - allow(System).to receive(:open_in_browser).twice - allow(System).to receive(:exec).with("git ls-remote --get-url origin").and_call_original allow(System).to receive(:exec).with("hub pull-request -f \"Test feature\" -b test_org/test_repo:test -h test_org/test_repo:test-feature").and_return("something") @@ -333,6 +327,5 @@ module Code git.finish end - end end diff --git a/spec/code/system_spec.rb b/spec/code/system_spec.rb index 37926e8..ff84787 100644 --- a/spec/code/system_spec.rb +++ b/spec/code/system_spec.rb @@ -1,5 +1,4 @@ require "code/system" -require "io/console" module Code module System From 1e54d16b061f95c77ebfdd2c26b5d61e23b29dac Mon Sep 17 00:00:00 2001 From: begedin Date: Wed, 29 Jul 2015 12:51:11 +0200 Subject: [PATCH 63/65] Renamed config properties --- bin/code | 2 +- lib/code/branch.rb | 16 ++++---- lib/code/config.rb | 22 +++++++---- spec/code/branch_spec.rb | 70 +++++++++++++++++++++++++++++++--- spec/code/config_spec.rb | 77 +++++++++++++++++++++++++++++++------- spec/code/git_spec.rb | 81 +--------------------------------------- 6 files changed, 153 insertions(+), 115 deletions(-) diff --git a/bin/code b/bin/code index 43b6d25..a0f5c7c 100755 --- a/bin/code +++ b/bin/code @@ -68,7 +68,7 @@ desc "Marks the current branch's pr as awaiting review" command :ready do |c| c.action do |global_options, options, args| $github_api.ensure_authorized - Code::Branch.current.mark_prs_as_awaiting_review + Code::Branch.current.mark_prs_as_ready end end diff --git a/lib/code/branch.rb b/lib/code/branch.rb index 41be165..418ee1e 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -49,11 +49,11 @@ def self.current end def self.development - new Config.get_development_branch_name + new Config.development_branch_name end def self.master - new Config.get_master_branch_name + new Config.master_branch_name end def self.create(branch_name) @@ -82,16 +82,16 @@ def exists? end def development? - name == Config.get_development_branch_name + name == Config.development_branch_name end def master? - name == Config.get_master_branch_name + name == Config.master_branch_name end def protected? - master_branch_name = Config.get_master_branch_name - development_branch_name = Config.get_development_branch_name + master_branch_name = Config.master_branch_name + development_branch_name = Config.development_branch_name protected_branch_names = [ master_branch_name, development_branch_name ] protected_branch_names.include? name end @@ -143,8 +143,8 @@ def has_pull_request? !pull_request.nil? end - def mark_prs_as_awaiting_review - label_prs("awaiting review") + def mark_prs_as_ready + label_prs(Config.ready_label) end def mark_prs_as_hotfix diff --git a/lib/code/config.rb b/lib/code/config.rb index f829436..d28aabf 100644 --- a/lib/code/config.rb +++ b/lib/code/config.rb @@ -3,13 +3,15 @@ module Code PROMPT_TEXTS = { - master_branch: "You didn't set the name of the master branch. What is it ('master')?", - development_branch: "You didn't set the name of the development branch. What is it ('development')?" + master_branch: "You didn't set the name of the master branch. What is it? (master)", + development_branch: "You didn't set the name of the development branch. What is it? (development)", + ready_label: "You didn't set the label used for marking the PR as ready. What is it? (awaiting review)" } DEFAULTS = { master_branch: "master", - development_branch: "development" + development_branch: "development", + ready_label: "awaiting review" } module Config @@ -20,14 +22,18 @@ def self.get(property) get_property_value(property) end - def self.get_master_branch_name + def self.master_branch_name self.get(:master_branch) end - def self.get_development_branch_name + def self.development_branch_name self.get(:development_branch) end + def self.ready_label + self.get(:ready_label) + end + private def ensure_file_exists @@ -40,7 +46,8 @@ def get_property_value(property) if config[property.to_s] property_value = config[property.to_s] else - property_value = System.prompt(PROMPT_TEXTS[property] || "Unknown") unless config[property] + property_value = System.prompt(PROMPT_TEXTS[property] || "Unknown") + property_value = DEFAULTS[property] if (!property_value || property_value.strip.empty?) store_property_to_config(property, property_value, config) end @@ -49,8 +56,7 @@ def get_property_value(property) end def store_property_to_config(property, property_value, config) - config.add(property, property_value) if property_value - config.add(property, DEFAULTS[property]) unless property_value + config.add(property, property_value) file = File.open ".codeconfig", "w" config.write file diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb index 9ae4761..a2a8fe2 100644 --- a/spec/code/branch_spec.rb +++ b/spec/code/branch_spec.rb @@ -11,8 +11,9 @@ module Code end before do - allow(Config).to receive(:get_master_branch_name).and_return "master" - allow(Config).to receive(:get_development_branch_name).and_return "development" + allow(Config).to receive(:master_branch_name).and_return "master" + allow(Config).to receive(:development_branch_name).and_return "development" + allow(Config).to receive(:ready_label).and_return "awaiting review" allow(System).to receive(:puts) Branch.setup_test_repo end @@ -436,17 +437,17 @@ module Code end end - describe "#mark_prs_as_awaiting_review" do + describe "#mark_prs_as_ready" do it "calls #label_prs with 'awaiting review'" do random_branch = Branch.new "random" expect(random_branch).to receive(:label_prs).with("awaiting review") - random_branch.mark_prs_as_awaiting_review + random_branch.mark_prs_as_ready end it "throws NoPRError if branch has no pull requests" do random_branch = Branch.new "random" expect(random_branch).to receive(:has_pull_request?).and_return(false) - expect { random_branch.mark_prs_as_awaiting_review }.to raise_error Branch::NoPRError + expect { random_branch.mark_prs_as_ready }.to raise_error Branch::NoPRError end end @@ -476,4 +477,63 @@ module Code end end end + + describe "user prompt" do + it "should be made during call to self.development to get the name of the development branch" do + expect(Config).to receive(:development_branch_name) + Branch.development + end + + it "should be made during call to self.development to get the name of the development branch" do + expect(Config).to receive(:master_branch_name) + Branch.master + end + + it "should be made during call to #development? to decide if the current branch is the development branch" do + expect(Config).to receive(:development_branch_name).and_return "test" + branch = Branch.new "test" + expect(branch.development?).to eq true + end + + it "should be made during call to #master? to decide if the current branch is the master branch" do + expect(Config).to receive(:master_branch_name).and_return "test" + branch = Branch.new "test" + expect(branch.master?).to eq true + end + + it "should be made during call to #protected? to decide if the current branch is either master or development" do + expect(Config).to receive(:development_branch_name).twice.and_return "test-dev" + expect(Config).to receive(:master_branch_name).twice.and_return "test-master" + branch = Branch.new "test-dev" + expect(branch.protected?).to eq true + branch = Branch.new "test-master" + expect(branch.protected?).to eq true + end + + it "should be made during call to #feature? to decide if the current branch is either master or development" do + expect(Config).to receive(:development_branch_name).twice.and_return "test-dev" + expect(Config).to receive(:master_branch_name).twice.and_return "test-master" + branch = Branch.new "test-dev" + expect(branch.feature?).to eq false + branch = Branch.new "test-master" + expect(branch.feature?).to eq false + end + + it "should be made during call to #delete! to decide if the current branch is protected" do + expect(Config).to receive(:development_branch_name).and_return "test-dev" + expect(Config).to receive(:master_branch_name).and_return "test-master" + + allow(System).to receive(:call) + branch = Branch.new "test" + branch.delete! + end + + it "should be made during call to #mark_prs_as_ready to determine the label marking the PR as ready" do + expect(Config).to receive(:ready_label).and_return "test-ready" + + branch = Branch.new "test" + allow(branch).to receive(:label_prs) + branch.mark_prs_as_ready + end + end end diff --git a/spec/code/config_spec.rb b/spec/code/config_spec.rb index 30e3cd3..108eb1e 100644 --- a/spec/code/config_spec.rb +++ b/spec/code/config_spec.rb @@ -56,25 +56,25 @@ module Code end end - describe "get_master_branch_name" do + describe "#master_branch_name" do context "when there is no previously stored property value" do with_file_and_content(".codeconfig", "") - let(:prompt_text) { "You didn't set the name of the master branch. What is it ('master')?" } + let(:prompt_text) { "You didn't set the name of the master branch. What is it? (master)" } it "asks the user for the property value" do expect(System).to receive(:prompt).with prompt_text - Config.get_master_branch_name + Config.master_branch_name end it "should return the value the user has provided" do allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") - expect(Config.get_master_branch_name).to eq ("test-value") + expect(Config.master_branch_name).to eq ("test-value") end it "should store the provided value into the file" do allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") - Config.get_master_branch_name + Config.master_branch_name file_content = File.read ".codeconfig" expect(file_content.strip).to eq "master_branch = \"test-value\"" end @@ -84,34 +84,34 @@ module Code with_file_and_content(".codeconfig", "master_branch = \"value\"\n") it "should not prompt the user for the property value" do expect(System).not_to receive(:prompt) - Config.get_master_branch_name + Config.master_branch_name end it "should return the stored property value" do - expect(Config.get_master_branch_name).to eq "value" + expect(Config.master_branch_name).to eq "value" end end end - describe "get_development_branch_name" do + describe "#development_branch_name" do context "when there is no previously stored property value" do with_file_and_content(".codeconfig", "") - let(:prompt_text) { "You didn't set the name of the development branch. What is it ('development')?" } + let(:prompt_text) { "You didn't set the name of the development branch. What is it? (development)" } it "asks the user for the property value" do expect(System).to receive(:prompt).with prompt_text - Config.get_development_branch_name + Config.development_branch_name end it "should return the value the user has provided" do allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") - expect(Config.get_development_branch_name).to eq ("test-value") + expect(Config.development_branch_name).to eq ("test-value") end it "should store the provided value into the file" do allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") - Config.get_development_branch_name + Config.development_branch_name file_content = File.read ".codeconfig" expect(file_content.strip).to eq "development_branch = \"test-value\"" end @@ -121,13 +121,62 @@ module Code with_file_and_content(".codeconfig", "development_branch = \"value\"\n") it "should not prompt the user for the property value" do expect(System).not_to receive(:prompt) - Config.get_development_branch_name + Config.development_branch_name end it "should return the stored property value" do - expect(Config.get_development_branch_name).to eq "value" + expect(Config.development_branch_name).to eq "value" end end end + + describe "#ready_label" do + context "when there is no previously stored property value" do + with_file_and_content(".codeconfig", "") + let(:prompt_text) { "You didn't set the label used for marking the PR as ready. What is it? (awaiting review)" } + + it "asks the user for the property value" do + expect(System).to receive(:prompt).with prompt_text + Config.ready_label + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + + expect(Config.ready_label).to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + Config.ready_label + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "ready_label = \"test-value\"" + end + end + + context "when there is a previously stored property value" do + with_file_and_content(".codeconfig", "ready_label = \"value\"\n") + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.ready_label + end + + it "should return the stored property value" do + expect(Config.ready_label).to eq "value" + end + end + end + + describe "default" do + with_file_and_content(".codeconfig", "") + + it "asks the user for the property value" do + allow(System).to receive(:prompt).and_return("\n") + + expect(Config.master_branch_name).to eq "master" + expect(Config.development_branch_name).to eq "development" + expect(Config.ready_label).to eq "awaiting review" + end + end end end diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index b71709f..9d62fed 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -18,8 +18,8 @@ module Code end before do - allow(Config).to receive(:get_master_branch_name).and_return "master" - allow(Config).to receive(:get_development_branch_name).and_return "development" + allow(Config).to receive(:master_branch_name).and_return "master" + allow(Config).to receive(:development_branch_name).and_return "development" allow(System).to receive(:puts) Git.setup_test_repo @@ -251,81 +251,4 @@ module Code end end end - - describe "user prompts" do - let(:git) { Git.new } - - before(:all) do - Git.send :include, GitExtras - end - - before do - allow(System).to receive(:puts) - Git.setup_test_repo - end - - it "should be made during call to #start to get the name of the development branch" do - expect(Config).to receive(:get_development_branch_name).exactly(3).times.and_return "test" - - allow(System).to receive(:call).with("checkout test") - allow(System).to receive(:call).with("pull origin test:test") - allow(System).to receive(:call).with("branch new-feature") - allow(System).to receive(:call).with("checkout new-feature") - - git.start "new-feature" - end - - it "should be made during call to #hotfix to get the name of the master branch" do - expect(Config).to receive(:get_master_branch_name).exactly(3).times.and_return "test" - - allow(System).to receive(:call).with("checkout test") - allow(System).to receive(:call).with("pull origin test:test") - allow(System).to receive(:call).with("branch hotfix-branch") - allow(System).to receive(:call).with("checkout hotfix-branch") - - git.hotfix "branch" - end - - it "should be made during call to #cancel to get the name of the development_branch" do - expect(Config).to receive(:get_development_branch_name).exactly(2).times.and_return "test" - - dev_branch = Branch.create "test" - allow(git).to receive(:current_branch).and_return(dev_branch) - allow(STDOUT).to receive(:puts).with("Nothing to cancel (already on test)") - - git.cancel - end - - it "should be made during call to #publish to get the names of master and development branches" do - expect(Config).to receive(:get_development_branch_name).exactly(2).times.and_return "test" - expect(Config).to receive(:get_master_branch_name).times.and_return "test" - - branch = Branch.create("test-feature") - allow(git).to receive(:current_branch).and_return(branch) - allow(git).to receive(:uncommitted_changes?).and_return(false) - allow(git).to receive(:main_repo).and_return("test_org/test_repo") - allow(git).to receive(:push) - - allow(System).to receive(:exec).with("git ls-remote --get-url origin").and_call_original - allow(System).to receive(:exec).with("hub pull-request -f \"Test feature\" -b test_org/test_repo:test -h test_org/test_repo:test-feature").and_return("something") - - git.publish - end - - it "should be made during call to #finish, to get the name of the development_branch" do - expect(Config).to receive(:get_development_branch_name).exactly(3).times.and_return "dev-test" - expect(Config).to receive(:get_master_branch_name).times.and_return "master-test" - - branch = Branch.new("test-branch") - allow(git).to receive(:current_branch).and_return(branch) - - allow(System).to receive(:call).with("checkout dev-test") - allow(System).to receive(:call).with("pull origin dev-test:dev-test") - allow(System).to receive(:call).with("branch -d test-branch") - - allow(git).to receive(:fetch) - - git.finish - end - end end From 96bbed9e3dac07a464e23c6e6a14074835da99ed Mon Sep 17 00:00:00 2001 From: Venkat Dinavahi Date: Tue, 4 Aug 2015 13:30:59 -0400 Subject: [PATCH 64/65] added reveal command and bundle command --- .codeconfig | 1 + bin/code | 8 ++++++++ lib/code/git.rb | 11 +++++++++++ lib/code/system.rb | 8 ++++++++ lib/code/system/os.rb | 10 +++++++--- 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .codeconfig diff --git a/.codeconfig b/.codeconfig new file mode 100644 index 0000000..9c37b34 --- /dev/null +++ b/.codeconfig @@ -0,0 +1 @@ +development_branch=development \ No newline at end of file diff --git a/bin/code b/bin/code index a0f5c7c..2f4ca30 100755 --- a/bin/code +++ b/bin/code @@ -143,6 +143,14 @@ command :merged do |c| end end +desc 'Bundles up the current repo' +command :bundle do |c| + c.action do |global_options, options, args| + bundle_path = $git.bundle + Code::System.reveal(bundle_path) + end +end + pre do |global, command, options, args| $git = Code::Git.new $github_api = Code::GitHubAPI.new diff --git a/lib/code/git.rb b/lib/code/git.rb index d21245f..3e8825d 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -54,6 +54,11 @@ def cancel end end + def bundle + System.call "bundle create #{default_bundle_path} --all" + default_bundle_path + end + # NOT used anywhere at the moment. Did we loose track of a supported command? def commit(message) System.call 'add -A' @@ -96,6 +101,12 @@ def prune_remote_branches private + def default_bundle_path + parent_directory = File.expand_path('..', Dir.pwd) + file_name = System.current_directory_name + '.bundle' + File.join(parent_directory, file_name) + end + def stash System.call('stash') end diff --git a/lib/code/system.rb b/lib/code/system.rb index c3804cb..0b286f7 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -23,6 +23,10 @@ def prompt_hidden(prompt_text) input.strip end + def current_directory_name + File.basename(Dir.getwd) + end + def error(message) abort red(message) end @@ -33,7 +37,11 @@ def open_in_browser(url) def open(item) command = OS.current.open_command + `#{command} #{item}` + end + def reveal(item) + command = OS.current.reveal_command `#{command} #{item}` end diff --git a/lib/code/system/os.rb b/lib/code/system/os.rb index 80b4c7e..f9fbcb7 100644 --- a/lib/code/system/os.rb +++ b/lib/code/system/os.rb @@ -27,11 +27,15 @@ def linux? end def open_command - command = "open" if osx? - command = "xdg-open" if linux? + return "open" if osx? + return "xdg-open" if linux? + end - command + def reveal_command + return "open -R" if osx? + return "xdg-open" if linux? end + end end end From 0010982196bd3d350e04ac38ab592534d0d6762d Mon Sep 17 00:00:00 2001 From: Venkat Dinavahi Date: Tue, 4 Aug 2015 13:32:46 -0400 Subject: [PATCH 65/65] added codeconfig --- .codeconfig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.codeconfig b/.codeconfig index 9c37b34..985d671 100644 --- a/.codeconfig +++ b/.codeconfig @@ -1 +1,3 @@ -development_branch=development \ No newline at end of file +development_branch = "development" +master_branch = "master" +