Skip to content

Instantly share code, notes, and snippets.

@matsuda
Last active March 15, 2019 10:53
Show Gist options
  • Save matsuda/a8f041c3c39a9ae1d97d38ea34fd7e2f to your computer and use it in GitHub Desktop.
Save matsuda/a8f041c3c39a9ae1d97d38ea34fd7e2f to your computer and use it in GitHub Desktop.
Carthageのoutdatedを実行し、最新のバージョンがリリースされてるかチェックしてSlackへ通知するFastlane action
module Fastlane
module Actions
module SharedValues
CARTHAGE_OUTDATED_OUTDATED_LIST = :CARTHAGE_OUTDATED_OUTDATED_LIST
CARTHAGE_OUTDATED_REPOSITORY_LIST = :CARTHAGE_OUTDATED_REPOSITORY_LIST
CARTHAGE_OUTDATED_SLACK_JSON = :CARTHAGE_OUTDATED_SLACK_JSON
end
class CarthageOutdatedAction < Action
def self.run(params)
# fastlane will take care of reading in the parameter and fetching the environment variable:
# Check Slack info
UI.message "Parameter SLACK url: #{params[:slack_url]}"
UI.message "Parameter SLACK username: #{params[:slack_username]}"
cmd = ["carthage outdated"]
result = Actions.sh(cmd.join(' '))
separator = "The following dependencies are outdated:\n"
result = result.split(separator)[1]
# ex.)
# KeychainAccess "v3.1.1" -> "v3.1.2" (Latest: "v3.1.2")
libs = result.split("\n")
Actions.lane_context[SharedValues::CARTHAGE_OUTDATED_OUTDATED_LIST] = libs
repos = resolved
Actions.lane_context[SharedValues::CARTHAGE_OUTDATED_REPOSITORY_LIST] = repos
attachements = []
libs.each_with_index do |lib, i|
lib = lib.gsub(/"/, "") # Remove '"'
texts = lib.split(" ")
name = texts.shift
attachement = {title: name}
current_version = texts[0]
next_version = texts[2]
if current_version == next_version
attachement[:color] = "warning"
else
attachement[:color] = "danger"
url = repos[name]
attachement[:title_link] = "#{url}/releases/tag/#{next_version}"
texts[2] = "*#{next_version}*"
end
attachement[:text] = texts.join(" ")
# attachement = {color: color, title: name, text: texts.join(" "), title_link: "#{url}/releases/tag/#{next_version}"}
attachements << attachement
end
slack_url = params[:slack_url]
slack_username = params[:slack_username]
json = JSON.generate(
text: "Carthage new version",
pretext: "Check outdated carthage libraries.",
attachments: attachements,
username: slack_username,
mrkdwn_in: ["text", "pretext"]
)
Actions.lane_context[SharedValues::CARTHAGE_OUTDATED_SLACK_JSON] = json
command = <<~EOF
curl -X POST --data-urlencode 'payload=#{json}' #{slack_url}
EOF
sh command
end
# {"RxSwift"=>"https://github.com/ReactiveX/RxSwift",
# "SVProgressHUD"=>"https://github.com/SVProgressHUD/SVProgressHUD"}
def self.resolved
lock_file = "Cartfile.resolved"
# dir = File.dirname(File.expand_path(__FILE__))
# file = File.join(dir, lock_file)
file = lock_file
results = {}
File.open(file) do |f|
f.each_line do |line|
array = line.gsub(/"/, "").split(" ")
repo = array[1]
names = repo.split("/")
url = "https://github.com/#{repo}"
results[names[1]] = url
end
rescue => e
puts e
end
results
end
def self.description
"Check Carthage outdated"
end
def self.details
"Check Carthage outdated and post state to channel in Slack"
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :slack_url,
env_name: "FL_CARTHAGE_OUTDATED_SLACK_URL", # The name of the environment variable
description: "SLACK url for CarthageOutdatedAction", # a short description of this parameter
verify_block: proc do |value|
UI.user_error!("No SLACK url for CarthageOutdatedAction given, pass using `slack_url: 'url'`") unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :slack_username,
env_name: "FL_CARTHAGE_OUTDATED_SLACK_USERNAME", # The name of the environment variable
description: "SLACK usernaem for CarthageOutdatedAction", # a short description of this parameter
verify_block: proc do |value|
UI.user_error!("No SLACK username for CarthageOutdatedAction given, pass using `slack_username: 'username'`") unless (value and not value.empty?)
end),
]
end
def self.output
[
['CARTHAGE_OUTDATED_OUTDATED_LIST', 'A list of outdated Carthage libs'],
['CARTHAGE_OUTDATED_REPOSITORY_LIST', 'A list of using Carthage libs'],
['CARTHAGE_OUTDATED_SLACK_JSON', 'A JSON for post to slack'],
]
end
def self.return_value
# If your method provides a return value, you can describe here what it does
end
def self.authors
"matsuda"
end
def self.is_supported?(platform)
# you can do things like
#
# true
#
# platform == :ios
#
# [:ios, :mac].include?(platform)
#
platform == :ios
end
end
end
end
module Fastlane
module Actions
module SharedValues
COCOAPODS_OUTDATED_OUTDATED_LIST = :COCOAPODS_OUTDATED_OUTDATED_LIST
COCOAPODS_OUTDATED_SLACK_JSON = :COCOAPODS_OUTDATED_SLACK_JSON
end
class CocoapodsOutdatedAction < Action
def self.run(params)
# fastlane will take care of reading in the parameter and fetching the environment variable:
# Check Slack info
UI.message "Parameter SLACK url: #{params[:slack_url]}"
UI.message "Parameter SLACK username: #{params[:slack_username]}"
cmd = ["bundle exec pod outdated"]
cmd << "--no-repo-update" if params[:repo_update] == false
result = Actions.sh(cmd.join(' '))
separator = "The following pod updates are available:\n"
result = result.split(separator)[1]
# ex.)
# - Fabric 1.7.8 -> 1.9.0 (latest version 1.9.0)
libs = result.split("\n")
Actions.lane_context[SharedValues::COCOAPODS_OUTDATED_OUTDATED_LIST] = libs
attachements = []
libs.each_with_index do |lib, i|
lib = lib.gsub(/- /, "") # Remove '[- ]Fabric'
texts = lib.split(" ")
name = texts.shift
attachement = {title: name}
current_version = texts[0]
next_version = texts[2]
if current_version == next_version
attachement[:color] = "warning"
else
attachement[:color] = "danger"
texts[2] = "*#{next_version}*"
end
attachement[:text] = texts.join(" ")
# attachement = {color: color, title: name, text: texts.join(" ")}
attachements << attachement
end
slack_url = params[:slack_url]
slack_username = params[:slack_username]
json = JSON.generate(
text: "CocoaPods new version",
pretext: "Check outdated cocoapods libraries.",
attachments: attachements,
username: slack_username,
mrkdwn_in: ["text", "pretext"]
)
Actions.lane_context[SharedValues::COCOAPODS_OUTDATED_SLACK_JSON] = json
command = <<~EOF
curl -X POST --data-urlencode 'payload=#{json}' #{slack_url}
EOF
sh command
end
#####################################################
# @!group Documentation
#####################################################
def self.description
"Check CocoaPods outdated"
end
def self.details
"Check CocoaPods outdated and post state to channel in Slack"
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :slack_url,
env_name: "FL_COCOAPODS_OUTDATED_SLACK_URL", # The name of the environment variable
description: "SLACK url for CocoaPodsOutdatedAction", # a short description of this parameter
verify_block: proc do |value|
UI.user_error!("No SLACK url for CocoaPodsOutdatedAction given, pass using `slack_url: 'url'`") unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :slack_username,
env_name: "FL_COCOAPODS_OUTDATED_SLACK_USERNAME", # The name of the environment variable
description: "SLACK usernaem for CocoaPodsOutdatedAction", # a short description of this parameter
verify_block: proc do |value|
UI.user_error!("No SLACK username for CocoaPodsOutdatedAction given, pass using `slack_username: 'username'`") unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :repo_update,
env_name: "FL_COCOAPODS_OUTDATED_REPO_UPDATE",
description: "Skip running `pod repo update` before install",
is_string: false,
type: Boolean,
optional: true),
]
end
def self.output
[
['COCOAPODS_OUTDATED_OUTDATED_LIST', 'A list of outdated CocoaPods libs'],
['COCOAPODS_OUTDATED_SLACK_JSON', 'A JSON for post to slack'],
]
end
def self.return_value
# If your method provides a return value, you can describe here what it does
end
def self.authors
"matsuda"
end
def self.is_supported?(platform)
# you can do things like
#
# true
#
# platform == :ios
#
# [:ios, :mac].include?(platform)
#
platform == :ios
end
end
end
end
desc "check carthage outdated"
lane :check_carthage_outdated do
carthage_outdated(
slack_url: "https://hooks.slack.com/services/xxxx",
slack_username: "MyBot"
)
end
desc "check cocoapods outdated"
lane :check_cocoapods_outdated do |options|
cocoapods_outdated(
slack_url: "https://hooks.slack.com/services/xxxx",
slack_username: "MyBot"
repo_update: options[:repo_update]
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment