<commit_before>require "cli/parser"
require "utils/github"

module Homebrew
  module_function

  def fetch_failed_logs_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `fetch-failed-logs` [<options>] <formula>

        Fetch failed job logs from GitHub Actions workflow run.

        By default searches through workflow runs triggered by pull_request event.
      EOS
      flag "--tap=",
        description: "Search given tap."
      switch "--dispatched",
        description: "Search through workflow runs triggered by repository_dispatch event."
      switch "--quiet",
        description: "Print only the logs or error if occurred, nothing more."
      switch :verbose
      switch :debug
      named 1
    end
  end

  def print_failed_logs(file)
    # Border lines indexes
    brew_index = -1
    failed_index = -1

    # Find indexes of border lines
    content = File.read(file).lines
    content.each_with_index do |line, index|
      if /.*==> .*FAILED.*/.match?(line)
        failed_index = index
        break
      elsif /.*==>.* .*brew .+/.match?(line)
        brew_index = index
      end
    end

    # One of the border lines weren't found
    return if brew_index.negative? || failed_index.negative?

    # Remove timestamp prefix on every line
    content.map! do |line|
      line.split(" ")[1..-1]&.join(" ")
    end

    # Print only interesting lines
    puts content[brew_index..failed_index]
  end

  def fetch_failed_logs
    fetch_failed_logs_args.parse

    formula = Homebrew.args.resolved_formulae.first
    event = Homebrew.args.dispatched? ? "repository_dispatch" : "pull_request"
    tap_name = Homebrew.args.tap || "homebrew/core"
    repo = Tap.fetch(tap_name).full_name

    # First get latest workflow runs
    url = "https://api.github.com/repos/#{repo}/actions/runs?status=failure&event=#{event}"
    response = GitHub.open_api(url, request_method: :GET, scopes: ["repo"])
    workflow_runs = response["workflow_runs"]

    # Then iterate over them and find the matching one...
    workflow_run = workflow_runs.find do |run|
      # If the workflow run was triggered by a repository dispatch event, then
      # check if any step name in all its jobs is equal to formula
      if run["event"] == "repository_dispatch"
        url = run["jobs_url"]
        response = GitHub.open_api(url, request_method: :GET, scopes: ["repo"])
        jobs = response["jobs"]
        jobs.find do |job|
          steps = job["steps"]
          steps.find do |step|
            step["name"].match(formula.name)
          end
        end<commit_msg>fetch-failed-logs: don't hardcode homebrew/core<commit_after>require "cli/parser"
require "utils/github"

module Homebrew
  module_function

  def fetch_failed_logs_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `fetch-failed-logs` [<options>] <formula>

        Fetch failed job logs from GitHub Actions workflow run.

        By default searches through workflow runs triggered by pull_request event.
      EOS
      flag "--tap=",
        description: "Search given tap."
      switch "--dispatched",
        description: "Search through workflow runs triggered by repository_dispatch event."
      switch "--quiet",
        description: "Print only the logs or error if occurred, nothing more."
      switch :verbose
      switch :debug
      named 1
    end
  end

  def print_failed_logs(file)
    # Border lines indexes
    brew_index = -1
    failed_index = -1

    # Find indexes of border lines
    content = File.read(file).lines
    content.each_with_index do |line, index|
      if /.*==> .*FAILED.*/.match?(line)
        failed_index = index
        break
      elsif /.*==>.* .*brew .+/.match?(line)
        brew_index = index
      end
    end

    # One of the border lines weren't found
    return if brew_index.negative? || failed_index.negative?

    # Remove timestamp prefix on every line
    content.map! do |line|
      line.split(" ")[1..-1]&.join(" ")
    end

    # Print only interesting lines
    puts content[brew_index..failed_index]
  end

  def fetch_failed_logs
    fetch_failed_logs_args.parse

    formula = Homebrew.args.resolved_formulae.first
    event = Homebrew.args.dispatched? ? "repository_dispatch" : "pull_request"
    tap_name = Homebrew.args.tap || CoreTap.instance.name
    repo = Tap.fetch(tap_name).full_name

    # First get latest workflow runs
    url = "https://api.github.com/repos/#{repo}/actions/runs?status=failure&event=#{event}"
    response = GitHub.open_api(url, request_method: :GET, scopes: ["repo"])
    workflow_runs = response["workflow_runs"]

    # Then iterate over them and find the matching one...
    workflow_run = workflow_runs.find do |run|
      # If the workflow run was triggered by a repository dispatch event, then
      # check if any step name in all its jobs is equal to formula
      if run["event"] == "repository_dispatch"
        url = run["jobs_url"]
        response = GitHub.open_api(url, request_method: :GET, scopes: ["repo"])
        jobs = response["jobs"]
        jobs.find do |job|
          steps = job["steps"]
          steps.find do |step|
            step["name"].match(formula.name)
          end
        end
{'fraction_non_alphanumeric': 0.10584638899503249, 'fraction_numerical': 0.0019105846388995033, 'mean_word_length': 2.6659663865546217, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '7115403', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1512, 'n_words': 456}