Class: Danger::DangerMissingCodeowners

Inherits:
Plugin
  • Object
show all
Defined in:
lib/missing_codeowners/plugin.rb

Overview

Parses the CODEOWNERS file and verifies if files have at least one owner. Works with GitHub and GitLab. Results are passed out as a table in markdown.

Examples:

Verifying files missing codeowners.


missing_codeowners.verify

See Also:

  • andre-alves/danger-missing_codeowners

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#files_missing_codeownersArray<String>

The list of files that are missing owners.

Returns:

  • (Array<String>)


21
22
23
# File 'lib/missing_codeowners/plugin.rb', line 21

def files_missing_codeowners
  @files_missing_codeowners
end

#ignored_filesArray<String>

The list of files to ignore. These are naive prefixes, so ‘foo/` will match `foo/bar`. No globbing.

Returns:

  • (Array<String>)


47
48
49
# File 'lib/missing_codeowners/plugin.rb', line 47

def ignored_files
  @ignored_files
end

#max_number_of_files_to_reportInt

The maximum number of files missing owners Danger should report. Default is 100.

Returns:

  • (Int)


31
32
33
# File 'lib/missing_codeowners/plugin.rb', line 31

def max_number_of_files_to_report
  @max_number_of_files_to_report
end

#severityString

Defines the severity level of the execution. Possible values are: ‘error’ or ‘warning’. Default is ‘error’.

Returns:

  • (String)


36
37
38
# File 'lib/missing_codeowners/plugin.rb', line 36

def severity
  @severity
end

#verboseBool

Provides additional logging diagnostic information. Default is false.

Returns:

  • (Bool)


41
42
43
# File 'lib/missing_codeowners/plugin.rb', line 41

def verbose
  @verbose
end

#verify_all_filesBool

Whether all files or only ones in PR diff to be reported. Default is false.

Returns:

  • (Bool)


26
27
28
# File 'lib/missing_codeowners/plugin.rb', line 26

def verify_all_files
  @verify_all_files
end

Instance Method Details

#verify(*opts, &block) ⇒ void

This method returns an undefined value.

Verifies files for missing owners. Generates a ‘markdown` list of warnings for the prose in a corpus of .markdown and .md files.

Parameters:

  • files (String)

    The list of files you want to verify, defaults to nil. if nil, modified and added files from the diff will be used.

  • ignored_files (String)

    The list of files you want to ignore, defaults to []. The files are treated as prefixes, so ‘foo/` will match `foo/bar`.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/missing_codeowners/plugin.rb', line 62

def verify(*opts, &block)
  @verify_all_files ||= false
  @max_number_of_files_to_report ||= 100
  @severity ||= "error"
  @verbose ||= false
  @ignored_files ||= opts.first.kind_of?(Hash) ? opts.first[:ignored_files] : []
  files = opts.first.kind_of?(Hash) ? opts.first[:files] : opts.first

  files_to_verify = (files || files_from_git).delete_if do |file|
    @ignored_files.any? { |ignored_file| file.start_with?(ignored_file) }
  end

  log "Files to verify:"
  log files_to_verify.join("\n")

  codeowners_path = find_codeowners_file
  codeowners_lines = read_codeowners_file(codeowners_path)
  codeowners_spec = parse_codeowners_spec(codeowners_lines)
  @files_missing_codeowners = files_to_verify.reject { |file| codeowners_spec.match file }

  if @files_missing_codeowners.any?
    log "Files missing CODEOWNERS:"
    log @files_missing_codeowners.join("\n")

    markdown format_missing_owners_message(@files_missing_codeowners, @max_number_of_files_to_report)
    danger_message = "Add CODEOWNERS rules to match all files."
    @severity == "error" ? (fail danger_message) : (warn danger_message)

    yield @files_missing_codeowners if block
  else
    log "No files missing CODEOWNERS."

    yield [] if block
  end

  log "-----"
end