Class: Danger::CodeBuild

Inherits:
CI
  • Object
show all
Defined in:
lib/danger/ci_source/code_build.rb

Overview

### CI Setup

In CodeBuild, make sure to correctly forward CODEBUILD_BUILD_ID, CODEBUILD_SOURCE_VERSION, CODEBUILD_SOURCE_REPO_URL and DANGER_GITHUB_API_TOKEN. In CodeBuild with batch builds, make sure to correctly forward CODEBUILD_BUILD_ID, CODEBUILD_WEBHOOK_TRIGGER, CODEBUILD_SOURCE_REPO_URL, CODEBUILD_BATCH_BUILD_IDENTIFIER and DANGER_GITHUB_API_TOKEN.

### Token Setup

Add your ‘DANGER_GITHUB_API_TOKEN` to your project. Edit -> Environment -> Additional configuration -> Create a parameter

Instance Attribute Summary

Attributes inherited from CI

#pull_request_id, #repo_slug, #repo_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CI

available_ci_sources, inherited, #supports?

Constructor Details

#initialize(env) ⇒ CodeBuild

Returns a new instance of CodeBuild.



27
28
29
30
31
32
33
34
35
# File 'lib/danger/ci_source/code_build.rb', line 27

def initialize(env)
  self.repo_slug = self.class.extract_repo_slug(env)
  if env["CODEBUILD_BATCH_BUILD_IDENTIFIER"]
    self.pull_request_id = env["CODEBUILD_WEBHOOK_TRIGGER"].split("/")[1].to_i
  else
    self.pull_request_id = env["CODEBUILD_SOURCE_VERSION"].split("/")[1].to_i
  end
  self.repo_url = self.class.extract_repo_url(env)
end

Class Method Details

.extract_pr_url(env) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/danger/ci_source/code_build.rb', line 51

def self.extract_pr_url(env)
  if env["CODEBUILD_BATCH_BUILD_IDENTIFIER"]
    return nil unless env.key? "CODEBUILD_WEBHOOK_TRIGGER"
    return nil unless env.key? "CODEBUILD_SOURCE_REPO_URL"
    return nil unless env["CODEBUILD_WEBHOOK_TRIGGER"].split("/").length == 2

    event_type, pr_number = env["CODEBUILD_WEBHOOK_TRIGGER"].split("/")
    return nil unless event_type == "pr"
  else
    return nil unless env.key? "CODEBUILD_SOURCE_VERSION"
    return nil unless env.key? "CODEBUILD_SOURCE_REPO_URL"
    return nil unless env["CODEBUILD_SOURCE_VERSION"].split("/").length == 2

    _source_origin, pr_number = env["CODEBUILD_SOURCE_VERSION"].split("/")
  end
  github_repo_url = env["CODEBUILD_SOURCE_REPO_URL"].gsub(/\.git$/, "")

  "#{github_repo_url}/pull/#{pr_number}"
end

.extract_repo_slug(env) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/danger/ci_source/code_build.rb', line 37

def self.extract_repo_slug(env)
  return nil unless env.key? "CODEBUILD_SOURCE_REPO_URL"

  gh_host = env["DANGER_GITHUB_HOST"] || "github.com"

  env["CODEBUILD_SOURCE_REPO_URL"].gsub(%r{^.*?#{Regexp.escape(gh_host)}/(.*?)(\.git)?$}, '\1')
end

.extract_repo_url(env) ⇒ Object



45
46
47
48
49
# File 'lib/danger/ci_source/code_build.rb', line 45

def self.extract_repo_url(env)
  return nil unless env.key? "CODEBUILD_SOURCE_REPO_URL"

  env["CODEBUILD_SOURCE_REPO_URL"].gsub(/\.git$/, "")
end

.validates_as_ci?(env) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/danger/ci_source/code_build.rb', line 15

def self.validates_as_ci?(env)
  env.key? "CODEBUILD_BUILD_ID"
end

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/danger/ci_source/code_build.rb', line 19

def self.validates_as_pr?(env)
  !!self.extract_pr_url(env)
end

Instance Method Details

#supported_request_sourcesObject



23
24
25
# File 'lib/danger/ci_source/code_build.rb', line 23

def supported_request_sources
  @supported_request_sources ||= [Danger::RequestSources::GitHub]
end