Sha256: f46c105ebd589911e3a3841f45fbce0c41fe85ba4efa22bf1986a7a118c2a9ff

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module Jets::Git
  class Github < Base
    def info
      info = {
        git_system: "github",
        git_branch: git_branch,
        git_sha: git_sha,
        git_dirty: false
        # git_message: nil,
        # git_version: nil,
      }
      info[:git_url] = git_url if git_url
      info
    end

    def git_branch
      if build_type == "pull_request"
        pr.dig("pull_request", "head", "ref")
      else # push
        ENV["GITHUB_REF_NAME"]
      end
    end

    def git_sha
      if build_type == "pull_request"
        pr.dig("pull_request", "head", "sha")
      else # push
        ENV["GITHUB_SHA"]
      end
    end

    def git_url
      host = ENV["GITHUB_SERVER_URL"] || "https://github.com"
      full_repo = ENV["GITHUB_REPOSITORY"]
      "#{host}/#{full_repo}"
    end

    # GitHub webhook JSON payload in file and path is set in GITHUB_EVENT_PATH
    def pr
      return {} unless ENV["GITHUB_EVENT_PATH"]
      JSON.load(IO.read(ENV["GITHUB_EVENT_PATH"]))
    end

    def build_type
      ENV["GITHUB_EVENT_NAME"]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jets-6.0.5 lib/jets/git/github.rb
jets-6.0.4 lib/jets/git/github.rb
jets-6.0.3 lib/jets/git/github.rb
jets-6.0.2 lib/jets/git/github.rb