Class: DeployLog::Github::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_log/github/helper.rb

Constant Summary collapse

LINE_FORMAT =
"%s (%s)\n - Created by %s\n - Branch: %s\n - Merged by %s on %s\n - Changes: %s\n -- %s\n\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_repo, force) ⇒ Helper

Returns a new instance of Helper.



13
14
15
16
17
18
# File 'lib/deploy_log/github/helper.rb', line 13

def initialize(user_repo, force)
  @client = ::Octokit::Client.new(login: ENV['GITHUB_USER'], password: ENV['GITHUB_TOKEN'])
  @repo_location = user_repo
  @force = force
  puts force.inspect
end

Instance Attribute Details

#forceObject

Returns the value of attribute force.



9
10
11
# File 'lib/deploy_log/github/helper.rb', line 9

def force
  @force
end

Instance Method Details

#pulls_in_timeframe(date_start = nil, date_end = nil) ⇒ Object



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
# File 'lib/deploy_log/github/helper.rb', line 20

def pulls_in_timeframe(date_start = nil, date_end = nil)
  cache = cache_file(date_start, date_end)
  return cache[:contents] if should_show_cache(cache)

  @client.auto_paginate = true
  list = @client.pull_requests(@repo_location,
    state: :closed,
    per_page: 500,
    sort: 'long-running'
  )

  prs_covered = 0

  File.open(cache[:path], 'w+') do |f|
    list.each do |pr|
      next unless (date_start..date_end).cover? pr.merged_at

      prs_covered += 1

      f.write(
        sprintf(
          LINE_FORMAT,
          pr.title,
          pr.html_url,
          pr.user.,
          pr.head.ref,
          user_who_merged(pr.number),
          formatted_time(pr.merged_at, true),
          pr.diff_url,
          committers_for(pr.number).join("\n -- ")
        )
      )
    end

    f.write("============================================================\n#{prs_covered} PR(s) merged from #{date_start} to #{date_end}\n============================================================\n")
  end

  return ::Notify.warning("No pull requests have been merged in the requested date range (#{date_start} - #{date_end})") if prs_covered.zero?

  system('cat /tmp/github-deploys.log')
end

#search_pulls_by(value, field = :title) ⇒ Object



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
# File 'lib/deploy_log/github/helper.rb', line 62

def search_pulls_by(value, field = :title)
  cache = cache_file(value, field)
  return cache[:contents] if should_show_cache(cache)

  list = @client.pull_requests(@repo_location,
    :state => :all,
    :per_page => 100
    )
  prs_covered = 0

  File.open(cache[:path], 'w+') do |f|
    list.each do |pr|
      next unless nested_hash_value(pr, field).match?(/#{value}\b/)

      prs_covered += 1

      f.write(
        sprintf(
          LINE_FORMAT,
          pr.title,
          pr.html_url,
          pr.user.,
          pr.head.ref,
          user_who_merged(pr.number),
          formatted_time(pr.merged_at, true),
          pr.diff_url,
          committers_for(pr.number).join("\n -- ")
        )
      )
    end

    f.write("============================================================\n#{prs_covered} PR(s) matched\n============================================================\n")
  end

  return ::Notify.warning("No pull requests match the requested term (#{value})") if prs_covered.zero?

  system('cat /tmp/github-deploys.log')
end