Commit e9241478 authored by Jeremy Jackson's avatar Jeremy Jackson Committed by Igor Drozdov

Add improved readme content for all new projects

This also introduces a new way to configure the readme template on new
project creation which can be used by other services like the
`Projects::CreateService`. It utilizes ERB for customizing the contents
of the readme templates.

Changelog: added
parent ccbf8904
# frozen_string_literal: true
class NewProjectReadmeContentExperiment < ApplicationExperiment # rubocop:disable Gitlab/NamespacedClass
TEMPLATE_PATH = Rails.root.join('app', 'experiments', 'templates', 'new_project_readme_content')
include Rails.application.routes.url_helpers
def run_with(project, variant: nil)
@project = project
publish_to_database
run(variant)
end
def control_behavior
template('readme_basic.md')
end
def advanced_behavior
template('readme_advanced.md')
end
def redirect(to_url)
experiment_redirect_url(self, url: to_url)
end
private
def template(name)
ERB.new(File.read(TEMPLATE_PATH.join("#{name}.tt")), trim_mode: '<>').result(binding)
end
end
...@@ -167,7 +167,7 @@ module Projects ...@@ -167,7 +167,7 @@ module Projects
end end
def readme_content def readme_content
@readme_template.presence || experiment(:new_project_readme_content, namespace: @project.namespace).run_with(@project) @readme_template.presence || ReadmeRendererService.new(@project, current_user).execute
end end
def skip_wiki? def skip_wiki?
......
# frozen_string_literal: true
module Projects
class ReadmeRendererService < BaseService
include Rails.application.routes.url_helpers
TEMPLATE_PATH = Rails.root.join('app', 'views', 'projects', 'readme_templates')
def execute
render(params[:template_name] || :default)
end
private
def render(template_name)
ERB.new(File.read(sanitized_filename(template_name)), trim_mode: '<>').result(binding)
end
def sanitized_filename(template_name)
path = Gitlab::Utils.check_path_traversal!("#{template_name}.md.tt")
path = TEMPLATE_PATH.join(path).to_s
Gitlab::Utils.check_allowed_absolute_path!(path, [TEMPLATE_PATH.to_s])
path
end
end
end
# <%= project.name %>
<%= project.description %>
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin <%= project.http_url_to_repo %>
git branch -M <%= project.default_branch_or_main %>
git push -uf origin <%= project.default_branch_or_main %>
```
## Integrate with your tools
- [ ] [Set up project integrations](<%= project_settings_integrations_url(project) %>)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
---
name: new_project_readme_content
introduced_by_url:
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/323641
milestone: '14.0'
type: experiment
group: group::adoption
default_enabled: false
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe NewProjectReadmeContentExperiment, :experiment do
subject { described_class.new(namespace: project.namespace) }
let(:project) { create(:project, name: 'Experimental', description: 'An experiment project') }
it "renders the basic README" do
expect(subject.run_with(project)).to eq(<<~MARKDOWN.strip)
# Experimental
An experiment project
MARKDOWN
end
describe "the advanced variant" do
let(:markdown) { subject.run_with(project, variant: :advanced) }
let(:initial_url) { 'https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file' }
it "renders the project details" do
expect(markdown).to include(<<~MARKDOWN.strip)
# Experimental
An experiment project
## Getting started
MARKDOWN
end
it "renders redirect URLs" do
url = Rails.application.routes.url_helpers.experiment_redirect_url(subject, url: initial_url)
expect(url).to include("/-/experiment/#{subject.to_param}?")
expect(markdown).to include(url)
end
end
end
...@@ -590,58 +590,55 @@ RSpec.describe Projects::CreateService, '#execute' do ...@@ -590,58 +590,55 @@ RSpec.describe Projects::CreateService, '#execute' do
opts[:initialize_with_readme] = '1' opts[:initialize_with_readme] = '1'
end end
shared_examples 'creates README.md' do shared_examples 'a repo with a README.md' do
it { expect(project.repository.commit_count).to be(1) } it { expect(project.repository.commit_count).to be(1) }
it { expect(project.repository.readme.name).to eql('README.md') } it { expect(project.repository.readme.name).to eql('README.md') }
it { expect(project.repository.readme.data).to include('# GitLab') } it { expect(project.repository.readme.data).to include(expected_content) }
end end
it_behaves_like 'creates README.md' it_behaves_like 'a repo with a README.md' do
let(:expected_content) do
<<~MARKDOWN
cd existing_repo
git remote add origin #{project.http_url_to_repo}
git branch -M master
git push -uf origin master
MARKDOWN
end
end
context 'and a default_branch_name is specified' do context 'and a readme_template is specified' do
before do before do
allow(Gitlab::CurrentSettings) opts[:readme_template] = "# GitLab\nThis is customized readme."
.to receive(:default_branch_name) end
.and_return('example_branch')
it_behaves_like 'a repo with a README.md' do
let(:expected_content) { "# GitLab\nThis is customized readme." }
end end
end
it_behaves_like 'creates README.md' context 'and a default_branch_name is specified' do
before do
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return('example_branch')
end
it 'creates README.md within the specified branch rather than master' do it 'creates the correct branch' do
branches = project.repository.branches branches = project.repository.branches
expect(branches.size).to eq(1) expect(branches.size).to eq(1)
expect(branches.collect(&:name)).to contain_exactly('example_branch') expect(branches.collect(&:name)).to contain_exactly('example_branch')
end end
describe 'advanced readme content', experiment: :new_project_readme_content do it_behaves_like 'a repo with a README.md' do
before do let(:expected_content) do
stub_experiments(new_project_readme_content: :advanced) <<~MARKDOWN
end cd existing_repo
it_behaves_like 'creates README.md'
it 'includes advanced content in the README.md' do
content = project.repository.readme.data
expect(content).to include <<~MARKDOWN
git remote add origin #{project.http_url_to_repo} git remote add origin #{project.http_url_to_repo}
git branch -M example_branch git branch -M example_branch
git push -uf origin example_branch git push -uf origin example_branch
MARKDOWN MARKDOWN
end end
end end
context 'and readme_template is specified' do
before do
opts[:readme_template] = "# GitLab\nThis is customized template."
end
it_behaves_like 'creates README.md'
it 'creates README.md with specified template' do
expect(project.repository.readme.data).to include('This is customized template.')
end
end
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::ReadmeRendererService, '#execute' do
using RSpec::Parameterized::TableSyntax
subject(:service) { described_class.new(project, nil, opts) }
let_it_be(:project) { create(:project, title: 'My Project', description: '_custom_description_') }
let(:opts) { {} }
it 'renders the an ERB readme template' do
expect(service.execute).to start_with(<<~MARKDOWN)
# My Project
_custom_description_
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin #{project.http_url_to_repo}
git branch -M master
git push -uf origin master
```
MARKDOWN
end
context 'with a custom template' do
before do
allow(File).to receive(:read).and_call_original
end
it 'renders that template file' do
opts[:template_name] = :custom_readme
expect(service).to receive(:sanitized_filename).with(:custom_readme).and_return('custom_readme.md.tt')
expect(File).to receive(:read).with('custom_readme.md.tt').and_return('_custom_readme_file_content_')
expect(service.execute).to eq('_custom_readme_file_content_')
end
context 'with path traversal in mind' do
where(:template_name, :exception, :expected_path) do
'../path/traversal/bad' | [Gitlab::Utils::PathTraversalAttackError, 'Invalid path'] | nil
'/bad/template' | [StandardError, 'path /bad/template.md.tt is not allowed'] | nil
'good/template' | nil | 'good/template.md.tt'
end
with_them do
it 'raises the expected exception on bad paths' do
opts[:template_name] = template_name
if exception
expect { subject.execute }.to raise_error(*exception)
else
expect(File).to receive(:read).with(described_class::TEMPLATE_PATH.join(expected_path).to_s).and_return('')
expect { subject.execute }.not_to raise_error
end
end
end
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