Commit bd88dd2c authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '26261_support_default_branch' into 'master'

Add default_branch support for Project API

See merge request gitlab-org/gitlab!63771
parents e6feccff 24e3c5bf
......@@ -11,6 +11,7 @@ module Projects
@initialize_with_readme = Gitlab::Utils.to_boolean(@params.delete(:initialize_with_readme))
@import_data = @params.delete(:import_data)
@relations_block = @params.delete(:relations_block)
@default_branch = @params.delete(:default_branch)
build_topics
end
......@@ -147,7 +148,7 @@ module Projects
def create_readme
commit_attrs = {
branch_name: @project.default_branch_or_main,
branch_name: @default_branch.presence || @project.default_branch_or_main,
commit_message: 'Initial commit',
file_path: 'README.md',
file_content: "# #{@project.name}\n\n#{@project.description}"
......
......@@ -1171,7 +1171,7 @@ POST /projects
| `ci_config_path` | string | **{dotted-circle}** No | The path to CI configuration file. |
| `container_expiration_policy_attributes` | hash | **{dotted-circle}** No | Update the image cleanup policy for this project. Accepts: `cadence` (string), `keep_n` (integer), `older_than` (string), `name_regex` (string), `name_regex_delete` (string), `name_regex_keep` (string), `enabled` (boolean). Valid values for `cadence` are: `1d` (every day), `7d` (every week), `14d` (every two weeks), `1month` (every month), or `3month` (every quarter). |
| `container_registry_enabled` | boolean | **{dotted-circle}** No | Enable container registry for this project. |
| `default_branch` | string | **{dotted-circle}** No | The [default branch](../user/project/repository/branches/default.md) name. |
| `default_branch` | string | **{dotted-circle}** No | The [default branch](../user/project/repository/branches/default.md) name. Requires `initialize_with_readme` to be `true`. |
| `description` | string | **{dotted-circle}** No | Short project description. |
| `emails_disabled` | boolean | **{dotted-circle}** No | Disable email notifications. |
| `external_authorization_classification_label` **(PREMIUM)** | string | **{dotted-circle}** No | The classification label for the project. |
......@@ -1246,6 +1246,7 @@ POST /projects/user/:user_id
| `ci_config_path` | string | **{dotted-circle}** No | The path to CI configuration file. |
| `container_registry_enabled` | boolean | **{dotted-circle}** No | Enable container registry for this project. |
| `description` | string | **{dotted-circle}** No | Short project description. |
| `default_branch` | string | **{dotted-circle}** No | The [default branch](../user/project/repository/branches/default.md) name. Requires `initialize_with_readme` to be `true`. |
| `emails_disabled` | boolean | **{dotted-circle}** No | Disable email notifications. |
| `external_authorization_classification_label` **(PREMIUM)** | string | **{dotted-circle}** No | The classification label for the project. |
| `forking_access_level` | string | **{dotted-circle}** No | One of `disabled`, `private`, or `enabled`. |
......
......@@ -234,6 +234,7 @@ module API
params do
optional :name, type: String, desc: 'The name of the project'
optional :path, type: String, desc: 'The path of the repository'
optional :default_branch, type: String, desc: 'The default branch of the project'
at_least_one_of :name, :path
use :optional_create_project_params
use :create_params
......
......@@ -109,6 +109,43 @@ RSpec.describe API::Projects do
end
end
shared_examples_for 'create project with default branch parameter' do
let(:params) { { name: 'Foo Project', initialize_with_readme: true, default_branch: default_branch } }
let(:default_branch) { 'main' }
it 'creates project with provided default branch name' do
expect { request }.to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:created)
project = Project.find(json_response['id'])
expect(project.default_branch).to eq(default_branch)
end
context 'when branch name is empty' do
let(:default_branch) { '' }
it 'creates project with a default project branch name' do
expect { request }.to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:created)
project = Project.find(json_response['id'])
expect(project.default_branch).to eq('master')
end
end
context 'when initialize with readme is not set' do
let(:params) { super().merge(initialize_with_readme: nil) }
it 'creates project with a default project branch name' do
expect { request }.to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:created)
project = Project.find(json_response['id'])
expect(project.default_branch).to be_nil
end
end
end
describe 'GET /projects' do
shared_examples_for 'projects response' do
it 'returns an array of projects' do
......@@ -947,6 +984,10 @@ RSpec.describe API::Projects do
expect(project.path).to eq('path-project-Foo')
end
it_behaves_like 'create project with default branch parameter' do
let(:request) { post api('/projects', user), params: params }
end
it 'creates last project before reaching project limit' do
allow_any_instance_of(User).to receive(:projects_limit_left).and_return(1)
post api('/projects', user2), params: { name: 'foo' }
......@@ -1427,6 +1468,10 @@ RSpec.describe API::Projects do
expect(project.path).to eq('path-project-Foo')
end
it_behaves_like 'create project with default branch parameter' do
let(:request) { post api("/projects/user/#{user.id}", admin), params: params }
end
it 'responds with 400 on failure and not project' do
expect { post api("/projects/user/#{user.id}", admin) }
.not_to change { Project.count }
......
......@@ -298,7 +298,7 @@ RSpec.describe Projects::CreateService, '#execute' do
context 'error handling' do
it 'handles invalid options' do
opts[:default_branch] = 'master'
opts[:invalid] = 'option'
expect(create_project(user, opts)).to eq(nil)
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