Giter VIP home page Giter VIP logo

fastlane-plugin-slack_bot's Introduction

Fastlane slack_bot plugin

fastlane Plugin Badge ![License] Gem Version Twitter: @manish

A fastlane plugin to customize your automation workflow(s) with a Slack Bot 🤖 using the Slack APIs

About

A fastlane plugin to post slack message and much more using Slack bot api token. 🚀
Note: Fastlane comes with built-in slack action by default, which uses slack webhook url and have webhook limitations.
i.e Listing couple of slack webhook url limitations:

  • can't post a direct message to a slack user.
  • can’t post a message inside a slack thread.
  • can’t update a posted slack message.
  • can’t list and upload a file inside a slack channel.
  • many more, compare to a Slack Bot 🤖 using the Slack APIs

Getting Started

  1. Generate Slack token for Fastlane bot

    • Add a slack Bot user
    • Choose a name for your bot, e.g. "fastlane"
    • Save API Token
  2. Add plugin in your project

fastlane add_plugin slack_bot

If you are using fastlane using Gemfile in your project, add it to your project by running:

bundle exec fastlane add_plugin slack_bot
  1. Use slack_bot features inside your lane in Fastfile whenever you want.

Features

Using this slack_bot plugin, you can:

  • Post a message using chat.postMessage Slack API

    • Post a message to a public channel
    • Post a message to a private channel
    • Post a message to a slack user (DM)
    • Post a message inside a slack thread 🧵
    • Post a message with custom Bot username and icon
  • Update a message using chat.update Slack API

    • update a message in a channel
  • Delete a message using chat.delete Slack API

    • delete a message in a channel
  • List of files in a channel using files.list Slack API

    • A list of files in a channel, It can be filtered and sliced in various ways.
  • Upload a file using files.upload Slack API

    • Upload a file to a public channel
    • Upload a file to a private channel
    • Upload a file to a slack user (DM)
    • Upload a file inside a slack thread 🧵
    • Upload a file with multiple channels/users

Examples (Post a message)

Let’s post a message to the default slack bot channel.

# share on Slack
post_to_slack(message: "App successfully released!")

Let’s post a direct message to a slack user that unit tests CI has been failed.

# share on Slack
post_to_slack(
  message: "CI: Your unit tests on #{ENV['CI_COMMIT_REF_NAME']} failed",
  channel: "@SlackUsername" # This can be Slack userID, instead of username i.e @UXXXXX
)

Let’s post a slack message to the #ios-team channel about the new test-flight build.

lane :beta do
  gym # Build the app and create .ipa file
  pilot # Upload build to TestFlight

  version_number = get_version_number # Get project version
  build_number = get_build_number # Get build number
  beta_release_name = "#{version_number}-#{build_number}-beta-release"

  # share on Slack
  post_to_slack(
    message: "Hi team, we have a new test-flight beta build: #{beta_release_name}",
    channel: "#ios-team"
  )
end

Let’s post a slack message with custom payload.

# share on Slack
post_to_slack(
  api_token: "xyz", # Preferably configure as ENV['SLACK_API_TOKEN']
  message: "App successfully released!",
  channel: "#channel",  # Optional, by default will post to the default channel configured for the Slack Bot.
  success: true,        # Optional, defaults to true.
  payload: {  # Optional, lets you specify any number of your own Slack attachments.
    "Build Date" => Time.new.to_s,
    "Built by" => "Jenkins",
  },
  default_payloads: [:git_branch, :git_author], # Optional, lets you specify an allowlist of default payloads to include. Pass an empty array to suppress all the default payloads.
        # Don't add this key, or pass nil, if you want all the default payloads. The available default payloads are: `lane`, `test_result`, `git_branch`, `git_author`, `last_git_commit`, `last_git_commit_hash`.
  attachment_properties: { # Optional, lets you specify any other properties available for attachments in the slack API (see https://api.slack.com/docs/attachments).
       # This hash is deep merged with the existing properties set using the other properties above. This allows your own fields properties to be appended to the existing fields that were created using the `payload` property for instance.
    thumb_url: "http://example.com/path/to/thumb.png",
    fields: [{
      title: "My Field",
      value: "My Value",
      short: true
    }]
  }
)

Let’s post a slack message inside a slack thread 🧵

lane :release do
  # Start the release with slack release thread
  release_thread = post_to_slack(
    message: "Good morning team, CI has started the AppStore release. You can find more information inside this thread 🧵",
    channel: "#ios-team"
  )

  # Important: Save this slack thread timestamp for futher slack messages
  release_thread_ts = release_thread[:json]["ts"]

  gym # Build the app and create .ipa file

  # Post an update in release thread
  post_to_slack(
    message: "App has been build successfully! 💪",
    channel: "#ios-team",
    thread_ts: release_thread_ts
  )

  deliver # Upload build to AppStore

  # Post an update in release thread
  post_to_slack(
    message: "App has been uploaded to the AppStore and submitted for Apple's review! 🚀",
    channel: "#ios-team",
    thread_ts: release_thread_ts
  )
end

Let’s post a message with custom slack bot username and icon

# share on Slack
post_to_slack(
  message: "App successfully released!",
  username: "Release Bot", # Overrides the bot's username
  icon_url: "https://fastlane.tools/assets/img/fastlane_icon.png" # Overrides the bot's icon
)

Examples (Upload a file)

Let’s Upload a file to a slack channel that main branch unit tests has been failed, see scan logs.

# File upload on Slack
file_upload_to_slack(
  initial_comment: "CI: main-branch unit tests failed",
  file_path: "scan.log",
  channels: "#ios-team" # Comma-separated list of slack #channel names where the file will be shared
)

Let’s Upload a file to a slack user that your branch unit tests has been failed, see scan logs.

# File upload on Slack
file_upload_to_slack(
  initial_comment: "CI: Your unit tests on #{ENV['CI_COMMIT_REF_NAME']} failed",
  file_path: "scan.log",
  channels: "@SlackUsername" # This can be Slack userID, instead of username i.e @UXXXXX
)

Let’s Upload a file inside a slack thread 🧵

lane :release do
  # Start the release with slack release thread
  release_thread = post_to_slack(
    message: "Good morning team, CI has started the AppStore release. You can find more information inside this thread 🧵",
    channel: "#ios-team"
  )

  # Important: Save this slack thread timestamp for futher slack messages
  release_thread_ts = release_thread[:json]["ts"]

  gym # Build the app and create .ipa file

  # Post an update in release thread
  post_to_slack(
    message: "App has been build successfully! 💪",
    channel: "#ios-team",
    thread_ts: release_thread_ts
  )

  deliver # Upload build to AppStore

  # Post an update in release thread
  post_to_slack(
    message: "App has been uploaded to the AppStore and submitted for Apple's review! 🚀",
    channel: "#ios-team",
    thread_ts: release_thread_ts
  )

  # Spaceship logs file upload on Slack
  file_upload_to_slack(
    initial_comment: "Deliver:: Spaceship logs",
    file_path: "spaceship.log",
    channels: "#ios-team",
    thread_ts: release_thread_ts
  )
end

About Fastlane

fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. To learn more, check out fastlane.tools.

fastlane-plugin-slack_bot's People

Contributors

crazymanish avatar eungkyu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

fastlane-plugin-slack_bot's Issues

Error with file upload/slack API

Upon trying to upload an .apk file I came across and error between the plugin and slacks API. In the plugin file it's passing in a file name minus the file type suffix however when reviewing the slack API the suffix is required when supplying a file name. The result is when uploading an apk you get the following API call:

file_path: "./dev/release/output/flavour/my-apk.apk
# file_upload_to_slack.rb
# Slack upload params - currently missing suffix from filename

filename: "my-apk",
filetype: "apk",

This results in slack trying to guess the file type and for apks they fall back to .zip - which isn't correct since APK is a supported file type. This can be worked around currently by ensuring we supply a file_name: ...#{type} however the plugin should be smart enough to actually append the suffix to file name (or not strip it in the first place).

# Expected payload

filename: "my-apk.apk",
filetype: "apk",

attachment_properties thumb url does not work anymore

thumb_url does not work anymore, does not display the image.

eg: JSON

attachment_properties: {
        thumb_url: 'https://i.imgur.com/jpC7MzJ.png',
        fields: [{
          title: "Release",
          value: SLACK_VERSION,
          short: true
        },
        {
          title: "Release Notes",
          value: SLACK_RELEASE_NOTES,
          short: false
        },
        {
          title: "Store",
          value: "Apple ",
          short: false
        }]
      }

Results:

Screenshot 2024-04-04 at 10 00 26 AM

`file_upload_to_slack` returns `stream timeout`

Hello,

I'm trying to post a message inside a thread to a previously uploaded file to the channel via file_upload_to_slack . Unfortunately, the result of file_upload_to_slack looks like this:

{:status=>nil, :body=>"stream timeout", :json=>{}}

and it's impossible to get a thread_ts to use it in post_to_slack.

Could you please suggest how to post a message inside the thread to a previously uploaded file via file_upload_to_slack ?

Thanks in advance.

Custom username and icon don't work

Hello there! Thank you for releasing this wonderful plugin. It's really useful. Just reporting some issue related to custom username and icon url. I don't get customised username and icon when using:

release_thread = post_to_slack(
  as_user: false,
  api_token: slack_api_token,
  channel: slack_channel,
  message: "Test for custom icon and bot name",
  icon_url: "https://fastlane.tools/assets/img/fastlane_icon.png",
  username: "Release Bot",
)

screenshot

It's probably because of the line

payload[:as_user] = options[:as_user] if options[:as_user] # default is false

I don't know Ruby very well, but I suspect it doesn't set as_user explicitly as false in payload, but only when it's true. According to Slack's documentation it must be set explicitly to make it working:

Legacy authorship
Classic Slack apps using the umbrella bot scope can't request additional scopes to adjust message authorship.

For classic Slack apps, the best way to control the authorship of a message is to be explicit with the as_user parameter.

If you don't use the as_user parameter, chat.postMessage will guess the most appropriate as_user interpretation based on the kind of token you're using.

If as_user is not provided at all, then the value is inferred, based on the scopes granted to the caller: If the caller could post with as_user passed as false, then that is how the method behaves; otherwise, the method behaves as if as_user were passed as true.

https://api.slack.com/methods/chat.postMessage#legacy_authorship

It would be nice to have it working for all actions (message, message in thread, upload, upload in thread).

Channel field is wrongly passed

Hello,

so i've encountered problem with channel field, according to this document https://api.slack.com/methods/chat.postMessage#channels

you can pass as a channel field only formats like:

  1. hash symbol - #<your_channel>
  2. channel id - C024BE91L
  3. user id - U0G9QF9C6

so in your implementation you're adding either # or @ with passed value which prevents from sending it correctly. I've tried passing username with @ in every single way and this doesn't work. Another thing is that default channel also doesn't work, at least when i try leave that field empty.

Ruby 3.0 issue - wrong number of arguments (given 2, expected 1)

I'm having a crash when trying to use the plugin:

/usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-plugin-slack_bot-1.0.0/lib/fastlane/plugin/slack_bot/helper/slack_bot_link_formatter_helper.rb:41:in `initialize': \e[31m[!] wrong number of arguments (given 2, expected 1)\e[0m (ArgumentError)
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-plugin-slack_bot-1.0.0/lib/fastlane/plugin/slack_bot/helper/slack_bot_link_formatter_helper.rb:35:in `new'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-plugin-slack_bot-1.0.0/lib/fastlane/plugin/slack_bot/helper/slack_bot_link_formatter_helper.rb:35:in `format'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-plugin-slack_bot-1.0.0/lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb:15:in `run'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:263:in `block (2 levels) in execute_action'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/actions/actions_helper.rb:69:in `execute_action'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:255:in `block in execute_action'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:229:in `chdir'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:229:in `execute_action'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:157:in `trigger_action_by_name'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/fast_file.rb:159:in `method_missing'
        from Fastfile:94:in `block in parsing_binding'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/lane.rb:33:in `call'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:49:in `block in execute'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:45:in `chdir'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/runner.rb:45:in `execute'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/lane_manager.rb:47:in `cruise_lane'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/command_line_handler.rb:36:in `handle'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/commands_generator.rb:109:in `block (2 levels) in run'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/commander-4.6.0/lib/commander/command.rb:187:in `call'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/commander-4.6.0/lib/commander/command.rb:157:in `run'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/commander-4.6.0/lib/commander/runner.rb:444:in `run_active_command'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane_core/lib/fastlane_core/ui/fastlane_runner.rb:76:in `run!'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/commander-4.6.0/lib/commander/delegates.rb:18:in `run!'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/commands_generator.rb:353:in `run'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/commands_generator.rb:42:in `start'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/fastlane/lib/fastlane/cli_tools_distributor.rb:122:in `take_off'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/gems/fastlane-2.188.0/bin/fastlane:23:in `<top (required)>'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/bin/fastlane:23:in `load'
        from /usr/local/Cellar/fastlane/2.188.0/libexec/bin/fastlane:23:in `<main>'

Awesome plugin btw, thanks for sharing!!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.