[fastlane action] update commit status in GitHub

Updated
Apr 22, 2021 6:10 AM
Created
Apr 18, 2021 5:37 AM
Tags
iOSMemo
Keywords
Date

image
lane :something do
    github_commit_status(
      owner: "muukii",
      repo: "Brightroom",
      sha: last_git_commit[:commit_hash],
      state: "success",
      targetURL: "",
      description: "hello",
      context: "my-checks",
    )
  end

module Fastlane
  module Actions
    module SharedValues
    end

    class GithubCommitStatusAction < Action
      def self.run(params)
        token = params[:token]
        owner = params[:owner]
        repo = params[:repo]
        sha = params[:sha]
        state = params[:state]
        targetURL = params[:targetURL]
        description = params[:description]
        context = params[:context]

        github_client = Faraday::Connection.new(:url => "https://api.github.com/") do |builder|
          builder.use Faraday::Request::UrlEncoded
          # builder.use Faraday::Response::Logger
          builder.adapter Faraday.default_adapter
        end

        response = github_client.post do |req|
          req.url "/repos/#{owner}/#{repo}/statuses/#{sha}"
          req.headers["Authorization"] = "token #{token}"
          req.headers["Content-Type"] = "application/json"

          json = JSON.generate({
            "state" => state,
            "context" => context,
            "target_url" => targetURL,
            "description" => description,
          })

          req.body = json
        end
        JSON.parse(response.body)
      end

      #####################################################
      # @!group Documentation
      #####################################################

      def self.description
        "Updates the status of commit"
      end

      def self.details
        # Optional:
        # this is your chance to provide a more detailed description of this action
        "Updates the status of commit"
      end

      def self.available_options
        [
          FastlaneCore::ConfigItem.new(
            key: :token,
            env_name: "GITHUB_ACCESS_TOKEN", # The name of the environment variable
            description: "API Token for GithubCommitStatus", # a short description of this parameter
            verify_block: proc do |value|
              UI.user_error!("No API token for GithubCommitStatus given, pass using `api_token: 'token'`") unless (value and not value.empty?)
              # UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
            end,
          ),
          FastlaneCore::ConfigItem.new(
            key: :owner,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
              UI.user_error!("Invalid parameter") unless (value and not value.empty?)
              # UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
            end,
          ),
          FastlaneCore::ConfigItem.new(
            key: :repo,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
              UI.user_error!("Invalid parameter") unless (value and not value.empty?)
              # UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
            end,
          ),
          FastlaneCore::ConfigItem.new(
            key: :sha,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
              UI.user_error!("Invalid parameter") unless (value and not value.empty?)
              # UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
            end,
          ),
          FastlaneCore::ConfigItem.new(
            key: :state,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
              UI.user_error!("Invalid parameter") unless (value and not value.empty?)
              # UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
            end,
          ),
          FastlaneCore::ConfigItem.new(
            key: :targetURL,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
            end,
          ),
          FastlaneCore::ConfigItem.new(
            key: :description,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
            end,
          ),

          FastlaneCore::ConfigItem.new(
            key: :context,
            env_name: "", # The name of the environment variable
            description: "", # a short description of this parameter
            verify_block: proc do |value|
            end,
          ),
        ]
      end

      def self.output
        # Define the shared values you are going to provide
        # Example
        []
      end

      def self.return_value
        # If your method provides a return value, you can describe here what it does
      end

      def self.authors
        # So no one will ever forget your contribution to fastlane :) You are awesome btw!
        ["muukii"]
      end

      def self.is_supported?(platform)
        true
      end
    end
  end
end