Skip to content

Commit c073195

Browse files
committed
Add option strict
This fixes #13
1 parent 35822c8 commit c073195

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ inputs:
1616
description: "Specify any file path in which you want to put the mapping file"
1717
required: false
1818
default: "mapping.txt"
19+
strict:
20+
description: "Specify whether the strict mode is on or off"
21+
required: false
22+
default: false
1923
added_files:
2024
description: "Get added files"
2125
modified_files:
@@ -63,6 +67,7 @@ runs:
6367
env:
6468
QIITA_ACCESS_TOKEN: ${{ inputs.qiita_access_token }}
6569
MAPPING_FILEPATH: ${{ inputs.mapping_filepath }}
70+
STRICT: ${{ inputs.strict }}
6671
GITHUB_ACTION_PATH: ${{ github.action_path }}
6772
ADDED_FILES: ${{ steps.get-added-files.outputs.files }}
6873
MODIFIED_FILES: ${{ steps.get-modified-files.outputs.files }}

lib/error.rb

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def initialize(msg: 'A mapping filepath is not found. The env MAPPING_FILEPATH i
1212
end
1313
end
1414

15+
class InvalidStrictError < StandardError
16+
def initialize(msg: 'The env STRICT is invalid.')
17+
super(msg)
18+
end
19+
end
20+
1521
class InvalidHeaderTitleError < StandardError
1622
def initialize(msg: 'A title of an article is invalid.')
1723
super(msg)

lib/qiita.rb

+43-10
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ def initialize(content:, header:, mode:, path:)
3131
def publish
3232
connection = Faraday.new(API_BASE_URL)
3333

34-
response = case @mode
35-
when 'create'
34+
response = if create?
3635
connection.post(&request_params)
37-
when 'update'
36+
elsif update?
3837
connection.patch(&request_params)
3938
end
4039

@@ -48,9 +47,14 @@ def publish
4847
)
4948
end
5049

51-
JSON.parse(response.body)
50+
new_item_id = JSON.parse(response.body)['id']
51+
update_mapping_file(new_item_id) if new_item_id && create?
52+
53+
true
5254
end
5355

56+
private
57+
5458
# Update a mapping file
5559
def update_mapping_file(item_id)
5660
raise CannotGetQiitaItemIDError if item_id.nil? || item_id.empty?
@@ -61,8 +65,6 @@ def update_mapping_file(item_id)
6165
end
6266
end
6367

64-
private
65-
6668
def request_params
6769
Proc.new do |request|
6870
request.url(request_url)
@@ -75,7 +77,11 @@ def request_params
7577
end
7678

7779
def request_url
78-
id = @mode == 'update' ? "/#{item_id}" : nil
80+
id = if create?
81+
nil
82+
elsif update?
83+
"/#{item_id}"
84+
end
7985

8086
"#{API_ITEM_ENDPOINT}#{id}"
8187
end
@@ -90,11 +96,27 @@ def request_body
9096
title: @header['title']
9197
}.freeze
9298

93-
body = body.merge(tweet: public?) if @mode == 'create'
99+
body = body.merge(tweet: public?) if create?
94100

95101
body.to_json
96102
end
97103

104+
def create?
105+
return true if @mode == 'create'
106+
107+
# Publish as a new article if mapping information is missing but
108+
# ENV['STRICT'] is set to 'false'
109+
return true if @mode == 'update' && item_id.nil? && ENV['STRICT'] == 'false'
110+
111+
false
112+
end
113+
114+
def update?
115+
return true if @mode == 'update' && item_id
116+
117+
false
118+
end
119+
98120
def public?
99121
@header['published']
100122
end
@@ -117,10 +139,21 @@ def tags
117139

118140
# Get a Qiita item ID corresponding to an article path
119141
def item_id
120-
# An error handling
121-
raise QiitaItemIDNotFoundError if mappings.grep(/\A^#{Regexp.escape(@path)}/).empty?
142+
if mappings.grep(/\A^#{Regexp.escape(@path)}/).empty?
143+
# If mapping information is missing, and ENV['STRICT'] is set to
144+
# 'true', then raise an error
145+
#
146+
# If mapping information is missing, but ENV['STRICT'] is set to
147+
# 'false', then return nil instead
148+
#
149+
raise QiitaItemIDNotFoundError if ENV['STRICT'] == 'true'
150+
return nil
151+
end
152+
122153
raise QiitaItemIDDuplicationError if mappings.grep(/\A^#{Regexp.escape(@path)}/).length != 1
123154
raise QiitaItemIDNotMatchedError if mappings.grep(/\A^#{Regexp.escape(@path)}/).first.split.length != 2
155+
156+
# TODO: Use Validator.item_id
124157
if mappings.grep(/\A^#{Regexp.escape(@path)}/).first.split.last.match(/\A[0-9a-f]{20}\z/).nil?
125158
raise InvalidQiitaItemIDError
126159
end

lib/validator.rb

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ module Validator
1111
def env
1212
raise QiitaAccessTokenNotFoundError if ENV['QIITA_ACCESS_TOKEN'].nil? || ENV['QIITA_ACCESS_TOKEN'].empty?
1313
raise MappingFilepathNotFoundError if ENV['MAPPING_FILEPATH'].nil? || ENV['MAPPING_FILEPATH'].empty?
14+
raise InvalidStrictError.new(msg: 'The env STRICT is missing.') if ENV['STRICT'].nil? || ENV['STRICT'].empty?
15+
16+
if ENV['STRICT'] != 'true' && ENV['STRICT'] != 'false'
17+
raise InvalidStrictError.new(msg: 'The env STRICT must be true or false')
18+
end
1419
end
1520

1621
# Check required header params

main.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
ENV['ADDED_FILES']&.split&.each do |path|
2424
article = Article.new(path: path)
2525
qiita = Qiita.new(content: article.content, header: YAML.safe_load(article.header), mode: 'create', path: path)
26-
response_body = qiita.publish
27-
qiita.update_mapping_file(response_body['id'])
26+
qiita.publish
2827
end
2928

3029
ENV['MODIFIED_FILES']&.split&.each do |path|

0 commit comments

Comments
 (0)