Class: Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo_developer/app/services/diff.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(mode:) ⇒ Object



4
5
6
7
8
9
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
# File 'lib/nexmo_developer/app/services/diff.rb', line 4

def self.generate(mode:)
  document_paths = {
    documentation: {
      documents: Dir.glob("#{Rails.configuration.docs_base_path}/_documentation/**/*.md"),
      origin: Pathname.new("#{Rails.configuration.docs_base_path}/_documentation"),
      base_url_path: '',
    },
    api: {
      documents: Dir.glob("#{Rails.configuration.docs_base_path}/_api/**/*.md"),
      origin: Pathname.new("#{Rails.configuration.docs_base_path}/_api"),
      base_url_path: '/api',
    },
    tutorials: {
      documents: Dir.glob("#{Rails.configuration.docs_base_path}/_use_cases/**/*.md"),
      origin: Pathname.new("#{Rails.configuration.docs_base_path}/_use_cases"),
      base_url_path: '/tutorials',
    },
  }

  document_paths.each do |_, config|
    config[:documents].map do |document_path|
      document_path = Pathname.new(document_path)
      relative_path = "#{config[:base_url_path]}/#{document_path.relative_path_from(config[:origin])}".gsub('.md', '')
      path = "#{Rails.root}/tmp/diff/#{mode}/#{relative_path}.html"
      directory = File.dirname(path)
      FileUtils.mkdir_p(directory) unless File.directory?(directory)

      begin
        document = File.read(document_path)
        body_html = Nexmo::Markdown::Renderer.new.call(document)
        document = Nokogiri::HTML::DocumentFragment.parse(body_html)

        ['id', 'data-tabs-content', 'data-id', 'data-open', 'data-collapsible-id'].each do |attribute|
          document.css("[#{attribute}]").each do |element|
            element[attribute] = nil
          end
        end

        document.css('.tabs a').each do |element|
          element['href'] = nil
        end

        File.open(path, 'w') do |file|
          file.write document.to_html
        end
      rescue StandardError
        Rails.logger.error("File failed to generate - #{relative_path}".colorize(:yellow))
        File.open(path, 'w') do |file|
          file.write <<~HEREDOC
            ###############################################################################
            File failed to generate. Likely due to a dependent file being moved or removed.
            ###############################################################################
          HEREDOC
        end
      end
    end
  end
end

Instance Method Details

#diff(mode) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nexmo_developer/app/services/diff.rb', line 63

def diff(mode)
  return @output if @output

  @output = []

  Dir.glob("#{Rails.root}/tmp/diff/base/**/*.html") do |base_document_path|
    compare_document_path = base_document_path.sub("#{Rails.root}/tmp/diff/base/", "#{Rails.root}/tmp/diff/compare/")

    base_document = File.read(base_document_path)
    compare_document = File.read(compare_document_path)
    path = base_document_path.sub("#{Rails.root}/tmp/diff/base", '').sub('.html', '')

    diff_response = Diffy::Diff.new(base_document, compare_document, context: 0).to_s(mode)

    unless diff_response.empty? || diff_response == "\n"
      @output << {
        path: path,
        diff: diff_response,
      }
    end
  end

  @output.reject!(&:empty?)
end

#report_cliObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nexmo_developer/app/services/diff.rb', line 88

def report_cli
  if @output.any?
    Rails.logger.info("#{@output.size} changes detected".colorize(:light_red))
    @output.reject.each do |result|
      Rails.logger.info(
        <<~HEREDOC
          #{result[:path]}
           #{result[:diff]}


        HEREDOC
      )
    end

    exit 1 # rubocop:disable Rails/Exit
  else
    Rails.logger.info('No changes detected'.colorize(:green))
  end
end

#report_pull_requestObject



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/nexmo_developer/app/services/diff.rb', line 108

def report_pull_request
  if @output.any?
    time = Time.new.to_i
    branch = "code-example-update-#{time}"
    Rails.logger.info("Checking out new branch - #{branch}".colorize(:yellow))
    system "git checkout -b #{branch}"
    Rails.logger.info('Adding repo files'.colorize(:yellow))
    system 'git add .repos'
    Rails.logger.info('Commiting changes'.colorize(:yellow))
    system "git commit -m 'Automated: Updating code examples'"
    Rails.logger.info('Pushing'.colorize(:yellow))
    system "git push [email protected]:Nexmo/nexmo-developer.git #{branch}"

    body = "#{@output.size} changes detected\n\n"
    @output.reject.each do |result|
      body << <<~HEREDOC
        - [ ] `#{result[:path]}`

        ```diff
        #{result[:diff]}
        ```

      HEREDOC
    end

    Rails.logger.info("Notifying Nexmo Developer of branch - #{branch}".colorize(:yellow))
    RestClient.post ENV['OPEN_PULL_REQUEST_ENDPOINT'], {
      'branch' => branch,
      'body' => body,
      'secret' => ENV['CI_SECRET'],
    }.to_json, {
      content_type: :json,
      accept: :json,
    }
  else
    Rails.logger.info('No changes detected'.colorize(:green))
  end
end