Commit e166a802 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg Committed by Alfredo Sumaran

Backend for a gitignores dropdown

parent 56eb4200
...@@ -184,4 +184,8 @@ module BlobHelper ...@@ -184,4 +184,8 @@ module BlobHelper
Other: licenses.reject(&:featured).map { |license| [license.name, license.key] } Other: licenses.reject(&:featured).map { |license| [license.name, license.key] }
} }
end end
def gitignores_for_select
@gitignores_for_select ||= Gitlab::Gitignore.all
end
end end
...@@ -472,9 +472,7 @@ class Repository ...@@ -472,9 +472,7 @@ class Repository
def changelog def changelog
cache.fetch(:changelog) do cache.fetch(:changelog) do
tree(:head).blobs.find do |file| file_on_head(/\A(changelog|history|changes|news)/i)
file.name =~ /\A(changelog|history|changes|news)/i
end
end end
end end
...@@ -482,9 +480,7 @@ class Repository ...@@ -482,9 +480,7 @@ class Repository
return nil unless head_exists? return nil unless head_exists?
cache.fetch(:license_blob) do cache.fetch(:license_blob) do
tree(:head).blobs.find do |file| file_on_head(/\A(licen[sc]e|copying)(\..+|\z)/i)
file.name =~ /\A(licen[sc]e|copying)(\..+|\z)/i
end
end end
end end
...@@ -496,6 +492,14 @@ class Repository ...@@ -496,6 +492,14 @@ class Repository
end end
end end
def gitignore
return nil if !exists? || empty?
cache.fetch(:gitignore) do
file_on_head(/\A\.gitignore\z/)
end
end
def gitlab_ci_yml def gitlab_ci_yml
return nil unless head_exists? return nil unless head_exists?
...@@ -989,4 +993,8 @@ class Repository ...@@ -989,4 +993,8 @@ class Repository
def head_exists? def head_exists?
exists? && !empty? && !rugged.head_unborn? exists? && !empty? && !rugged.head_unborn?
end end
def file_on_head(regex)
tree(:head).blobs.find { |file| file.name =~ regex }
end
end end
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
.license-selector.js-license-selector.hide .license-selector.js-license-selector.hide
= select_tag :license_type, grouped_options_for_select(licenses_for_select, @project.repository.license_key), include_blank: true, class: 'select2 license-select', data: {placeholder: 'Choose a license template', project: @project.name, fullname: @project.namespace.human_name} = select_tag :license_type, grouped_options_for_select(licenses_for_select, @project.repository.license_key), include_blank: true, class: 'select2 license-select', data: {placeholder: 'Choose a license template', project: @project.name, fullname: @project.namespace.human_name}
.gitignore-selector.js-gitignore-selector.hide
= select_tag :gitignore_template, options_for_select(Gitlab::Gitignore.all), include_blank: true, class: 'select2 gitignore-select', data: {placeholder: 'Choose a .gitignore template'}
.encoding-selector .encoding-selector
= select_tag :encoding, options_for_select([ "base64", "text" ], "text"), class: 'select2' = select_tag :encoding, options_for_select([ "base64", "text" ], "text"), class: 'select2'
......
...@@ -15,10 +15,14 @@ ...@@ -15,10 +15,14 @@
If you already have files you can push them using command line instructions below. If you already have files you can push them using command line instructions below.
%p %p
Otherwise you can start with adding a Otherwise you can start with adding a
= link_to "README", new_readme_path, class: 'underlined-link' = succeed ',' do
= link_to "README", new_readme_path, class: 'underlined-link'
a
= succeed ',' do
= link_to "LICENSE", add_special_file_path(@project, file_name: 'LICENSE'), class: 'underlined-link'
or a or a
= link_to "LICENSE", add_special_file_path(@project, file_name: 'LICENSE'), class: 'underlined-link' = link_to '.gitignore', add_special_file_path(@project, file_name: '.gitignore'), class: 'underlined-link'
file to this project. to this project.
- if can?(current_user, :push_code, @project) - if can?(current_user, :push_code, @project)
%div{ class: container_class } %div{ class: container_class }
......
...@@ -58,5 +58,6 @@ module API ...@@ -58,5 +58,6 @@ module API
mount ::API::Runners mount ::API::Runners
mount ::API::Licenses mount ::API::Licenses
mount ::API::Subscriptions mount ::API::Subscriptions
mount ::API::Gitignores
end end
end end
...@@ -457,5 +457,13 @@ module API ...@@ -457,5 +457,13 @@ module API
expose(:limitations) { |license| license.meta['limitations'] } expose(:limitations) { |license| license.meta['limitations'] }
expose :content expose :content
end end
class GitignoresList < Grape::Entity
expose :name
end
class Gitignore < Grape::Entity
expose :name, :content
end
end end
end end
module API
class Gitignores < Grape::API
# Get the list of the available gitignore templates
#
# Example Request:
# GET /gitignores
get 'gitignores' do
present Gitlab::Gitignore.all, with: Entities::GitignoresList
end
# Get the text for a specific gitignore
#
# Parameters:
# key (required) - The key of a license
#
# Example Request:
# GET /gitignores/elixir
#
get 'gitignores/:key' do
required_attributes! [:key]
gitignore = Gitlab::Gitignore.find(params[:key])
not_found!('.gitignore') unless gitignore
present gitignore, with: Entities::Gitignore
end
end
end
module Gitlab
class Gitignore
FILTER_REGEX = /\.gitignore\z/.freeze
attr_accessor :name, :directory
def initialize(name, directory)
@name = name
@directory = directory
end
def content
File.read(path)
end
class << self
def all
languages_frameworks + global
end
def find(key)
file_name = "#{key}.gitignore"
directory = select_directory(file_name)
directory ? new(key, directory) : nil
end
def global
files_for_folder(global_dir).map { |f| new(f, global_dir) }
end
def languages_frameworks
files_for_folder(gitignore_dir).map { |f| new(f, gitignore_dir) }
end
end
private
def path
File.expand_path("#{name}.gitignore", directory)
end
class << self
def select_directory(file_name)
[self.gitignore_dir, self.global_dir].find { |dir| File.exist?(File.expand_path(file_name, dir)) }
end
def global_dir
File.expand_path('Global', gitignore_dir)
end
def gitignore_dir
File.expand_path('vendor/gitignore', Rails.root)
end
def files_for_folder(dir)
gitignores = []
Dir.entries(dir).each do |e|
next unless e.end_with?('.gitignore')
gitignores << e.gsub(FILTER_REGEX, '')
end
gitignores
end
end
end
end
namespace :gitlab do
desc "GitLab | Update gitignore"
task :update_gitignore do
dir = File.expand_path('vendor', Rails.root)
FileUtils.cd(dir)
dir = File.expand_path('gitignore', dir)
clone_gitignores(dir)
remove_unneeded_files(dir)
puts "Done".green
end
def clone_gitignores(dir)
FileUtils.rm_rf(dir) if Dir.exist?(dir)
system('git clone --depth=1 --branch=master https://github.com/github/gitignore.git')
end
def remove_unneeded_files(dir)
[File.expand_path('Global', dir), dir].each do |path|
Dir.entries(path).reject { |e| e =~ /(\.{1,2}|Global|\.gitignore)\z/ }.each do |file|
FileUtils.rm_rf File.expand_path(file, path)
end
end
end
end
require 'spec_helper'
describe Gitlab::Gitignore do
subject { Gitlab::Gitignore }
describe '.all' do
it 'strips the gitignore suffix' do
expect(subject.all.first.name).not_to end_with('.gitignore')
end
it 'combines the globals and rest' do
all = subject.all.map(&:name)
expect(all).to include('Vim')
expect(all).to include('Ruby')
end
end
describe '.find' do
it 'returns nil if the file does not exist' do
expect(subject.find('mepmep-yadida')).to be nil
end
it 'returns the Gitignore object of a valid file' do
ruby = subject.find('Ruby')
expect(ruby).to be_a Gitlab::Gitignore
end
end
describe '#content' do
it 'loads the full file' do
gitignore = subject.new('Ruby', File.expand_path('vendor/gitignore', Rails.root))
expect(gitignore.name).to eq 'Ruby'
expect(gitignore.content).to start_with('*.gem')
end
end
end
require 'spec_helper'
describe API::Gitignores, api: true do
include ApiHelpers
describe 'Entity Gitignore' do
before { get api('/gitignores/Ruby') }
it { expect(json_response['name']).to eq('Ruby') }
it { expect(json_response['content']).to include('*.gem') }
end
describe 'Entity GitignoresList' do
before { get api('/gitignores') }
it { expect(json_response.first['name']).to be_truthy }
it { expect(json_response.first['content']).to be_falsey }
end
describe 'GET /gitignores' do
it 'returns a list of available license templates' do
get api('/gitignores')
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.size).to be > 15
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment