class CommitLeaderboardStage

Attributes

auhor_avatar_url[RW]
author_username[RW]
options[RW]
queue_file_path[RW]

Public Instance Methods

run(args, options) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 6
def run(args, options)
  abort "No author username given." if args[0].nil?
  abort "No author avatar url given." if args[1].nil?

  process_params(args, options)

  check_required_tools
  check_if_repository_is_present
  check_if_commit_is_present

  append_to_queue(build_commit_payload)
end

Private Instance Methods

append_to_queue(payload) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 62
def append_to_queue(payload)
  `echo [] > #{@queue_file_path}` unless File.exist?(@queue_file_path)

  commits = read_commits_from_queue(payload)
  if @options.verbose
    puts ">> Adding commit to the queue:"
    print_queue_item(payload)
  end
  File.open(@queue_file_path, "w") do |file|
    commits << payload
    file.write(JSON.generate(commits))
  end
end
build_commit_payload() click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 49
def build_commit_payload
  {
    sha: `git rev-parse HEAD`.strip,
    message: `git log -1 --pretty=%B`.strip,
    repository: `basename $(git rev-parse --show-toplevel)`.strip,
    branch: `git rev-parse --abbrev-ref HEAD`.strip,
    author: {
      username: @author_username,
      avatar_url: @author_avatar_url
    }
  }
end
check_if_commit_is_present() click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 43
def check_if_commit_is_present
  return if system("git rev-parse --verify HEAD", out: File::NULL)

  abort(">> No commits found. Please commit your changes first.")
end
check_if_repository_is_present() click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 37
def check_if_repository_is_present
  return if system("git rev-parse --is-inside-work-tree", out: File::NULL)

  abort(">> Not a git repository. Please run this command in a git repository.")
end
check_required_tools() click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 31
def check_required_tools
  return if system("git --version", out: File::NULL)

  abort(">> Git is not installed. Please install it first.")
end
print_queue_item(payload) click to toggle source
process_params(args, options) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 21
def process_params(args, options)
  @options = options

  @author_username = args[0]
  @author_avatar_url = args[1]

  @queue_file_path = args[2] || "~/.renuo-commit-leaderboard.json"
  @queue_file_path = File.expand_path(@queue_file_path)
end
read_commits_from_queue(payload) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 76
def read_commits_from_queue(payload)
  commits = nil

  File.open(@queue_file_path, "r") do |file|
    commits = JSON.parse(file.read)

    if commits.any? { |commit| commit["sha"] == payload[:sha] }
      abort(">> Commit has already been added to the queue")
    end
  rescue JSON::ParserError
    abort(">> Invalid JSON format in the queue file.")
  end

  commits
end
truncate_text(text, truncate_length) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_stage.rb, line 106
def truncate_text(text, truncate_length)
  text[0..truncate_length]
end