Skip to content

Commit

Permalink
Report back build state to the correct scm repository
Browse files Browse the repository at this point in the history
We were using the head (source) repository instead of
the base (target) repository to report back the
build state of a workflow. This is wrong since the
state is reported using the scm token of the target
repo owner/maintainer, which leads to permission
problems (since it doesn't associate the reported
state to the PR, but directly to the commit on the
source repository).

We also changed the download url, we should only
take the "workflow.yml" file from the target
repository.

Co-authored-by: Saray Cabrera Padrón <scabrerapadron@suse.de>
  • Loading branch information
krauselukas and saraycp committed May 28, 2021
1 parent 45159fb commit 8df4946
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/app/models/workflow/step/branch_package_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def branch_request_file_github
action: @scm_extractor_payload[:action],
pull_request: {
head: {
repo: { full_name: @scm_extractor_payload[:repository_full_name] },
repo: { full_name: @scm_extractor_payload[:source_repository_full_name] },
sha: @scm_extractor_payload[:commit_sha]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/services/scm_status_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call
if github?
github_client = Octokit::Client.new(access_token: @scm_token)
# https://docs.github.com/en/rest/reference/repos#create-a-commit-status
github_client.create_status("#{@payload[:repository_full_name]}",
github_client.create_status("#{@payload[:target_repository_full_name]}",
@payload[:commit_sha],
@state,
{ context: 'OBS Workflow' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def github_extractor_payload
source_branch: @payload.dig('pull_request', 'head', 'ref'),
target_branch: @payload.dig('pull_request', 'base', 'ref'),
action: @payload['action'], # TODO: Names may differ, maybe we need to find our own naming (defer to service?)
repository_full_name: @payload.dig('pull_request', 'head', 'repo', 'full_name'),
source_repository_full_name: @payload.dig('pull_request', 'head', 'repo', 'full_name'),
target_repository_full_name: @payload.dig('pull_request', 'base', 'repo', 'full_name'),
event: @event
}.with_indifferent_access
end
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/services/workflows/yaml_downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def download_yaml_file
def download_url
case @scm_payload[:scm]
when 'github'
"https://raw.githubusercontent.com/#{@scm_payload[:repository_full_name]}/#{@scm_payload[:target_branch]}/.obs/workflows.yml"
"https://raw.githubusercontent.com/#{@scm_payload[:target_repository_full_name]}/#{@scm_payload[:target_branch]}/.obs/workflows.yml"
when 'gitlab'
"https://gitlab.com/#{@scm_payload[:path_with_namespace]}/-/raw/#{@scm_payload[:target_branch]}/.obs/workflows.yml"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@
pull_request: {
head: {
repo: {
full_name: 'danidoni/test_repo',
full_name: 'iggy/source_repo',
html_url: 'https://github.com/openSUSE/open-build-service'
},
ref: 'add-changes',
sha: '9e0ea1fd99c9000cbb8b8c9d28763d0ddace0b65'
},
base: {
repo: {
full_name: 'iggy/target_repo',
},
ref: 'main'
}
},
Expand All @@ -79,7 +82,8 @@
source_branch: 'add-changes',
target_branch: 'main',
action: 'opened',
repository_full_name: 'danidoni/test_repo',
source_repository_full_name: 'iggy/source_repo',
target_repository_full_name: 'iggy/target_repo',
event: 'pull_request'
}
end
Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/services/workflows/yaml_downloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
end

context 'github' do
let(:payload) { { scm: 'github', target_branch: 'master', repository_full_name: 'openSUSE/obs-server' } }
let(:url) { "https://raw.githubusercontent.com/#{payload[:repository_full_name]}/#{payload[:target_branch]}/.obs/workflows.yml" }
let(:payload) { { scm: 'github', target_branch: 'master', target_repository_full_name: 'openSUSE/obs-server' } }
let(:url) { "https://raw.githubusercontent.com/#{payload[:target_repository_full_name]}/#{payload[:target_branch]}/.obs/workflows.yml" }

it { expect(Down).to have_received(:download).with(url, max_size: max_size) }
end
Expand Down

0 comments on commit 8df4946

Please sign in to comment.