Class: WayOfWorking::Audit::Github::Generators::Exec

Inherits:
Thor::Group
  • Object
show all
Defined in:
lib/way_of_working/audit/github/generators/exec.rb

Overview

This generator runs the github audit

Instance Method Summary collapse

Instance Method Details

#check_for_github_organisation_environment_variablesObject



39
40
41
42
43
44
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 39

def check_for_github_organisation_environment_variables
  @github_organisation = ENV.fetch('GITHUB_ORGANISATION', nil)
  return unless @github_organisation.nil?

  abort(Rainbow("\nMissing GITHUB_ORGANISATION environment variable").red)
end

#check_for_github_token_environment_variablesObject



32
33
34
35
36
37
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 32

def check_for_github_token_environment_variables
  @github_token = ENV.fetch('GITHUB_TOKEN', nil)
  return unless @github_token.nil?

  abort(Rainbow("\nMissing GITHUB_TOKEN environment variable").red)
end

#check_github_organisation_remotesObject



50
51
52
53
54
55
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 50

def check_github_organisation_remotes
  abort(Rainbow("\nGitHub is not an upstream repository.").red) if github_organisation_remotes.empty?

  # say("Limiting audit to #{path}\n", :yellow) if path
  say "\nRunning..."
end

#filter_all_if_specifiedObject



66
67
68
69
70
71
72
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 66

def filter_all_if_specified
  return if options[:all] || options[:name]

  @repositories = @repositories.select do |repo|
    github_organisation_remotes.include?(repo.name)
  end
end

#filter_by_name_array_if_specifiedObject



74
75
76
77
78
79
80
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 74

def filter_by_name_array_if_specified
  return unless options[:name]

  @repositories = @repositories.select do |repo|
    options[:name].include?(repo.name)
  end
end

#filter_by_topic_if_specifiedObject



82
83
84
85
86
87
88
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 82

def filter_by_topic_if_specified
  return unless options[:topic]

  @repositories = @repositories.select do |repo|
    repo.topics.include?(options[:topic])
  end
end

#filter_by_visibility_if_specifiedObject



90
91
92
93
94
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 90

def filter_by_visibility_if_specified
  return unless options[:public]

  @repositories = @repositories.reject(&:private?)
end

#prep_auditObject



57
58
59
60
61
62
63
64
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 57

def prep_audit
  @auditor = ::WayOfWorking::Audit::Github::Auditor.new(@github_token, @github_organisation, options[:fix])

  # Loop though all the repos
  @repositories = @auditor.repositories
rescue Octokit::Unauthorized
  abort(Rainbow("\nGITHUB_TOKEN has expired or does not have sufficient permission").red)
end

#run_auditObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 96

def run_audit
  @audit_ok = true
  unarchived_repos.each do |repo|
    @auditor.audit(repo) do |rule|
      case rule.status
      when :not_applicable
        # Do nothing
      when :passed
        say "#{rule.tags.inspect} Passed #{rule.name}"
      when :failed
        say "#{rule.tags.inspect} Failed #{rule.name}: #{rule.errors.to_sentence}"
        @audit_ok = false
      else
        abort(Rainbow("Unknown response #{rule.status.inspect}").red)
      end
    end
  end
end

#run_lastObject



115
116
117
118
119
120
121
122
123
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 115

def run_last
  say("\nTotal time taken: #{(Time.now - @start_time).to_i} seconds", :yellow)

  if @audit_ok
    say("\nGitHub audit succeeded!", :green)
  else
    abort(Rainbow("\nGitHub audit failed!").red)
  end
end

#start_timerObject



46
47
48
# File 'lib/way_of_working/audit/github/generators/exec.rb', line 46

def start_timer
  @start_time = Time.now
end