Module: Github::CLI

Includes:
Logging
Included in:
Github
Defined in:
lib/github/cli.rb

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Instance Method Details

#check_token(*args, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/github/cli.rb', line 14

def check_token(*args, options)
  token = options[:token] || ""
  _assert_not_nil_token(token)
  
  url = "https://api.github.com/users/codertocat"
  res = HTTParty.get(url, headers: { 
  "Authorization" => "token #{token}"})
  data = JSON.parse(res.body)
  return unless !data.member?("login")
  logger.error "Github token is invalid."
  exit 1
end

#rename_default_branch(*args, options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/github/cli.rb', line 27

def rename_default_branch(*args, options)
  token = options[:token]
  
  repo_path = options[:repo] || ""
  _assert_not_nil_repo(repo_path)
  
  old_branch = args[0] || "master"
  new_branch = args[1] || "main"
  if old_branch == new_branch
    logger.error "Branch #{branch} is the same as #{based_on_branch}"
    exit 1
  end
  
  _rename_prompt(repo_path, token, old_branch, new_branch)
  
  repo = Github::RepoX.new(token, repo_path)
  # get the sha
  old_branch_sha = repo.branch_sha(old_branch)
  # create new branch
  logger.debug "Creating branch #{new_branch} based on #{old_branch}."
  repo.create_branch(new_branch, old_branch_sha) {
    _delete_branch_prompt(repo_path, token, new_branch)
    logger.debug "Deleting branch #{new_branch}."
    repo.delete_branch(new_branch)
  }
  # set default branch
  logger.debug "Setting #{new_branch} as default branch."
  repo.set_default_branch(new_branch)
  # delete old branch
  logger.debug "Deleting #{old_branch}."
  repo.delete_branch(old_branch)

  # local
  if new_branch == "main"
    global_default_branch = `git config --global init.defaultBranch`.strip
    if global_default_branch != new_branch
      `git config --global init.defaultBranch #{new_branch}`
      logger.debug "Setting #{new_branch} as global default branch."
    end
  end
  
  logger.info "Finished."
end