|
| 1 | +module Fastlane |
| 2 | + module Actions |
| 3 | + class BuildkiteAnnotateAction < Action |
| 4 | + def self.run(params) |
| 5 | + message = params[:message] |
| 6 | + context = params[:context] |
| 7 | + style = params[:style] |
| 8 | + |
| 9 | + if message.nil? |
| 10 | + # Delete an annotation, but swallow the error if the annotation didn't exist — to avoid having |
| 11 | + # this action failing or printing a red log for no good reason — hence the `|| true` |
| 12 | + ctx_param = "--context #{context.shellescape}" unless context.nil? |
| 13 | + sh("buildkite-agent annotation remove #{ctx_param} || true") |
| 14 | + else |
| 15 | + # Add new annotation using `buildkite-agent` |
| 16 | + extra_params = { |
| 17 | + context: context, |
| 18 | + style: style |
| 19 | + }.compact.flat_map { |k, v| ["--#{k}", v] } |
| 20 | + sh('buildkite-agent', 'annotate', *extra_params, params[:message]) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + ##################################################### |
| 25 | + # @!group Documentation |
| 26 | + ##################################################### |
| 27 | + |
| 28 | + def self.description |
| 29 | + 'Add or remove annotations to the current Buildkite build' |
| 30 | + end |
| 31 | + |
| 32 | + def self.details |
| 33 | + <<~DETAILS |
| 34 | + Add or remove annotations to the current Buildkite build. |
| 35 | +
|
| 36 | + Has to be run on a CI job (where a `buildkite-agent` is running), e.g. typically by a lane |
| 37 | + that is triggered as part of a Buildkite CI step. |
| 38 | +
|
| 39 | + See https://buildkite.com/docs/agent/v3/cli-annotate |
| 40 | + DETAILS |
| 41 | + end |
| 42 | + |
| 43 | + def self.available_options |
| 44 | + [ |
| 45 | + FastlaneCore::ConfigItem.new( |
| 46 | + key: :context, |
| 47 | + env_name: 'BUILDKITE_ANNOTATION_CONTEXT', |
| 48 | + description: 'The context of the annotation used to differentiate this annotation from others', |
| 49 | + type: String, |
| 50 | + optional: true |
| 51 | + ), |
| 52 | + FastlaneCore::ConfigItem.new( |
| 53 | + key: :style, |
| 54 | + env_name: 'BUILDKITE_ANNOTATION_STYLE', |
| 55 | + description: 'The style of the annotation (`success`, `info`, `warning` or `error`)', |
| 56 | + type: String, |
| 57 | + optional: true, |
| 58 | + verify_block: proc do |value| |
| 59 | + valid_values = %w[success info warning error] |
| 60 | + next if value.nil? || valid_values.include?(value) |
| 61 | + |
| 62 | + UI.user_error!("Invalid value `#{value}` for parameter `style`. Valid values are: #{valid_values.join(', ')}") |
| 63 | + end |
| 64 | + ), |
| 65 | + FastlaneCore::ConfigItem.new( |
| 66 | + key: :message, |
| 67 | + description: 'The message to use in the new annotation. Supports GFM-Flavored Markdown. ' \ |
| 68 | + + 'If message is nil, any existing annotation with the provided context will be deleted', |
| 69 | + type: String, |
| 70 | + optional: true, |
| 71 | + default_value: nil # nil message = delete existing annotation if any |
| 72 | + ), |
| 73 | + ] |
| 74 | + end |
| 75 | + |
| 76 | + def self.authors |
| 77 | + ['Automattic'] |
| 78 | + end |
| 79 | + |
| 80 | + def self.is_supported?(platform) |
| 81 | + true |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments