Class: CfnGuardian::CodeCommit

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cfnguardian/codecommit.rb

Instance Method Summary collapse

Methods included from Logging

colors, included, logger, #logger, logger=

Constructor Details

#initialize(repo_name) ⇒ CodeCommit

Returns a new instance of CodeCommit.



9
10
11
12
# File 'lib/cfnguardian/codecommit.rb', line 9

def initialize(repo_name)
  @repo_name = repo_name
  @client = Aws::CodeCommit::Client.new()
end

Instance Method Details

#get_commit_history(branch, count) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cfnguardian/codecommit.rb', line 22

def get_commit_history(branch,count)
  history = []

  begin
    commit = get_last_commit(branch)
  rescue Aws::CodeCommit::Errors::BranchDoesNotExistException => e
    logger.error "Branch #{branch} does not exist in the #{@repo_name} repository"
    return []
  rescue Aws::CodeCommit::Errors::RepositoryDoesNotExistException => e
    logger.error "Respository #{@repo_name} does not exist in this AWS account or region"
    return []
  end
  
  count.times do
    
    resp = @client.get_commit({
      repository_name: @repo_name,
      commit_id: commit
    })
    
    time = Time.strptime(resp.commit.committer.date,'%s')
    
    history << {
      message: resp.commit.message,
      author: resp.commit.author.name,
      date: time.localtime.strftime("%d/%m/%Y %I:%M %p"),
      id: resp.commit.commit_id
    }
    
    if resp.commit.parents.any?
      commit = resp.commit.parents.first
    else
      break
    end
    
  end
  
  return history
end

#get_last_commit(branch = 'master') ⇒ Object



14
15
16
17
18
19
20
# File 'lib/cfnguardian/codecommit.rb', line 14

def get_last_commit(branch='master')
  resp = @client.get_branch({
    repository_name: @repo_name,
    branch_name: branch,
  })
  return resp.branch.commit_id
end