-
-
Notifications
You must be signed in to change notification settings - Fork 109
トップページの news セクションを RSS から自動生成 #1704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nacchan99
wants to merge
17
commits into
main
Choose a base branch
from
feat/1577-auto-news-from-rss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
92dd480
feat: RSSフィードチェックタスクを追加
nacchan99 2a9c1f2
ci: news:fetchタスクを毎朝9:00 JSTに実行するワークフローを追加
nacchan99 711eabe
Newsモデルとマイグレーション、テストファイルを追加
nacchan99 0f45187
ニュース YAML→DB インポートタスクを追加&news.yml を初期化
nacchan99 66f64e1
Release Phaseに ニュースポイントを追加
nacchan99 dfceb2f
fetch_newsタスクのフィードURLを https://coderdojo.jp/#news に修正
nacchan99 4030fa4
feat:fetch_newsタスクのフィードURLを https://news.coderdojo.jp/feed/ に修正
nacchan99 98b657e
feat: ニュースセクションをDBからの動的表示に切り替え
nacchan99 c5e4b24
refactor: ニュース絵文字の判定ロジックを改善
nacchan99 88358e8
テストが通らない問題を解消するため origin/main の変更をマージ
nacchan99 aa79f4a
refactor: ニュース表示ロジックを改善
nacchan99 f995b08
test: サンプルニュースに絵文字ありのテストケースを追加
nacchan99 2200d6b
fix: 不要な改行を削除
nacchan99 46e7e97
fix: 不要なスペースを削除
nacchan99 4974b01
refactor: ニュース取得ロジックをモデルに移動
nacchan99 f577bcb
ワークフローをブランチでも実行
nacchan99 0fe9433
chore: update news.yml via GitHub Actions
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Fetch News | ||
|
||
on: | ||
schedule: | ||
# 毎朝 9:00 JST | ||
- cron: '0 0 * * *' | ||
workflow_dispatch: | ||
pull_request: | ||
branches: | ||
- "*" | ||
|
||
jobs: | ||
fetch: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version-file: .ruby-version | ||
bundler-cache: true | ||
|
||
- name: Install dependencies | ||
run: bundle install --jobs 4 --retry 3 | ||
|
||
- name: Run news:fetch task | ||
run: bin/rails news:fetch | ||
|
||
- name: Commit updated news.yml | ||
run: | | ||
git config user.name "github-actions[bot]" | ||
git config user.email "github-actions[bot]@users.noreply.github.com" | ||
git add db/news.yml | ||
if ! git diff --cached --quiet; then | ||
git commit -m "chore: update news.yml via GitHub Actions" | ||
git push | ||
else | ||
echo "No changes in db/news.yml" | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class News < ApplicationRecord | ||
scope :recent, -> { order(published_at: :desc) } | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateNews < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :news do |t| | ||
t.string :title | ||
t.string :url | ||
t.datetime :published_at | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :news, :url, unique: true | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
news: | ||
- url: https://news.coderdojo.jp/2024/12/25/box-japan-to-coderdojo/ | ||
title: "\U0001F384128台のノート PC 寄贈、Box Japan から CoderDojo へ \U0001F381" | ||
published_at: Wed, 25 Dec 2024 11:00:40 +0000 | ||
id: https://news.coderdojo.jp/2024/12/25/box-japan-to-coderdojo/ | ||
- url: https://news.coderdojo.jp/2025/01/14/dojoletter-vol-79-2024%e5%b9%b411%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.79 2024年11月号 | ||
published_at: Tue, 14 Jan 2025 03:30:45 +0000 | ||
id: https://news.coderdojo.jp/2025/01/14/dojoletter-vol-79-2024%e5%b9%b411%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/06/10/dojoletter-vol-85-2025%e5%b9%b404%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.85 2025年04月号 | ||
published_at: Tue, 10 Jun 2025 03:30:18 +0000 | ||
id: https://news.coderdojo.jp/2025/06/10/dojoletter-vol-85-2025%e5%b9%b404%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/07/10/dojoletter-vol-86-2025%e5%b9%b405%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.86 2025年05月号 | ||
published_at: Thu, 10 Jul 2025 04:00:07 +0000 | ||
id: https://news.coderdojo.jp/2025/07/10/dojoletter-vol-86-2025%e5%b9%b405%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/04/10/dojoletter-vol-83-2025%e5%b9%b402%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.83 2025年02月号 | ||
published_at: Thu, 10 Apr 2025 03:45:27 +0000 | ||
id: https://news.coderdojo.jp/2025/04/10/dojoletter-vol-83-2025%e5%b9%b402%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/05/12/dojoletter-vol-84-2025%e5%b9%b403%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.84 2025年03月号 | ||
published_at: Mon, 12 May 2025 04:00:33 +0000 | ||
id: https://news.coderdojo.jp/2025/05/12/dojoletter-vol-84-2025%e5%b9%b403%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/03/10/dojoletter-vol-82-2025%e5%b9%b401%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.82 2025年01月号 | ||
published_at: Mon, 10 Mar 2025 04:00:33 +0000 | ||
id: https://news.coderdojo.jp/2025/03/10/dojoletter-vol-82-2025%e5%b9%b401%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/02/10/dojoletter-vol-80-2024%e5%b9%b412%e6%9c%88%e5%8f%b7/ | ||
title: DojoLetter Vol.80 2024年12月号 | ||
published_at: Mon, 10 Feb 2025 04:00:55 +0000 | ||
id: https://news.coderdojo.jp/2025/02/10/dojoletter-vol-80-2024%e5%b9%b412%e6%9c%88%e5%8f%b7/ | ||
- url: https://news.coderdojo.jp/2025/02/14/coderdojo-de-nyaicecode/ | ||
title: "\U0001F3B2 ダイス×プログラミング『ニャイス!コード』を、CoderDojo に75台寄贈" | ||
published_at: Fri, 14 Feb 2025 08:24:07 +0000 | ||
id: https://news.coderdojo.jp/2025/02/14/coderdojo-de-nyaicecode/ | ||
- url: https://news.coderdojo.jp/2025/04/04/55-laptops-to-coderdojo/ | ||
title: 米国系 IT 企業から CoderDojo へ、55 台のノート PC 寄贈 | ||
published_at: Fri, 04 Apr 2025 10:00:32 +0000 | ||
id: https://news.coderdojo.jp/2025/04/04/55-laptops-to-coderdojo/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'rss' | ||
require 'open-uri' | ||
require 'yaml' | ||
require 'active_support/broadcast_logger' | ||
|
||
namespace :news do | ||
desc 'RSS フィードから最新ニュースを取得し、db/news.yml に書き出す' | ||
task fetch: :environment do | ||
# ロガー設定(ファイル+コンソール出力) | ||
file_logger = ActiveSupport::Logger.new('log/news.log') | ||
console = ActiveSupport::Logger.new(STDOUT) | ||
logger = ActiveSupport::BroadcastLogger.new(file_logger, console) | ||
|
||
logger.info('==== START news:fetch ====') | ||
|
||
# テスト/ステージング環境ではサンプルファイル、本番は実サイトのフィード | ||
feed_urls = if Rails.env.test? || Rails.env.staging? | ||
[Rails.root.join('spec', 'fixtures', 'sample_news.rss').to_s] | ||
else | ||
[ | ||
'https://news.coderdojo.jp/feed/', | ||
# 必要に応じて他 Dojo の RSS もここに追加可能 | ||
# 'https://coderdojotokyo.org/feed', | ||
] | ||
end | ||
|
||
# RSS 取得&パース | ||
items = feed_urls.flat_map do |url| | ||
logger.info("Fetching RSS → #{url}") | ||
begin | ||
URI.open(url) do |rss| | ||
feed = RSS::Parser.parse(rss, false) | ||
feed.items.map do |item| | ||
{ | ||
'url' => item.link, | ||
'title' => item.title, | ||
'published_at' => item.pubDate.to_s | ||
} | ||
end | ||
end | ||
rescue => e | ||
logger.warn("⚠️ Failed to fetch #{url}: #{e.message}") | ||
[] | ||
end | ||
end | ||
|
||
# 重複排除&日付降順ソート | ||
unique = items.uniq { |i| i['url'] } | ||
sorted = unique.sort_by { |i| i['published_at'] }.reverse | ||
|
||
# id を追加 | ||
sorted.each { |i| i['id'] = i['url'] } | ||
|
||
# YAML に書き出し | ||
File.open('db/news.yml', 'w') do |f| | ||
f.write({ 'news' => sorted }.to_yaml) | ||
end | ||
|
||
logger.info("✅ Wrote #{sorted.size} items to db/news.yml") | ||
logger.info('==== END news:fetch ====') | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'yaml' | ||
|
||
namespace :news do | ||
desc "db/news.yml を読み込んで News テーブルを upsert する" | ||
task import_from_yaml: :environment do | ||
yaml_path = Rails.root.join('db', 'news.yml') | ||
raw = YAML.load_file(yaml_path) | ||
|
||
# entries を計算 | ||
entries = raw['news'] || [] | ||
|
||
entries.each do |attrs| | ||
news = News.find_or_initialize_by(url: attrs['url']) | ||
news.assign_attributes( | ||
title: attrs['title'], | ||
published_at: attrs['published_at'] | ||
) | ||
news.save! | ||
puts "[news] #{news.published_at.to_date} #{news.title}" | ||
end | ||
|
||
puts "Imported #{entries.size} items." | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FactoryBot.define do | ||
factory :news do | ||
title { "MyString" } | ||
url { "MyString" } | ||
published_at { "2025-06-30 13:06:11" } | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<rss version="2.0"> | ||
<channel> | ||
<title>Sample News Feed</title> | ||
<link>https://coderdojo.jp/</link> | ||
<description>テスト用のサンプルニュースフィード</description> | ||
|
||
<item> | ||
<title>テスト記事①</title> | ||
<link>https://example.com/articles/1</link> | ||
<pubDate>Mon, 01 Jun 2025 10:00:00 +0900</pubDate> | ||
<description>サンプル記事の本文①</description> | ||
</item> | ||
|
||
<item> | ||
<title>テスト記事②</title> | ||
<link>https://example.com/articles/2</link> | ||
<pubDate>Tue, 02 Jun 2025 11:30:00 +0900</pubDate> | ||
<description>サンプル記事の本文②</description> | ||
</item> | ||
|
||
<item> | ||
<title>🎉 テスト記事③</title> | ||
<link>https://example.com/articles/3</link> | ||
<pubDate>Wed, 03 Jun 2025 12:00:00 +0900</pubDate> | ||
<description>絵文字ありのサンプル記事の本文③</description> | ||
</item> | ||
|
||
</channel> | ||
</rss> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe News, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下が良さそうです👀