Module: Smdev::PrCritic

Defined in:
lib/smdev/pr_critic.rb

Class Method Summary collapse

Class Method Details

.analyze(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/smdev/pr_critic.rb', line 10

def self.analyze(options = {})

  welcome_message

  github_owner = ENV['GITHUB_OWNER']
  github_repo = ENV['GITHUB_REPO']

  if github_owner.nil? || github_repo.nil?
    error 'Please set GITHUB_OWNER and GITHUB_REPO in your .env file.'
    return
  end

  token = ENV['GITHUB_TOKEN']
  if token.nil?
    print "GitHub personal access token: "
    token = STDIN.noecho(&:gets).chomp
    puts ""
  end

  client = Octokit::Client.new(access_token: token)

  begin
    state = options[:pr_state] || 'open'
    puts "Fetching #{state} pull requests...".bold.green
    prs = client.pull_requests("#{github_owner}/#{github_repo}", state: state)

    if prs.empty?
      puts "No #{state} pull requests found."
      return
    end

    header 'Select a Pull Request from the list below:'
    divider
    prs.each_with_index do |pr, index|
      puts "[#{index + 1}] ##{pr.number}: #{pr.title}"
    end

    divider
    print 'Enter the number corresponding to the Pull Request: '
    pr_index = gets.chomp.to_i - 1

    if pr_index < 0 || pr_index >= prs.size
      error 'Invalid selection. Exiting.'
      return
    end

    pr = prs[pr_index]
    pr_number = pr.number

    files = client.pull_request_files("#{github_owner}/#{github_repo}", pr_number)

    pr_content = "Pull Request Name: #{pr.title}\n\nDescription:\n#{pr.body}\n\nFiles Changed:\n"
    files.each do |file|
      pr_content += "- #{file.filename}\nChanges:\n#{file.patch}\n\n"
    end

    Clipboard.copy(pr_content)
    divider
    header 'Pull Request details and changes have been copied to the clipboard!'
    header 'Visit the following link to analyze it:'
    link 'https://chatgpt.com/g/g-67918801886081919bebf4848a94fe29-rails-pr-reviewer'
    divider

    loop do
      header 'Additional options:'
      divider
      puts '1. Copy the entire content of the changed files to clipboard.'
      puts '2. Copy the content of db/schema.rb to the clipboard.'
      puts '3. Exit'
      print 'Enter your choice: '
      choice = gets.chomp.to_i

      case choice
      when 1
        file_contents = ""
        files.each do |file|
          content = client.contents("#{github_owner}/#{github_repo}", path: file.filename, ref: pr.head.sha).content
          decoded_content = Base64.decode64(content)
          file_contents += "File: #{file.filename}\n\n#{decoded_content}\n\n"
        end
        Clipboard.copy(file_contents)

        divider
        status 'The content of the changed files has been copied to the clipboard!'
        divider
      when 2
        schema_content = client.contents("#{github_owner}/#{github_repo}", path: 'db/schema.rb', ref: pr.head.sha).content
        decoded_schema_content = Base64.decode64(schema_content)
        Clipboard.copy(decoded_schema_content)

        divider
        status 'The content of db/schema.rb has been copied to the clipboard!'
        divider
      when 3
        divider
        header 'Exiting. Goodbye!'
        break
      else
        error 'Invalid choice. Please try again.'
        divider
      end
    end

  rescue Octokit::NotFound
    puts 'Pull Request not found. Please ensure the PR number is correct.'
  rescue Octokit::Unauthorized
    puts 'Unauthorized! Please check your GitHub token in the .env file.'
  rescue StandardError => e
    puts "An error occurred: #{e.message}"
  end

end

.dividerObject



139
140
141
# File 'lib/smdev/pr_critic.rb', line 139

def self.divider
  puts '---------------------------------------------------------------------------------------------'
end

.error(message) ⇒ Object



135
136
137
# File 'lib/smdev/pr_critic.rb', line 135

def self.error(message)
  puts message.red.bold
end

.header(message) ⇒ Object



123
124
125
# File 'lib/smdev/pr_critic.rb', line 123

def self.header(message)
  puts message.bold
end


131
132
133
# File 'lib/smdev/pr_critic.rb', line 131

def self.link(message)
  puts message.blue.underline.bold
end

.status(message) ⇒ Object



127
128
129
# File 'lib/smdev/pr_critic.rb', line 127

def self.status(message)
  puts message.green.bold
end

.welcome_messageObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/smdev/pr_critic.rb', line 143

def self.welcome_message
  puts ' ██▓███  ██▀███      ▄████▄  ██▀███  ██▄▄▄█████▓██▓▄████▄   '.red
  puts ' ▓██░  ██▓██ ▒ ██▒   ▒██▀ ▀█ ▓██ ▒ ██▓██▓  ██▒ ▓▓██▒██▀ ▀█  '.red
  puts ' ▓██░ ██▓▓██ ░▄█ ▒   ▒▓█    ▄▓██ ░▄█ ▒██▒ ▓██░ ▒▒██▒▓█    ▄ '.red
  puts ' ▒██▄█▓▒ ▒██▀▀█▄     ▒▓▓▄ ▄██▒██▀▀█▄ ░██░ ▓██▓ ░░██▒▓▓▄ ▄██▒'.red
  puts ' ▒██▒ ░  ░██▓ ▒██▒   ▒ ▓███▀ ░██▓ ▒██░██░ ▒██▒ ░░██▒ ▓███▀ ░'.red
  puts ' ▒▓▒░ ░  ░ ▒▓ ░▒▓░   ░ ░▒ ▒  ░ ▒▓ ░▒▓░▓   ▒ ░░  ░▓ ░ ░▒ ▒  ░'.red
  puts ' ░▒ ░      ░▒ ░ ▒░     ░  ▒    ░▒ ░ ▒░▒ ░   ░    ▒ ░ ░  ▒   '.red
  puts ' ░░        ░░   ░    ░         ░░   ░ ▒ ░ ░      ▒ ░        '.red
  puts ' ░        ░ ░        ░     ░          ░ ░ ░                 '.red
  puts ' ░                             ░                            '.red
  puts ' '
  puts '---------------------------------------------------------------------------------------------'.yellow
  puts 'This tool can help you analyze your pull request, point out issues and recommend improvements'.bold.yellow
  puts '---------------------------------------------------------------------------------------------'.yellow
end