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 =
["CODE", "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

#forked_ssh_urlObject (readonly)

Returns the value of attribute forked_ssh_url.



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

def forked_ssh_url
  @forked_ssh_url
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



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

def cloneCurrentLesson
	puts "Cloning lesson..."
	begin
		Git.clone(ssh_url, lesson_name, path: rootDir)
	rescue Git::GitExecuteError => err
		sentry.log_exception(err,
			{
				'event': 'cloning',
				'lesson_name' => lesson_name,
				'current-directory' => Dir.pwd
			}
		)
	end
end

#forkCurrentLessonObject



99
100
101
102
103
104
# File 'lib/commit-live/lesson/open.rb', line 99

def forkCurrentLesson
	puts "Forking lesson..."
	github = CommitLive::Github.new()
	github.post(lesson_repo, true)
	@forked_ssh_url = github.getValue("sshUrl")
end

#is_projectObject



56
57
58
59
# File 'lib/commit-live/lesson/open.rb', line 56

def is_project
	isProject = lesson.getValue('isProject')
	!isProject.nil? && isProject == 1
end

#is_project_assignmentObject



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

def is_project_assignment
	isProjectAssignment = lesson.getValue('isProjectAssignment')
	!isProjectAssignment.nil? && isProjectAssignment == 1
end

#lesson_forkedObject



51
52
53
54
# File 'lib/commit-live/lesson/open.rb', line 51

def lesson_forked
	forked = lesson.getValue('forked')
	!forked.nil? && forked == 1
end

#lesson_nameObject



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

def lesson_name
	lesson.getValue('titleSlug')
end

#lesson_repoObject



61
62
63
# File 'lib/commit-live/lesson/open.rb', line 61

def lesson_repo
	lesson.getValue('repoUrl')
end

#lesson_typeObject



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

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
165
166
167
168
169
# 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': is_project_assignment ? track_slug : lesson_name,
				'message': {
					'fileName': 'readme.md',
					'type': 'forked',
					'value': true
				}
			}
			response = api.post(
				url,
				headers: {
					'content-type': 'application/json',
				},
				body: {
					'message': Oj.dump(message, mode: :compat),
				}
			)
		end
	rescue Timeout::Error
		puts "The Assignment already exists! Use the File-Browser to open files under it."
		exit
	end
end

#openALesson(puzzle_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/commit-live/lesson/open.rb', line 65

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 == "CODE"
			forkCurrentLesson
		end
		# clone forked lesson into machine
		cloneCurrentLesson
		# change group owner
		change_grp_owner
		# lesson forked API change
		if lesson_type == "PRACTICE" || is_project
			open_lesson
		end
	else
		open_lesson
	end
	if !is_project && lesson_type == "CODE" && !lesson_forked
		lesson_status.update('forked', track_slug)
	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
		forked_ssh_url
	end
end

#track_slugObject



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

def track_slug
	lesson.getValue('titleSlugTestCase')
end