Class: Dependabot::Clients::CodeCommit

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/clients/codecommit.rb

Defined Under Namespace

Classes: NotFound

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, credentials) ⇒ CodeCommit

Client #



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dependabot/clients/codecommit.rb', line 28

def initialize(source, credentials)
  @source = source
  @cc_client =
    if credentials
      Aws::CodeCommit::Client.new(
        access_key_id: credentials.fetch("username"),
        secret_access_key: credentials.fetch("password"),
        region: credentials.fetch("region")
      )
    else
      Aws::CodeCommit::Client.new
    end
end

Class Method Details

.for_source(source:, credentials:) ⇒ Object

Constructor methods #



15
16
17
18
19
20
21
22
# File 'lib/dependabot/clients/codecommit.rb', line 15

def self.for_source(source:, credentials:)
  credential =
    credentials
    .select { |cred| cred["type"] == "git_source" }
    .find { |cred| cred["region"] == source.hostname }

  new(source, credential)
end

Instance Method Details

#branch(branch_name) ⇒ Object



76
77
78
79
80
81
# File 'lib/dependabot/clients/codecommit.rb', line 76

def branch(branch_name)
  cc_client.get_branch(
    repository_name: source.unscoped_repo,
    branch_name: branch_name
  )
end

#commits(repo, branch_name = source.branch) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dependabot/clients/codecommit.rb', line 123

def commits(repo, branch_name = source.branch)
  retrieved_commits = fetch_commits(repo, branch_name, 5)

  result = @cc_client.batch_get_commits(
    commit_ids: retrieved_commits,
    repository_name: repo
  )

  # sort the results by date
  result.commits.sort! { |a, b| b.author.date <=> a.author.date }
  result
end

#create_branch(repo, branch_name, commit_id) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/dependabot/clients/codecommit.rb', line 158

def create_branch(repo, branch_name, commit_id)
  cc_client.create_branch(
    repository_name: repo,
    branch_name: branch_name,
    commit_id: commit_id
  )
end

#create_commit(branch_name, author_name, base_commit, commit_message, files) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dependabot/clients/codecommit.rb', line 166

def create_commit(branch_name, author_name, base_commit, commit_message,
                  files)
  cc_client.create_commit(
    repository_name: source.unscoped_repo,
    branch_name: branch_name,
    parent_commit_id: base_commit,
    author_name: author_name,
    commit_message: commit_message,
    put_files: files.map do |file|
      {
        file_path: file.path,
        file_mode: "NORMAL",
        file_content: file.content
      }
    end
  )
end

#create_pull_request(pr_name, target_branch, source_branch, pr_description) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dependabot/clients/codecommit.rb', line 184

def create_pull_request(pr_name, target_branch, source_branch,
                        pr_description)
  cc_client.create_pull_request(
    title: pr_name,
    description: pr_description,
    targets: [
      repository_name: source.unscoped_repo,
      source_reference: target_branch,
      destination_reference: source_branch
    ]
  )
end

#fetch_commit(repo, branch) ⇒ Object



42
43
44
45
46
47
# File 'lib/dependabot/clients/codecommit.rb', line 42

def fetch_commit(repo, branch)
  cc_client.get_branch(
    branch_name: branch,
    repository_name: repo
  ).branch.commit_id
end

#fetch_commits(repo, branch_name, result_count) ⇒ Object

work around b/c codecommit doesn’t have a ‘get all commits’ api..



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dependabot/clients/codecommit.rb', line 84

def fetch_commits(repo, branch_name, result_count)
  top_commit = fetch_commit(repo, branch_name)
  retrieved_commits = []
  pending_commits = []

  # get the parent commit ids from the latest commit on the default branch
  latest_commit = @cc_client.get_commit(
    repository_name: repo,
    commit_id: top_commit
  )

  # add the parent commit ids to the pending_commits array
  pending_commits.push(*latest_commit.commit.parents)

  # iterate over the array of pending commits and
  # get each of the corresponding parent commits
  until pending_commits.empty? || retrieved_commits.count > result_count
    commit_id = pending_commits[0]

    # get any parent commits from the provided commit
    parent_commits = @cc_client.get_commit(
      repository_name: repo,
      commit_id: commit_id
    )

    # remove the previously retrieved_commits
    # form the pending_commits array
    pending_commits.delete(commit_id)
    # add the commit id to the retrieved_commits array
    retrieved_commits << commit_id
    # add the retrieved parent commits to the pending_commits array
    pending_commits.push(*parent_commits.commit.parents)
  end

  retrieved_commits << top_commit
  result = retrieved_commits | pending_commits
  result
end

#fetch_default_branch(repo) ⇒ Object



49
50
51
52
53
# File 'lib/dependabot/clients/codecommit.rb', line 49

def fetch_default_branch(repo)
  cc_client.get_repository(
    repository_name: repo
  )..default_branch
end

#fetch_file_contents(repo, commit, path) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/dependabot/clients/codecommit.rb', line 66

def fetch_file_contents(repo, commit, path)
  cc_client.get_file(
    repository_name: repo,
    commit_specifier: commit,
    file_path: path
  ).file_content
  rescue Aws::CodeCommit::Errors::FileDoesNotExistException
    raise NotFound
end

#fetch_repo_contents(repo, commit = nil, path = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/dependabot/clients/codecommit.rb', line 55

def fetch_repo_contents(repo, commit = nil, path = nil)
  actual_path = path
  actual_path = "/" if path.to_s.empty?

  cc_client.get_folder(
    repository_name: repo,
    commit_specifier: commit,
    folder_path: actual_path
  )
end

#pull_requests(repo, state, branch) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dependabot/clients/codecommit.rb', line 136

def pull_requests(repo, state, branch)
  pull_request_ids = @cc_client.list_pull_requests(
    repository_name: repo,
    pull_request_status: state
  ).pull_request_ids

  result = []
  # list_pull_requests only gets us the pull request id
  # get_pull_request has all the info we need
  pull_request_ids.each do |id|
    pr_hash = @cc_client.get_pull_request(
      pull_request_id: id
    )
    # only include PRs from the referenced branch
    if pr_hash.pull_request.pull_request_targets[0]
              .source_reference.include? branch
      result << pr_hash
    end
  end
  result
end