Class: Fue::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/fue/finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, _options = {}) ⇒ Finder

Returns a new instance of Finder.



7
8
9
# File 'lib/fue/finder.rb', line 7

def initialize(token, _options = {})
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



5
6
7
# File 'lib/fue/finder.rb', line 5

def token
  @token
end

Instance Method Details

#author_id(username) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/fue/finder.rb', line 11

def author_id(username)
  query = <<-GRAPHQL
    query($login: String!) {
      user(login: $login) {
        id
      }
    }
  GRAPHQL
  graphql_client.query(query, login: username).data.user.id
end

#contributors(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fue/finder.rb', line 89

def contributors(options = {})
  query = <<-GRAPHQL
    query($owner: String!, $name: String!, $cursor: String) {
      repository(owner: $owner, name: $name) {
        defaultBranchRef {
          target {
            ... on Commit {
              history(first: 100, after: $cursor) {
                nodes {
                  author {
                    user {
                      login
                    }
                  }
                }
                pageInfo {
                  endCursor
                }
              }
            }
          }
        }
      }
    }
  GRAPHQL

  repo_owner, repo_name = options[:repo].split('/', 2)

  query_options = {
    owner: repo_owner,
    name: repo_name
  }

  logins = Set.new

  $stdout.write 'Fetching contributors .' if options[:verbose]

  loop do
    response = graphql_client.query(query, query_options)
    history = response&.data&.repository&.default_branch_ref&.target&.history
    history&.nodes&.each do |node|
       = node.author.user&.
      logins <<  if 
    end
    query_options[:cursor] = history&.page_info.end_cursor
    $stdout.write '.' if options[:verbose]
    break unless query_options[:cursor]
  end

  puts " found #{logins.size} contributor#{logins.size == 1 ? '' : 's'}." if options[:verbose]

  logins.map do ||
    [, emails(options.merge(username: ))]
  rescue StandardError => e
    warn e.to_s
  end.compact.to_h
end

#emails(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fue/finder.rb', line 22

def emails(options = {})
  query = <<-GRAPHQL
    query($login: String!, $author_id: ID!, $depth: Int!, $breadth: Int!, $cursor: String) {
      user(login: $login) {
        repositories(last: $breadth, after: $cursor, isFork:false, privacy: PUBLIC) {
          nodes {
            defaultBranchRef {
              target {
                ... on Commit {
                  history(first: $depth, author: { id: $author_id }) {
                    nodes {
                      author {
                        email
                        name
                      }
                    }
                  }
                }
              }
            }
          }
          pageInfo {
            startCursor
          }
        }
      }
    }
  GRAPHQL

  # max number of repositories to search
  max_breadth = options[:breadth] || 10

  query_options = {
    login: options[:username],
    author_id: author_id(options[:username]),
    # max number of commits to look into
    depth: options[:depth] || 1
  }

  $stdout.write "Searching for emails for #{options[:username]} ." if options[:verbose]

  emails = Set.new

  loop do
    query_options[:breadth] = [max_breadth, 100].min
    response = graphql_client.query(query, query_options)
    repositories = response&.data&.user&.repositories
    repositories&.nodes&.each do |history|
      default_history = history.default_branch_ref&.target&.history
      default_history&.nodes&.each do |node|
        emails << "#{node.author.name} <#{node.author.email}>"
      end
    end
    query_options[:cursor] = repositories&.page_info&.start_cursor
    break unless query_options[:cursor]

    max_breadth -= 100
    break if max_breadth <= 0

    $stdout.write '.' if options[:verbose]
  end

  puts " found #{emails.size} email address#{emails.size == 1 ? '' : 'es'}." if options[:verbose]

  emails.to_a
end