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



125
126
127
128
129
130
# File 'lib/dependabot/clients/codecommit.rb', line 125

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

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



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dependabot/clients/codecommit.rb', line 187

def commits(repo, branch_name = T.must(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



238
239
240
241
242
243
244
# File 'lib/dependabot/clients/codecommit.rb', line 238

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/dependabot/clients/codecommit.rb', line 256

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



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/dependabot/clients/codecommit.rb', line 283

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



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



141
142
143
144
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
# File 'lib/dependabot/clients/codecommit.rb', line 141

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



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

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



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

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dependabot/clients/codecommit.rb', line 208

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