summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-12-14 09:21:28 +0000
committerAlejandro Rodríguez <alejorro70@gmail.com>2016-12-14 10:35:55 -0300
commitd4f70db4637fd83d4bc17305d5ad30caab00ef9a (patch)
treef32bee9913018ab1874811baa4865fa410c38bab
parent9b1b55440f506719459655fe34bf809b84a701e0 (diff)
downloadgitlab-ce-d4f70db4637fd83d4bc17305d5ad30caab00ef9a.tar.gz
Merge branch 'process-commit-worker-migration-encoding' into 'master'
Encode when migrating ProcessCommitWorker jobs ## What does this MR do? This adds encoding logic to the migration for ProcessCommitWorker, ensuring it doesn't throw errors when the input can not be converted to UTF-8 without extra help. ## What are the relevant issue numbers? https://gitlab.com/gitlab-org/gitlab-ce/issues/25489 See merge request !8064
-rw-r--r--changelogs/unreleased/process-commit-worker-migration-encoding.yml4
-rw-r--r--db/migrate/20161124141322_migrate_process_commit_worker_jobs.rb20
-rw-r--r--spec/migrations/migrate_process_commit_worker_jobs_spec.rb27
3 files changed, 44 insertions, 7 deletions
diff --git a/changelogs/unreleased/process-commit-worker-migration-encoding.yml b/changelogs/unreleased/process-commit-worker-migration-encoding.yml
new file mode 100644
index 00000000000..26aabd9b647
--- /dev/null
+++ b/changelogs/unreleased/process-commit-worker-migration-encoding.yml
@@ -0,0 +1,4 @@
+---
+title: Encode input when migrating ProcessCommitWorker jobs to prevent migration errors
+merge_request:
+author:
diff --git a/db/migrate/20161124141322_migrate_process_commit_worker_jobs.rb b/db/migrate/20161124141322_migrate_process_commit_worker_jobs.rb
index 453a44e271a..77e0c40d850 100644
--- a/db/migrate/20161124141322_migrate_process_commit_worker_jobs.rb
+++ b/db/migrate/20161124141322_migrate_process_commit_worker_jobs.rb
@@ -47,14 +47,14 @@ class MigrateProcessCommitWorkerJobs < ActiveRecord::Migration
hash = {
id: commit.oid,
- message: commit.message,
+ message: encode(commit.message),
parent_ids: commit.parent_ids,
authored_date: commit.author[:time],
- author_name: commit.author[:name],
- author_email: commit.author[:email],
+ author_name: encode(commit.author[:name]),
+ author_email: encode(commit.author[:email]),
committed_date: commit.committer[:time],
- committer_email: commit.committer[:email],
- committer_name: commit.committer[:name]
+ committer_email: encode(commit.committer[:email]),
+ committer_name: encode(commit.committer[:name])
}
payload['args'][2] = hash
@@ -89,4 +89,14 @@ class MigrateProcessCommitWorkerJobs < ActiveRecord::Migration
end
end
end
+
+ def encode(data)
+ encoding = Encoding::UTF_8
+
+ if data.encoding == encoding
+ data
+ else
+ data.encode(encoding, invalid: :replace, undef: :replace)
+ end
+ end
end
diff --git a/spec/migrations/migrate_process_commit_worker_jobs_spec.rb b/spec/migrations/migrate_process_commit_worker_jobs_spec.rb
index 52428547a9f..6a93deb5412 100644
--- a/spec/migrations/migrate_process_commit_worker_jobs_spec.rb
+++ b/spec/migrations/migrate_process_commit_worker_jobs_spec.rb
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20161124141322_migrate_process_commit_worker_jobs.rb')
@@ -59,6 +61,10 @@ describe MigrateProcessCommitWorkerJobs do
Sidekiq.redis { |r| r.llen('queue:process_commit') }
end
+ def pop_job
+ JSON.load(Sidekiq.redis { |r| r.lpop('queue:process_commit') })
+ end
+
before do
Sidekiq.redis do |redis|
job = JSON.dump(args: [project.id, user.id, commit.oid])
@@ -92,11 +98,28 @@ describe MigrateProcessCommitWorkerJobs do
expect(job_count).to eq(1)
end
+ it 'encodes data to UTF-8' do
+ allow_any_instance_of(Rugged::Repository).to receive(:lookup).
+ with(commit.oid).
+ and_return(commit)
+
+ allow(commit).to receive(:message).
+ and_return('김치'.force_encoding('BINARY'))
+
+ migration.up
+
+ job = pop_job
+
+ # We don't care so much about what is being stored, instead we just want
+ # to make sure the encoding is right so that JSON encoding the data
+ # doesn't produce any errors.
+ expect(job['args'][2]['message'].encoding).to eq(Encoding::UTF_8)
+ end
+
context 'a migrated job' do
let(:job) do
migration.up
-
- JSON.load(Sidekiq.redis { |r| r.lpop('queue:process_commit') })
+ pop_job
end
let(:commit_hash) do