class Greyatom::Submit::GitHelper

Constants

REPO_BELONGS_TO_US

Attributes

currentLesson[R]
dummyUsername[R]
git[R]
netrc[R]
remote_name[RW]

Public Class Methods

new() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 15
def initialize()
  @git = setGit
  @dummyUsername = 'gitint'
  @netrc = Greyatom::NetrcInteractor.new()
  @currentLesson = Greyatom::Current.new
end

Public Instance Methods

commitAndPush() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 22
def commitAndPush
  checkRemote
  addChanges
  commitChanges

  push
  createPullRequest
end

Private Instance Methods

addChanges() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 62
def addChanges
  puts 'Adding changes...'
  git.add(all: true)
end
checkRemote() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 49
def checkRemote
  netrc.read(machine: 'ga-extra')
  username = dummyUsername || 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
commitChanges() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 67
def commitChanges
  puts 'Committing changes...'
  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."
      exit 1
    else
      puts 'Sorry, something went wrong. Please try again.'
      exit 1
    end
  end
end
createPullRequest() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 99
def createPullRequest
  puts 'Creating Pull Request...'
  currentLesson.getCurrentLesson
  userGithub = Greyatom::Github.new()
  netrc.read
  username = dummyUsername || netrc.login
  begin
    Timeout::timeout(45) do
      parentRepo = currentLesson.getAttr('github_repo')
      pullRequest = userGithub.client.create_pull_request(parentRepo, 'master', "#{username}:master", "PR by #{username}")
      puts "Lesson submitted successfully!"
    end
  rescue Octokit::Error => err
    puts "Error while creating PR!"
    puts err
    exit
  rescue Timeout::Error
    puts "Please check your internet connection."
    exit
  end
end
push() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 82
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
    puts 'There was an error while pushing. Please try again later.'
    puts e.message
    exit 1
  rescue Timeout::Error
    puts "Can't reach GitHub right now. Please try again."
    exit 1
  end
end
setGit() click to toggle source
# File lib/greyatom/lesson/git-helper.rb, line 33
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