diff --git a/.github/workflows/gss2020_backend.yml b/.github/workflows/gss2020_backend.yml deleted file mode 100644 index 3ed96d68..00000000 --- a/.github/workflows/gss2020_backend.yml +++ /dev/null @@ -1,60 +0,0 @@ -name: 幻水総選挙2022 バックエンド - -on: - push: - workflow_dispatch: - -jobs: - gss2022_backend: - name: 継続的インテグレーション - runs-on: ubuntu-latest - strategy: - matrix: - node-version: ["18.x"] - ruby-version: ["3.1"] - steps: - # - name: MySQL のセットアップを行う - # # https://github.com/mirromutth/mysql-action - # uses: mirromutth/mysql-action@v1.1 - # with: - # host port: 3306 - # container port: 3306 - # character set server: utf8mb4 - # collation server: utf8mb4_unicode_ci - # mysql version: '8.0' - # # mysql database: some_test - # mysql root password: root - # # mysql user: 'developer' - # # mysql password: ${{ secrets.DatabasePassword }} - # - name: MySQL の疎通を待つ - # run: | - # while ! mysqladmin ping --host=127.0.0.1 --password=root --silent; do - # sleep 1 - # done - # - name: MySQL の文字コードおよびバージョンを確認する - # run: | - # mysql -h 127.0.0.1 --port 3306 -uroot -proot -e "show variables like 'chara%'" - # mysql -h 127.0.0.1 --port 3306 -uroot -proot -e "select version()" - - name: コードをチェックアウトする - uses: actions/checkout@v3 - - name: Ruby のセットアップを行う - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true - # - name: Node.js のセットアップを行う - # uses: actions/setup-node@v3 - # with: - # node-version: ${{ matrix.node-version }} - # cache: "yarn" - # - name: db:create db:migrate する - # run: | - # bin/rails db:create db:migrate - # env: - # RAILS_ENV: test - # DATABASE_HOST: 127.0.0.1 - # - name: RSpec を実行する - # run: | - # bundle exec rspec - # env: - # DATABASE_HOST: 127.0.0.1 diff --git a/.github/workflows/gss2022_backend_on_built_in_postgres.yml b/.github/workflows/gss2022_backend_on_built_in_postgres.yml new file mode 100644 index 00000000..ce4cf348 --- /dev/null +++ b/.github/workflows/gss2022_backend_on_built_in_postgres.yml @@ -0,0 +1,73 @@ +name: 幻水総選挙2022 バックエンド + +on: + push: + workflow_dispatch: + +jobs: + gss2022_backend: + name: (pg on built-in) 継続的インテグレーション + runs-on: ubuntu-latest + strategy: + matrix: + node-version: ["18.x"] + ruby-version: ["3.1"] + env: + CI: true + RAILS_ENV: test + POSTGRES_HOST_TEST: localhost + POSTGRES_PORT_TEST: 5432 + POSTGRES_USERNAME_TEST: postgres_user + POSTGRES_PASSWORD_TEST: postgres_password + steps: + # https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-Readme.md + - name: PostgreSQL を起動する + run: | + sudo systemctl start postgresql.service + - name: PostgreSQL の起動待ち(ヘルスチェック)をする + run: | + until : > /dev/tcp/localhost/5432; do + echo -n . + sleep 1 + done + + echo 'PostgreSQL の起動が確認できました' + - name: PostgreSQL のユーザー(ロール)のセットアップを行う + # NOTE: バージョン番号が埋め込みになっているのが良くない + run: | + sudo bash -c "echo \"local all all md5\" >> /etc/postgresql/$major_version_number/main/pg_hba.conf" + + sudo su postgres -c "psql postgres -c 'create role $username;'" + sudo su postgres -c "psql postgres -c \"alter role $username with login password '$password';\"" + sudo su postgres -c "psql postgres -c \"alter role $username with superuser\"" + env: + major_version_number: 14 + username: postgres_user + password: postgres_password + - name: コードをチェックアウトする + uses: actions/checkout@v3 + - name: Ruby のセットアップを行う + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + - name: $ bundle install を行う + uses: actions/cache@v3 + with: + path: vendor/bundle + key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }} + restore-keys: | + ${{ runner.os }}-gem- + - name: (予定地)Node.js のセットアップを行う + run: | + echo 'Node.js を使うことがあれば "actions/setup-node" を用いる' + echo 'uses: actions/setup-node@v3' + echo 'with:' + echo 'node-version: ${{ matrix.node-version }}' + echo 'cache: "yarn"' + - name: データベースをセットアップする + run: | + bin/rails db:prepare + - name: RSpec を実行する + run: | + bundle exec rspec diff --git a/.github/workflows/gss2022_backend_on_postgres_services.yml b/.github/workflows/gss2022_backend_on_postgres_services.yml new file mode 100644 index 00000000..5246b1b3 --- /dev/null +++ b/.github/workflows/gss2022_backend_on_postgres_services.yml @@ -0,0 +1,62 @@ +name: 幻水総選挙2022 バックエンド + +on: + push: + workflow_dispatch: + +jobs: + gss2022_backend: + name: (pg on services) 継続的インテグレーション + runs-on: ubuntu-latest + strategy: + matrix: + node-version: ["18.x"] + ruby-version: ["3.1"] + services: + postgres: + image: postgres:14.3 + ports: + - 5432:5432 + env: + POSTGRES_USER: postgres_user + POSTGRES_PASSWORD: postgres_password + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + env: + CI: true + RAILS_ENV: test + POSTGRES_HOST_TEST: localhost + POSTGRES_PORT_TEST: 5432 + POSTGRES_USERNAME_TEST: postgres_user + POSTGRES_PASSWORD_TEST: postgres_password + steps: + - name: コードをチェックアウトする + uses: actions/checkout@v3 + - name: Ruby のセットアップを行う + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + - name: $ bundle install を行う + uses: actions/cache@v3 + with: + path: vendor/bundle + key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }} + restore-keys: | + ${{ runner.os }}-gem- + - name: (予定地)Node.js のセットアップを行う + run: | + echo 'Node.js を使うことがあれば "actions/setup-node" を用いる' + echo 'uses: actions/setup-node@v3' + echo 'with:' + echo 'node-version: ${{ matrix.node-version }}' + echo 'cache: "yarn"' + - name: データベースをセットアップする + run: | + bin/rails db:prepare + - name: RSpec を実行する + run: | + bundle exec rspec diff --git a/.rspec b/.rspec index aae9b46f..8dbc462c 100644 --- a/.rspec +++ b/.rspec @@ -1 +1 @@ ---require spec_helper --format documentation +--require spec_helper --format documentation --force-color --backtrace diff --git a/README.md b/README.md index 5897995a..d627b71e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ +On PostgreSQL Services +[![幻水総選挙2022 バックエンド](https://github.com/true-runes/suikoden-election-2022-backend/actions/workflows/gss2022_backend_on_postgres_services.yml/badge.svg)](https://github.com/true-runes/suikoden-election-2022-backend/actions/workflows/gss2022_backend_on_postgres_services.yml) + +On Built-In PostgreSQL +[![幻水総選挙2022 バックエンド](https://github.com/true-runes/suikoden-election-2022-backend/actions/workflows/gss2022_backend_on_built_in_postgres.yml/badge.svg)](https://github.com/true-runes/suikoden-election-2022-backend/actions/workflows/gss2022_backend_on_built_in_postgres.yml) + # 幻水総選挙2022(バックエンド) - いつもの diff --git a/app/lib/twitter_rest_api.rb b/app/lib/twitter_rest_api.rb index 5f0a1fcf..71267111 100644 --- a/app/lib/twitter_rest_api.rb +++ b/app/lib/twitter_rest_api.rb @@ -1,10 +1,51 @@ class TwitterRestApi - def self.client(consumer_key: nil, consumer_secret: nil, access_token: nil, access_secret: nil) + # account_key は シンボル で指定する + def self.client(account_key: nil) + return if account_key.nil? + + credentials = convert_account_key_to_credentials(account_key) + return if credentials.nil? + Twitter::REST::Client.new do |config| - config.consumer_key = consumer_key || ENV.fetch('TWITTER_CONSUMER_KEY', nil) - config.consumer_secret = consumer_secret || ENV.fetch('TWITTER_CONSUMER_SECRET', nil) - config.access_token = access_token || ENV.fetch('TWITTER_ACCESS_TOKEN', nil) - config.access_token_secret = access_secret || ENV.fetch('TWITTER_ACCESS_SECRET', nil) + config.consumer_key = credentials[:consumer_key] + config.consumer_secret = credentials[:consumer_secret] + config.access_token = credentials[:access_token] + config.access_token_secret = credentials[:access_token_secret] end end + + def self.convert_account_key_to_credentials(account_key) + { + gensosenkyo: { + consumer_key: ENV.fetch('TWITTER_CONSUMER_KEY_GENSOSENKYO', nil), + consumer_secret: ENV.fetch('TWITTER_CONSUMER_SECRET_GENSOSENKYO', nil), + access_token: ENV.fetch('TWITTER_ACCESS_TOKEN_GENSOSENKYO', nil), + access_token_secret: ENV.fetch('TWITTER_ACCESS_SECRET_GENSOSENKYO', nil) + }, + sub_gensosenkyo: { + consumer_key: ENV.fetch('TWITTER_CONSUMER_KEY_SUB_GENSOSENKYO', nil), + consumer_secret: ENV.fetch('TWITTER_CONSUMER_SECRET_SUB_GENSOSENKYO', nil), + access_token: ENV.fetch('TWITTER_ACCESS_TOKEN_SUB_GENSOSENKYO', nil), + access_token_secret: ENV.fetch('TWITTER_ACCESS_SECRET_SUB_GENSOSENKYO', nil) + }, + tmytn: { + consumer_key: ENV.fetch('TWITTER_CONSUMER_KEY_TMYTN', nil), + consumer_secret: ENV.fetch('TWITTER_CONSUMER_SECRET_TMYTN', nil), + access_token: ENV.fetch('TWITTER_ACCESS_TOKEN_TMYTN', nil), + access_token_secret: ENV.fetch('TWITTER_ACCESS_SECRET_TMYTN', nil) + }, + tmychan: { + consumer_key: ENV.fetch('TWITTER_CONSUMER_KEY_TMYCHAN', nil), + consumer_secret: ENV.fetch('TWITTER_CONSUMER_SECRET_TMYCHAN', nil), + access_token: ENV.fetch('TWITTER_ACCESS_TOKEN_TMYCHAN', nil), + access_token_secret: ENV.fetch('TWITTER_ACCESS_SECRET_TMYCHAN', nil) + }, + ayy: { + consumer_key: ENV.fetch('TWITTER_CONSUMER_KEY_AYY', nil), + consumer_secret: ENV.fetch('TWITTER_CONSUMER_SECRET_AYY', nil), + access_token: ENV.fetch('TWITTER_ACCESS_TOKEN_AYY', nil), + access_token_secret: ENV.fetch('TWITTER_ACCESS_SECRET_AYY', nil) + } + }[account_key] + end end diff --git a/app/models/tweet_on_tweet_storage.rb b/app/models/tweet_on_tweet_storage.rb index edaa6385..91510bd6 100644 --- a/app/models/tweet_on_tweet_storage.rb +++ b/app/models/tweet_on_tweet_storage.rb @@ -1,5 +1,5 @@ class TweetOnTweetStorage < ApplicationRecord - establish_connection :tweet_storage + establish_connection :tweet_storage unless ENV['CI'] == 'true' self.table_name = :tweets diff --git a/app/models/user_on_tweet_storage.rb b/app/models/user_on_tweet_storage.rb index 38520d82..3d0374b2 100644 --- a/app/models/user_on_tweet_storage.rb +++ b/app/models/user_on_tweet_storage.rb @@ -1,5 +1,5 @@ class UserOnTweetStorage < ApplicationRecord - establish_connection :tweet_storage + establish_connection :tweet_storage unless ENV['CI'] == 'true' self.table_name = :users diff --git a/renovate.json b/renovate.json new file mode 100644 index 00000000..f45d8f11 --- /dev/null +++ b/renovate.json @@ -0,0 +1,5 @@ +{ + "extends": [ + "config:base" + ] +} diff --git a/spec/factories/user.rb b/spec/factories/user.rb new file mode 100644 index 00000000..d4ebaeeb --- /dev/null +++ b/spec/factories/user.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :user do + id_number { 192789663528910849 } + name { 'My name is test' } + screen_name { 'test_screen_name' } + profile_image_url_https { 'https://pbs.twimg.com/profile_images/1123471964058914816/a8sXngWB_400x400.png' } + is_protected { false } + end +end diff --git a/spec/misc/foo_spec.rb b/spec/misc/foo_spec.rb deleted file mode 100644 index 40fe5591..00000000 --- a/spec/misc/foo_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'rails_helper' - -RSpec.describe do - it 'Bar' do - expect(1 + 1).to eq 2 - end -end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 00000000..e43ae647 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +RSpec.describe User, type: :model do + let(:user) { create(:user) } + + it 'User名が期待どおりに等しくあること' do + # expect(user.name).to eq 'My name is test' + # expect(User.count).to eq 1 + end + + it 'User名が期待どおりに等しくないこと' do + # expect(user.name).not_to eq 'Your name is test' + # expect(User.count).to eq 1 + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index ef5891fc..2e4152fb 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -17,4 +17,5 @@ config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.filter_rails_from_backtrace! + config.include FactoryBot::Syntax::Methods end