From 027be381331ce7a7ae2792eaf155ccb773b01628 Mon Sep 17 00:00:00 2001 From: eliegaboriau <93646702+eliegaboriau@users.noreply.github.com> Date: Fri, 16 Jun 2023 10:36:08 +0200 Subject: [PATCH 1/2] Avoid generating many images (#27) --- .github/workflows/ci_cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index 5a4a6008..03aa8a4e 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -133,7 +133,7 @@ jobs: namespace: ${{ vars.REGISTRY_NAMESPACE }} password: ${{ secrets.TOKEN }} image_name: ${{ vars.IMAGE_NAME }} - tag: "develop-${{ github.sha }}" + tag: "develop" generate_release: name: Generate release needs: [ lint, tests, test_build ] From 8d00e39fd9901cb44242d7d9083832104f8f3f88 Mon Sep 17 00:00:00 2001 From: Guillaume MORET <90462045+AyakorK@users.noreply.github.com> Date: Tue, 20 Jun 2023 09:40:25 +0200 Subject: [PATCH 2/2] [Backport] - Rake task to repair data from dcd-app (#26) * Backport changes from decidim-app * Fix rubocop issue * Rename file and backport changes from dcd-app (master branch) * Remove separator * Fix tests * Fix lint --- config/locales/en.yml | 5 -- config/locales/fr.yml | 5 -- lib/tasks/repair_data.rake | 75 +++++++++++++++++++ lib/tasks/repare_data.rake | 71 ------------------ .../proposals/proposals_controller_spec.rb | 2 +- spec/lib/tasks/repair_data_spec.rb | 71 ++++++++++++++++++ spec/lib/tasks/repare_data_spec.rb | 70 ----------------- 7 files changed, 147 insertions(+), 152 deletions(-) create mode 100644 lib/tasks/repair_data.rake delete mode 100644 lib/tasks/repare_data.rake create mode 100644 spec/lib/tasks/repair_data_spec.rb delete mode 100644 spec/lib/tasks/repare_data_spec.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 0016b500..4985b7ea 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,11 +15,6 @@ en: destroy: error: Error success: Success - admin: - participatory_space_private_users: - create: - error: Error - success: Success amendments: emendation: announcement: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 9e8cee6b..15041c68 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -15,11 +15,6 @@ fr: destroy: error: Votre compte a été supprimé avec succès. success: Il y a eu un problème lors de la suppression de votre compte. - admin: - participatory_space_private_users: - create: - error: Erreur - success: Succès amendments: emendation: announcement: diff --git a/lib/tasks/repair_data.rake b/lib/tasks/repair_data.rake new file mode 100644 index 00000000..cb824e5a --- /dev/null +++ b/lib/tasks/repair_data.rake @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +namespace :decidim do + namespace :repair do + desc "Check for nicknames that doesn't respect valid format and update them, if needed force update with REPAIR_NICKNAME_FORCE=1" + # TODO: Extract to a lib + task nickname: :environment do + logger = Logger.new($stdout) + logger.info("[decidim:repair:nickname] :: Checking all nicknames...") + invalid_users = Decidim::User.where.not("nickname ~* ?", "^[\\w-]+$") + + if invalid_users.blank? + logger.info("[decidim:repair:nickname] :: All nicknames seems to be valid") + logger.info("[decidim:repair:nickname] :: Operation terminated") + else + logger.info("[decidim:repair:nickname] :: Found #{invalid_users.count} invalids nicknames") + logger.info("[decidim:repair:nickname] :: Invalid user IDs : [#{invalid_users.map(&:id).join(", ")}]") + + updated_users = [] + invalid_users.each do |user| + chars = [] + + user.nickname.codepoints.each do |ascii_code| + char = ascii_to_valid_char(ascii_code) + chars << char if char.present? + end + + new_nickname = chars.join.downcase + logger.info("[decidim:repair:nickname] :: User (##{user.id}) renaming nickname from '#{user.nickname}' to '#{new_nickname}'") + user.nickname = new_nickname + + updated_users << user + end + + if ask_for_permission(logger, updated_users.count) + logger.info("[decidim:repair:nickname] :: Updating users...") + updated_users.each do |user| + user.save! + logger.info("[decidim:repair:nickname] :: User (##{user.id}) successfully updated") + rescue ActiveRecord::RecordInvalid => e + logger.error("[decidim:repair:nickname] :: User (##{user.id}) failed to update : #{e.message}") + logger.error("[decidim:repair:nickname] :: Trying with a another nickname: #{user.nickname}-#{user.id}") + user.nickname = "#{user.nickname}#{user.id}" + user.save! + rescue StandardError => e + logger.error("[decidim:repair:nickname] :: User (##{user.id}) an error occured") + logger.error("[decidim:repair:nickname] :: #{e}") + end + else + logger.info("[decidim:repair:nickname] :: Operation terminated") + end + end + end + end +end + +def ask_for_permission(logger, users_count) + logger.info("[decidim:repair:nickname] :: Do you want to update these #{users_count} users ?") + return true if ENV["REPAIR_NICKNAME_FORCE"] == "1" + + logger.info("[decidim:repair:nickname] :: prepend REPAIR_NICKNAME_FORCE=1 to your command to update") + + false +end + +def ascii_to_valid_char(id) + letters = ("A".."Z").to_a.join("").codepoints + letters += ("a".."z").to_a.join("").codepoints + digits = ("0".."9").to_a.join("").codepoints + special_chars = %w(- _).join("").codepoints + + valid_ascii_code = letters + digits + special_chars + + valid_ascii_code.include?(id) ? id.chr : "" +end diff --git a/lib/tasks/repare_data.rake b/lib/tasks/repare_data.rake deleted file mode 100644 index 50eb4a13..00000000 --- a/lib/tasks/repare_data.rake +++ /dev/null @@ -1,71 +0,0 @@ -# frozen_string_literal: true - -namespace :decidim do - namespace :repare do - desc "Check for nicknames that doesn't respect valid format and update them" - task nickname: :environment do - logger = Logger.new($stdout) - logger.info("[data:repare:nickname] :: Checking all nicknames...") - invalid_users = Decidim::User.where.not("nickname ~* ?", "^[\\w-]+$") - - if invalid_users.blank? - logger.info("[data:repare:nickname] :: All nicknames seems to be valid") - logger.info("[data:repare:nickname] :: Operation terminated") - exit 0 - end - - logger.info("[data:repare:nickname] :: Found #{invalid_users.count} invalids nicknames") - logger.info("[data:repare:nickname] :: Invalid user IDs : [#{invalid_users.map(&:id).join(", ")}]") - - updated_users = [] - invalid_users.each do |user| - chars = [] - - user.nickname.codepoints.each do |ascii_code| - char = ascii_to_valid_char(ascii_code) - chars << char if char.present? - end - - new_nickname = chars.join.downcase - logger.info("[data:repare:nickname] :: User (##{user.id}) renaming nickname from '#{user.nickname}' to '#{new_nickname}'") - user.nickname = new_nickname - - updated_users << user - end - - if ask_for_permission(updated_users.count) - logger.info("[data:repare:nickname] :: Updating users...") - updated_users.each do |user| - user.save! - logger.info("[data:repare:nickname] :: User (##{user.id}) successfully updated") - rescue StandardError => e - logger.error("[data:repare:nickname] :: User (##{user.id}) an error occured") - logger.error("[data:repare:nickname] :: #{e}") - end - else - logger.info("[data:repare:nickname] :: Operation terminated") - end - logger.close - - exit 0 - end - end -end - -def ask_for_permission(users_count) - $stdout.puts "Do you want to update these #{users_count} users ? [y/n]" - answer = $stdin.gets.chomp - - %w(y Y yes YES).include?(answer) -end - -def ascii_to_valid_char(id) - letters = ("A".."Z").to_a.join.codepoints - letters += ("a".."z").to_a.join.codepoints - digits = ("0".."9").to_a.join.codepoints - special_chars = %w(- _).join.codepoints - - valid_ascii_code = letters + digits + special_chars - - valid_ascii_code.include?(id) ? id.chr : "" -end diff --git a/spec/controllers/decidim/proposals/proposals_controller_spec.rb b/spec/controllers/decidim/proposals/proposals_controller_spec.rb index 71bb3a9b..f8bf57a7 100644 --- a/spec/controllers/decidim/proposals/proposals_controller_spec.rb +++ b/spec/controllers/decidim/proposals/proposals_controller_spec.rb @@ -285,7 +285,7 @@ module Proposals put :withdraw, params: params.merge(id: proposal.id) - expect(flash[:alert]).to eq("You are not authorized to perform this action") + expect(flash[:alert]).to eq("Unauthorized") expect(response).to have_http_status(:found) proposal.reload expect(proposal.withdrawn?).to be false diff --git a/spec/lib/tasks/repair_data_spec.rb b/spec/lib/tasks/repair_data_spec.rb new file mode 100644 index 00000000..6dc43dd8 --- /dev/null +++ b/spec/lib/tasks/repair_data_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe "rake decidim:repair:nickname", type: :task do + let!(:organization) { create(:organization) } + let(:task_cmd) { :"decidim:repair:nickname" } + + let!(:valid_user) { create(:user, nickname: "Azerty_Uiop123", organization: organization) } + let(:invalid_user_1) { build(:user, nickname: "Foo bar", organization: organization) } + let(:invalid_user_2) { build(:user, nickname: "Foo M. bar", organization: organization) } + let(:invalid_user_3) { build(:user, nickname: "Foo-Bar_fooo$", organization: organization) } + let(:invalid_user_4) { build(:user, nickname: "foo.bar.foo", organization: organization) } + let(:invalid_user_5) { build(:user, nickname: ".foobar.foo_bar.", organization: organization) } + let(:invalid_user_6) { build(:user, nickname: "Foo bar", organization: organization) } + let(:invalid_user_7) { build(:user, nickname: "Foo bar", organization: organization) } + + let(:repair_nick_name_force) { "1" } + + let(:environment) do + { + "REPAIR_NICKNAME_FORCE" => repair_nick_name_force + } + end + + before do + invalid_user_1.save(validate: false) + invalid_user_2.save(validate: false) + invalid_user_3.save(validate: false) + invalid_user_4.save(validate: false) + invalid_user_5.save(validate: false) + invalid_user_6.save(validate: false) + invalid_user_7.save(validate: false) + + Rake::Task[task_cmd].reenable + end + + it "updates invalid nicknames" do + with_modified_env(environment) do + Rake::Task[task_cmd].invoke + + expect(invalid_user_1.reload.nickname).to eq("foobar") + expect(invalid_user_2.reload.nickname).to eq("foombar") + expect(invalid_user_3.reload.nickname).to eq("foo-bar_fooo") + expect(invalid_user_4.reload.nickname).to eq("foobarfoo") + expect(invalid_user_5.reload.nickname).to eq("foobarfoo_bar") + expect(invalid_user_6.reload.nickname).to eq("foobar#{invalid_user_6.id}") + expect(invalid_user_7.reload.nickname).to eq("foobar#{invalid_user_7.id}") + expect(valid_user.reload.nickname).to eq("Azerty_Uiop123") + end + end + + context "when user refuses update" do + let(:repair_nick_name_force) { "0" } + + it "updates invalid nicknames" do + with_modified_env(environment) do + Rake::Task[task_cmd].invoke + + expect(invalid_user_1.reload.nickname).to eq("Foo bar") + expect(invalid_user_2.reload.nickname).to eq("Foo M. bar") + expect(invalid_user_3.reload.nickname).to eq("Foo-Bar_fooo$") + expect(invalid_user_4.reload.nickname).to eq("foo.bar.foo") + expect(invalid_user_5.reload.nickname).to eq(".foobar.foo_bar.") + expect(invalid_user_6.reload.nickname).to eq("Foo bar") + expect(invalid_user_7.reload.nickname).to eq("Foo bar") + expect(valid_user.reload.nickname).to eq("Azerty_Uiop123") + end + end + end +end diff --git a/spec/lib/tasks/repare_data_spec.rb b/spec/lib/tasks/repare_data_spec.rb deleted file mode 100644 index 1c34c161..00000000 --- a/spec/lib/tasks/repare_data_spec.rb +++ /dev/null @@ -1,70 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe "rake decidim:repare:nickname", type: :task do - let!(:organization) { create(:organization) } - let(:task_cmd) { :"decidim:repare:nickname" } - - let!(:user) { create(:user, organization: organization) } - let!(:valid_user_2) { create(:user, nickname: "Azerty_Uiop123", organization: organization) } - let(:invalid_user_1) { build(:user, nickname: "Foo bar", organization: organization) } - let(:invalid_user_2) { build(:user, nickname: "Foo M. bar", organization: organization) } - let(:invalid_user_3) { build(:user, nickname: "Foo-Bar_fooo$", organization: organization) } - let(:invalid_user_4) { build(:user, nickname: "foo.bar.foo", organization: organization) } - let(:invalid_user_5) { build(:user, nickname: ".foobar.foo_bar.", organization: organization) } - - context "when executing task" do - before do - invalid_user_1.save(validate: false) - invalid_user_2.save(validate: false) - invalid_user_3.save(validate: false) - invalid_user_4.save(validate: false) - invalid_user_5.save(validate: false) - end - - it "exits with 0 code" do - allow($stdin).to receive(:gets).and_return("y") - - expect { Rake::Task[task_cmd].invoke }.to raise_error(SystemExit) do |error| - expect(error.status).to eq(0) - end - end - - context "when user accepts update" do - it "updates invalid nicknames" do - allow($stdin).to receive(:gets).and_return("y") - - expect { Rake::Task[task_cmd].invoke }.to change(invalid_user_1, :nickname).from("Foo bar").to("foobar") - - invalid_user_1.reload - expect(invalid_user_1.nickname).to eq("foobar") - invalid_user_2.reload - expect(invalid_user_2.nickname).to eq("foombar") - invalid_user_3.reload - expect(invalid_user_3.nickname).to eq("foobarfooo") - invalid_user_4.reload - expect(invalid_user_4.nickname).to eq("foobarfoo") - end - end - - context "when user refuses update" do - it "updates invalid nicknames" do - allow($stdin).to receive(:gets).and_return("n") - - expect { Rake::Task[task_cmd].invoke }.not_to change(invalid_user_1, :nickname) - - invalid_user_1.reload - expect(invalid_user_1.nickname).to eq("Foo bar") - invalid_user_2.reload - expect(invalid_user_2.nickname).to eq("Foo M. bar") - invalid_user_3.reload - expect(invalid_user_3.nickname).to eq("Foo-Bar_fooo$") - invalid_user_4.reload - expect(invalid_user_4.nickname).to eq("foo.bar.foo") - invalid_user_5.reload - expect(invalid_user_5.nickname).to eq(".foobar.foo_bar.") - end - end - end -end