Skip to content

Commit affbe4d

Browse files
authored
fix(github_app): improve repository updates handling (#79)
- Refactored queries to use parameterized statements for `installation_id` and `repositories` ## Description <!-- Describe your changes in detail --> ## Type of Change <!-- Mark relevant items with an [x] --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation/Repository docs update - [ ] Performance improvement - [ ] Code refactoring - [ ] Test updates ## Testing <!-- How has this been tested? --> - [ ] Unit tests added/updated - [ ] Integration tests added/updated - [ ] Manual testing performed - [ ] Not applicable ## Documentation <!-- Mark relevant items with an [x] --> - [ ] Documentation update required - [ ] Changelog update required ## Related Issues <!-- Link related issues below. Insert the issue link or issue number --> - Closes: <!-- insert issue link here --> - Related to: <!-- insert issue link here -->
1 parent 4bdf326 commit affbe4d

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

github_hooks/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ else
8484
docker run --rm $(VOLUME_BIND) $(CONTAINER_ENV_VARS) $(IMAGE):$(IMAGE_TAG) bundle exec rubocop --format progress --format offenses --format junit --out out/results.xml
8585
endif
8686

87-
check.ruby.code: check.prepare
87+
check.ruby.code:
8888
$(MAKE) check.code LANGUAGE=ruby
8989

90-
check.ruby.deps: check.prepare
90+
check.ruby.deps:
9191
$(MAKE) check.deps LANGUAGE=ruby CHECK_DEPS_OPTS="-w app,rt-watchman"
9292

9393
pb.gen:

github_hooks/lib/semaphore/github_app/hook.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,13 @@ def self.accept_permissions(installation_id)
9090
end
9191

9292
def self.add_repositories(installation_id, repositories)
93-
sql_repositories = "[\"#{repositories.join('", "')}\"]"
9493
sql = <<-SQL
9594
UPDATE github_app_installations
96-
SET repositories = (SELECT to_jsonb(array_agg(DISTINCT b)) FROM (SELECT jsonb_array_elements_text(repositories || '#{sql_repositories}') as b FROM github_app_installations WHERE installation_id = '#{installation_id}') as c)
97-
WHERE installation_id = #{installation_id}
95+
SET repositories = (SELECT to_jsonb(array_agg(DISTINCT b)) FROM (SELECT jsonb_array_elements_text(repositories || $1::jsonb) AS b FROM github_app_installations WHERE installation_id = $2 ) AS c )
96+
WHERE installation_id = $2
9897
SQL
9998

100-
GithubAppInstallation.connection.exec_update(sql, "Adds GitHub App repositories")
99+
GithubAppInstallation.connection.exec_update(sql, "Adds GitHub App repositories", [repositories.to_json, installation_id])
101100

102101
repositories.each do |slug|
103102
# GitHub sends us a webhook before API is ready to admit that changes took place.
@@ -107,14 +106,13 @@ def self.add_repositories(installation_id, repositories)
107106
end
108107

109108
def self.remove_repositories(installation_id, repositories)
110-
sql_repositories = "'#{repositories.join("', '")}'"
111109
sql = <<-SQL
112110
UPDATE github_app_installations
113-
SET repositories = to_jsonb(array_diff((SELECT array_agg(trim(JsonString::text, '"')) FROM jsonb_array_elements(repositories) JsonString), array[#{sql_repositories}]))
114-
WHERE installation_id = #{installation_id}
111+
SET repositories = to_jsonb(array_diff((SELECT array_agg(trim(JsonString::text, '"')) FROM jsonb_array_elements(repositories) JsonString), $2::text[]))
112+
WHERE installation_id = $1
115113
SQL
116114

117-
GithubAppInstallation.connection.exec_update(sql, "Removes GitHub App repositories")
115+
GithubAppInstallation.connection.exec_update(sql, "Removes GitHub App repositories", [installation_id, "{#{repositories.join(",")}}"])
118116

119117
repositories.each do |slug|
120118
# GitHub sends us a webhook before API is ready to admit that changes took place.

0 commit comments

Comments
 (0)