<commit_before>require 'inch_ci/action'

module Action
  module Project
    class RebuildViaHook
      include InchCI::Action

      exposes :result

      def initialize(params)
        if params[:payload]
          payload = JSON[params[:payload]]
          project = InchCI::Store::FindProject.call(project_uid(payload))
          enqueue_build(project, branch_name(payload))
          @result = "OK"
        else
          @result = "ERROR"
        end
      end

      private

      def enqueue_build(project, branch_name)
        build = InchCI::Worker::Project::Build.enqueue(project.repo_url, branch_name)
      end

      def branch_name(payload)
        payload['ref'] =~ /^refs\/heads\/(.+)$/ && $1
      end

      def project_uid(payload)
        if web_url = payload['repository'] && payload['repository']['url']
          info = InchCI::RepoURL.new(web_url)
          info.project_uid
        end
      end
    end
  end
end<commit_msg>Update RebuildViaHook to accept different param format<commit_after>require 'inch_ci/action'

module Action
  module Project
    class RebuildViaHook
      include InchCI::Action

      exposes :result

      def initialize(params)
        if params[:payload]
          process_payload JSON[params[:payload]]
        elsif params[:ref]
          process_payload params
        else
          @result = "ERROR"
        end
      end

      private

      def enqueue_build(project, branch_name)
        build = InchCI::Worker::Project::Build.enqueue(project.repo_url, branch_name)
      end

      def branch_name(payload)
        payload['ref'] =~ /^refs\/heads\/(.+)$/ && $1
      end

      def process_payload(payload)
        project = InchCI::Store::FindProject.call(project_uid(payload))
        enqueue_build(project, branch_name(payload))
        @result = "OK"
      end

      def project_uid(payload)
        if web_url = payload['repository'] && payload['repository']['url']
          info = InchCI::RepoURL.new(web_url)
          info.project_uid
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10433934666016577, 'fraction_numerical': 0.0009751340809361287, 'mean_word_length': 2.320388349514563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5036560', 'n_tokens_mistral': 607, 'n_tokens_neox': 591, 'n_words': 126}