class CommitLive::Submit::GitHelper

Constants

HOME_DIR
REPO_BELONGS_TO_US

Attributes

currentLesson[R]
git[R]
lessonName[R]
netrc[R]
remote_name[RW]
rootDir[R]
sentry[R]
status[R]
track_slug[R]

Public Class Methods

new(trackSlug) click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 18
def initialize(trackSlug)
        @track_slug = trackSlug
        @git = setGit
        @netrc = CommitLive::NetrcInteractor.new()
        @currentLesson = CommitLive::Current.new
        @status = CommitLive::Status.new
        @lessonName = repo_name(remote: 'origin')
        @sentry = CommitLive::Sentry.new()
        if File.exists?("#{HOME_DIR}/.ga-config")
                @rootDir = YAML.load(File.read("#{HOME_DIR}/.ga-config"))[:workspace]
        end
end

Public Instance Methods

commitAndPush() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 31
def commitAndPush
        checkRemote
        check_if_practice_lesson
        check_if_user_in_right_folder
        # Check if User passed test cases
        if is_test_case_passed
                # Push to User's Github
                addChanges
                commitChanges
                push

                if !is_submitted_pr
                        # Create Pull Request
                        createPullRequest
                        update_lesson_status
                end

                puts "Done."
        else
                puts "It seems you have not passed all the test cases."
                puts "Please execute `clive test` before submitting your code!"
                exit
        end
end

Private Instance Methods

addChanges() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 152
def addChanges
        git.add(all: true)
end
checkRemote() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 140
def checkRemote
        netrc.read(machine: 'ga-extra')
        username = netrc.login
        if git.remote.url.match(/#{username}/i).nil? && git.remote.url.match(/#{REPO_BELONGS_TO_US.join('|').gsub('-','\-')}/i).nil?
                puts "It doesn't look like you're in a lesson directory."
                puts 'Please cd into an appropriate directory and try again.'
                exit 1
        else
                self.remote_name = git.remote.name
        end
end
check_if_practice_lesson() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 101
def check_if_practice_lesson
        currentLesson.getCurrentLesson(track_slug)
        if is_project || is_practice
                puts 'This is a Project. Go to individual assignments and follow intructions given on how to submit them.' if is_project
                puts 'This is a Practice Lesson. No need to submit your code.' if is_practice
                exit 1
        end
end
check_if_user_in_right_folder() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 110
def check_if_user_in_right_folder
        dirname = File.basename(Dir.getwd)
        if dirname != title_slug
                table = Terminal::Table.new do |t|
                        t.rows = [["cd ~/Workspace/code/#{title_slug}/"]]
                end
                puts "It seems that you are in the wrong directory."
                puts "Use the following command to go there"
                puts table
                puts "Then use the `clive submit <track-slug>` command"
                exit 1
        end
end
commitChanges() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 156
def commitChanges
        begin
                git.commit('Done')
        rescue Git::GitExecuteError => e
                if e.message.match(/nothing to commit/)
                        puts "It looks like you have no changes to commit."
                        puts "Pushing previous commits if any."
                else
                        puts 'Sorry, something went wrong. Please try again.'
                        exit 1
                end
        end
end
createPullRequest() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 214
def createPullRequest
        puts 'Creating Pull Request...'
        github = CommitLive::Github.new()
        github.post(repo_url)
end
dir_path() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 58
def dir_path
        filePath = "#{title_slug}/"
        filePath += "#{test_slug}/" if is_project_assignment
        return filePath
end
is_practice() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 88
def is_practice
        lessonType = currentLesson.getValue('type')
        !lessonType.nil? && lessonType == "PRACTICE"
end
is_project() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 83
def is_project
        isProject = currentLesson.getValue('isProject')
        !isProject.nil? && isProject == 1
end
is_project_assignment() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 73
def is_project_assignment
        isProjectAssignment = currentLesson.getValue('isProjectAssignment')
        !isProjectAssignment.nil? && isProjectAssignment == 1
end
is_submitted_pr() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 78
def is_submitted_pr
        isSubmittedPr = currentLesson.getValue('submittedPr')
        !isSubmittedPr.nil? && isSubmittedPr == 1
end
is_test_case_passed() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 64
def is_test_case_passed
        isTestCasesPassed = currentLesson.getValue('testCasesPassed')
        !isTestCasesPassed.nil? && isTestCasesPassed == 1
end
pull_changes() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 174
def pull_changes
        begin
                Timeout::timeout(15) do
                        git.pull
                        puts "Now you will have to run `clive submit` again."
                        exit 1
                end
        rescue Timeout::Error
                puts "Can't reach GitHub right now. Please try again."
                exit 1
        end
end
push() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 187
def push()
        puts 'Pushing changes to GitHub...'
        push_remote = git.remote(self.remote_name)
        begin
                Timeout::timeout(15) do
                        git.push(push_remote)
                end
        rescue Git::GitExecuteError => e
                rollback_last_commit()
                if e.message.match(/'git pull ...'/)
                        puts "There are some remote changes to pull."
                        puts "Pulling the changes..."
                        pull_changes
                else
                        sentry.log_exception(e,
                                {
                                        'event': 'pushing',
                                        'lesson_name' => lessonName,
                                }
                        )
                end
        rescue Timeout::Error
                puts "Can't reach GitHub right now. Please try again."
                exit 1
        end
end
repo_name(remote: remote_name) click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 220
def repo_name(remote: remote_name)
        url = git.remote(remote).url
        url.match(/^.+[\w-]+\/(.*?)(?:\.git)?$/)[1]
end
repo_url() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 93
def repo_url
        currentLesson.getValue('repoUrl')
end
rollback_last_commit() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 170
def rollback_last_commit
        system("git reset HEAD~1")
end
setGit() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 124
def setGit
        begin
                Git.open(FileUtils.pwd)
        rescue ArgumentError => e
                if e.message.match(/path does not exist/)
                        puts "It doesn't look like you're in a lesson directory."
                        puts 'Please cd into an appropriate directory and try again.'

                        exit 1
                else
                        puts 'Sorry, something went wrong. Please try again.'
                        exit 1
                end
        end
end
test_slug() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 69
def test_slug
        currentLesson.getValue('testCase')
end
title_slug() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 97
def title_slug
        currentLesson.getValue('titleSlug')
end
update_lesson_status() click to toggle source
# File lib/commit-live/lesson/git-helper.rb, line 225
def update_lesson_status
        file_path = "#{rootDir}/#{dir_path}/build.py"
        status.update('submittedPr', track_slug, true, {}, file_path)
end