Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e402f71

Browse filesBrowse files
committed
Add script
1 parent 2ddd1e4 commit e402f71
Copy full SHA for e402f71

File tree

3 files changed

+173
-0
lines changed
Filter options

3 files changed

+173
-0
lines changed

‎lib/article.rb

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
class Article
4+
def initialize(path:)
5+
@path = path
6+
end
7+
8+
# Get a content of an article file without a YAML header
9+
def content
10+
File.open(@path, 'r') do |file|
11+
file.read.gsub(/\A#{Regexp.escape("---\n" + header + '---')}/, '').gsub(/\A\n+/, '')
12+
end
13+
end
14+
15+
# Get a YAML header of an article file
16+
def header
17+
header = ''
18+
19+
File.open(@path, 'r') do |file|
20+
file.each_line.with_index do |line, index|
21+
next if index.zero? && line =~ /^---/ # Go onto next if the first "---" line of the YAML header
22+
break if index.nonzero? && line =~ /^---/ # Break if the last "---" line of the YAML header
23+
24+
header += line
25+
end
26+
end
27+
28+
header
29+
end
30+
end

‎lib/qiita.rb

Copy file name to clipboard
+116Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
require 'faraday'
5+
6+
class Qiita
7+
API_BASE_URL = 'https://qiita.com'
8+
API_ITEM_ENDPOINT = '/api/v2/items'
9+
10+
# @content: String: A content of an article
11+
# @header: Hash: A YAML header of an article
12+
# @mode: String: A value to indicate that an article is created, updated, or deleted
13+
# @path: String: A path where an article exists
14+
def initialize(content:, header:, mode:, path:)
15+
@content = content
16+
@header = header
17+
@mode = mode
18+
@path = path
19+
end
20+
21+
# Publish an article to Qiita
22+
#
23+
# Returns a parsed response body
24+
# https://qiita.com/api/v2/docs
25+
#
26+
def publish
27+
connection = Faraday.new(API_BASE_URL)
28+
29+
response = case @mode
30+
when 'create'
31+
connection.post(&request_params)
32+
when 'update'
33+
connection.patch(&request_params)
34+
end
35+
36+
JSON.parse(response.body)
37+
end
38+
39+
# Update a mapping file
40+
def update_mapping_file(item_id)
41+
File.open(ENV['MAPPING_FILEPATH'], 'a') do |file|
42+
file.puts "#{@path}, #{item_id}"
43+
end
44+
end
45+
46+
private
47+
48+
def request_params
49+
Proc.new do |request|
50+
request.url(request_url)
51+
request.headers['Authorization'] = "Bearer #{ENV['QIITA_ACCESS_TOKEN']}"
52+
request.headers['Content-Type'] = 'application/json'
53+
request.body = request_body
54+
end
55+
end
56+
57+
def request_url
58+
id = @mode == 'update' ? "/#{item_id}" : nil
59+
60+
"#{API_ITEM_ENDPOINT}#{id}"
61+
end
62+
63+
def request_body
64+
body = {
65+
body: @content.force_encoding('UTF-8'),
66+
coediting: false,
67+
group_url_name: nil,
68+
private: private?,
69+
tags: tags,
70+
title: @header['title']
71+
}.freeze
72+
73+
body = body.merge(tweet: public?) if @mode == 'create'
74+
75+
body.to_json
76+
end
77+
78+
def public?
79+
@header['published']
80+
end
81+
82+
def private?
83+
!@header['published']
84+
end
85+
86+
# Get tags from a YAML header
87+
#
88+
# [
89+
# { name: 'PulseAudio' },
90+
# { name: 'Bluetooth' },
91+
# { name: 'RaspberryPi' }
92+
# ]
93+
#
94+
def tags
95+
@header['topics'].map { |topic| { name: topic } }
96+
end
97+
98+
# Get a Qiita item ID corresponding to an article path
99+
def item_id
100+
mappings.grep(/\A^#{Regexp.escape(@path)}/).first.split.last
101+
end
102+
103+
# Get a content of a mapping file
104+
#
105+
# [
106+
# "articles/heroku-postdeploy-runs-only-once.md, 1c57bd07cf0eb8ae807e",
107+
# "articles/heroku-rails-mysql.md, 09dac6e4340b85e35be4",
108+
# "articles/rails-command-hangs.md, 5f0f0a9bba2ec1dfab67"
109+
# ]
110+
#
111+
def mappings
112+
File.open(ENV['MAPPING_FILEPATH'], 'r') do |file|
113+
file.read.split("\n")
114+
end
115+
end
116+
end

‎main.rb

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Load if it is not executed via GitHub Actions
4+
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
5+
unless ENV['CI']
6+
require 'pry'
7+
require 'dotenv'
8+
9+
Dotenv.load
10+
end
11+
12+
require 'yaml'
13+
require_relative 'lib/article'
14+
require_relative 'lib/qiita'
15+
16+
ENV['ADDED_FILES']&.split&.each do |path|
17+
article = Article.new(path: path)
18+
qiita = Qiita.new(content: article.content, header: YAML.safe_load(article.header), mode: 'create', path: path)
19+
response_body = qiita.publish
20+
qiita.update_mapping_file(response_body['id'])
21+
end
22+
23+
ENV['MODIFIED_FILES']&.split&.each do |path|
24+
article = Article.new(path: path)
25+
qiita = Qiita.new(content: article.content, header: YAML.safe_load(article.header), mode: 'update', path: path)
26+
qiita.publish
27+
end

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.