2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-08 15:42:28 +00:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
# Module that can be used to detect if a path points to a special file such as
|
|
|
|
# a README or a CONTRIBUTING file.
|
|
|
|
module FileDetector
|
|
|
|
PATTERNS = {
|
2017-05-17 16:27:30 +00:00
|
|
|
# Project files
|
2020-01-13 03:07:51 +00:00
|
|
|
readme: /\A(#{Regexp.union(*Gitlab::MarkupHelper::PLAIN_FILENAMES).source})(\.(txt|#{Regexp.union(*Gitlab::MarkupHelper::EXTENSIONS).source}))?\z/i,
|
2018-01-27 05:35:53 +00:00
|
|
|
changelog: %r{\A(changelog|history|changes|news)[^/]*\z}i,
|
2018-09-06 12:36:19 +00:00
|
|
|
license: %r{\A((un)?licen[sc]e|copying)(\.[^/]+)?\z}i,
|
2018-01-27 05:35:53 +00:00
|
|
|
contributing: %r{\Acontributing[^/]*\z}i,
|
2016-11-08 15:42:28 +00:00
|
|
|
version: 'version',
|
2017-05-17 16:27:30 +00:00
|
|
|
avatar: /\Alogo\.(png|jpg|gif)\z/,
|
2018-01-27 05:35:53 +00:00
|
|
|
issue_template: %r{\A\.gitlab/issue_templates/[^/]+\.md\z},
|
|
|
|
merge_request_template: %r{\A\.gitlab/merge_request_templates/[^/]+\.md\z},
|
2019-05-01 10:16:03 +00:00
|
|
|
metrics_dashboard: %r{\A\.gitlab/dashboards/[^/]+\.yml\z},
|
2018-05-18 11:01:27 +00:00
|
|
|
xcode_config: %r{\A[^/]*\.(xcodeproj|xcworkspace)(/.+)?\z},
|
2017-05-17 16:27:30 +00:00
|
|
|
|
|
|
|
# Configuration files
|
2016-11-08 15:42:28 +00:00
|
|
|
gitignore: '.gitignore',
|
|
|
|
gitlab_ci: '.gitlab-ci.yml',
|
2017-10-11 15:47:03 +00:00
|
|
|
route_map: '.gitlab/route-map.yml',
|
2017-05-17 16:27:30 +00:00
|
|
|
|
|
|
|
# Dependency files
|
2019-12-18 12:07:48 +00:00
|
|
|
cargo_toml: 'Cargo.toml',
|
2018-01-27 05:35:53 +00:00
|
|
|
cartfile: %r{\ACartfile[^/]*\z},
|
2017-05-17 16:27:30 +00:00
|
|
|
composer_json: 'composer.json',
|
|
|
|
gemfile: /\A(Gemfile|gems\.rb)\z/,
|
|
|
|
gemfile_lock: 'Gemfile.lock',
|
2018-01-27 05:35:53 +00:00
|
|
|
gemspec: %r{\A[^/]*\.gemspec\z},
|
2017-05-17 16:27:30 +00:00
|
|
|
godeps_json: 'Godeps.json',
|
|
|
|
package_json: 'package.json',
|
|
|
|
podfile: 'Podfile',
|
2018-01-27 05:35:53 +00:00
|
|
|
podspec_json: %r{\A[^/]*\.podspec\.json\z},
|
|
|
|
podspec: %r{\A[^/]*\.podspec\z},
|
|
|
|
requirements_txt: %r{\A[^/]*requirements\.txt\z},
|
2019-12-16 12:07:43 +00:00
|
|
|
yarn_lock: 'yarn.lock',
|
|
|
|
|
|
|
|
# OpenAPI Specification files
|
2020-03-23 15:09:36 +00:00
|
|
|
openapi: %r{[^/]*(openapi|swagger)[^/]*\.(yaml|yml|json)\z}i
|
2017-02-21 23:32:18 +00:00
|
|
|
}.freeze
|
2016-11-08 15:42:28 +00:00
|
|
|
|
|
|
|
# Returns an Array of file types based on the given paths.
|
|
|
|
#
|
|
|
|
# This method can be used to check if a list of file paths (e.g. of changed
|
|
|
|
# files) involve any special files such as a README or a LICENSE file.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# types_in_paths(%w{README.md foo/bar.txt}) # => [:readme]
|
|
|
|
def self.types_in_paths(paths)
|
|
|
|
types = Set.new
|
|
|
|
|
|
|
|
paths.each do |path|
|
|
|
|
type = type_of(path)
|
|
|
|
|
|
|
|
types << type if type
|
|
|
|
end
|
|
|
|
|
|
|
|
types.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the type of a file path, or nil if none could be detected.
|
|
|
|
#
|
|
|
|
# Returned types are Symbols such as `:readme`, `:version`, etc.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# type_of('README.md') # => :readme
|
|
|
|
# type_of('VERSION') # => :version
|
|
|
|
def self.type_of(path)
|
|
|
|
PATTERNS.each do |type, search|
|
|
|
|
did_match = if search.is_a?(Regexp)
|
2017-10-11 15:47:03 +00:00
|
|
|
path =~ search
|
2016-11-08 15:42:28 +00:00
|
|
|
else
|
2017-10-11 15:47:03 +00:00
|
|
|
path.casecmp(search) == 0
|
2016-11-08 15:42:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return type if did_match
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|