pantograph 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +1 -0
  3. data/LICENSE +21 -0
  4. data/README.md +197 -0
  5. data/bin/bin-proxy +19 -0
  6. data/bin/pantograph +23 -0
  7. data/pantograph/README.md +11 -0
  8. data/pantograph/lib/assets/ActionDetails.md.erb +106 -0
  9. data/pantograph/lib/assets/Actions.md.erb +43 -0
  10. data/pantograph/lib/assets/DefaultPantfileTemplate +20 -0
  11. data/pantograph/lib/assets/completions/completion.bash +23 -0
  12. data/pantograph/lib/assets/completions/completion.fish +39 -0
  13. data/pantograph/lib/assets/completions/completion.sh +12 -0
  14. data/pantograph/lib/assets/completions/completion.zsh +23 -0
  15. data/pantograph/lib/assets/custom_action_template.rb +80 -0
  16. data/pantograph/lib/assets/report_template.xml.erb +15 -0
  17. data/pantograph/lib/pantograph/action.rb +194 -0
  18. data/pantograph/lib/pantograph/action_collector.rb +35 -0
  19. data/pantograph/lib/pantograph/actions/README.md +3 -0
  20. data/pantograph/lib/pantograph/actions/actions_helper.rb +166 -0
  21. data/pantograph/lib/pantograph/actions/add_extra_platforms.rb +45 -0
  22. data/pantograph/lib/pantograph/actions/artifactory.rb +157 -0
  23. data/pantograph/lib/pantograph/actions/bundle_install.rb +156 -0
  24. data/pantograph/lib/pantograph/actions/changelog_from_git_commits.rb +197 -0
  25. data/pantograph/lib/pantograph/actions/clipboard.rb +52 -0
  26. data/pantograph/lib/pantograph/actions/cloc.rb +89 -0
  27. data/pantograph/lib/pantograph/actions/create_pull_request.rb +190 -0
  28. data/pantograph/lib/pantograph/actions/danger.rb +131 -0
  29. data/pantograph/lib/pantograph/actions/debug.rb +32 -0
  30. data/pantograph/lib/pantograph/actions/default_platform.rb +47 -0
  31. data/pantograph/lib/pantograph/actions/download.rb +76 -0
  32. data/pantograph/lib/pantograph/actions/echo.rb +14 -0
  33. data/pantograph/lib/pantograph/actions/ensure_bundle_exec.rb +59 -0
  34. data/pantograph/lib/pantograph/actions/ensure_env_vars.rb +58 -0
  35. data/pantograph/lib/pantograph/actions/ensure_git_branch.rb +69 -0
  36. data/pantograph/lib/pantograph/actions/ensure_git_status_clean.rb +81 -0
  37. data/pantograph/lib/pantograph/actions/erb.rb +88 -0
  38. data/pantograph/lib/pantograph/actions/get_build_number_repository.rb +120 -0
  39. data/pantograph/lib/pantograph/actions/get_github_release.rb +163 -0
  40. data/pantograph/lib/pantograph/actions/git_add.rb +93 -0
  41. data/pantograph/lib/pantograph/actions/git_branch.rb +58 -0
  42. data/pantograph/lib/pantograph/actions/git_commit.rb +80 -0
  43. data/pantograph/lib/pantograph/actions/git_pull.rb +53 -0
  44. data/pantograph/lib/pantograph/actions/git_submodule_update.rb +52 -0
  45. data/pantograph/lib/pantograph/actions/git_tag_exists.rb +74 -0
  46. data/pantograph/lib/pantograph/actions/github_api.rb +262 -0
  47. data/pantograph/lib/pantograph/actions/gradle.rb +278 -0
  48. data/pantograph/lib/pantograph/actions/import.rb +49 -0
  49. data/pantograph/lib/pantograph/actions/import_from_git.rb +71 -0
  50. data/pantograph/lib/pantograph/actions/is_ci.rb +51 -0
  51. data/pantograph/lib/pantograph/actions/jira.rb +115 -0
  52. data/pantograph/lib/pantograph/actions/lane_context.rb +60 -0
  53. data/pantograph/lib/pantograph/actions/last_git_commit.rb +58 -0
  54. data/pantograph/lib/pantograph/actions/last_git_tag.rb +51 -0
  55. data/pantograph/lib/pantograph/actions/make_changelog_from_jenkins.rb +81 -0
  56. data/pantograph/lib/pantograph/actions/min_pantograph_version.rb +57 -0
  57. data/pantograph/lib/pantograph/actions/nexus_upload.rb +230 -0
  58. data/pantograph/lib/pantograph/actions/notification.rb +75 -0
  59. data/pantograph/lib/pantograph/actions/number_of_commits.rb +75 -0
  60. data/pantograph/lib/pantograph/actions/opt_out_usage.rb +40 -0
  61. data/pantograph/lib/pantograph/actions/pantograph_version.rb +15 -0
  62. data/pantograph/lib/pantograph/actions/println.rb +14 -0
  63. data/pantograph/lib/pantograph/actions/prompt.rb +119 -0
  64. data/pantograph/lib/pantograph/actions/push_git_tags.rb +76 -0
  65. data/pantograph/lib/pantograph/actions/push_to_git_remote.rb +127 -0
  66. data/pantograph/lib/pantograph/actions/puts.rb +68 -0
  67. data/pantograph/lib/pantograph/actions/reset_git_repo.rb +121 -0
  68. data/pantograph/lib/pantograph/actions/rocket.rb +83 -0
  69. data/pantograph/lib/pantograph/actions/rsync.rb +74 -0
  70. data/pantograph/lib/pantograph/actions/ruby_version.rb +56 -0
  71. data/pantograph/lib/pantograph/actions/say.rb +56 -0
  72. data/pantograph/lib/pantograph/actions/scp.rb +114 -0
  73. data/pantograph/lib/pantograph/actions/set_github_release.rb +274 -0
  74. data/pantograph/lib/pantograph/actions/sh.rb +71 -0
  75. data/pantograph/lib/pantograph/actions/skip_docs.rb +52 -0
  76. data/pantograph/lib/pantograph/actions/slack.rb +288 -0
  77. data/pantograph/lib/pantograph/actions/sonar.rb +156 -0
  78. data/pantograph/lib/pantograph/actions/ssh.rb +162 -0
  79. data/pantograph/lib/pantograph/actions/twitter.rb +89 -0
  80. data/pantograph/lib/pantograph/actions/update_pantograph.rb +177 -0
  81. data/pantograph/lib/pantograph/actions/zip.rb +120 -0
  82. data/pantograph/lib/pantograph/auto_complete.rb +82 -0
  83. data/pantograph/lib/pantograph/boolean.rb +5 -0
  84. data/pantograph/lib/pantograph/cli_tools_distributor.rb +183 -0
  85. data/pantograph/lib/pantograph/command_line_handler.rb +43 -0
  86. data/pantograph/lib/pantograph/commands_generator.rb +344 -0
  87. data/pantograph/lib/pantograph/configuration_helper.rb +26 -0
  88. data/pantograph/lib/pantograph/core_ext/bundler_monkey_patch.rb +14 -0
  89. data/pantograph/lib/pantograph/documentation/actions_list.rb +214 -0
  90. data/pantograph/lib/pantograph/documentation/docs_generator.rb +95 -0
  91. data/pantograph/lib/pantograph/documentation/markdown_docs_generator.rb +221 -0
  92. data/pantograph/lib/pantograph/environment_printer.rb +282 -0
  93. data/pantograph/lib/pantograph/erb_template_helper.rb +30 -0
  94. data/pantograph/lib/pantograph/features.rb +4 -0
  95. data/pantograph/lib/pantograph/helper/README.md +29 -0
  96. data/pantograph/lib/pantograph/helper/dotenv_helper.rb +50 -0
  97. data/pantograph/lib/pantograph/helper/gem_helper.rb +26 -0
  98. data/pantograph/lib/pantograph/helper/git_helper.rb +135 -0
  99. data/pantograph/lib/pantograph/helper/gradle_helper.rb +62 -0
  100. data/pantograph/lib/pantograph/helper/sh_helper.rb +134 -0
  101. data/pantograph/lib/pantograph/junit_generator.rb +27 -0
  102. data/pantograph/lib/pantograph/lane.rb +97 -0
  103. data/pantograph/lib/pantograph/lane_list.rb +77 -0
  104. data/pantograph/lib/pantograph/lane_manager.rb +140 -0
  105. data/pantograph/lib/pantograph/lane_manager_base.rb +92 -0
  106. data/pantograph/lib/pantograph/markdown_table_formatter.rb +62 -0
  107. data/pantograph/lib/pantograph/new_action.rb +47 -0
  108. data/pantograph/lib/pantograph/one_off.rb +45 -0
  109. data/pantograph/lib/pantograph/other_action.rb +29 -0
  110. data/pantograph/lib/pantograph/pant_file.rb +377 -0
  111. data/pantograph/lib/pantograph/pantograph_require.rb +75 -0
  112. data/pantograph/lib/pantograph/plugins/plugin_fetcher.rb +55 -0
  113. data/pantograph/lib/pantograph/plugins/plugin_generator.rb +86 -0
  114. data/pantograph/lib/pantograph/plugins/plugin_generator_ui.rb +19 -0
  115. data/pantograph/lib/pantograph/plugins/plugin_info.rb +49 -0
  116. data/pantograph/lib/pantograph/plugins/plugin_info_collector.rb +159 -0
  117. data/pantograph/lib/pantograph/plugins/plugin_manager.rb +387 -0
  118. data/pantograph/lib/pantograph/plugins/plugin_search.rb +46 -0
  119. data/pantograph/lib/pantograph/plugins/plugin_update_manager.rb +70 -0
  120. data/pantograph/lib/pantograph/plugins/plugins.rb +12 -0
  121. data/pantograph/lib/pantograph/plugins/template/%gem_name%.gemspec.erb +35 -0
  122. data/pantograph/lib/pantograph/plugins/template/.circleci/config.yml +43 -0
  123. data/pantograph/lib/pantograph/plugins/template/.gitignore +12 -0
  124. data/pantograph/lib/pantograph/plugins/template/.rspec +5 -0
  125. data/pantograph/lib/pantograph/plugins/template/.rubocop.yml +179 -0
  126. data/pantograph/lib/pantograph/plugins/template/.travis.yml +4 -0
  127. data/pantograph/lib/pantograph/plugins/template/Gemfile +6 -0
  128. data/pantograph/lib/pantograph/plugins/template/LICENSE.erb +21 -0
  129. data/pantograph/lib/pantograph/plugins/template/README.md.erb +52 -0
  130. data/pantograph/lib/pantograph/plugins/template/Rakefile +9 -0
  131. data/pantograph/lib/pantograph/plugins/template/lib/pantograph/plugin/%plugin_name%/actions/%plugin_name%_action.rb.erb +47 -0
  132. data/pantograph/lib/pantograph/plugins/template/lib/pantograph/plugin/%plugin_name%/helper/%plugin_name%_helper.rb.erb +16 -0
  133. data/pantograph/lib/pantograph/plugins/template/lib/pantograph/plugin/%plugin_name%/version.rb.erb +5 -0
  134. data/pantograph/lib/pantograph/plugins/template/lib/pantograph/plugin/%plugin_name%.rb.erb +16 -0
  135. data/pantograph/lib/pantograph/plugins/template/pantograph/Pantfile.erb +3 -0
  136. data/pantograph/lib/pantograph/plugins/template/pantograph/Pluginfile.erb +1 -0
  137. data/pantograph/lib/pantograph/plugins/template/spec/%plugin_name%_action_spec.rb.erb +9 -0
  138. data/pantograph/lib/pantograph/plugins/template/spec/spec_helper.rb.erb +15 -0
  139. data/pantograph/lib/pantograph/runner.rb +371 -0
  140. data/pantograph/lib/pantograph/server/action_command.rb +61 -0
  141. data/pantograph/lib/pantograph/server/action_command_return.rb +14 -0
  142. data/pantograph/lib/pantograph/server/command_executor.rb +7 -0
  143. data/pantograph/lib/pantograph/server/command_parser.rb +36 -0
  144. data/pantograph/lib/pantograph/server/control_command.rb +23 -0
  145. data/pantograph/lib/pantograph/server/json_return_value_processor.rb +72 -0
  146. data/pantograph/lib/pantograph/server/socket_server.rb +232 -0
  147. data/pantograph/lib/pantograph/server/socket_server_action_command_executor.rb +101 -0
  148. data/pantograph/lib/pantograph/setup/setup.rb +290 -0
  149. data/pantograph/lib/pantograph/setup/setup_android.rb +64 -0
  150. data/pantograph/lib/pantograph/setup/setup_ios.rb +412 -0
  151. data/pantograph/lib/pantograph/shells.rb +6 -0
  152. data/pantograph/lib/pantograph/supported_platforms.rb +28 -0
  153. data/pantograph/lib/pantograph/tools.rb +10 -0
  154. data/pantograph/lib/pantograph/version.rb +5 -0
  155. data/pantograph/lib/pantograph.rb +51 -0
  156. data/pantograph_core/README.md +79 -0
  157. data/pantograph_core/lib/assets/XMLTemplate.xml.erb +12 -0
  158. data/pantograph_core/lib/pantograph_core/analytics/action_completion_context.rb +34 -0
  159. data/pantograph_core/lib/pantograph_core/analytics/action_launch_context.rb +38 -0
  160. data/pantograph_core/lib/pantograph_core/analytics/analytics_event_builder.rb +23 -0
  161. data/pantograph_core/lib/pantograph_core/analytics/analytics_ingester_client.rb +54 -0
  162. data/pantograph_core/lib/pantograph_core/analytics/analytics_session.rb +71 -0
  163. data/pantograph_core/lib/pantograph_core/cert_checker.rb +116 -0
  164. data/pantograph_core/lib/pantograph_core/command_executor.rb +99 -0
  165. data/pantograph_core/lib/pantograph_core/configuration/commander_generator.rb +103 -0
  166. data/pantograph_core/lib/pantograph_core/configuration/config_item.rb +314 -0
  167. data/pantograph_core/lib/pantograph_core/configuration/configuration.rb +332 -0
  168. data/pantograph_core/lib/pantograph_core/configuration/configuration_file.rb +182 -0
  169. data/pantograph_core/lib/pantograph_core/core_ext/shellwords.rb +63 -0
  170. data/pantograph_core/lib/pantograph_core/core_ext/string.rb +17 -0
  171. data/pantograph_core/lib/pantograph_core/env.rb +9 -0
  172. data/pantograph_core/lib/pantograph_core/feature/feature.rb +51 -0
  173. data/pantograph_core/lib/pantograph_core/features.rb +4 -0
  174. data/pantograph_core/lib/pantograph_core/globals.rb +27 -0
  175. data/pantograph_core/lib/pantograph_core/helper.rb +409 -0
  176. data/pantograph_core/lib/pantograph_core/keychain_importer.rb +74 -0
  177. data/pantograph_core/lib/pantograph_core/languages.rb +14 -0
  178. data/pantograph_core/lib/pantograph_core/module.rb +29 -0
  179. data/pantograph_core/lib/pantograph_core/pantograph_folder.rb +39 -0
  180. data/pantograph_core/lib/pantograph_core/pantograph_pty.rb +57 -0
  181. data/pantograph_core/lib/pantograph_core/pkg_file_analyser.rb +44 -0
  182. data/pantograph_core/lib/pantograph_core/print_table.rb +131 -0
  183. data/pantograph_core/lib/pantograph_core/string_filters.rb +51 -0
  184. data/pantograph_core/lib/pantograph_core/swag.rb +85 -0
  185. data/pantograph_core/lib/pantograph_core/tag_version.rb +31 -0
  186. data/pantograph_core/lib/pantograph_core/test_parser.rb +107 -0
  187. data/pantograph_core/lib/pantograph_core/ui/disable_colors.rb +17 -0
  188. data/pantograph_core/lib/pantograph_core/ui/errors/pantograph_common_error.rb +19 -0
  189. data/pantograph_core/lib/pantograph_core/ui/errors/pantograph_crash.rb +11 -0
  190. data/pantograph_core/lib/pantograph_core/ui/errors/pantograph_error.rb +25 -0
  191. data/pantograph_core/lib/pantograph_core/ui/errors/pantograph_exception.rb +19 -0
  192. data/pantograph_core/lib/pantograph_core/ui/errors/pantograph_shell_error.rb +11 -0
  193. data/pantograph_core/lib/pantograph_core/ui/errors.rb +1 -0
  194. data/pantograph_core/lib/pantograph_core/ui/github_issue_inspector_reporter.rb +62 -0
  195. data/pantograph_core/lib/pantograph_core/ui/implementations/shell.rb +159 -0
  196. data/pantograph_core/lib/pantograph_core/ui/interface.rb +205 -0
  197. data/pantograph_core/lib/pantograph_core/ui/pantograph_runner.rb +276 -0
  198. data/pantograph_core/lib/pantograph_core/ui/ui.rb +26 -0
  199. data/pantograph_core/lib/pantograph_core/update_checker/changelog.rb +37 -0
  200. data/pantograph_core/lib/pantograph_core/update_checker/update_checker.rb +107 -0
  201. data/pantograph_core/lib/pantograph_core.rb +45 -0
  202. metadata +987 -0
@@ -0,0 +1,53 @@
1
+ module Pantograph
2
+ module Actions
3
+ class GitPullAction < Action
4
+ def self.run(params)
5
+ commands = []
6
+
7
+ unless params[:only_tags]
8
+ commands += ["git pull &&"]
9
+ end
10
+
11
+ commands += ["git fetch --tags"]
12
+
13
+ Actions.sh(commands.join(' '))
14
+ end
15
+
16
+ def self.description
17
+ "Executes a simple git pull command"
18
+ end
19
+
20
+ def self.available_options
21
+ [
22
+ PantographCore::ConfigItem.new(key: :only_tags,
23
+ description: "Simply pull the tags, and not bring new commits to the current branch from the remote",
24
+ is_string: false,
25
+ optional: true,
26
+ default_value: false,
27
+ verify_block: proc do |value|
28
+ UI.user_error!("Please pass a valid value for only_tags. Use one of the following: true, false") unless value.kind_of?(TrueClass) || value.kind_of?(FalseClass)
29
+ end)
30
+ ]
31
+ end
32
+
33
+ def self.authors
34
+ ["KrauseFx", "JaviSoto"]
35
+ end
36
+
37
+ def self.is_supported?(platform)
38
+ true
39
+ end
40
+
41
+ def self.example_code
42
+ [
43
+ 'git_pull',
44
+ 'git_pull(only_tags: true) # only the tags, no commits'
45
+ ]
46
+ end
47
+
48
+ def self.category
49
+ :source_control
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,52 @@
1
+ module Pantograph
2
+ module Actions
3
+ class GitSubmoduleUpdateAction < Action
4
+ def self.run(params)
5
+ commands = ["git submodule update"]
6
+ commands += ["--init"] if params[:init]
7
+ commands += ["--recursive"] if params[:recursive]
8
+ Actions.sh(commands.join(' '))
9
+ end
10
+
11
+ #####################################################
12
+ # @!group Documentation
13
+ #####################################################
14
+
15
+ def self.description
16
+ "Executes a git submodule command"
17
+ end
18
+
19
+ def self.available_options
20
+ [
21
+ PantographCore::ConfigItem.new(key: :recursive,
22
+ description: "Should the submodules be updated recursively",
23
+ type: Boolean,
24
+ default_value: false),
25
+ PantographCore::ConfigItem.new(key: :init,
26
+ description: "Should the submodules be initiated before update",
27
+ type: Boolean,
28
+ is_string: false,
29
+ default_value: false)
30
+ ]
31
+ end
32
+
33
+ def self.output
34
+ end
35
+
36
+ def self.return_value
37
+ end
38
+
39
+ def self.authors
40
+ ["braunico"]
41
+ end
42
+
43
+ def self.is_supported?(platform)
44
+ return true
45
+ end
46
+
47
+ def self.category
48
+ :source_control
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,74 @@
1
+ module Pantograph
2
+ module Actions
3
+ class GitTagExistsAction < Action
4
+ def self.run(params)
5
+ tag_ref = "refs/tags/#{params[:tag].shellescape}"
6
+ if params[:remote]
7
+ command = "git ls-remote -q --exit-code #{params[:remote_name].shellescape} #{tag_ref}"
8
+ else
9
+ command = "git rev-parse -q --verify #{tag_ref}"
10
+ end
11
+ exists = true
12
+ Actions.sh(
13
+ command,
14
+ log: PantographCore::Globals.verbose?,
15
+ error_callback: ->(result) { exists = false }
16
+ )
17
+ exists
18
+ end
19
+
20
+ #####################################################
21
+ # @!group Documentation
22
+ #####################################################
23
+
24
+ def self.description
25
+ "Checks if the git tag with the given name exists in the current repo"
26
+ end
27
+
28
+ def self.available_options
29
+ [
30
+ PantographCore::ConfigItem.new(key: :tag,
31
+ description: "The tag name that should be checked"),
32
+ PantographCore::ConfigItem.new(key: :remote,
33
+ description: "Whether to check remote. Defaults to `false`",
34
+ type: Boolean,
35
+ default_value: false,
36
+ optional: true),
37
+ PantographCore::ConfigItem.new(key: :remote_name,
38
+ description: "The remote to check. Defaults to `origin`",
39
+ default_value: 'origin',
40
+ optional: true)
41
+ ]
42
+ end
43
+
44
+ def self.return_value
45
+ "Boolean value whether the tag exists or not"
46
+ end
47
+
48
+ def self.output
49
+ [
50
+ ]
51
+ end
52
+
53
+ def self.authors
54
+ ["antondomashnev"]
55
+ end
56
+
57
+ def self.is_supported?(platform)
58
+ true
59
+ end
60
+
61
+ def self.example_code
62
+ [
63
+ 'if git_tag_exists(tag: "1.1.0")
64
+ UI.message("Found it 🚀")
65
+ end'
66
+ ]
67
+ end
68
+
69
+ def self.category
70
+ :source_control
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,262 @@
1
+ module Pantograph
2
+ module Actions
3
+ module SharedValues
4
+ GITHUB_API_STATUS_CODE = :GITHUB_API_STATUS_CODE
5
+ GITHUB_API_RESPONSE = :GITHUB_API_RESPONSE
6
+ GITHUB_API_JSON = :GITHUB_API_JSON
7
+ end
8
+
9
+ class GithubApiAction < Action
10
+ class << self
11
+ def run(params)
12
+ require 'json'
13
+
14
+ http_method = (params[:http_method] || 'GET').to_s.upcase
15
+ url = construct_url(params[:server_url], params[:path], params[:url])
16
+ headers = construct_headers(params[:api_token], params[:headers])
17
+ payload = construct_body(params[:body], params[:raw_body])
18
+ error_handlers = params[:error_handlers] || {}
19
+ secure = params[:secure]
20
+
21
+ response = call_endpoint(
22
+ url,
23
+ http_method,
24
+ headers,
25
+ payload,
26
+ secure
27
+ )
28
+
29
+ status_code = response[:status]
30
+ result = {
31
+ status: status_code,
32
+ body: response.body || "",
33
+ json: parse_json(response.body) || {}
34
+ }
35
+
36
+ if status_code.between?(200, 299)
37
+ UI.verbose("Response:")
38
+ UI.verbose(response.body)
39
+ UI.verbose("---")
40
+ yield(result) if block_given?
41
+ else
42
+ handled_error = error_handlers[status_code] || error_handlers['*']
43
+ if handled_error
44
+ handled_error.call(result)
45
+ else
46
+ UI.error("---")
47
+ UI.error("Request failed:\n#{http_method}: #{url}")
48
+ UI.error("Headers:\n#{headers}")
49
+ UI.error("---")
50
+ UI.error("Response:")
51
+ UI.error(response.body)
52
+ UI.user_error!("GitHub responded with #{status_code}\n---\n#{response.body}")
53
+ end
54
+ end
55
+
56
+ Actions.lane_context[SharedValues::GITHUB_API_STATUS_CODE] = result[:status]
57
+ Actions.lane_context[SharedValues::GITHUB_API_RESPONSE] = result[:body]
58
+ Actions.lane_context[SharedValues::GITHUB_API_JSON] = result[:json]
59
+
60
+ return result
61
+ end
62
+
63
+ #####################################################
64
+ # @!group Documentation
65
+ #####################################################
66
+
67
+ def description
68
+ "Call a GitHub API endpoint and get the resulting JSON response"
69
+ end
70
+
71
+ def details
72
+ [
73
+ "Calls any GitHub API endpoint. You must provide your GitHub Personal token (get one from [https://github.com/settings/tokens/new](https://github.com/settings/tokens/new)).",
74
+ "Out parameters provide the status code and the full response JSON if valid, otherwise the raw response body.",
75
+ "Documentation: [https://developer.github.com/v3](https://developer.github.com/v3)."
76
+ ].join("\n")
77
+ end
78
+
79
+ def available_options
80
+ [
81
+ PantographCore::ConfigItem.new(key: :server_url,
82
+ env_name: "FL_GITHUB_API_SERVER_URL",
83
+ description: "The server url. e.g. 'https://your.internal.github.host/api/v3' (Default: 'https://api.github.com')",
84
+ default_value: "https://api.github.com",
85
+ optional: true,
86
+ verify_block: proc do |value|
87
+ UI.user_error!("Please include the protocol in the server url, e.g. https://your.github.server/api/v3") unless value.include?("//")
88
+ end),
89
+ PantographCore::ConfigItem.new(key: :api_token,
90
+ env_name: "FL_GITHUB_API_TOKEN",
91
+ description: "Personal API Token for GitHub - generate one at https://github.com/settings/tokens",
92
+ sensitive: true,
93
+ code_gen_sensitive: true,
94
+ is_string: true,
95
+ default_value: ENV["GITHUB_API_TOKEN"],
96
+ default_value_dynamic: true,
97
+ optional: false),
98
+ PantographCore::ConfigItem.new(key: :http_method,
99
+ env_name: "FL_GITHUB_API_HTTP_METHOD",
100
+ description: "The HTTP method. e.g. GET / POST",
101
+ default_value: "GET",
102
+ optional: true,
103
+ verify_block: proc do |value|
104
+ unless %w(GET POST PUT DELETE HEAD CONNECT PATCH).include?(value.to_s.upcase)
105
+ UI.user_error!("Unrecognised HTTP method")
106
+ end
107
+ end),
108
+ PantographCore::ConfigItem.new(key: :body,
109
+ env_name: "FL_GITHUB_API_REQUEST_BODY",
110
+ description: "The request body in JSON or hash format",
111
+ is_string: false,
112
+ default_value: {},
113
+ optional: true),
114
+ PantographCore::ConfigItem.new(key: :raw_body,
115
+ env_name: "FL_GITHUB_API_REQUEST_RAW_BODY",
116
+ description: "The request body taken verbatim instead of as JSON, useful for file uploads",
117
+ is_string: true,
118
+ optional: true),
119
+ PantographCore::ConfigItem.new(key: :path,
120
+ env_name: "FL_GITHUB_API_PATH",
121
+ description: "The endpoint path. e.g. '/repos/:owner/:repo/readme'",
122
+ optional: true),
123
+ PantographCore::ConfigItem.new(key: :url,
124
+ env_name: "FL_GITHUB_API_URL",
125
+ description: "The complete full url - used instead of path. e.g. 'https://uploads.github.com/repos/pantograph...'",
126
+ optional: true,
127
+ verify_block: proc do |value|
128
+ UI.user_error!("Please include the protocol in the url, e.g. https://uploads.github.com") unless value.include?("//")
129
+ end),
130
+ PantographCore::ConfigItem.new(key: :error_handlers,
131
+ description: "Optional error handling hash based on status code, or pass '*' to handle all errors",
132
+ is_string: false,
133
+ default_value: {},
134
+ optional: true),
135
+ PantographCore::ConfigItem.new(key: :headers,
136
+ description: "Optional headers to apply",
137
+ is_string: false,
138
+ default_value: {},
139
+ optional: true),
140
+ PantographCore::ConfigItem.new(key: :secure,
141
+ env_name: "FL_GITHUB_API_SECURE",
142
+ description: "Optionally disable secure requests (ssl_verify_peer)",
143
+ type: Boolean,
144
+ default_value: true,
145
+ optional: true)
146
+ ]
147
+ end
148
+
149
+ def output
150
+ [
151
+ ['GITHUB_API_STATUS_CODE', 'The status code returned from the request'],
152
+ ['GITHUB_API_RESPONSE', 'The full response body'],
153
+ ['GITHUB_API_JSON', 'The parsed json returned from GitHub']
154
+ ]
155
+ end
156
+
157
+ def return_value
158
+ "A hash including the HTTP status code (:status), the response body (:body), and if valid JSON has been returned the parsed JSON (:json)."
159
+ end
160
+
161
+ def authors
162
+ ["tommeier"]
163
+ end
164
+
165
+ def example_code
166
+ [
167
+ 'result = github_api(
168
+ server_url: "https://api.github.com",
169
+ api_token: ENV["GITHUB_TOKEN"],
170
+ http_method: "GET",
171
+ path: "/repos/:owner/:repo/readme",
172
+ body: { ref: "master" }
173
+ )',
174
+ '# Alternatively call directly with optional error handling or block usage
175
+ GithubApiAction.run(
176
+ server_url: "https://api.github.com",
177
+ api_token: ENV["GITHUB_TOKEN"],
178
+ http_method: "GET",
179
+ path: "/repos/:owner/:repo/readme",
180
+ error_handlers: {
181
+ 404 => proc do |result|
182
+ UI.message("Something went wrong - I couldn\'t find it...")
183
+ end,
184
+ \'*\' => proc do |result|
185
+ UI.message("Handle all error codes other than 404")
186
+ end
187
+ }
188
+ ) do |result|
189
+ UI.message("JSON returned: #{result[:json]}")
190
+ end
191
+ '
192
+ ]
193
+ end
194
+
195
+ def is_supported?(platform)
196
+ true
197
+ end
198
+
199
+ def category
200
+ :source_control
201
+ end
202
+
203
+ private
204
+
205
+ def construct_headers(api_token, overrides)
206
+ require 'base64'
207
+ headers = { 'User-Agent' => 'pantograph-github_api' }
208
+ headers['Authorization'] = "Basic #{Base64.strict_encode64(api_token)}" if api_token
209
+ headers.merge(overrides || {})
210
+ end
211
+
212
+ def construct_url(server_url, path, url)
213
+ return_url = (server_url && path) ? File.join(server_url, path) : url
214
+
215
+ UI.user_error!("Please provide either `server_url` (e.g. https://api.github.com) and 'path' or full 'url' for GitHub API endpoint") unless return_url
216
+
217
+ return_url
218
+ end
219
+
220
+ def construct_body(body, raw_body)
221
+ body ||= {}
222
+
223
+ if raw_body
224
+ raw_body
225
+ elsif body.kind_of?(Hash)
226
+ body.to_json
227
+ elsif body.kind_of?(Array)
228
+ body.to_json
229
+ else
230
+ UI.user_error!("Please provide valid JSON, or a hash as request body") unless parse_json(body)
231
+ body
232
+ end
233
+ end
234
+
235
+ def parse_json(value)
236
+ JSON.parse(value)
237
+ rescue JSON::ParserError
238
+ nil
239
+ end
240
+
241
+ def call_endpoint(url, http_method, headers, body, secure)
242
+ require 'excon'
243
+
244
+ Excon.defaults[:ssl_verify_peer] = secure
245
+ middlewares = Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower] # allow redirect in case of repo renames
246
+
247
+ UI.verbose("#{http_method} : #{url}")
248
+
249
+ connection = Excon.new(url)
250
+ connection.request(
251
+ method: http_method,
252
+ headers: headers,
253
+ middlewares: middlewares,
254
+ body: body,
255
+ debug_request: PantographCore::Globals.verbose?,
256
+ debug_response: PantographCore::Globals.verbose?
257
+ )
258
+ end
259
+ end
260
+ end
261
+ end
262
+ end