Commit 7e2d97df authored by lauraMon's avatar lauraMon

Updates handling of `sha` being `nil`

* Adds helper for finding a sha so it can return nil
parent f699bbc9
......@@ -99,10 +99,18 @@ module Gitlab
initial_config
end
def find_sha(project)
branches = project&.repository&.branches || []
unless branches.empty?
project.repository.root_ref_sha
end
end
def build_context(project:, sha:, user:, parent_pipeline:)
Config::External::Context.new(
project: project,
sha: sha || project&.repository&.root_ref_sha,
sha: sha || find_sha(project),
user: user,
parent_pipeline: parent_pipeline,
variables: project&.predefined_variables&.to_runner_variables)
......
......@@ -21,7 +21,7 @@ module Gitlab
def initialize(project:, current_user:, sha: nil)
@project = project
@current_user = current_user
@sha = sha || project.repository.commit.sha
@sha = sha || project.repository.commit&.sha
end
def validate(content, dry_run: false)
......
......@@ -406,6 +406,24 @@ RSpec.describe API::Lint do
end
end
context 'with an empty repository' do
let_it_be(:empty_project) { create(:project_empty_repo) }
let_it_be(:yaml_content) do
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
end
before do
empty_project.add_developer(api_user)
end
it 'passes validation without errors' do
post api("/projects/#{empty_project.id}/ci/lint", api_user), params: { content: yaml_content }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['valid']).to eq(true)
expect(json_response['errors']).to eq([])
end
end
context 'when unauthenticated' do
let_it_be(:api_user) { nil }
......
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