fastlane-plugin-discord_webhook 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1c623e59ee75e6ee3443dbf34fda73c834a831bc2a90ff0a0bb6cb0a86924146
4
+ data.tar.gz: e726c49563e2df88536facd4cc9492d6bbc8a15d01b131dbf147cb6fbeff1d8d
5
+ SHA512:
6
+ metadata.gz: b493ed928ef47e68b470d13da492a5b150dc4365511bf16f4aad30ffbd4fdabc193df426fe18ba67a93e406d033ed212dbd4c765c5a6622a15e3baec682f3875
7
+ data.tar.gz: b2bf0e922d25b2660ab0a38d2c68c7b8c8242a9fc5fb651d5a84a70f031609a111540890685e955b5811dd1fcac40457fdae8d6ef7e8a99851ae4a0da843e6c6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Takuma Homma <nagomimatcha@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # discord_webhook plugin
2
+
3
+ [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-discord_webhook)
4
+
5
+ ## Getting Started
6
+
7
+ This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-discord_webhook`, add it to your project by running:
8
+
9
+ ```bash
10
+ fastlane add_plugin discord_webhook
11
+ ```
12
+
13
+ ## About discord_webhook
14
+
15
+ Send a message using Discord Webhook
16
+
17
+ ## Usage
18
+
19
+ | Key | Description |
20
+ | :--: | :--: |
21
+ | `webhook_url` | Discord Webhook URL. |
22
+ | `username` | [Optional] Username. It will override webhook's username if specified. |
23
+ | `message` | The message you want to send. |
24
+ | `attachment_file_path` | [Optional] File path to upload. See details for supported types: https://discord.com/developers/docs/reference#uploading-files |
25
+ | `embed_payload` | [Optional] Embed Object. You can specify one object. See details: https://discord.com/developers/docs/resources/message#embed-object |
26
+
27
+ ## Example
28
+
29
+ ```ruby
30
+ lane :discord_message do
31
+ discord_webhook(
32
+ webhook_url: 'https://yourDiscordWebhookUrl',
33
+ message: 'hello'
34
+ )
35
+ end
36
+ ```
37
+
38
+ ```ruby
39
+ lane :discord_message_with_file do
40
+ discord_webhook(
41
+ webhook_url: 'https://yourDiscordWebhookUrl',
42
+ message: 'hello',
43
+ attachment_file_path: '/home/mataku/screenshot.png',
44
+ # Be careful to use relative path.
45
+ # Fastfile is into fastlane/, but fastlane tries to load from working directory.
46
+ )
47
+ end
48
+ ```
49
+
50
+ ```ruby
51
+ lane :discord_message_with_embed do
52
+ discord_webhook(
53
+ webhook_url: 'https://yourDiscordWebhookUrl',
54
+ message: 'hello',
55
+ embed_payload: {
56
+ title: 'embed_title',
57
+ description: 'embed_description',
58
+ url: 'https://github.com/mataku',
59
+ thumbnail: {
60
+ url: 'https://github.com/mataku.png'
61
+ }
62
+ }
63
+ )
64
+ end
65
+ ```
66
+
67
+ ## Issues and Feedback
68
+
69
+ For any other issues and feedback about this plugin, please submit it to this repository.
70
+
71
+ ## Troubleshooting
72
+
73
+ If you have trouble using plugins, check out the [Plugins Troubleshooting](https://docs.fastlane.tools/plugins/plugins-troubleshooting/) guide.
@@ -0,0 +1,119 @@
1
+ require 'fastlane/action'
2
+ require 'json'
3
+ require 'net/http'
4
+ require_relative '../helper/discord_webhook_helper'
5
+ require_relative '../discord_client'
6
+
7
+ module Fastlane
8
+ module Actions
9
+ class DiscordWebhookAction < Action
10
+ def self.run(params)
11
+ webhook_url = params[:webhook_url]
12
+ if webhook_url.nil? || webhook_url.empty?
13
+ UI.user_error!('`webhook_url:` parameter is invalid. Please provide a valid URL.')
14
+ end
15
+
16
+ message = params[:message]
17
+ username = params[:username]
18
+ file_path = params[:attachment_file_path]
19
+ embed_payload = params[:embed_payload]
20
+
21
+ client = DiscordWebhook::DiscordClient.new(webhook_url)
22
+ begin
23
+ response = client.send_message(
24
+ username: username,
25
+ content: message,
26
+ file_path: file_path,
27
+ embed_payload: embed_payload
28
+ )
29
+ handle_response(response)
30
+ rescue StandardError => e
31
+ UI.user_error!(e.message)
32
+ end
33
+ end
34
+
35
+ def self.description
36
+ "Send a message using Discord Webhook"
37
+ end
38
+
39
+ def self.authors
40
+ ["Takuma Homma"]
41
+ end
42
+
43
+ def self.return_value
44
+ # If your method provides a return value, you can describe here what it does
45
+ end
46
+
47
+ def self.details
48
+ "Send a message using Discord Webhook"
49
+ end
50
+
51
+ def self.available_options
52
+ [
53
+ FastlaneCore::ConfigItem.new(
54
+ key: :webhook_url,
55
+ env_name: "FL_DISCORD_WEBHOOK_URL",
56
+ description: "Discord Webhook URL",
57
+ optional: false,
58
+ type: String,
59
+ verify_block: proc do |value|
60
+ UI.user_error!('`webhook_url:` parameter is invalid. Please provide a valid URL.') if value.nil? || value.empty?
61
+ end
62
+ ),
63
+ FastlaneCore::ConfigItem.new(
64
+ key: :message,
65
+ env_name: "FL_DISCORD_WEBHOOK_MESSAGE",
66
+ description: "The message you want to send",
67
+ optional: true,
68
+ type: String
69
+ ),
70
+ FastlaneCore::ConfigItem.new(
71
+ key: :username,
72
+ env_name: "FL_DISCORD_WEBHOOK_USERNAME",
73
+ description: "[Optional] Username",
74
+ optional: true,
75
+ type: String
76
+ ),
77
+ FastlaneCore::ConfigItem.new(
78
+ key: :attachment_file_path,
79
+ env_name: "FL_DISCORD_WEBHOOK_ATTACHMENT_FILE_PATH",
80
+ description: "[Optional] File path to upload",
81
+ optional: true,
82
+ type: String,
83
+ verify_block: proc do |value|
84
+ if value
85
+ UI.user_error!("#{value} not found! Specify correct file path.") unless File.exist?(value)
86
+ end
87
+ end
88
+ ),
89
+ # See: https://discord.com/developers/docs/resources/message#embed-object
90
+ FastlaneCore::ConfigItem.new(
91
+ key: :embed_payload,
92
+ env_name: "FL_DISCORD_WEBHOOK_EMBED_PAYLOAD",
93
+ description: "[Optional] Embed Object. You can specify one object. See details: https://discord.com/developers/docs/resources/message#embed-object",
94
+ optional: true,
95
+ type: Hash
96
+ )
97
+ # TODO: multiple file and embed contents if needed
98
+ ]
99
+ end
100
+
101
+ def self.is_supported?(platform)
102
+ true
103
+ end
104
+
105
+ def self.handle_response(response)
106
+ if response.kind_of?(Net::HTTPSuccess)
107
+ UI.success('Successfully sent a message to Discord')
108
+ else
109
+ error_body = begin
110
+ JSON.parse(response.body)['message']
111
+ rescue StandardError
112
+ ''
113
+ end
114
+ UI.user_error!("Failed to send a message to Discord: #{error_body}")
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,98 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module Fastlane
5
+ module DiscordWebhook
6
+ class DiscordClient
7
+ attr_accessor :webhook_url
8
+
9
+ # @param webhook_url [String]
10
+ def initialize(webhook_url)
11
+ @webhook_url = webhook_url
12
+ end
13
+
14
+ # @param username [String]
15
+ # @param content [String]
16
+ # @param file_path [String]
17
+ # @param embed_payload [Hash]
18
+ # @param debug [Boolean]
19
+ def send_message(username:, content:, file_path:, embed_payload:, debug: false)
20
+ if file_path.nil? || file_path.empty?
21
+ send_message_with_embed(username: username, content: content, embed_payload: embed_payload, debug: debug)
22
+ else
23
+ send_message_with_file(username: username, content: content, file_path: file_path, embed_payload: embed_payload, debug: debug)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # @param username [String]
30
+ # @param content [String]
31
+ # @param file_path [String]
32
+ # @param embed_payload [Hash]
33
+ # @param debug [Boolean]
34
+ def send_message_with_file(username:, content:, file_path:, embed_payload:, debug: false)
35
+ uri = URI.parse(@webhook_url)
36
+ request = Net::HTTP::Post.new(uri)
37
+ embed_payload_content = if embed_payload.nil? || embed_payload.empty?
38
+ []
39
+ else
40
+ [embed_payload]
41
+ end
42
+ payload_json = if !username.nil? && !username.empty?
43
+ { username: username, content: content, embeds: embed_payload_content }
44
+ else
45
+ { content: content, embeds: embed_payload_content }
46
+ end
47
+ request.set_form(
48
+ [
49
+ [
50
+ 'payload_json',
51
+ payload_json.to_json
52
+ ],
53
+ [
54
+ 'file1',
55
+ File.open(file_path)
56
+ ]
57
+ ],
58
+ 'multipart/form-data'
59
+ )
60
+ http = Net::HTTP.new(uri.hostname, uri.port)
61
+ http.use_ssl = true
62
+ http.set_debug_output($stderr) if debug
63
+ http.start do |h|
64
+ h.request(request)
65
+ end
66
+ end
67
+
68
+ # @param username [String]
69
+ # @param content [String]
70
+ # @param embed_payload [Hash]
71
+ # @param debug [Boolean]
72
+ def send_message_with_embed(username:, content:, embed_payload:, debug: false)
73
+ uri = URI.parse(@webhook_url)
74
+ request = Net::HTTP::Post.new(uri)
75
+ request.content_type = 'application/json'
76
+ embed_payload_content = if embed_payload.nil? || embed_payload.empty?
77
+ []
78
+ else
79
+ [embed_payload]
80
+ end
81
+
82
+ body = {
83
+ content: content,
84
+ embeds: embed_payload_content
85
+ }
86
+ body[:username] = username if !username.nil? && !username.empty?
87
+ request.body = body.to_json
88
+
89
+ http = Net::HTTP.new(uri.hostname, uri.port)
90
+ http.use_ssl = true
91
+ http.set_debug_output($stderr) if debug
92
+ http.start do |h|
93
+ h.request(request)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,86 @@
1
+ require 'fastlane_core/ui/ui'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module Fastlane
6
+ UI = FastlaneCore::UI unless Fastlane.const_defined?(:UI)
7
+
8
+ module Helper
9
+ class DiscordWebhookHelper
10
+ # class methods that you define here become available in your action
11
+ # as `Helper::DiscordWebhookHelper.your_method`
12
+
13
+ def self.send_message_with_file(webhook_url:, username:, content:, file_path:, embed_title:, embed_description:, embed_url:, embed_icon_url:, debug: false)
14
+ uri = URI.parse(webhook_url)
15
+ request = Net::HTTP::Post.new(uri)
16
+ payload_json = if !username.nil? && !username.empty?
17
+ { username: username, content: content }
18
+ else
19
+ { content: content }
20
+ end
21
+ request.set_form(
22
+ [
23
+ [
24
+ 'payload_json',
25
+ payload_json.to_json
26
+ ],
27
+ [
28
+ 'file1',
29
+ File.open(file_path)
30
+ ]
31
+ ],
32
+ 'multipart/form-data'
33
+ )
34
+ http = Net::HTTP.new(uri.hostname, uri.port)
35
+ http.use_ssl = true
36
+ http.set_debug_output($stderr) if debug
37
+ response = http.start do |h|
38
+ h.request(request)
39
+ end
40
+ handle_response(response)
41
+ end
42
+
43
+ def self.send_message_with_embed(webhook_url:, username:, content:, embed_title:, embed_description:, embed_url:, embed_thumbnail_url:, debug: false)
44
+ uri = URI.parse(webhook_url)
45
+ request = Net::HTTP::Post.new(uri)
46
+ request.content_type = 'application/json'
47
+ embed = {
48
+ type: 'link',
49
+ title: embed_title,
50
+ description: embed_description,
51
+ url: embed_url
52
+ }
53
+ if !embed_icon_url.nil? && !embed_icon_url.empty?
54
+ embed[:thumbnail] = {
55
+ url: embed_thumbnail_url
56
+ }
57
+ end
58
+
59
+ body = {
60
+ content: content,
61
+ embeds: [embed]
62
+ }
63
+ body[:username] = username if !username.nil? && !username.empty?
64
+ request.body = body.to_json
65
+
66
+ http = Net::HTTP.new(uri.hostname, uri.port)
67
+ http.use_ssl = true
68
+ http.set_debug_output($stderr) if debug
69
+ response = http.start do |h|
70
+ h.request(request)
71
+ end
72
+
73
+ handle_response(response)
74
+ end
75
+
76
+ def self.handle_response(response)
77
+ if response.kind_of?(Net::HTTPSuccess)
78
+ UI.success('Successfully sent a message to Discord')
79
+ else
80
+ error_body = JSON.parse(response.body)
81
+ UI.user_error!("Failed to send a message to Discord: #{error_body}")
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ module Fastlane
2
+ module DiscordWebhook
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'fastlane/plugin/discord_webhook/version'
2
+
3
+ module Fastlane
4
+ module DiscordWebhook
5
+ # Return all .rb files inside the "actions" and "helper" directory
6
+ def self.all_classes
7
+ Dir[File.expand_path('**/{actions,helper}/*.rb', File.dirname(__FILE__))]
8
+ end
9
+ end
10
+ end
11
+
12
+ # By default we want to import all available actions and helpers
13
+ # A plugin can contain any number of actions and plugins
14
+ Fastlane::DiscordWebhook.all_classes.each do |current|
15
+ require current
16
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastlane-plugin-discord_webhook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takuma Homma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: nagomimatcha@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/fastlane/plugin/discord_webhook.rb
22
+ - lib/fastlane/plugin/discord_webhook/actions/discord_webhook_action.rb
23
+ - lib/fastlane/plugin/discord_webhook/discord_client.rb
24
+ - lib/fastlane/plugin/discord_webhook/helper/discord_webhook_helper.rb
25
+ - lib/fastlane/plugin/discord_webhook/version.rb
26
+ homepage: https://github.com/mataku/fastlane-plugin-discord_webhook
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ rubygems_mfa_required: 'true'
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.7'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.5.16
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A fastlane plugin to send a message via Discord Webhook
50
+ test_files: []