lib/learn_open/opener.rb in learn-open-1.1.59 vs lib/learn_open/opener.rb in learn-open-1.2.0

- old
+ new

@@ -1,17 +1,17 @@ module LearnOpen class Opener - attr_reader :editor, :client, :lessons_dir, :file_path, :get_next_lesson - attr_accessor :lesson, :repo_dir, :lesson_is_lab, :lesson_id, :later_lesson + attr_reader :editor, :client, :lessons_dir, :file_path, :get_next_lesson, :token + attr_accessor :lesson, :repo_dir, :lesson_is_lab, :lesson_id, :later_lesson, :dot_learn def self.run(lesson:, editor_specified:, get_next_lesson:) new(lesson, editor_specified, get_next_lesson).run end def initialize(lesson, editor, get_next_lesson) - _login, token = Netrc.read['learn-config'] - @client = LearnWeb::Client.new(token: token) + _login, @token = Netrc.read['learn-config'] + @client = LearnWeb::Client.new(token: @token) @lesson = lesson @editor = editor @get_next_lesson = get_next_lesson @lessons_dir = YAML.load(File.read(File.expand_path('~/.learn-config')))[:learn_directory] @@ -37,10 +37,33 @@ end end private + def ping_fork_completion(retries=3) + begin + Timeout::timeout(15) do + client.submit_event( + event: 'fork', + learn_oauth_token: token, + repo_name: repo_dir, + base_org_name: lesson.split('/')[0], + forkee: { full_name: nil } + ) + end + rescue Timeout::Error + if retries > 0 + puts "There was a problem forking and cloning this lesson. Retrying..." + ping_fork_completion(retries-1) + else + puts "There is an issue connecting to Learn. Please try again." + File.write(file_path, 'ERROR: Error connecting to Learn') + exit + end + end + end + def warn_if_necessary temp_args = nil if self.later_lesson puts 'WARNING: You are attempting to open a lesson that is beyond your current lesson.' @@ -81,22 +104,25 @@ puts "Getting current lesson..." self.lesson = get_current_lesson_forked_repo self.lesson_is_lab = current_lesson.lab self.lesson_id = current_lesson.id self.later_lesson = false + self.dot_learn = current_lesson.dot_learn elsif !lesson && get_next_lesson puts "Getting next lesson..." self.lesson = get_next_lesson_forked_repo self.lesson_is_lab = next_lesson.lab self.lesson_id = next_lesson.id self.later_lesson = false + self.dot_learn = next_lesson.dot_learn else puts "Looking for lesson..." - self.lesson = ensure_correct_lesson.repo_slug + self.lesson = ensure_correct_lesson.clone_repo self.lesson_is_lab = correct_lesson.lab self.lesson_id = correct_lesson.lesson_id self.later_lesson = correct_lesson.later_lesson + self.dot_learn = correct_lesson.dot_learn end self.repo_dir = lesson.split('/').last end @@ -109,11 +135,11 @@ end def get_current_lesson_forked_repo(retries=3) begin Timeout::timeout(15) do - current_lesson.forked_repo + current_lesson.clone_repo end rescue Timeout::Error if retries > 0 puts "There was a problem getting your lesson from Learn. Retrying..." get_current_lesson_forked_repo(retries-1) @@ -126,11 +152,11 @@ end def get_next_lesson_forked_repo(retries=3) begin Timeout::timeout(15) do - next_lesson.forked_repo + next_lesson.clone_repo end rescue Timeout::Error if retries > 0 puts "There was a problem getting your next lesson from Learn. Retrying..." get_next_lesson_forked_repo(retries-1) @@ -165,23 +191,26 @@ def fork_repo(retries=3) if !repo_exists? File.write(file_path, 'Forking repository...') puts "Forking lesson..." - begin - Timeout::timeout(15) do - client.fork_repo(repo_name: repo_dir) + + if !dot_learn || dot_learn[:github] != false + begin + Timeout::timeout(15) do + client.fork_repo(repo_name: repo_dir) + end + rescue Timeout::Error + if retries > 0 + puts "There was a problem forking this lesson. Retrying..." + fork_repo(retries-1) + else + puts "There is an issue connecting to Learn. Please try again." + File.write(file_path, 'ERROR: Error connecting to Learn') + exit + end end - rescue Timeout::Error - if retries > 0 - puts "There was a problem forking this lesson. Retrying..." - fork_repo(retries-1) - else - puts "There is an issue connecting to Learn. Please try again." - File.write(file_path, 'ERROR: Error connecting to Learn') - exit - end end end end def clone_repo(retries=3) @@ -211,10 +240,14 @@ File.write(file_path, 'ERROR: Error cloning. Try again.') exit end end end + + if dot_learn && dot_learn[:github] == false + ping_fork_completion + end end def repo_exists? File.exists?("#{lessons_dir}/#{repo_dir}") end @@ -240,13 +273,13 @@ puts "Sorry, there seems to be a problem with this lesson. Please submit a bug report to bugs@learn.co and try again later." puts "If you'd like to work on your next lesson now, type: learn next" File.write(file_path, 'ERROR: Problem parsing lesson data. Try again.') exit end - rescue NoMethodError => e + rescue NoMethodError, Errno::ENOENT => e if xcodeproj_file? || xcworkspace_file? true - elsif e.message.match(/for false:FalseClass/) + elsif e.message.match(/for false:FalseClass/) || e.message.match(/No such file or directory/) false else puts "Sorry, there seems to be a problem with this lesson. Please submit a bug report to bugs@learn.co and try again later." puts "If you'd like to work on your next lesson now, type: learn next" File.write(file_path, 'ERROR: Problem parsing lesson data. Try again.')