Commit 9b2160bd authored by Jacob Schatz's avatar Jacob Schatz Committed by Micaël Bergeron

Static analysis pass

parent eefea6d2
require 'spec_helper' require 'spec_helper'
describe Gitlab::Pseudonymity do describe Gitlab::Pseudonymity do
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:base_dir) { Dir.mktmpdir } let(:base_dir) { Dir.mktmpdir }
subject(:pseudo) { Pseudonymity::Table.new } subject(:pseudo) { Pseudonymity::Table.new }
...@@ -16,15 +15,8 @@ describe Gitlab::Pseudonymity do ...@@ -16,15 +15,8 @@ describe Gitlab::Pseudonymity do
pseudo.config["output"]["csv"] = base_dir pseudo.config["output"]["csv"] = base_dir
pseudo.config["tables"] = { pseudo.config["tables"] = {
"projects" => { "projects" => {
"whitelist" => [ "whitelist" => %w(id name path description),
"id", "pseudo" => %w(id)
"name",
"path",
"description"
],
"pseudo" => [
"id"
]
} }
} }
...@@ -44,14 +36,14 @@ describe Gitlab::Pseudonymity do ...@@ -44,14 +36,14 @@ describe Gitlab::Pseudonymity do
if line_num == 0 if line_num == 0
columns = line.split(",") columns = line.split(",")
end end
if line_num == 1 if line_num == 1
project_data = line.split(",") project_data = line.split(",")
break break
end end
end end
# check if CSV columns are correct # check if CSV columns are correct
expect(columns.to_set).to eq(["id", "name", "path", "description\n"].to_set) expect(columns.to_set).to eq(%W(id name path description\n).to_set)
# is it pseudonymous # is it pseudonymous
expect(project_data[0]).not_to eq(1) expect(project_data[0]).not_to eq(1)
......
...@@ -13,7 +13,7 @@ module Pseudonymity ...@@ -13,7 +13,7 @@ module Pseudonymity
columns = results.columns # Assume they all have the same table columns = results.columns # Assume they all have the same table
to_filter = @anon_fields & columns to_filter = @anon_fields & columns
Enumerator.new do | yielder | Enumerator.new do |yielder|
results.each do |result| results.each do |result|
to_filter.each do |field| to_filter.each do |field|
secret = Rails.application.secrets[:secret_key_base] secret = Rails.application.secrets[:secret_key_base]
...@@ -39,10 +39,12 @@ module Pseudonymity ...@@ -39,10 +39,12 @@ module Pseudonymity
def tables_to_csv def tables_to_csv
tables = config["tables"] tables = config["tables"]
@csv_output = config["output"]["csv"] @csv_output = config["output"]["csv"]
if !File.directory?(@csv_output)
unless File.directory?(@csv_output)
raise "No such directory #{@csv_output}" raise "No such directory #{@csv_output}"
end end
tables.each do | k, v |
tables.each do |k, v|
@schema[k] = {} @schema[k] = {}
table_to_csv(k, v["whitelist"], v["pseudo"]) table_to_csv(k, v["whitelist"], v["pseudo"])
end end
...@@ -50,7 +52,7 @@ module Pseudonymity ...@@ -50,7 +52,7 @@ module Pseudonymity
file_list_to_json file_list_to_json
end end
def get_and_log_file_name(ext, prefix=nil, filename=nil) def get_and_log_file_name(ext, prefix = nil, filename = nil)
file_timestamp = filename || "#{prefix}_#{Time.now.to_i}" file_timestamp = filename || "#{prefix}_#{Time.now.to_i}"
file_timestamp = "#{file_timestamp}.#{ext}" file_timestamp = "#{file_timestamp}.#{ext}"
@output_files << file_timestamp @output_files << file_timestamp
...@@ -69,9 +71,7 @@ module Pseudonymity ...@@ -69,9 +71,7 @@ module Pseudonymity
def table_to_csv(table, whitelist_columns, pseudonymity_columns) def table_to_csv(table, whitelist_columns, pseudonymity_columns)
sql = "SELECT #{whitelist_columns.join(",")} FROM #{table};" sql = "SELECT #{whitelist_columns.join(",")} FROM #{table};"
# type_sql = "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '#{table}';"
results = ActiveRecord::Base.connection.exec_query(sql) results = ActiveRecord::Base.connection.exec_query(sql)
# type_results = ActiveRecord::Base.connection.exec_query(type_sql)
type_results = ActiveRecord::Base.connection.columns(table) type_results = ActiveRecord::Base.connection.columns(table)
type_results = type_results.select do |c| type_results = type_results.select do |c|
...@@ -79,9 +79,11 @@ module Pseudonymity ...@@ -79,9 +79,11 @@ module Pseudonymity
end end
type_results = type_results.map do |c| type_results = type_results.map do |c|
data_type = c.sql_type data_type = c.sql_type
if @config["tables"][table]["pseudo"].include?(c.name) if @config["tables"][table]["pseudo"].include?(c.name)
data_type = "character varying" data_type = "character varying"
end end
{ name: c.name, data_type: data_type } { name: c.name, data_type: data_type }
end end
set_schema_column_types(table, type_results) set_schema_column_types(table, type_results)
...@@ -92,7 +94,7 @@ module Pseudonymity ...@@ -92,7 +94,7 @@ module Pseudonymity
end end
def set_schema_column_types(table, type_results) def set_schema_column_types(table, type_results)
type_results.each do | type_result | type_results.each do |type_result|
@schema[table][type_result[:name]] = type_result[:data_type] @schema[table][type_result[:name]] = type_result[:data_type]
end end
# hard coded because all mapping keys in GL are id # hard coded because all mapping keys in GL are id
...@@ -106,14 +108,14 @@ module Pseudonymity ...@@ -106,14 +108,14 @@ module Pseudonymity
def write_to_csv_file(title, contents) def write_to_csv_file(title, contents)
file_path = get_and_log_file_name("csv", title) file_path = get_and_log_file_name("csv", title)
column_names = contents.first.keys column_names = contents.first.keys
contents = CSV.generate do | csv | contents = CSV.generate do |csv|
csv << column_names csv << column_names
contents.each do |x| contents.each do |x|
csv << x.values csv << x.values
end end
end end
File.open(file_path, 'w') { |file| file.write(contents) } File.open(file_path, 'w') { |file| file.write(contents) }
return file_path file_path
end end
private :write_to_csv_file private :write_to_csv_file
......
...@@ -71,7 +71,7 @@ namespace :gitlab do ...@@ -71,7 +71,7 @@ namespace :gitlab do
end end
desc 'Output pseudonymity dump of selected tables' desc 'Output pseudonymity dump of selected tables'
task :pseudonymity_dump => :environment do task pseudonymity_dump: :environment do
table = Pseudonymity::Table.new table = Pseudonymity::Table.new
table.tables_to_csv table.tables_to_csv
end end
......
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