class Fastlane::Actions::GithubJobStatusAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 58
def self.authors
  ["Justin Singer"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 70
def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :token,
      env_name: 'GITHUB_JOB_STATUS_TOKEN',
      description: 'OAuth token for :owner GitHub account',
      verify_block: proc { |value| UI.user_error! "Token must be specified" if value.empty? }
    ),
    FastlaneCore::ConfigItem.new(
      key: :owner,
      env_name: 'GITHUB_JOB_STATUS_OWNER',
      description: 'The github owner or username',
      verify_block: proc { |value| UI.user_error! "Owner must be specified" if value.empty? }
    ),
    FastlaneCore::ConfigItem.new(
      key: :repo,
      env_name: 'GITHUB_JOB_STATUS_REPO',
      description: 'The github repo',
      verify_block: proc { |value| UI.user_error! "Repo must be specified" if value.empty? }
    ),
    FastlaneCore::ConfigItem.new(
      key: :sha,
      env_name: 'GITHUB_JOB_STATUS_SHA',
      description: 'The github sha of the commit',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :job_name,
      env_name: 'GITHUB_JOB_STATUS_JOB_NAME',
      description: 'The string displayed next to the status indicator but before the description',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :description,
      env_name: 'GITHUB_JOB_STATUS_DESCRIPTION',
      description: 'The string displayed after job name',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :build_url,
      env_name: 'GITHUB_JOB_STATUS_BUILD_URL',
      description: 'The url of the build upon which we are reporting the status',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :state,
      env_name: 'GITHUB_JOB_STATUS_STATE',
      description: 'The state of a build; must be pending, success, error, or failure',
      verify_block: proc do |value|
        unless ['pending', 'success', 'error', 'failure'].include?(value)
          UI.user_error! "Invalid state '#{value}' given. State must be pending, success, error, or failure."
        end
      end
    )
  ]
end
description() click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 54
def self.description
  "Post the status of your test jobs to your pull requests"
end
description_for_state(state) click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 41
def self.description_for_state(state)
  case state
  when 'pending'
    'Job pending'
  when 'success'
    'Job succeeded'
  when 'error'
    'Job failed'
  when 'failure'
    'Job unstable'
  end
end
details() click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 66
def self.details
  "Uses github's status API (https://developer.github.com/v3/repos/statuses/) to display the status of continuous integration jobs directly on pull requests."
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 127
def self.is_supported?(platform)
  true
end
post(url, payload, headers) click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 37
def self.post(url, payload, headers)
  RestClient.post(url, payload, headers)
end
return_value() click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 62
def self.return_value
  "True if status posted sucessfully, False otherwise"
end
run(params) click to toggle source
# File lib/fastlane/plugin/github_job_status/actions/github_job_status_action.rb, line 6
def self.run(params)
  api = "https://api.github.com/repos"
  # if sha not provided then use last_commit
  sha = !params[:sha] ? Fastlane::Actions.last_git_commit_dict[:commit_hash] : params[:sha]
  url = "#{api}/#{params[:owner]}/#{params[:repo]}/statuses/#{sha}"

  headers = {
    'Authorization' => "token #{params[:token]}",
    'User-Agent' => 'fastlane',
    'content_type' => :json,
    'accept' => :json
  }

  payload = {
    state: params[:state],
    description: !params[:description] ? description_for_state(params[:state]) : params[:description]
  }
  context = params[:job_name]
  payload['context'] = context unless context.nil?
  target_url = params[:build_url]
  payload['target_url'] = target_url unless target_url.nil?

  begin
    post(url, payload.to_json, headers)
    true
  rescue RestClient::ExceptionWithResponse => e
    UI.error "Status failed to post to GitHub. Recieved the following response from the server: #{e.response}"
    false
  end
end