Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use github_html_url equivalent to github_id when searching (#9240)

ulfertse5111ab1d777
diff --git a/modules/github_integration/app/models/github_pull_request.rb b/modules/github_integration/app/models/github_pull_request.rb
index f9d99dc9069..2533d806195 100644
--- a/modules/github_integration/app/models/github_pull_request.rb
+++ b/modules/github_integration/app/models/github_pull_request.rb
@@ -57,14 +57,18 @@ class GithubPullRequest < ApplicationRecord
validate :validate_labels_schema
scope :without_work_package, -> { left_outer_joins(:work_packages).where(work_packages: { id: nil }) }
- scope :partial, -> {
- where(body: nil,
- comments_count: nil,
- review_comments_count: nil,
- additions_count: nil,
- deletions_count: nil,
- changed_files_count: nil)
- }
+
+ def self.find_by_github_identifiers(id: nil, url: nil, initialize: false)
+ raise ArgumentError, "needs an id or an url" if id.nil? && url.blank?
+
+ found = where(github_id: id).or(where(github_html_url: url)).take
+
+ if found
+ found
+ elsif initialize
+ new(github_id: id, github_html_url: url)
+ end
+ end
##
# When a PR lives long enough and receives many pushes, the same check (say, a CI test run) can be run multiple times.
diff --git a/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb b/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb
index d0fe177684c..337bc320753 100644
--- a/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb
+++ b/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb
@@ -46,12 +46,9 @@ module OpenProject::GithubIntegration::Services
private
def find_or_initialize(payload)
- GithubPullRequest.partial.find_by(
- github_html_url: payload.fetch('html_url')
- ) ||
- GithubPullRequest.find_or_initialize_by(
- github_id: payload.fetch('id')
- )
+ GithubPullRequest.find_by_github_identifiers(id: payload.fetch('id'),
+ url: payload.fetch('html_url'),
+ initialize: true)
end
# Receives the input from the github webhook and translates them
diff --git a/modules/github_integration/spec/models/github_pull_request_spec.rb b/modules/github_integration/spec/models/github_pull_request_spec.rb
index aba3d40fd72..ee9dea6e01f 100644
--- a/modules/github_integration/spec/models/github_pull_request_spec.rb
+++ b/modules/github_integration/spec/models/github_pull_request_spec.rb
@@ -80,69 +80,89 @@ describe GithubPullRequest do
end
end
- describe '#partial?' do
- context 'when the body is set' do
- subject { described_class.new(body: 'something').partial? }
-
- it { is_expected.to be_falsey }
+ describe '.find_by_github_identifiers' do
+ let(:github_id) { 5 }
+ let(:github_url) { 'https://github.com/opf/openproject/pull/123' }
+ let(:pull_request) do
+ FactoryBot.create(:github_pull_request,
+ github_id: github_id,
+ github_html_url: github_url)
end
- context 'when the comments_count is set' do
- subject { described_class.new(comments_count: 5).partial? }
-
- it { is_expected.to be_falsey }
+ context 'when the github_id attribute matches' do
+ it 'finds by github_id' do
+ expect(described_class.find_by_github_identifiers(id: pull_request.github_id))
+ .to eql pull_request
+ end
end
- context 'when the review_comments_count is set' do
- subject { described_class.new(review_comments_count: 5).partial? }
-
- it { is_expected.to be_falsey }
+ context 'when the github_html_url attribute matches' do
+ it 'finds by github_html_url' do
+ expect(described_class.find_by_github_identifiers(url: pull_request.github_html_url))
+ .to eql pull_request
+ end
end
- context 'when the additions_count is set' do
- subject { described_class.new(additions_count: 5).partial? }
-
- it { is_expected.to be_falsey }
+ context 'when the provided github_id does not match' do
+ it 'is nil' do
+ expect(described_class.find_by_github_identifiers(id: pull_request.github_id + 1))
+ .to be_nil
+ end
end
- context 'when the deletions_count is set' do
- subject { described_class.new(deletions_count: 5).partial? }
+ context 'when the provided github_html_url does not match' do
+ it 'is nil' do
+ expect(described_class.find_by_github_identifiers(url: "#{pull_request.github_html_url}zzzz"))
+ .to be_nil
+ end
+ end
- it { is_expected.to be_falsey }
+ context 'when neither match' do
+ it 'is nil' do
+ expect(described_class.find_by_github_identifiers(id: pull_request.github_id + 1,
+ url: "#{pull_request.github_html_url}zzzz"))
+ .to be_nil
+ end
end
- context 'when the changed_files_count is set' do
- subject { described_class.new(changed_files_count: 5).partial? }
+ context 'when the provided github_html_url does not match but the github_id does' do
+ it 'is nil' do
+ expect(described_class.find_by_github_identifiers(id: pull_request.github_id,
+ url: "#{pull_request.github_html_url}zzzz"))
+ .to eql pull_request
+ end
+ end
- it { is_expected.to be_falsey }
+ context 'when the provided github_html_url does match but the github_id does not' do
+ it 'is nil' do
+ expect(described_class.find_by_github_identifiers(id: pull_request.github_id + 1,
+ url: pull_request.github_html_url))
+ .to eql pull_request
+ end
end
- context 'when the all of the above are nil set and the state is open' do
- subject do
- described_class.new(changed_files_count: nil,
- body: nil,
- comments_count: nil,
- review_comments_count: nil,
- additions_count: nil,
- deletions_count: nil,
- state: 'open').partial?
+ context 'when neither match but initialize is true' do
+ subject(:finder) do
+ described_class.find_by_github_identifiers(id: pull_request.github_id + 1,
+ url: "#{pull_request.github_html_url}zzzz",
+ initialize: true)
end
- it { is_expected.to be_truthy }
- end
+ it 'returns a pull reqeust' do
+ expect(finder)
+ .to be_a(described_class)
+ end
- context 'when the all of the above are nil set and the state is closed' do
- subject do
- described_class.new(changed_files_count: nil,
- body: nil,
- comments_count: nil,
- review_comments_count: nil,
- additions_count: nil,
- deletions_count: nil,
- state: 'closed').partial?
+ it 'returns a new record' do
+ expect(finder)
+ .to be_new_record
end
- it { is_expected.to be_truthy }
+ it 'has the privided attributes initialized' do
+ expect(finder.attributes.compact)
+ .to eql("github_id" => pull_request.github_id + 1,
+ "github_html_url" => "#{pull_request.github_html_url}zzzz")
+ end
end
end