Commit f540f888 authored by Furkan Ayhan's avatar Furkan Ayhan

Merge branch 'lint_branch_context' into 'master'

Add ref param to project level ci/lint

See merge request gitlab-org/gitlab!79992
parents 31a52b51 edb848b7
......@@ -171,6 +171,7 @@ POST /projects/:id/ci/lint
| `content` | string | yes | The CI/CD configuration content. |
| `dry_run` | boolean | no | Run [pipeline creation simulation](../ci/lint.md#simulate-a-pipeline), or only do static check. This is false by default. |
| `include_jobs` | boolean | no | If the list of jobs that would exist in a static check or pipeline simulation should be included in the response. This is false by default. |
| `ref` | string | no | When `dry_run` is `true`, sets the branch or tag to use. Defaults to the project's default branch when not set. |
Example request:
......@@ -220,6 +221,7 @@ GET /projects/:id/ci/lint
| ---------- | ------- | -------- | -------- |
| `dry_run` | boolean | no | Run pipeline creation simulation, or only do static check. |
| `include_jobs` | boolean | no | If the list of jobs that would exist in a static check or pipeline simulation should be included in the response. This is false by default. |
| `ref` | string | no | When `dry_run` is `true`, sets the branch or tag to use. Defaults to the project's default branch when not set. |
Example request:
......
......@@ -42,6 +42,7 @@ module API
params do
optional :dry_run, type: Boolean, default: false, desc: 'Run pipeline creation simulation, or only do static check.'
optional :include_jobs, type: Boolean, desc: 'Whether or not to include CI jobs in the response'
optional :ref, type: String, desc: 'Branch or tag used to execute a dry run. Defaults to the default branch of the project. Only used when dry_run is true'
end
get ':id/ci/lint', urgency: :low do
authorize! :download_code, user_project
......@@ -52,7 +53,7 @@ module API
result = Gitlab::Ci::Lint
.new(project: user_project, current_user: current_user)
.validate(content, dry_run: params[:dry_run])
.validate(content, dry_run: params[:dry_run], ref: params[:ref] || user_project.default_branch)
present result, with: Entities::Ci::Lint::Result, current_user: current_user, include_jobs: params[:include_jobs]
end
......@@ -66,13 +67,14 @@ module API
requires :content, type: String, desc: 'Content of .gitlab-ci.yml'
optional :dry_run, type: Boolean, default: false, desc: 'Run pipeline creation simulation, or only do static check.'
optional :include_jobs, type: Boolean, desc: 'Whether or not to include CI jobs in the response'
optional :ref, type: String, desc: 'Branch or tag used to execute a dry run. Defaults to the default branch of the project. Only used when dry_run is true'
end
post ':id/ci/lint', urgency: :low do
authorize! :create_pipeline, user_project
result = Gitlab::Ci::Lint
.new(project: user_project, current_user: current_user)
.validate(params[:content], dry_run: params[:dry_run])
.validate(params[:content], dry_run: params[:dry_run], ref: params[:ref] || user_project.default_branch)
status 200
present result, with: Entities::Ci::Lint::Result, current_user: current_user, include_jobs: params[:include_jobs]
......
......@@ -24,9 +24,9 @@ module Gitlab
@sha = sha || project&.repository&.commit&.sha
end
def validate(content, dry_run: false)
def validate(content, dry_run: false, ref: @project&.default_branch)
if dry_run
simulate_pipeline_creation(content)
simulate_pipeline_creation(content, ref)
else
static_validation(content)
end
......@@ -34,9 +34,9 @@ module Gitlab
private
def simulate_pipeline_creation(content)
def simulate_pipeline_creation(content, ref)
pipeline = ::Ci::CreatePipelineService
.new(@project, @current_user, ref: @project.default_branch)
.new(@project, @current_user, ref: ref)
.execute(:push, dry_run: true, content: content)
.payload
......
......@@ -7,9 +7,10 @@ RSpec.describe Gitlab::Ci::Lint do
let_it_be(:user) { create(:user) }
let(:lint) { described_class.new(project: project, current_user: user) }
let(:ref) { project.default_branch }
describe '#validate' do
subject { lint.validate(content, dry_run: dry_run) }
subject { lint.validate(content, dry_run: dry_run, ref: ref) }
shared_examples 'content is valid' do
let(:content) do
......@@ -251,6 +252,29 @@ RSpec.describe Gitlab::Ci::Lint do
end
end
context 'when using a ref other than the default branch' do
let(:ref) { 'feature' }
let(:content) do
<<~YAML
build:
stage: build
script: echo 1
rules:
- if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
test:
stage: test
script: echo 2
rules:
- if: "$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH"
YAML
end
it 'includes only jobs that are excluded on the default branch' do
expect(subject.jobs.size).to eq(1)
expect(subject.jobs[0][:name]).to eq('test')
end
end
it_behaves_like 'sets merged yaml'
include_context 'advanced validations' do
......
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