Search code examples
rubygithubdanger

How to detect png, jpg images using Ruby?


Just trying to detect png and jpg images in the github pull request using DangerFile. Here is everything that I found

modified_files_has_wrong_pic_formant = git.modified_files.any? { |file| /.*\.(png|jpg)/.match(File.extname(file)) }
added_files_has_wrong_pic_formant = git.added_files.any? { |file| /.*\.(png|jpg)/.match(File.extname(file)) }

Does it work or can you suggest a better solution?


Solution

  • Something like this should work

    failure "Don't commit PNG/JPG" if (git.added_files + git.modified_files).any?(/\.(png|jpg)\z/)
    

    Check all files in PR that they are not end with .png or .jpg