<commit_before>require_dependency "front_end_builds/application_controller"

module FrontEndBuilds
  class BuildsController < ApplicationController

    def index
      front_end = FrontEndBuilds::Build.find_best(build_search_params)

      if front_end
        render text: front_end.with_head_tag(csrf_tag)
      else
        render text: "not found", status: 404
      end
    end

    def create
      app = find_app

      if !app
        render(
          text: 'That app name/api combination was not found.',
          status: :unprocessable_entity
        )

      elsif app
        build = app.builds.new(build_create_params)

        if !build.save
          render(
            text: 'Could not create the build: ' + build.errors.full_messages.to_s,
            status: :unprocessable_entity
          )

        else
          build.fetch!
          build.activate!
          head :ok
        end

      else
        head :unprocessable_entity
      end
    end

    private

    def csrf_tag
      [
        "<meta name='csrf-param' content='#{request_forgery_protection_token}' />",
        "<meta name='csrf-token' content='#{form_authenticity_token}' />"
      ].join('').to_s
    end

    def build_search_params
      params.permit(:app_name, :branch, :sha, :job)
    end

    def build_create_params
      params.permit(
        :branch,
        :sha,
        :job
      )
    end

    def find_app<commit_msg>More fixes<commit_after>require_dependency "front_end_builds/application_controller"

module FrontEndBuilds
  class BuildsController < ApplicationController
    before_filter :set_app!, only: :create

    def index
      front_end = FrontEndBuilds::Build.find_best(build_search_params)

      if front_end
        render text: front_end.with_head_tag(csrf_tag)
      else
        render text: "not found", status: 404
      end
    end

    def create
      build = @app.builds.new(build_create_params)

      if build.save
        build.fetch!
        build.activate!
        head :ok

      else
        render(
          text: 'Could not create the build: ' + build.errors.full_messages.to_s,
          status: :unprocessable_entity
        )
      end
    end

    private

    def set_app!
      @app = find_app
      if @app.nil?
        render(
          text: 'That app name/api combination was not found.',
          status: :unprocessable_entity
        )
      end
    end

    def csrf_tag
      [
        "<meta name='csrf-param' content='#{request_forgery_protection_token}' />",
        "<meta name='csrf-token' content='#{form_authenticity_token}' />"
      ].join('').to_s
    end

    def build_search_params
      params.permit(:app_name, :branch, :sha, :job)
    end

    def build_create_params
      params.permit(
        :branch,
        :sha,
        :job
      )
    end

    def find_app
{'fraction_non_alphanumeric': 0.08512135068589519, 'fraction_numerical': 0.002110446711220542, 'mean_word_length': 2.0352187833511204, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '3222076', 'n_tokens_mistral': 887, 'n_tokens_neox': 858, 'n_words': 194}