Class: Homebrew::DevCmd::Contributions Private

Inherits:
AbstractCommand show all
Includes:
SystemCommand::Mixin
Defined in:
dev-cmd/contributions.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/contributions.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Constant Summary collapse

PRIMARY_REPOS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%w[
  Homebrew/brew
  Homebrew/homebrew-core
  Homebrew/homebrew-cask
].freeze
CONTRIBUTION_TYPES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let({
  merged_pr_author:   "merged PRs authored",
  merged_pr_merger:   "PRs merged",
  merged_pr:          "qualifying merged PRs",
  approved_pr_review: "approved-review search matches",
  coauthor:           "co-authored commits",
}.freeze, T::Hash[Symbol, String])
MAINTAINER_CONTRIBUTION_TYPES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:merged_pr_merger, :approved_pr_review, :closed_issue].freeze
MAINTAINER_USER_RENAMES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let({ "branchvincent" => "branchv" }.freeze, T::Hash[String, String])
MAX_RATE_LIMIT_RETRIES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

3
SECONDARY_RATE_LIMIT_DELAY =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

60
MAX_PR_SEARCH =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

100
MAX_PR_AUTHOR_BATCH =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

100
MAX_SEARCH_RESULTS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

1000
ACTIVITY_PERIOD_MONTHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

https://docs.brew.sh/Homebrew-Governance#maintainer

3
MAINTAINER_ACTIVITY_THRESHOLD =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

40
LEAD_ACTIVITY_PERIODS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

https://docs.brew.sh/Homebrew-Governance#lead-maintainer

4
LEAD_ACTIVITY_REPOSITORIES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

2
LEAD_REPOSITORY_ACTIVITY_THRESHOLD =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

20
MAX_CONTRIBUTIONS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

500
QUALIFYING_CONTRIBUTION_TYPES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:merged_pr, :approved_pr_review, :coauthor].freeze
GIT_LOG_FIELD_SEPARATOR =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"\x1f"
GIT_LOG_RECORD_SEPARATOR =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"\x1e"
GIT_LOG_COMMIT_HASH_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%H"
GIT_LOG_PARENT_HASHES_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%P"
GIT_LOG_AUTHOR_NAME_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%an"
GIT_LOG_AUTHOR_EMAIL_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%ae"
GIT_LOG_COMMITTER_NAME_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%cn"
GIT_LOG_COMMITTER_EMAIL_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%ce"
GIT_LOG_COMMITTER_DATE_SHORT_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%cs"
GIT_LOG_COMMIT_MESSAGE_FORMAT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"%B"
GIT_LOG_FIELDS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let({
  commit_hash: 0, parent_hashes: 1, author_name: 2, author_email: 3,
  committer_name: 4, committer_email: 5, commit_message: 6
}.freeze, T::Hash[Symbol, Integer])

Instance Method Summary collapse

Methods included from SystemCommand::Mixin

#command_sandbox, #system_command, #system_command!

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#approved_pull_request?(repository, number, user, from:, to:) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'dev-cmd/contributions.rb', line 684

def approved_pull_request?(repository, number, user, from:, to:)
  activity = cached_github_request(Hash, "merged-pr-activity", repository, number.to_s, to:) do
    query = <<~GRAPHQL
      query($owner: String!, $name: String!, $number: Int!, $after: String) {
        repository(owner: $owner, name: $name) {
          pullRequest(number: $number) {
            mergedAt
            author { login }
            timelineItems(itemTypes: [PULL_REQUEST_REVIEW, REVIEW_DISMISSED_EVENT], first: 100, after: $after) {
              nodes {
                ...ReviewFields
                ... on ReviewDismissedEvent {
                  createdAt
                  previousReviewState
                  review { ...ReviewFields }
                }
              }
              pageInfo { hasNextPage endCursor }
            }
          }
        }
      }
      fragment ReviewFields on PullRequestReview {
        id
        author { login }
        submittedAt
        state
        authorCanPushToRepository
      }
    GRAPHQL
    owner, name = repository.split("/", 2)
    merged_at = T.let("", String)
    review_events = T.let([], T::Array[[String, String, String, String]])
    GitHub::API.paginate_graphql(query, variables: { owner:, name:, number: }) do |result|
      pull_request = result.fetch("repository").fetch("pullRequest")
      merged_at = pull_request["mergedAt"].to_s
      next({ "hasNextPage" => false }) if merged_at.empty?

      timeline = pull_request.fetch("timelineItems")
      timeline.fetch("nodes").each do |event|
        review = event["review"] || event
        reviewer = review.dig("author", "login")
        date = review["submittedAt"]
        next unless reviewer.is_a?(String)
        next unless date.is_a?(String)
        next if date > merged_at || reviewer.casecmp?(pull_request.dig("author", "login").to_s)
        next unless review.fetch("authorCanPushToRepository")

        state = event["previousReviewState"] || review.fetch("state")
        next unless ["APPROVED", "CHANGES_REQUESTED"].include?(state)

        review_events << [date, review.fetch("id"), reviewer.downcase, state]
        if event["previousReviewState"] && event.fetch("createdAt") <= merged_at
          review_events << [event.fetch("createdAt"), review.fetch("id"), reviewer.downcase, "DISMISSED"]
        end
      end
      timeline.fetch("pageInfo")
    end
    approvals = T.let({}, T::Hash[String, [String, String]])
    review_events.sort_by(&:first).each do |date, id, reviewer, state|
      if state == "APPROVED"
        approvals[id] = [reviewer, date]
      elsif repository == "Homebrew/brew"
        if state == "CHANGES_REQUESTED"
          approvals.reject! { |_, (author, _)| author == reviewer }
        elsif state == "DISMISSED"
          approvals.delete(id)
        end
      end
    end
    approvers = if repository == "Homebrew/brew"
      first_approval = approvals.values.min_by { |_, date| date }
      first_approval ? [first_approval.first] : []
    else
      approvals.values.map(&:first).uniq
    end
    { "merged_at" => merged_at, "approvers" => approvers }
  end
  activity.fetch("merged_at") >= "#{from}T00:00:00Z" &&
    activity.fetch("merged_at") < "#{to}T00:00:00Z" && activity.fetch("approvers").include?(user.downcase)
end

#args ⇒ Homebrew::DevCmd::Contributions::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/contributions.rbi', line 10

def args; end

#cached_github_request(type, *key_parts, to:, &block) ⇒ T.type_parameter(:U)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the block's result, caching it as JSON for completed periods and waiting out rate limits. type is the JSON class the block returns so unexpected cached data is discarded rather than returned.

Parameters:

  • type (T::Class[T.type_parameter(:U)])
  • key_parts (String)
  • to (String)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U))


878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
# File 'dev-cmd/contributions.rb', line 878

def cached_github_request(type, *key_parts, to:, &block)
  cache_path = if Date.iso8601(to) <= Date.today
    # NUL cannot appear in usernames, repositories or dates so joining with it keeps key parts distinct.
    cache_key = ["1", *key_parts].join("\0")
    HOMEBREW_CACHE/"contributions--#{Digest::SHA256.hexdigest(cache_key)}.json"
  end
  if cache_path&.file?
    begin
      cached = JSON.parse(cache_path.read)
      return cached if cached.is_a?(type)

      opoo "Deleting contributions cache #{cache_path} containing #{cached.class} rather than #{type}."
    rescue JSON::ParserError, Errno::ENOENT => e
      opoo "Deleting unreadable contributions cache #{cache_path}: #{e}"
    end
    cache_path.unlink if cache_path.exist?
  end

  rate_limit_retries = 0
  result = begin
    yield
  rescue GitHub::API::RateLimitExceededError => e
    minimum = 1
    if e.resource == "secondary"
      raise if rate_limit_retries >= MAX_RATE_LIMIT_RETRIES

      minimum = SECONDARY_RATE_LIMIT_DELAY * (2**rate_limit_retries).to_i
      rate_limit_retries += 1
    end
    GitHub::API.sleep_for_rate_limit(e, minimum:)
    retry
  end
  if cache_path
    HOMEBREW_CACHE.mkpath
    cache_path.atomic_write(JSON.generate(result))
  end
  result
end

#closed_issue_counts(repository, from:, to:) ⇒ Hash{String => Integer}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'dev-cmd/contributions.rb', line 767

def closed_issue_counts(repository, from:, to:)
  cached_github_request(Hash, "separate-issue-closures", repository, from, to, to:) do
    query = <<~GRAPHQL
      query($owner: String!, $name: String!, $since: DateTime!, $after: String) {
        repository(owner: $owner, name: $name) {
          issues(first: 100, after: $after, filterBy: {since: $since},
                 orderBy: {field: CREATED_AT, direction: ASC}) {
            nodes {
              createdAt
              author { login }
              timelineItems(first: 100, itemTypes: [CLOSED_EVENT], since: $since) {
                nodes {
                  ... on ClosedEvent {
                    actor { login }
                    createdAt
                    closer {
                      __typename
                      ... on Commit { associatedPullRequests(first: 1) { nodes { merged } } }
                    }
                  }
                }
                pageInfo { hasNextPage }
              }
            }
            pageInfo { hasNextPage endCursor }
          }
        }
      }
    GRAPHQL
    owner, name = repository.split("/", 2)
    counts = T.let({}, T::Hash[String, Integer])
    GitHub::API.paginate_graphql(query, variables: { owner:, name:, since: "#{from}T00:00:00Z" }) do |result|
      issues = result.fetch("repository").fetch("issues")
      nodes = issues.fetch("nodes")
      relevant_issues = nodes.take_while { |issue| issue.fetch("createdAt") < "#{to}T00:00:00Z" }
      relevant_issues.each do |issue|
        timeline = issue.fetch("timelineItems")
        if timeline.fetch("pageInfo").fetch("hasNextPage")
          odie "Too many issue closures in #{repository}; use a shorter reporting period."
        end
        closers = timeline.fetch("nodes").filter_map do |event|
          date = event.fetch("createdAt")
          next if date < "#{from}T00:00:00Z" || date >= "#{to}T00:00:00Z"

          next if event.dig("closer", "__typename") == "PullRequest"
          next if event.dig("closer", "associatedPullRequests", "nodes")&.any? { |pr| pr.fetch("merged") }

          user = event.dig("actor", "login")
          next unless user.is_a?(String)
          next if user.casecmp?(issue.dig("author", "login").to_s)

          user.downcase
        end
        closers.uniq.each do |user|
          counts[user] = counts.fetch(user, 0) + 1
        end
      end
      (relevant_issues.length == nodes.length) ? issues.fetch("pageInfo") : { "hasNextPage" => false }
    end
    counts
  end
end

#github_identities_for_username(username, to:) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'dev-cmd/contributions.rb', line 854

def github_identities_for_username(username, to:)
  profile = cached_github_request(Hash, "user-profile", username, to, to:) do
    GitHub::API.open_rest(GitHub.url_to("users", username))
  rescue GitHub::API::HTTPNotFoundError
    {}
  end
  aliases = if args.maintainer_report_csv
    MAINTAINER_USER_RENAMES.filter_map { |former, current| former if current.casecmp?(username) }
  else
    []
  end
  [username, *aliases, profile["name"], profile["email"]].compact
end

#github_username_for(user, to:) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'dev-cmd/contributions.rb', line 831

def github_username_for(user, to:)
  user = MAINTAINER_USER_RENAMES.fetch(user.downcase, user) if args.maintainer_report_csv
  return user unless user.include?("@")

  noreply_username = github_noreply_username(user)
  return github_username_for(noreply_username, to:) if noreply_username

  matches = cached_github_request(Array, "public-email", user, to, to:) do
    GitHub.search("users", "\"#{user}\" in:email").fetch("items", [])
  end
  if matches.one?
     = matches.fetch(0)["login"]
    return  if .is_a?(String)
  end

  opoo "Could not find a unique public GitHub account for #{user}; skipping GitHub PR searches."
  nil
rescue GitHub::API::ValidationFailedError
  opoo "Could not search for a public GitHub account for #{user}; skipping GitHub PR searches."
  nil
end

#maintainer_activity_report(from, to, current_report: nil) ⇒ Hash{String => Array<(Boolean, Boolean)>}?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



1064
1065
1066
1067
1068
# File 'dev-cmd/contributions.rb', line 1064

def maintainer_activity_report(from, to, current_report: nil)
  maintainer_activity_totals(from, to, current_report:)&.transform_values do |totals, _capped|
    activity_met(totals.values)
  end
end

#maintainer_activity_totals(from, to, current_report: nil) ⇒ Hash{String => Array<(Hash{String => Integer}, Boolean)>}?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns each user's per-repository totals and whether any contributing report capped their counts.

Parameters:

Returns:



1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
# File 'dev-cmd/contributions.rb', line 1078

def maintainer_activity_totals(from, to, current_report: nil)
  require "csv"

  reports = T.let([], T::Array[T::Hash[String, T::Hash[String, Integer]]])
  capped_users = T.let([], T::Array[String])
  while from < to
    if current_report && current_report[0] == from && current_report[1] <= to
      reports << current_report[2]
      capped_users.concat(current_report[3])
      from = current_report[1]
      next
    end

    report_to, path = Dir["brew-contributions-#{from}-to-*.csv"].filter_map do |file|
      date = file[/\Abrew-contributions-\d{4}-\d{2}-\d{2}-to-(\d{4}-\d{2}-\d{2})\.csv\z/, 1]
      [date, file] if date && date > from && date <= to
    end.max
    return if path.nil? || report_to.nil?

    totals = T.let({}, T::Hash[String, T::Hash[String, Integer]])
    CSV.foreach(path, headers: true) do |row|
      next unless row.is_a?(CSV::Row)

      unless row.headers.include?("brew first approvals")
        opoo "#{path} uses obsolete activity criteria; regenerate it before assessing roles."
        return nil
      end

      username = row["username"]
      next unless username.is_a?(String)

      totals[username.downcase] = PRIMARY_REPOS.to_h do |repository|
        [repository, Integer(row.fetch("#{repository_name(repository)} total"))]
      end
      capped_users << username.downcase if row["capped"] == "true"
    end
    reports << totals
    from = report_to
  end
  reports.fetch(0).keys.filter_map do |user|
    next unless reports.all? { |report| report.key?(user) }

    user_totals = PRIMARY_REPOS.to_h do |repository|
      [repository, reports.sum { |report| report.fetch(user).fetch(repository) }]
    end
    [user, [user_totals, capped_users.include?(user)]]
  end.to_h
end

#maintainer_report_users(repository_refs, to) ⇒ Array<(Hash{String => String}, Hash{String => Boolean}, Hash{String => String, nil})>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'dev-cmd/contributions.rb', line 287

def maintainer_report_users(repository_refs, to)
  brew_path, brew_ref = repository_refs.fetch("Homebrew/brew")
  require "utils/git"
  period_end_ref = Utils.safe_popen_read(
    Utils::Git.git, "-C", brew_path, "rev-list", "-1", "--before=#{to}", brew_ref, "--", "README.md"
  ).strip
  odie "Could not find Homebrew/brew's README at the end of the reporting period." if period_end_ref.empty?

  user_names = T.let({}, T::Hash[String, String])
  lead_maintainers = T.let({}, T::Hash[String, T::Boolean])
  Utils.safe_popen_read(Utils::Git.git, "-C", brew_path, "show", "#{period_end_ref}:README.md")
       .dup.force_encoding(Encoding::UTF_8).each_line do |line|
    lead = line.start_with?("Homebrew's [Lead Maintainers]")
    next if !lead &&
            !line.start_with?("Homebrew's other Maintainers") &&
            !line.start_with?("Homebrew's maintainers are")

    line.scan(%r{\[([^\]]+)\]\(https://github\.com/([A-Za-z\d-]+)\)}).each do |match|
      next unless match.is_a?(Array)

      name = match.fetch(0)
      user = match.fetch(1)
      user = MAINTAINER_USER_RENAMES.fetch(user.downcase, user)
      user_names[user] = name
      lead_maintainers[user.downcase] = true if lead
    end
  end
  odie "Could not read the maintainers from Homebrew/brew's README." if user_names.empty?

  if (users = args.user.presence)
    requested_usernames = users.to_h do |user|
      [user, github_username_for(user, to:)&.downcase]
    end
    unresolved_users = requested_usernames.filter_map { |user, username| user if username.nil? }
    odie "Could not resolve GitHub usernames for: #{Utils::Text.to_sentence(unresolved_users)}." \
      if unresolved_users.present?

    maintainer_usernames = user_names.keys.map(&:downcase)
    non_maintainers = requested_usernames.filter_map do |user, username|
      user if username && maintainer_usernames.exclude?(username)
    end
    unless non_maintainers.empty?
      odie "Not listed as #{Utils.pluralize("Maintainer", non_maintainers.length)} at the end of the " \
           "reporting period: #{Utils::Text.to_sentence(non_maintainers)}."
    end

    selected_usernames = requested_usernames.values.compact
    user_names.select! { |user| selected_usernames.include?(user.downcase) }
  end

  maintainer_count = Utils.pluralize("maintainer", user_names.length, include_count: true)
  $stderr.puts "Scanning contributions for #{maintainer_count}..."
  maintainer_since_dates = user_names.to_h do |user, name|
    [user, maintainer_since(brew_path, period_end_ref, user, name)]
  end
  [user_names, lead_maintainers, maintainer_since_dates]
end

#maintainer_since(repository_path, ref, user, name) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'dev-cmd/contributions.rb', line 346

def maintainer_since(repository_path, ref, user, name)
  require "utils/git"

  git_log_format = [GIT_LOG_COMMIT_HASH_FORMAT, GIT_LOG_COMMITTER_DATE_SHORT_FORMAT]
                   .join(GIT_LOG_FIELD_SEPARATOR)
  candidates = ["https://github.com/#{user}", name].flat_map do |identity|
    Utils.safe_popen_read(
      Utils::Git.git, "-C", repository_path, "log", ref, "--fixed-strings",
      "-S#{identity}", "--format=#{git_log_format}", "--", "README.md"
    ).lines(chomp: true)
  end
  candidates.uniq!
  candidates.sort_by! { |candidate| candidate.split(GIT_LOG_FIELD_SEPARATOR, 2).fetch(1) }
  candidates.each do |candidate|
    commit, date = candidate.split(GIT_LOG_FIELD_SEPARATOR, 2)
    next if date.nil?

    readme = Utils.safe_popen_read(Utils::Git.git, "-C", repository_path, "show", "#{commit}:README.md")
    parent_readme = system_command(Utils::Git.git,
                                   args:         ["-C", repository_path, "show", "#{commit}^:README.md"],
                                   print_stderr: false).stdout
    return date if readme_mentions?(readme, user, name) && !readme_mentions?(parent_readme, user, name)
  end

  nil
end

#parse_git_log(output, users, github_identities: nil, authored_pull_requests: nil, merged_pull_requests: nil, merger_pull_requests: nil) ⇒ Hash{String => Hash{Symbol => Integer}}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
# File 'dev-cmd/contributions.rb', line 928

def parse_git_log(output, users, github_identities: nil, authored_pull_requests: nil, merged_pull_requests: nil,
                  merger_pull_requests: nil)
  counts = users.to_h do |user, _|
    [user, CONTRIBUTION_TYPES.keys.to_h { |type| [type, 0] }]
  end
  identity_users = T.let({}, T::Hash[String, String])
  users.each do |user, name|
    identity_users[user.downcase] = user
    identity_users[name.downcase] = user
  end
  # GitHub profile names are not unique so only match those belonging to one requested user.
  github_identity_users = T.let({}, T::Hash[String, T.nilable(String)])
  github_identities&.each do |user, identities|
    identities.map(&:downcase).uniq.each do |identity|
      github_identity_users[identity] = github_identity_users.key?(identity) ? nil : user
    end
  end
  github_identity_users.each do |identity, user|
    identity_users[identity] ||= user if user
  end
  merger_identities = args.maintainer_report_csv ? identity_users.dup : identity_users
  records = output.split(GIT_LOG_RECORD_SEPARATOR).filter_map do |record|
    values = record.strip.split(GIT_LOG_FIELD_SEPARATOR, GIT_LOG_FIELDS.length)
    next if values.length != GIT_LOG_FIELDS.length

    GIT_LOG_FIELDS.to_h { |field, index| [field, values.fetch(index)] }
  end
  record_identities = records.to_h do |fields|
    [fields.fetch(:commit_hash), [fields.fetch(:author_name), fields.fetch(:author_email)]]
  end

  # Resolve identities for each commit based on the author and committer information
  records.each do |fields|
    parents = fields.fetch(:parent_hashes).split
    source_owner = fields.fetch(:commit_message)[%r{\AMerge pull request #\d+ from ([^/\s]+)/}, 1]
    next if parents.length < 2 || source_owner.nil?

    user = identity_users[source_owner.downcase]
    source_identity = record_identities[parents.fetch(1)]
    next if user.nil? || source_identity.nil?

    name, email = source_identity
    identity_users[name.strip.downcase] ||= user
    identity_users[email.downcase] ||= user
  end

  # Map commit SHAs to their respective authors based on identity resolution
  commit_authors = T.let(records.to_h do |fields|
    [fields.fetch(:commit_hash), user_for_git_identity(
      fields.fetch(:author_name), fields.fetch(:author_email), identity_users
    )]
  end, T::Hash[String, T.nilable(String)])

  # Coauthor contributions with "Co-authored-by" in body
  records.each do |fields|
    coauthors = fields.fetch(:commit_message)
                      .scan(/^Co-authored-by:\s*(.*?)\s*<([^>]+)>/i).filter_map do |match|
      next unless match.is_a?(Array)

      user_for_git_identity(match.fetch(0), match.fetch(1), identity_users)
    end

    # Coauthor information from amended commit fields
    if [fields.fetch(:author_name), fields.fetch(:author_email)] !=
       [fields.fetch(:committer_name), fields.fetch(:committer_email)]
      coauthor = user_for_git_identity(
        fields.fetch(:committer_name), fields.fetch(:committer_email), identity_users
      )
      coauthors << coauthor if coauthor
    end

    # Increment coauthor contribution counts
    coauthors.uniq.each do |user|
      increment_contribution_count(counts.fetch(user), :coauthor)
    end

    parents = fields.fetch(:parent_hashes).split
    pull_request = fields.fetch(:commit_message).match(%r{\AMerge pull request #(\d+) from ([^/\s]+)/})
    next if parents.length < 2 || pull_request.nil?

    merger = user_for_git_identity(fields.fetch(:author_name), fields.fetch(:author_email), merger_identities)
    pull_request_id = pull_request[1]
    source_owner = pull_request[2]
    next if pull_request_id.nil? || source_owner.nil?

    author = identity_users[source_owner.downcase] || commit_authors[parents.fetch(1)]
    if author
      increment_contribution_count(counts.fetch(author), :merged_pr_author)
      authored_pull_requests&.fetch(author)&.add(pull_request_id)
    end
    if merger
      increment_contribution_count(counts.fetch(merger), :merged_pr_merger)
      merger_pull_requests&.fetch(merger)&.add(pull_request_id)
    end
    [author, merger].compact.uniq.each do |user|
      increment_contribution_count(counts.fetch(user), :merged_pr)
      merged_pull_requests&.fetch(user)&.add(pull_request_id)
    end
  end

  counts
end

#potential_maintainer_role(user, role, maintainer_since_date, period_end, activity, previous_reports) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • user (String)
  • role (String)
  • maintainer_since_date (Date, nil)
  • period_end (Date)
  • activity (Array<(Boolean, Boolean)>)
  • previous_reports (Array<Hash{String => Array<(Boolean, Boolean)>}>)

Returns:



1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'dev-cmd/contributions.rb', line 1137

def potential_maintainer_role(user, role, maintainer_since_date, period_end, activity, previous_reports)
  maintainer_activity_met, lead_activity_met = activity
  previous_activity = previous_reports.first&.[](user.downcase)
  return if previous_activity.nil?

  return "None" if !maintainer_activity_met && !previous_activity.fetch(0)

  if role == "Lead Maintainer"
    return "Maintainer" if !lead_activity_met && !previous_activity.fetch(1)

    return role
  end

  return role if maintainer_since_date.nil? || maintainer_since_date > period_end.prev_year(3)
  return role unless lead_activity_met

  previous_lead_activity = previous_reports.map { |report| report[user.downcase]&.fetch(1) }
  return if previous_lead_activity.length < LEAD_ACTIVITY_PERIODS - 1 || previous_lead_activity.any?(&:nil?)

  previous_lead_activity.all?(true) ? "Lead Maintainer" : role
end

#previous_maintainer_reports(from) ⇒ Array<Hash{String => Array<(Boolean, Boolean)>}>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'dev-cmd/contributions.rb', line 1035

def previous_maintainer_reports(from)
  reports = T.let([], T::Array[T::Hash[String, [T::Boolean, T::Boolean]]])
  period_end = Date.iso8601(from)
  (LEAD_ACTIVITY_PERIODS - 1).times do
    period_start = period_end.prev_month(ACTIVITY_PERIOD_MONTHS)
    path = Pathname("brew-contributions-#{period_start.iso8601}-to-#{period_end.iso8601}.csv")
    report = maintainer_activity_report(period_start.iso8601, period_end.iso8601)
    if report.nil?
      if reports.empty?
        opoo "Could not find #{path}; omitting the potential new role column."
      else
        opoo "Could not find #{path}; Lead Maintainer promotion data is incomplete."
      end
      break
    end

    reports << report
    period_end = period_start
  end
  reports
end

#run ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'dev-cmd/contributions.rb', line 126

def run
  maintainer_report_csv = args.maintainer_report_csv
  if args.maintainer_exempt && !maintainer_report_csv
    odie "`--maintainer-exempt` requires `--maintainer-report-csv`."
  end
  requested_users = args.user || []
  odie "`--user` must not contain empty values." if requested_users.compact.length != requested_users.length

  odie "Cannot get contributions as `$HOMEBREW_NO_GITHUB_API` is set!" if Homebrew::EnvConfig.no_github_api?
  Utils::GemSetup.install_bundler_gems!(groups: ["contributions"]) if args.csv? || maintainer_report_csv

  if maintainer_report_csv && !args.from && !args.to
    unless maintainer_report_csv.to_s.match?(/\A\d{4}-[1-4]\z/)
      odie "`--maintainer-report-csv` requires YEAR-QUARTER or both `--from` and `--to`."
    end
    quarter_parts = maintainer_report_csv.to_s.split("-")
    from, to = reporting_quarter_dates(quarter_parts.fetch(1).to_i, quarter_parts.fetch(0).to_i)
  else
    if maintainer_report_csv
      if args.from.nil? || args.to.nil?
        odie "`--maintainer-report-csv` requires both `--from` and `--to`."
      end
      if maintainer_report_csv.is_a?(String)
        odie "Use `--maintainer-report-csv` without a value with `--from` and `--to`."
      end
    end
    quarter = args.quarter.presence.to_i
    odie "Value for `--quarter` must be between 1 and 4." if args.quarter.present? && !quarter.between?(1, 4)
    quarter_dates = reporting_quarter_dates(quarter) unless quarter.zero?
    from = args.from.presence || quarter_dates&.first || Date.today.prev_year.iso8601
    to = args.to.presence || quarter_dates&.last || (Date.today + 1).iso8601
  end
  if maintainer_report_csv
    from = Date.iso8601(from).iso8601
    to = Date.iso8601(to).iso8601
    odie "`--from` must precede `--to`." if from >= to
    $stderr.puts "Maintainer report dates: #{from}-to-#{to}"
  elsif args.verbose?
    puts "Date range is #{time_period(from:, to:)}."
  end

  require "utils/github"

  organisation = T.let(nil, T.nilable(String))
  users = if maintainer_report_csv
    []
  elsif (team = args.team.presence)
    team_sections = team.split("/")
    organisation = team_sections.first.presence
    team_name = team_sections.last.presence
    if team_sections.length != 2 || organisation.nil? || team_name.nil?
      odie "Team must be in the format `organisation/team`!"
    end

    puts "Getting members for #{organisation}/#{team_name}..." if args.verbose?
    GitHub.members_by_team(organisation, team_name).keys
  elsif requested_users.present?
    requested_users
  else
    puts "Getting members for Homebrew/maintainers..." if args.verbose?
    GitHub.members_by_team("Homebrew", "maintainers").keys
  end
  user_names = users.to_h { |user| [user, user] }

  repositories = if maintainer_report_csv
    organisation = "Homebrew"
    PRIMARY_REPOS
  elsif (org = organisation.presence) || (org = args.organisation.presence)
    organisation = org
    puts "Getting repositories for #{organisation}..." if args.verbose?
    GitHub.organisation_repositories(organisation, from, to, args.verbose?)
  elsif (repos = args.repositories.presence) && repos.length == 1 && (first_repository = repos.first)
    case first_repository
    when "primary"
      PRIMARY_REPOS
    else
      Array(first_repository)
    end
  elsif (repos = args.repositories.presence)
    organisations = repos.map { |repository| repository.split("/").first }.uniq
    odie "All repositories must be under the same user or organisation!" if organisations.length > 1

    repos
  else
    PRIMARY_REPOS
  end
  organisation ||= repositories.fetch(0).split("/").fetch(0)
  repository_refs = prepare_contribution_repositories(repositories, required: maintainer_report_csv.present?)

  lead_maintainers = T.let({}, T::Hash[String, T::Boolean])
  maintainer_since_dates = T.let({}, T::Hash[String, T.nilable(String)])
  if maintainer_report_csv
    user_names, lead_maintainers, maintainer_since_dates = maintainer_report_users(repository_refs, to)
  end

  results = scan_contributions(
    organisation, repositories, repository_refs, user_names, from:, to:,
    skip_reviews_if_lead_met: maintainer_report_csv.present?,
    progress:                 maintainer_report_csv.present? || args.verbose?
  )
  grand_totals = results.transform_values { |user_results| total(user_results) }

  if maintainer_report_csv
    csv = generate_maintainer_report_csv(
      results, grand_totals, user_names, lead_maintainers, maintainer_since_dates, from, to
    )
    filename = "brew-contributions-#{from}-to-#{to}"
    filename += "-#{user_names.keys.map(&:downcase).sort.join("-")}" if requested_users.present?
    File.write("#{filename}.csv", csv)
    File.write("#{filename}.md", generate_maintainer_report_summary(csv, from, to))
    puts csv
    return
  end

  user_names.each_key do |username|
    grand_total = grand_totals.fetch(username)
    greater_than_total = T.let(grand_total.fetch(:merged_pr_author_hit_cap, 0).positive?, T::Boolean)
    contributions = CONTRIBUTION_TYPES.keys.filter_map do |type|
      type_count = grand_total[type]
      next if type_count.nil? || type_count.zero?

      count_prefix = ""
      if ([:merged_pr_author, :merged_pr].include?(type) && grand_total.fetch(:merged_pr_author_hit_cap,
                                                                              0).positive?) ||
         (type == :approved_pr_review && type_count >= MAX_PR_SEARCH) || type_count >= MAX_CONTRIBUTIONS
        greater_than_total ||= true
        count_prefix = ">="
      end

      pretty_type = CONTRIBUTION_TYPES.fetch(type)
      "#{count_prefix}#{Utils.pluralize("time", type_count, include_count: true)} (#{pretty_type})"
    end
    qualifying_total = contribution_count(
      grand_total.slice(*QUALIFYING_CONTRIBUTION_TYPES),
    )
    total = Utils.pluralize("time", qualifying_total, include_count: true)
    total_prefix = ">=" if greater_than_total
    contributions << "#{total_prefix}#{total} (total)"

    contributions_string = [
      "#{username} contributed",
      Utils::Text.to_sentence(contributions),
      "#{time_period(from:, to:)}.",
    ].join(" ")
    if args.csv?
      $stderr.puts contributions_string
    else
      puts contributions_string
    end
  end

  return unless args.csv?

  $stderr.puts
  puts generate_csv(grand_totals)
end

#scan_contributions(organisation, repositories, repository_refs, users, from:, to:, skip_reviews_if_lead_met:, progress:) ⇒ Hash{String => Hash{String => Hash{Symbol => Integer}}}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'dev-cmd/contributions.rb', line 385

def scan_contributions(organisation, repositories, repository_refs, users, from:, to:,
                       skip_reviews_if_lead_met:, progress:)
  results = users.to_h do |user, _|
    user_results = repositories.to_h do |repository|
      [repository, CONTRIBUTION_TYPES.keys.to_h { |type| [type, 0] }]
    end
    [user, user_results]
  end

  require "utils/github"
  github_users = users.keys.to_h { |user| [user, github_username_for(user, to:)] }
  # Usernames, real names and public emails, so commits under a maintainer's Git identity are matched.
  github_identities = github_users.filter_map do |user, github_user|
    [user, github_identities_for_username(github_user, to:)] if github_user
  end.to_h
  git_authored_pull_requests = users.keys.to_h do |user|
    [user, repositories.to_h { |repository| [repository, Set.new] }]
  end
  git_merged_pull_requests = users.keys.to_h do |user|
    [user, repositories.to_h { |repository| [repository, Set.new] }]
  end
  git_merger_pull_requests = users.keys.to_h do |user|
    [user, repositories.to_h { |repository| [repository, Set.new] }]
  end
  git_log_format_fields = [
    GIT_LOG_COMMIT_HASH_FORMAT, GIT_LOG_PARENT_HASHES_FORMAT, GIT_LOG_AUTHOR_NAME_FORMAT,
    GIT_LOG_AUTHOR_EMAIL_FORMAT, GIT_LOG_COMMITTER_NAME_FORMAT, GIT_LOG_COMMITTER_EMAIL_FORMAT,
    GIT_LOG_COMMIT_MESSAGE_FORMAT
  ]
  repository_refs.each do |repository, (repository_path, ref)|
    require "utils/git"
    git_log_format = git_log_format_fields.join(GIT_LOG_FIELD_SEPARATOR) + GIT_LOG_RECORD_SEPARATOR
    output = Utils.safe_popen_read(
      Utils::Git.git, "-C", repository_path, "log", ref, "--since=#{from}", "--before=#{to}",
      "--format=#{git_log_format}"
    )
    authored_pull_requests = users.keys.to_h { |user| [user, Set.new] }
    merged_pull_requests = users.keys.to_h { |user| [user, Set.new] }
    merger_pull_requests = users.keys.to_h { |user| [user, Set.new] }
    parse_git_log(output, users, github_identities:, authored_pull_requests:, merged_pull_requests:,
                  merger_pull_requests:)
      .each do |user, counts|
      results.fetch(user)[repository] = counts
      git_authored_pull_requests.fetch(user)[repository] = authored_pull_requests.fetch(user)
      git_merged_pull_requests.fetch(user)[repository] = merged_pull_requests.fetch(user)
      git_merger_pull_requests.fetch(user)[repository] = merger_pull_requests.fetch(user)
    end
  end

  if args.maintainer_report_csv
    results.each_value do |user_results|
      user_results.transform_values! do |counts|
        MAINTAINER_CONTRIBUTION_TYPES.to_h { |type| [type, counts.fetch(type, 0)] }
      end
    end
    return scan_maintainer_contributions(repositories, results, github_users, git_merger_pull_requests,
                                         from:, to:, skip_reviews_if_lead_met:, progress:)
  end

  merged_range = "#{from}..#{Date.iso8601(to).prev_day.iso8601}"
  users.each_key do |user|
    github_user = github_users.fetch(user)
    next if github_user.nil?

    merged_pull_requests = cached_github_request(Array, "merged-at", organisation, github_user, merged_range,
                                                 to:) do
      GitHub.search_issues("", is: "merged", user: organisation, author: github_user, merged: merged_range)
    rescue GitHub::API::ValidationFailedError
      opoo "Couldn't search GitHub for PRs authored by #{github_user}. Their profile might be private. " \
           "Defaulting to 0."
      []
    end
    capped_merged_pull_requests = merged_pull_requests.length >= MAX_PR_SEARCH
    if capped_merged_pull_requests
      results.fetch(user).fetch(repositories.fetch(0))[:merged_pr_author_hit_cap] =
        1
    end
    merged_pull_requests.each do |pull_request|
      repository = pull_request.fetch("repository_url").delete_prefix("#{GitHub::API_URL}/repos/")
      next unless repositories.include?(repository)

      authored_pull_requests = git_authored_pull_requests.fetch(user).fetch(repository)
      merged_pull_request_ids = git_merged_pull_requests.fetch(user).fetch(repository)
      add_merged_pull_request_id(pull_request, authored_pull_requests, merged_pull_request_ids)
    end
    repositories.each do |repository|
      counts = results.fetch(user).fetch(repository)
      authored_pull_requests = git_authored_pull_requests.fetch(user).fetch(repository)
      merged_pull_request_ids = git_merged_pull_requests.fetch(user).fetch(repository)
      update_merged_pull_request_counts(counts, authored_pull_requests, merged_pull_request_ids)
    end
    next unless skip_reviews_if_lead_met
    next unless capped_merged_pull_requests
    next if lead_activity_met?(results.fetch(user))

    repositories.each do |repository|
      break if lead_activity_met?(results.fetch(user))

      repository_counts = results.fetch(user).fetch(repository)
      repository_total = contribution_count(repository_counts.slice(*QUALIFYING_CONTRIBUTION_TYPES))
      qualifying_total = contribution_count(total(results.fetch(user)).slice(*QUALIFYING_CONTRIBUTION_TYPES))
      next if repository_total >= LEAD_REPOSITORY_ACTIVITY_THRESHOLD &&
              qualifying_total >= MAINTAINER_ACTIVITY_THRESHOLD

      $stderr.puts "Querying merged-PR search for #{user} in #{repository}..." if progress
      repository_pull_requests = cached_github_request(Array, "merged-at", repository, github_user,
                                                       merged_range, to:) do
        GitHub.search_issues("", is: "merged", repo: repository, author: github_user, merged: merged_range)
      end
      authored_pull_requests = git_authored_pull_requests.fetch(user).fetch(repository)
      merged_pull_request_ids = git_merged_pull_requests.fetch(user).fetch(repository)
      repository_pull_requests.each do |pull_request|
        add_merged_pull_request_id(pull_request, authored_pull_requests, merged_pull_request_ids)
      end
      update_merged_pull_request_counts(repository_counts, authored_pull_requests, merged_pull_request_ids)
    end
  end

  review_users = github_users.filter_map { |user, github_user| [user, github_user] if github_user }
  review_users.reject! { |user, _| lead_activity_met?(results.fetch(user)) } if skip_reviews_if_lead_met
  approved_to = Date.iso8601(to).prev_day.iso8601
  review_users.each_with_index do |(user, github_user), index|
    if progress
      $stderr.puts "Querying approved-review search for #{user} (#{index + 1}/#{review_users.length})..."
    end
    approved_reviews = cached_github_request(Array, "approved", organisation, github_user, from, approved_to,
                                             to:) do
      GitHub.search_approved_pull_requests_in_user_or_organisation(organisation, github_user,
                                                                   from:, to: approved_to)
    end
    capped_reviews = approved_reviews.length >= MAX_PR_SEARCH
    results.fetch(user).fetch(repositories.fetch(0))[:approved_pr_review_hit_cap] = 1 if capped_reviews
    approved_reviews.each do |pull_request|
      repository = pull_request.fetch("repository_url").delete_prefix("#{GitHub::API_URL}/repos/")
      next unless repositories.include?(repository)

      increment_contribution_count(results.fetch(user).fetch(repository), :approved_pr_review)
    end
    next unless skip_reviews_if_lead_met
    next unless capped_reviews
    next if lead_activity_met?(results.fetch(user))

    repositories.each do |repository|
      break if lead_activity_met?(results.fetch(user))

      repository_counts = results.fetch(user).fetch(repository)
      repository_total = contribution_count(repository_counts.slice(*QUALIFYING_CONTRIBUTION_TYPES))
      qualifying_total = contribution_count(total(results.fetch(user)).slice(*QUALIFYING_CONTRIBUTION_TYPES))
      next if repository_total >= LEAD_REPOSITORY_ACTIVITY_THRESHOLD &&
              qualifying_total >= MAINTAINER_ACTIVITY_THRESHOLD

      $stderr.puts "Querying approved-review search for #{user} in #{repository}..." if progress
      repository_reviews = cached_github_request(Array, "approved", repository, github_user,
                                                 from, approved_to, to:) do
        GitHub.search_issues("", is: "pr", review: "approved", repo: repository, reviewed_by: github_user,
                             from:, to: approved_to)
      end
      repository_counts[:approved_pr_review] = repository_reviews.length
    end
  end

  results
end

#scan_maintainer_contributions(repositories, results, github_users, git_merged_pull_requests, from:, to:, skip_reviews_if_lead_met:, progress:) ⇒ Hash{String => Hash{String => Hash{Symbol => Integer}}}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'dev-cmd/contributions.rb', line 558

def scan_maintainer_contributions(repositories, results, github_users, git_merged_pull_requests, from:, to:,
                                  skip_reviews_if_lead_met:, progress:)
  github_users.each do |user, github_user|
    next if github_user.nil?

    repositories.each do |repository|
      merged_pull_requests = git_merged_pull_requests.fetch(user).fetch(repository)
      next if merged_pull_requests.empty?

      $stderr.puts "Excluding self-merges for #{user} in #{repository}..." if progress
      counts = results.fetch(user).fetch(repository)
      verified_merges = Set.new
      numbers = merged_pull_requests.sort_by(&:to_i)
      numbers.each_slice(MAX_PR_AUTHOR_BATCH) do |batch|
        authors = cached_github_request(Hash, "pull-request-authors", repository, *batch, to:) do
          owner, name = repository.split("/", 2)
          query = <<~GRAPHQL
            query($owner: String!, $name: String!) {
              repository(owner: $owner, name: $name) {
                #{batch.map { |number| "pr#{number}: pullRequest(number: #{number}) { author { login } }" }.join(" ")}
              }
            }
          GRAPHQL
          response = GitHub::API.open_graphql(query, variables: { owner:, name: }, raise_errors: false)
          errors = response.fetch("errors", []).reject do |error|
            error["type"] == "NOT_FOUND" &&
              batch.any? { |number| error["path"] == ["repository", "pr#{number}"] }
          end
          raise GitHub::API::Error, errors.map { |error| error.fetch("message") }.join("\n") if errors.present?

          response.fetch("data").fetch("repository")
        end
        batch.each do |number|
          author = authors.fetch("pr#{number}")&.dig("author", "login")
          if author.nil?
            counts[:unverified_merges] = counts.fetch(:unverified_merges, 0) + 1
            next
          end
          next if author.casecmp?(github_user)

          verified_merges << number
        end
        limit = skip_reviews_if_lead_met ? MAINTAINER_ACTIVITY_THRESHOLD : MAX_CONTRIBUTIONS
        next if verified_merges.length < limit

        counts[:merges_skipped] = 1 if batch.last != numbers.last
        break
      end
      merged_pull_requests.replace(verified_merges)
      counts[:merged_pr_merger] = [verified_merges.length, MAX_CONTRIBUTIONS].min
    end
  end

  repositories.each do |repository|
    $stderr.puts "Querying issue closures in #{repository}..." if progress
    closures = closed_issue_counts(repository, from:, to:)
    github_users.each do |user, github_user|
      next if github_user.nil?

      results.fetch(user).fetch(repository)[:closed_issue] =
        [closures.fetch(github_user.downcase, 0), MAX_CONTRIBUTIONS].min
    end
  end

  github_users.each do |user, github_user|
    next if github_user.nil?

    repositories.each do |repository|
      counts = results.fetch(user).fetch(repository)
      if skip_reviews_if_lead_met && review_checks_complete?(results.fetch(user), repository)
        counts[:reviews_skipped] = 1
        next
      end

      $stderr.puts "Querying PR approvals for #{user} in #{repository}..." if progress
      page = 1
      loop do
        matches = cached_github_request(Hash, "merged-review-candidates", repository, github_user, from, to,
                                        page.to_s, to:) do
          uri = GitHub.url_to("search", "issues")
          uri.query = "#{GitHub.search_query_string("-author:#{github_user}", is: "merged", repo: repository,
                                                   reviewed_by: github_user,
                                                   merged: "#{from}..#{Date.iso8601(to).prev_day.iso8601}")}" \
                      "&page=#{page}"
          GitHub::API.open_rest(uri)
        end
        matches.fetch("items").each do |pull_request|
          number = pull_request.fetch("number")
          next if git_merged_pull_requests.fetch(user).fetch(repository).include?(number.to_s)
          next unless approved_pull_request?(repository, number, github_user, from:, to:)

          increment_contribution_count(counts, :approved_pr_review)
          break if skip_reviews_if_lead_met && review_checks_complete?(results.fetch(user), repository)
        end
        if skip_reviews_if_lead_met && review_checks_complete?(results.fetch(user), repository)
          counts[:reviews_skipped] = 1
          break
        end
        break if counts.fetch(:approved_pr_review) >= MAX_CONTRIBUTIONS

        if matches["incomplete_results"] ||
           (page * MAX_PR_SEARCH >= MAX_SEARCH_RESULTS && page * MAX_PR_SEARCH < matches.fetch("total_count"))
          odie "Incomplete review search for #{user} in #{repository}.\nUse a shorter reporting period."
        end
        break if page * MAX_PR_SEARCH >= matches.fetch("total_count")

        page += 1
      end
    end
  end

  results.each do |user, user_results|
    assessments = [false, true].freeze.map do |include_unverified|
      activity_met(user_results.values.map do |counts|
        contribution_count(counts.slice(*MAINTAINER_CONTRIBUTION_TYPES)) +
          (include_unverified ? counts.fetch(:unverified_merges, 0) : 0)
      end)
    end
    next if assessments.uniq.one?

    odie "#{user}: unavailable PRs could change the activity assessment."
  end
  results
end