Module: Terraspace::Cloud::Vcs::Commenter

Extended by:
Memoist
Included in:
Terraspace::CLI::Down, Terraspace::CLI::Plan, Terraspace::CLI::Up
Defined in:
lib/terraspace/cloud/vcs/commenter.rb

Instance Method Summary collapse

Instance Method Details

#able_to_comment?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 66

def able_to_comment?
  !!(vcs && vcs_vars[:full_repo] && vcs_vars[:pr_number])
end

#cloud_commentObject



18
19
20
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 18

def cloud_comment
  Terraspace::Cloud::Comment.new(@options.merge(stack: @mod.name, kind: kind))
end

#finalize_vars(local_git, vars) ⇒ Object

finalize branch_url since info is set from multiple layers



59
60
61
62
63
64
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 59

def finalize_vars(local_git, vars)
  if local_git.vcs_class # IE: LocalGit::Github
    vars[:branch_url] ||= local_git.vcs_class.new(vars).branch_url # IE: LocalGit::Github#branch_url
  end
  vars
end

#kindObject

stored on cloud: kind can be apply or destroy. its extra info



71
72
73
74
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 71

def kind
  is_destroy = Terraspace.command?("down") || @options[:args]&.include?('--destroy') || @options[:destroy]
  is_destroy ? "destroy" : "apply"
end

#pr_comment(record, cost) ⇒ Object

record is plan or update



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 6

def pr_comment(record, cost)
  return unless able_to_comment?

  resp = cloud_comment.get(record, cost)
  body = resp['data']['attributes']['body'] if resp
  if body
    vcs.comment(body)
  else
    logger.info "WARN: Unable to post a PR comment"
  end
end

#vcsObject



23
24
25
26
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 23

def vcs
  # IE: TerraspaceVcsGithub::Interface.new
  Terraspace::Cloud::Vcs.detect(full_repo: vcs_vars[:full_repo], pr_number: vcs_vars[:pr_number])
end

#vcs_varsObject

full_repo # env or .git * min required for pr url host # env or .git * min required for pr url branch_name # env or .git sha # env or .git dirty # env or .git pr_number # env * min required for pr url

commit_message # env build_system # env build_id # env build_number # env build_type # env

build_url commit_url branch_url pr_url



48
49
50
51
52
53
54
55
# File 'lib/terraspace/cloud/vcs/commenter.rb', line 48

def vcs_vars
  return {} unless Terraspace.cloud?
  ci_env = CiEnv.new
  local_git = LocalGit.new
  local_env = LocalEnv.new
  vars = local_git.vars.merge(ci_env.vars).merge(local_env.vars)
  finalize_vars(local_git, vars)
end