Class: KnifeGithubCleanup::GithubCleanup

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/github_cleanup.rb

Overview

This module is intended to help you cleaning up your locate repo’s. It will check if your repo if insync with the github and will not touch if this is not the case. Then it will check if you have any branches local and not on the github.

If it cannot find any uncommitted changes, it will safely remove your repo. It’s good practice to cleanup and re-download repos because this way they can move from organization to organization.

Instance Method Summary collapse

Instance Method Details

#repo_cleanup(repo) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/knife/github_cleanup.rb', line 83

def repo_cleanup(repo)
  cookbook_path = config[:cookbook_path] || Chef::Config[:cookbook_path]
  cookbook = File.join(cookbook_path,repo)
  if File.exists?(cookbook)
    if repo_status_clean?(repo, cookbook)
      # delete the repo 
      ui.info("Processing [ DELETE  ] #{repo}")
      FileUtils.remove_entry(cookbook) 
    end
  else
    puts "cannot find repo path for: #{repo}" unless config[:all]
  end
end

#repo_status_clean?(repo, cookbook) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chef/knife/github_cleanup.rb', line 97

def repo_status_clean?(repo, cookbook)
  shell_out!("git fetch", :cwd => cookbook)
  status = shell_out!("git status", :cwd => cookbook)
  unless status.stdout == "# On branch master\nnothing to commit (working directory clean)\n"
    ui.info("Processing [ COMMIT  ] #{repo} (Action needed!)")
    status.stdout.lines.each { |l| puts l.sub( /^/, "    ") }
    return false   
  end
  log = shell_out!("git log --branches --not --remotes --simplify-by-decoration --decorate --oneline", :cwd => cookbook)
  unless log.stdout.empty?
    ui.info("Processing [ BRANCH  ] #{repo} (Action needed!)")
    ui.info("    Please check your branches, one of them has unsaved changes")
    log.stdout.lines.each { |l| puts l.sub( /^/, "    ") }
    return false   
  end
  return true
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef/knife/github_cleanup.rb', line 52

def run

  #executing shell commands
  extend Chef::Mixin::ShellOut

  # validate base options from base module.
  validate_base_options      

  # Display information if debug mode is on.
  display_debug_info

  # Gather all repo information from github.
  all_repos = get_all_repos(@github_organizations.reverse)

  # Get all chef cookbooks and versions (hopefully chef does the error handeling).
  cookbooks = rest.get_rest("/cookbooks?num_version=1")

  # Get the cookbook names from the command line
  @cookbook_name = name_args.first unless name_args.empty?
  if @cookbook_name
    # repo = all_repos.select { |k,v| v["name"] == @cookbook_name }
    repo_cleanup(@cookbook_name)
  elsif config[:all]
    cookbooks.each do |c,v|
      repo_cleanup(c)
    end
  else
    Chef::Log.error("Please specify a repo name")
  end
end