Class: Napa::Deploy

Inherits:
Object
  • Object
show all
Defined in:
lib/napa/deploy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, revision: nil, force: false, github_repo: nil, github_token: nil) ⇒ Deploy

Returns a new instance of Deploy.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/napa/deploy.rb', line 8

def initialize(environment, revision: nil, force: false, github_repo: nil, github_token: nil)
  Napa.load_environment

  @github_repo  = github_repo || ENV['GITHUB_REPO']
  @github_token = github_token || ENV['GITHUB_OAUTH_TOKEN']
  @environment  = environment
  @revision     = revision || local_head_revision
  @force        = force

  @errors       = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/napa/deploy.rb', line 6

def errors
  @errors
end

#github_loginObject (readonly)

Returns the value of attribute github_login.



6
7
8
# File 'lib/napa/deploy.rb', line 6

def 
  @github_login
end

Instance Method Details

#any_local_uncommited_changes?Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/napa/deploy.rb', line 88

def any_local_uncommited_changes?
  changes = local_repo_status.changed.collect{|change| change[0]}
  @errors << "#{changes.to_sentence} have been changed and not committed." if changes.any?
end

#any_local_untracked_files?Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/napa/deploy.rb', line 93

def any_local_untracked_files?
  changes = local_repo_status.untracked.collect{|change| change[0]}
  @errors << "#{changes.to_sentence} file(s) have been added and are not tracked." if changes.any?
end

#deploy!Object



20
21
22
23
24
25
26
27
# File 'lib/napa/deploy.rb', line 20

def deploy!
  if deployable?
    set_github_tag
    "#{@revision} tagged as #{@environment} by #{@github_login} at #{Time.now.to_s(:long)}"
  else
    "Deploy error(s): #{@errors.join(' --- ')}"
  end
end

#deployable?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/napa/deploy.rb', line 72

def deployable?
  revision_exists_on_github?
  any_local_uncommited_changes?
  any_local_untracked_files?

  @errors.empty? || @force
end

#github_clientObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/napa/deploy.rb', line 54

def github_client
  return @github_client if @github_client
  @errors << "ENV['GITHUB_REPO'] is not defined" if @github_repo.nil?
  @errors << "ENV['GITHUB_OAUTH_TOKEN'] is not defined" if @github_token.nil?

  client = Octokit::Client.new(access_token: @github_token)
  begin
    @github_login = client.
    return @github_client = client
  rescue Octokit::Unauthorized
    @errors << "Access denied for GITHUB_OAUTH_TOKEN"
  end
end

#local_head_revisionObject



68
69
70
# File 'lib/napa/deploy.rb', line 68

def local_head_revision
  local_repo.object('HEAD').sha
end

#local_repoObject



46
47
48
# File 'lib/napa/deploy.rb', line 46

def local_repo
  @local_repo ||= Git.open('.')
end

#local_repo_statusObject



50
51
52
# File 'lib/napa/deploy.rb', line 50

def local_repo_status
  @local_repo_status ||= local_repo.status
end

#revision_exists_on_github?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/napa/deploy.rb', line 80

def revision_exists_on_github?
  begin
    github_client.commit(@github_repo, @revision)
  rescue Octokit::NotFound
    @errors << "Revision #{@revision} does not exist on #{@github_repo}, make sure you've merged your changes."
  end
end

#set_github_tagObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/napa/deploy.rb', line 29

def set_github_tag
  begin
    github_client.update_ref(
      @github_repo,
      "tags/#{@environment}",
      @revision,
      @force
    )
  rescue Octokit::UnprocessableEntity
    github_client.create_ref(
      @github_repo,
      "tags/#{@environment}",
      @revision
    )
  end
end