Class: CaptiveRelease::GithubDispatch

Inherits:
Object
  • Object
show all
Includes:
GithubHttp
Defined in:
lib/captive_release/github_dispatch.rb

Constant Summary collapse

GITHUB_API =
"https://api.github.com"

Constants included from GithubHttp

CaptiveRelease::GithubHttp::MAX_REDIRECTS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GithubHttp

#github_post, #parse_remote

Constructor Details

#initialize(workflow_file:, ref:, inputs:, remote_url:, token:) ⇒ GithubDispatch

Returns a new instance of GithubDispatch.



17
18
19
20
21
22
23
# File 'lib/captive_release/github_dispatch.rb', line 17

def initialize(workflow_file:, ref:, inputs:, remote_url:, token:)
  @workflow_file = workflow_file
  @ref           = ref
  @inputs        = inputs
  @remote_url    = remote_url
  @token         = token
end

Class Method Details

.dispatch(workflow_file:, ref:, inputs:, remote_url:, token:) ⇒ Object



13
14
15
# File 'lib/captive_release/github_dispatch.rb', line 13

def self.dispatch(workflow_file:, ref:, inputs:, remote_url:, token:)
  new(workflow_file: workflow_file, ref: ref, inputs: inputs, remote_url: remote_url, token: token).dispatch
end

Instance Method Details

#dispatchObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/captive_release/github_dispatch.rb', line 25

def dispatch
  owner, repo = parse_remote(@remote_url)
  uri = URI("#{GITHUB_API}/repos/#{owner}/#{repo}/actions/workflows/#{@workflow_file}/dispatches")

  payload = JSON.generate({ "ref" => @ref, "inputs" => @inputs })
  response = github_post(uri, @token, payload)

  unless response.code == "204"
    data = JSON.parse(response.body) rescue {}
    raise "GitHub API error (#{response.code}): #{data["message"] || response.body}"
  end

  "https://github.com/#{owner}/#{repo}/actions"
end