Class: Fastlane::Actions::PrCommentAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/stream_actions/actions/pr_comment.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.available_optionsObject



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/fastlane/plugin/stream_actions/actions/pr_comment.rb', line 31

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      env_name: 'GITHUB_REPOSITORY',
      key: :github_repo,
      description: 'GitHub repo name',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      env_name: 'GITHUB_PR_NUM',
      key: :pr_num,
      description: 'GitHub PR number',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :text,
      description: 'Comment text',
      is_string: true,
      verify_block: proc do |text|
        UI.user_error!("Text should not be empty") if text.to_s.empty?
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :edit_last_comment_with_text,
      description: 'If last comment contains this text it will be edited or replaced',
      is_string: true,
      optional: true
    )
  ]
end

.descriptionObject



27
28
29
# File 'lib/fastlane/plugin/stream_actions/actions/pr_comment.rb', line 27

def self.description
  'Comment in the PR'
end

.is_supported?(platform) ⇒ Boolean



62
63
64
# File 'lib/fastlane/plugin/stream_actions/actions/pr_comment.rb', line 62

def self.is_supported?(platform)
  true
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fastlane/plugin/stream_actions/actions/pr_comment.rb', line 4

def self.run(params)
  if params[:pr_num].to_s.empty? || params[:github_repo].to_s.empty?
    UI.important('Skipping the PR comment because PR number or GitHub repo is not set.')
  else
    if params[:edit_last_comment_with_text]
      text = params[:edit_last_comment_with_text]
      UI.message("Checking last comment for required pattern: '#{text}'")
      comments = sh("gh api repos/#{params[:github_repo]}/issues/#{params[:pr_num]}/comments --jq " \
                    "'[.[] | select(.body | test(\"#{text}\"; \"i\")) | {id, user: .user.login, html_url}]'")

      JSON.parse(comments).each do |comment|
        sh("gh api --method DELETE repos/#{params[:github_repo]}/issues/comments/#{comment['id']}")
      end
    end
    sh("gh pr comment #{params[:pr_num]} -b '#{params[:text]}'")
    UI.success('PR comment has been added.')
  end
end