Class: Github::Pulse::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/github/pulse/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path:, github_repo: nil, token: nil, since: nil, until: nil, small_threshold: 50, medium_threshold: 250) ⇒ Analyzer

Returns a new instance of Analyzer.



12
13
14
15
16
17
18
19
20
# File 'lib/github/pulse/analyzer.rb', line 12

def initialize(repo_path:, github_repo: nil, token: nil, since: nil, until: nil, small_threshold: 50, medium_threshold: 250)
  @repo_path = File.expand_path(repo_path)
  @github_repo = github_repo
  @token = token
  @since = since
  @until_date = binding.local_variable_get(:until)
  @small_threshold = small_threshold
  @medium_threshold = medium_threshold
end

Instance Attribute Details

#github_repoObject (readonly)

Returns the value of attribute github_repo.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def github_repo
  @github_repo
end

#medium_thresholdObject (readonly)

Returns the value of attribute medium_threshold.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def medium_threshold
  @medium_threshold
end

#repo_pathObject (readonly)

Returns the value of attribute repo_path.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def repo_path
  @repo_path
end

#sinceObject (readonly)

Returns the value of attribute since.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def since
  @since
end

#small_thresholdObject (readonly)

Returns the value of attribute small_threshold.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def small_threshold
  @small_threshold
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def token
  @token
end

#until_dateObject (readonly)

Returns the value of attribute until_date.



10
11
12
# File 'lib/github/pulse/analyzer.rb', line 10

def until_date
  @until_date
end

Instance Method Details

#analyzeObject



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
88
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
# File 'lib/github/pulse/analyzer.rb', line 22

def analyze
  report = {
    metadata: {
      analyzed_at: Time.now.iso8601,
      repository: nil,
      period: {
        since: since,
        until: until_date
      }
    },
    pull_requests: {},
    commits: {},
    lines_of_code: {},
    commit_activity: {},
    visualization_data: {}
  }

  # Try to analyze local git repository ONLY if no explicit GitHub repo is specified
  # OR if the local repo matches the specified GitHub repo
  should_analyze_local = false
  local_github_repo = nil
  
  if File.exist?(File.join(repo_path, ".git"))
    git_analyzer = GitAnalyzer.new(repo_path)
    local_github_repo = git_analyzer.remote_url
    
    # Only analyze local git if:
    # 1. No GitHub repo was specified (analyzing local only)
    # 2. The specified GitHub repo matches the local repo's remote
    if !github_repo || (github_repo == local_github_repo)
      should_analyze_local = true
      
      # Get commits by author
      commits = git_analyzer.analyze_commits(since: since, until_date: until_date)
      report[:commits] = format_commits_data(commits)
      
      # Get lines of code by author
      report[:lines_of_code] = git_analyzer.lines_of_code_by_author
      
      # Get commit activity
      report[:commit_activity] = git_analyzer.commit_activity_by_day
      
      # Set GitHub repo from remote if not specified
      @github_repo ||= local_github_repo
    end
  end

  # If we have GitHub repo info, fetch additional data
  if github_repo
    # Try gh CLI first if no token provided
    if !token
      require_relative "gh_client"
      gh_client = GhClient.new(repo: github_repo)
      
      if gh_client.available?
        puts "Using GitHub CLI (gh) for API access..."
        
        # Get repository info
        report[:metadata][:repository] = gh_client.repository_info
        
        # Get pull requests
        prs = gh_client.pull_requests(since: since, until_date: until_date)
        report[:pull_requests] = format_pull_requests_data(prs)
        
        # Get contributor statistics
        contrib_stats = gh_client.contributors_stats
        report[:contributor_stats] = format_contributor_stats(contrib_stats)
        
        # Get commits from GitHub if not analyzing local
        if !should_analyze_local
          commits = gh_client.commits_data(since: since, until_date: until_date)
          report[:commits] = format_commits_data(commits)
        end
        
        # Get commit activity from GitHub
        if report[:commit_activity].empty?
          activity = gh_client.commit_activity
          report[:commit_activity] = format_commit_activity(activity)
        end
      else
        puts "Warning: No GitHub token provided and gh CLI not available or not authenticated."
        puts "To enable GitHub features, either:"
        puts "  1. Set GITHUB_TOKEN environment variable"
        puts "  2. Install and authenticate gh CLI: https://cli.github.com"
      end
    else
      # Use token-based client
      github_client = GithubClient.new(repo: github_repo, token: token)
      
      # Get repository info
      report[:metadata][:repository] = github_client.repository_info
      
      # Get pull requests
      prs = github_client.pull_requests(since: since, until_date: until_date)
      report[:pull_requests] = format_pull_requests_data(prs)
      
      # Get contributor statistics
      contrib_stats = github_client.contributors_stats
      report[:contributor_stats] = format_contributor_stats(contrib_stats)
      
      # Get commits from GitHub if not analyzing local
      if !should_analyze_local
        # For token-based client, we'd need to implement a commits method
        # For now, contributor stats provides some commit data
      end
      
      # Get commit activity from GitHub
      if report[:commit_activity].empty?
        activity = github_client.commit_activity
        report[:commit_activity] = format_commit_activity(activity)
      end
    end
  end

  # Generate visualization data
  report[:visualization_data] = generate_visualization_data(report)

  report
end