Skip to content

Commit

Permalink
Add option strict
Browse files Browse the repository at this point in the history
This fixes #13
  • Loading branch information
noraworld committed Mar 16, 2022
1 parent 35822c8 commit c073195
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
description: "Specify any file path in which you want to put the mapping file"
required: false
default: "mapping.txt"
strict:
description: "Specify whether the strict mode is on or off"
required: false
default: false
added_files:
description: "Get added files"
modified_files:
Expand Down Expand Up @@ -63,6 +67,7 @@ runs:
env:
QIITA_ACCESS_TOKEN: ${{ inputs.qiita_access_token }}
MAPPING_FILEPATH: ${{ inputs.mapping_filepath }}
STRICT: ${{ inputs.strict }}
GITHUB_ACTION_PATH: ${{ github.action_path }}
ADDED_FILES: ${{ steps.get-added-files.outputs.files }}
MODIFIED_FILES: ${{ steps.get-modified-files.outputs.files }}
Expand Down
6 changes: 6 additions & 0 deletions lib/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def initialize(msg: 'A mapping filepath is not found. The env MAPPING_FILEPATH i
end
end

class InvalidStrictError < StandardError
def initialize(msg: 'The env STRICT is invalid.')
super(msg)
end
end

class InvalidHeaderTitleError < StandardError
def initialize(msg: 'A title of an article is invalid.')
super(msg)
Expand Down
53 changes: 43 additions & 10 deletions lib/qiita.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def initialize(content:, header:, mode:, path:)
def publish
connection = Faraday.new(API_BASE_URL)

response = case @mode
when 'create'
response = if create?
connection.post(&request_params)
when 'update'
elsif update?
connection.patch(&request_params)
end

Expand All @@ -48,9 +47,14 @@ def publish
)
end

JSON.parse(response.body)
new_item_id = JSON.parse(response.body)['id']
update_mapping_file(new_item_id) if new_item_id && create?

true
end

private

# Update a mapping file
def update_mapping_file(item_id)
raise CannotGetQiitaItemIDError if item_id.nil? || item_id.empty?
Expand All @@ -61,8 +65,6 @@ def update_mapping_file(item_id)
end
end

private

def request_params
Proc.new do |request|
request.url(request_url)
Expand All @@ -75,7 +77,11 @@ def request_params
end

def request_url
id = @mode == 'update' ? "/#{item_id}" : nil
id = if create?
nil
elsif update?
"/#{item_id}"
end

"#{API_ITEM_ENDPOINT}#{id}"
end
Expand All @@ -90,11 +96,27 @@ def request_body
title: @header['title']
}.freeze

body = body.merge(tweet: public?) if @mode == 'create'
body = body.merge(tweet: public?) if create?

body.to_json
end

def create?
return true if @mode == 'create'

# Publish as a new article if mapping information is missing but
# ENV['STRICT'] is set to 'false'
return true if @mode == 'update' && item_id.nil? && ENV['STRICT'] == 'false'

false
end

def update?
return true if @mode == 'update' && item_id

false
end

def public?
@header['published']
end
Expand All @@ -117,10 +139,21 @@ def tags

# Get a Qiita item ID corresponding to an article path
def item_id
# An error handling
raise QiitaItemIDNotFoundError if mappings.grep(/\A^#{Regexp.escape(@path)}/).empty?
if mappings.grep(/\A^#{Regexp.escape(@path)}/).empty?
# If mapping information is missing, and ENV['STRICT'] is set to
# 'true', then raise an error
#
# If mapping information is missing, but ENV['STRICT'] is set to
# 'false', then return nil instead
#
raise QiitaItemIDNotFoundError if ENV['STRICT'] == 'true'
return nil
end

raise QiitaItemIDDuplicationError if mappings.grep(/\A^#{Regexp.escape(@path)}/).length != 1
raise QiitaItemIDNotMatchedError if mappings.grep(/\A^#{Regexp.escape(@path)}/).first.split.length != 2

# TODO: Use Validator.item_id
if mappings.grep(/\A^#{Regexp.escape(@path)}/).first.split.last.match(/\A[0-9a-f]{20}\z/).nil?
raise InvalidQiitaItemIDError
end
Expand Down
5 changes: 5 additions & 0 deletions lib/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ module Validator
def env
raise QiitaAccessTokenNotFoundError if ENV['QIITA_ACCESS_TOKEN'].nil? || ENV['QIITA_ACCESS_TOKEN'].empty?
raise MappingFilepathNotFoundError if ENV['MAPPING_FILEPATH'].nil? || ENV['MAPPING_FILEPATH'].empty?
raise InvalidStrictError.new(msg: 'The env STRICT is missing.') if ENV['STRICT'].nil? || ENV['STRICT'].empty?

if ENV['STRICT'] != 'true' && ENV['STRICT'] != 'false'
raise InvalidStrictError.new(msg: 'The env STRICT must be true or false')
end
end

# Check required header params
Expand Down
3 changes: 1 addition & 2 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
ENV['ADDED_FILES']&.split&.each do |path|
article = Article.new(path: path)
qiita = Qiita.new(content: article.content, header: YAML.safe_load(article.header), mode: 'create', path: path)
response_body = qiita.publish
qiita.update_mapping_file(response_body['id'])
qiita.publish
end

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

0 comments on commit c073195

Please sign in to comment.