Class: Dependabot::Clients::CodeCommit

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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

Returns a new instance of CodeCommit.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dependabot/clients/codecommit.rb', line 47

def initialize(source, credentials)
  @source = source
  @cc_client =
    T.let(
      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,
      Aws::CodeCommit::Client
    )
end

Class Method Details

.for_source(source:, credentials:) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dependabot/clients/codecommit.rb', line 27

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



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

def branch(branch_name)
  T.cast(
    cc_client.get_branch(
      repository_name: source.unscoped_repo,
      branch_name: branch_name
    ),
    Seahorse::Client::Response
  )
end

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dependabot/clients/codecommit.rb', line 191

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

  result = T.cast(
    @cc_client.batch_get_commits(
      commit_ids: retrieved_commits,
      repository_name: repo
    ),
    Seahorse::Client::Response
  )

  # sort the results by date
  output = T.cast(result.data, Aws::CodeCommit::Types::BatchGetCommitsOutput)
  commits = output.commits
  commits&.sort_by! { |commit| commit.author&.date || "" }
  commits&.reverse!
  result
end

#create_branch(repo, branch_name, commit_id) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/dependabot/clients/codecommit.rb', line 250

def create_branch(repo, branch_name, commit_id)
  T.cast(
    cc_client.create_branch(
      repository_name: repo,
      branch_name: branch_name,
      commit_id: commit_id
    ),
    Seahorse::Client::Response
  )
end

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/dependabot/clients/codecommit.rb', line 271

def create_commit(
  branch_name,
  author_name,
  base_commit,
  commit_message,
  files
)
  T.cast(
    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
    ),
    Seahorse::Client::Response
  )
end

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



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/dependabot/clients/codecommit.rb', line 306

def create_pull_request(
  pr_name,
  target_branch,
  source_branch,
  pr_description
)
  T.cast(
    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 }
      ]
    ),
    Seahorse::Client::Response
  )
end

#fetch_commit(repo, branch) ⇒ Object



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

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dependabot/clients/codecommit.rb', line 145

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



74
75
76
77
78
# File 'lib/dependabot/clients/codecommit.rb', line 74

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

#fetch_file_contents(repo, commit, path) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/dependabot/clients/codecommit.rb', line 110

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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dependabot/clients/codecommit.rb', line 88

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

  T.cast(
    cc_client.get_folder(
      repository_name: repo,
      commit_specifier: commit,
      folder_path: actual_path
    ),
    Seahorse::Client::Response
  )
end

#pull_requests(repo, state, branch) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/dependabot/clients/codecommit.rb', line 218

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 = T.let([], T::Array[Seahorse::Client::Response])
  # 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 = T.cast(
      @cc_client.get_pull_request(
        pull_request_id: id
      ),
      Seahorse::Client::Response
    )
    output = T.cast(pr_hash.data, Aws::CodeCommit::Types::GetPullRequestOutput)
    # only include PRs from the referenced branch
    source_reference = T.must(output.pull_request.pull_request_targets[0]).source_reference
    result << pr_hash if source_reference.delete_prefix("refs/heads/") == branch.delete_prefix("refs/heads/")
  end
  result
end