Class: CommitLive::Open

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-live/lesson/open.rb

Constant Summary collapse

HOME_DIR =
File.expand_path("~")
ALLOWED_TYPES =
["LAB", "PRACTICE"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpen

Returns a new instance of Open.



17
18
19
20
21
22
23
24
# File 'lib/commit-live/lesson/open.rb', line 17

def initialize()
  if File.exists?("#{HOME_DIR}/.ga-config")
    @rootDir = YAML.load(File.read("#{HOME_DIR}/.ga-config"))[:workspace]
  end
  @lesson = CommitLive::Current.new
  @lesson_status = CommitLive::Status.new
  @sentry = CommitLive::Sentry.new()
end

Instance Attribute Details

#forkedRepoObject (readonly)

Returns the value of attribute forkedRepo.



12
13
14
# File 'lib/commit-live/lesson/open.rb', line 12

def forkedRepo
  @forkedRepo
end

#lessonObject (readonly)

Returns the value of attribute lesson.



12
13
14
# File 'lib/commit-live/lesson/open.rb', line 12

def lesson
  @lesson
end

#lesson_statusObject (readonly)

Returns the value of attribute lesson_status.



12
13
14
# File 'lib/commit-live/lesson/open.rb', line 12

def lesson_status
  @lesson_status
end

#rootDirObject (readonly)

Returns the value of attribute rootDir.



12
13
14
# File 'lib/commit-live/lesson/open.rb', line 12

def rootDir
  @rootDir
end

#sentryObject (readonly)

Returns the value of attribute sentry.



12
13
14
# File 'lib/commit-live/lesson/open.rb', line 12

def sentry
  @sentry
end

Instance Method Details

#cdToLessonObject



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/commit-live/lesson/open.rb', line 125

def cdToLesson
  puts "Opening lesson..."
  Dir.chdir("#{rootDir}/#{lesson_name}")
  puts "Done."
  if File.exists?("#{HOME_DIR}/.lastdirectory")
    filename = "#{HOME_DIR}/.lastdirectory"
    File.open(filename, 'w') do |out|
      out << "#{rootDir}/#{lesson_name}"
    end
  end
  exec("#{ENV['SHELL']} -l")
end

#change_grp_ownerObject



121
122
123
# File 'lib/commit-live/lesson/open.rb', line 121

def change_grp_owner
  system("chgrp -R ubuntu #{rootDir}/#{lesson_name}")
end

#cloneCurrentLessonObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/commit-live/lesson/open.rb', line 102

def cloneCurrentLesson
  puts "Cloning lesson..."
  begin
    Timeout::timeout(15) do
      Git.clone(ssh_url, lesson_name, path: rootDir)
    end
  rescue Git::GitExecuteError => err
    sentry.log_exception(err,
      {
        'event': 'cloning',
        'lesson_name' => lesson_name,
      }
    )
  rescue Timeout::Error
    puts "Cannot clone this lesson right now. Please try again."
    exit
  end
end

#forkCurrentLessonObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/commit-live/lesson/open.rb', line 82

def forkCurrentLesson
  puts "Forking lesson..."
  github = CommitLive::Github.new()
  begin
    Timeout::timeout(15) do
      @forkedRepo = github.client.fork(lesson_repo)
    end
  rescue Octokit::Error => err
    sentry.log_exception(err,
      {
        'event': 'forking',
        'lesson_name' => lesson_name,
      }
    )
  rescue Timeout::Error
    puts "Please check your internet connection."
    exit
  end
end

#lesson_forkedObject



42
43
44
# File 'lib/commit-live/lesson/open.rb', line 42

def lesson_forked
  lesson.getValue('forked')
end

#lesson_nameObject



34
35
36
# File 'lib/commit-live/lesson/open.rb', line 34

def lesson_name
  lesson.getValue('track_slug')
end

#lesson_repoObject



46
47
48
# File 'lib/commit-live/lesson/open.rb', line 46

def lesson_repo
  lesson.getValue('repo_url')
end

#lesson_typeObject



38
39
40
# File 'lib/commit-live/lesson/open.rb', line 38

def lesson_type
  lesson.getValue('type')
end

#open_lessonObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/commit-live/lesson/open.rb', line 138

def open_lesson
  begin
    Timeout::timeout(15) do
      api = CommitLive::API.new("https://chat.commit.live")
      netrc = CommitLive::NetrcInteractor.new()
      netrc.read(machine: 'ga-extra')
      username = netrc.
      url = URI.escape("/send/#{username}")
      message = {
        'type': 'open-lesson',
        'title': lesson_name
      }
      response = api.post(
        url,
        headers: {
          'content-type': 'application/json',
        },
        body: {
          'message': Oj.dump(message, mode: :compat),
        }
      )
    end
  rescue Timeout::Error
    puts "Open Lesson WebSocket call failed."
    exit
  end
end

#openALesson(puzzle_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/commit-live/lesson/open.rb', line 50

def openALesson(puzzle_name)
  # get currently active lesson
  puts "Getting lesson..."
  lesson.getCurrentLesson(puzzle_name)

  if !ALLOWED_TYPES.include? lesson_type
    puts "This is a read only lesson!"
    exit
  end

  if !File.exists?("#{rootDir}/#{lesson_name}")
    # fork lesson repo via github api
    if lesson_type == "LAB"
      forkCurrentLesson
    end
    # clone forked lesson into machine
    cloneCurrentLesson
    # change group owner
    change_grp_owner
    # lesson forked API change
    if lesson_type == "LAB" && !lesson_forked
      lesson_status.update('forked', lesson_name)
    end
    if lesson_type == "PRACTICE"
      open_lesson
    end
  end
  # install dependencies
  # cd into it and invoke bash
  cdToLesson
end

#ssh_urlObject



26
27
28
29
30
31
32
# File 'lib/commit-live/lesson/open.rb', line 26

def ssh_url
  if lesson_type === "PRACTICE"
    "[email protected]:#{lesson_repo}.git"
  else
    forkedRepo.ssh_url
  end
end