chef/supermarket

View on GitHub
src/supermarket/engines/fieri/app/models/contributing_file_worker.rb

Summary

Maintainability
A
1 hr
Test Coverage
require "sidekiq"
require "net/http"
require "octokit"

class ContributingFileWorker < SourceRepoWorker
  include ::Sidekiq::Worker

  def perform(cookbook_json, cookbook_name)
    evaluate_result = evaluate(cookbook_json)

    Net::HTTP.post_form(
      URI.parse("#{ENV["FIERI_SUPERMARKET_ENDPOINT"]}/api/v1/quality_metrics/contributing_file_evaluation"),
      fieri_key: ENV["FIERI_KEY"],
      cookbook_name: cookbook_name,
      contributing_file_failure: evaluate_result,
      contributing_file_feedback: give_feedback(evaluate_result)
    )
  end

  private

  def evaluate(cookbook_json)
    repo = source_repo(cookbook_json)

    # if no match for repo from #source_user_repo, fails metric
    return true if repo.blank?

    begin
      octokit_client.contents(repo, path: "CONTRIBUTING.md")
      # if found, does not fail the metric
      false
    rescue Octokit::NotFound
      # if not found, does fail the metric
      true
    end
  end

  def give_feedback(failure_result)
    if failure_result
      I18n.t(
        "quality_metrics.contributing_file.failure"
      )
    else
      I18n.t(
        "quality_metrics.contributing_file.success"
      )
    end
  end
end