From c073195dde4e5bbb9fc3733b3114c1e5a53d5eb1 Mon Sep 17 00:00:00 2001 From: noraworld Date: Wed, 16 Mar 2022 23:04:21 +0900 Subject: [PATCH] Add option strict This fixes #13 --- action.yml | 5 +++++ lib/error.rb | 6 ++++++ lib/qiita.rb | 53 +++++++++++++++++++++++++++++++++++++++--------- lib/validator.rb | 5 +++++ main.rb | 3 +-- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/action.yml b/action.yml index 65fad31..d5a7395 100644 --- a/action.yml +++ b/action.yml @@ -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: @@ -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 }} diff --git a/lib/error.rb b/lib/error.rb index 27e8838..acf86e1 100644 --- a/lib/error.rb +++ b/lib/error.rb @@ -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) diff --git a/lib/qiita.rb b/lib/qiita.rb index 658d196..4317f8c 100644 --- a/lib/qiita.rb +++ b/lib/qiita.rb @@ -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 @@ -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? @@ -61,8 +65,6 @@ def update_mapping_file(item_id) end end - private - def request_params Proc.new do |request| request.url(request_url) @@ -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 @@ -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 @@ -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 diff --git a/lib/validator.rb b/lib/validator.rb index 05c7b3f..d26c356 100644 --- a/lib/validator.rb +++ b/lib/validator.rb @@ -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 diff --git a/main.rb b/main.rb index d4f38f0..f49f034 100644 --- a/main.rb +++ b/main.rb @@ -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|