Skip to content

Commit 1c3a7f8

Browse files
authored
Merge pull request #13985 from yeikel/fix-github-actions-comment-not-updated
Fix github actions versions comment not updated in an edge case
2 parents 85bbf0c + 4a60b95 commit 1c3a7f8

File tree

5 files changed

+85
-8
lines changed

5 files changed

+85
-8
lines changed

common/lib/dependabot/git_commit_checker.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,17 @@ def refs_for_tag_with_detail
246246

247247
sig { params(commit_sha: T.nilable(String)).returns(T.nilable(String)) }
248248
def most_specific_version_tag_for_sha(commit_sha)
249-
tags = local_tags.select { |t| t.commit_sha == commit_sha && version_class.correct?(t.name) }
250-
.sort_by { |t| version_class.new(t.name) }
249+
tags = local_tags_matching_sha(commit_sha)
251250
return if tags.empty?
252251

253252
tags[-1]&.name
254253
end
255254

255+
sig { params(commit_sha: T.nilable(String)).returns(T::Array[String]) }
256+
def most_specific_version_tags_for_sha(commit_sha)
257+
local_tags_matching_sha(commit_sha).map(&:name)
258+
end
259+
256260
sig { params(tags: T::Array[Dependabot::GitRef]).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
257261
def max_local_tag(tags)
258262
max_version_tag = tags.max_by { |t| version_from_tag(t) }
@@ -333,6 +337,12 @@ def allowed_versions(local_tags)
333337
.reject { |t| tag_is_prerelease?(t) && !wants_prerelease? }
334338
end
335339

340+
sig { params(commit_sha: T.nilable(String)).returns(T::Array[Dependabot::GitRef]) }
341+
def local_tags_matching_sha(commit_sha)
342+
local_tags.select { |t| t.commit_sha == commit_sha && version_class.correct?(t.name) }
343+
.sort_by { |t| version_class.new(t.name) }
344+
end
345+
336346
sig { params(version: T.any(String, Gem::Version)).returns(T::Boolean) }
337347
def pinned_ref_in_release?(version)
338348
raise "Not a git dependency!" unless git_dependency?

github_actions/lib/dependabot/github_actions/file_updater.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ def updated_version_comment(comment, old_ref, new_ref)
107107
git_checker = Dependabot::GitCommitChecker.new(dependency: dependency, credentials: credentials)
108108
return unless git_checker.ref_looks_like_commit_sha?(old_ref)
109109

110-
previous_version_tag = git_checker.most_specific_version_tag_for_sha(old_ref)
111-
return unless previous_version_tag # There's no tag for this commit
110+
previous_version_tags = git_checker.most_specific_version_tags_for_sha(old_ref)
111+
return unless previous_version_tags.any? # There's no tag for this commit
112112

113-
previous_version = version_class.new(previous_version_tag).to_s
114-
return unless comment.end_with? previous_version
113+
previous_version = previous_version_tags.map { |tag| version_class.new(tag).to_s }
114+
.find { |version| comment.end_with? version }
115+
return unless previous_version
115116

116117
new_version_tag = git_checker.most_specific_version_tag_for_sha(new_ref)
117118
return unless new_version_tag

github_actions/spec/dependabot/github_actions/file_updater_spec.rb

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,76 @@
474474

475475
it "doesn't update version comments" do
476476
allow(Dependabot::GitCommitChecker).to receive(:new).and_return(git_checker)
477-
allow(git_checker).to receive(:ref_looks_like_commit_sha?).and_return true
478-
allow(git_checker).to receive(:most_specific_version_tag_for_sha).and_return("v2.1.0", nil, nil)
477+
allow(git_checker).to receive_messages(
478+
ref_looks_like_commit_sha?: true,
479+
most_specific_version_tags_for_sha: ["v2.1.0"]
480+
)
481+
allow(git_checker).to receive(:most_specific_version_tag_for_sha).and_return(nil, nil)
479482
old_version = dependency.previous_requirements[1].dig(:source, :ref)
480483
expect(updated_workflow_file.content).not_to match(/@#{old_version}\s+#.*#{dependency.version}/)
481484
end
482485
end
483486
end
484487

488+
context "with pinned SHA hash matching multiple tags and version in comment different from latest matching tag" do
489+
let(:service_pack_url) do
490+
"https://github.com/github/codeql-action.git/info/refs" \
491+
"?service=git-upload-pack"
492+
end
493+
let(:workflow_file_body) do
494+
fixture("workflow_files", "pinned_sources_version_comments.yml")
495+
end
496+
let(:previous_version) { "3.29.5" }
497+
let(:new_ref) { "2d92b76c45b91eb80fc44c74ce3fce0ee94e8f9d" }
498+
let(:dependency) do
499+
Dependabot::Dependency.new(
500+
name: "github/codeql-action",
501+
version: "3.30.0",
502+
package_manager: "github_actions",
503+
previous_version: previous_version,
504+
previous_requirements: [{
505+
requirement: nil,
506+
groups: [],
507+
file: ".github/workflows/workflow.yml",
508+
source: {
509+
type: "git",
510+
url: "https://github.com/github/codeql-action",
511+
ref: "51f77329afa6477de8c49fc9c7046c15b9a4e79d",
512+
branch: nil
513+
},
514+
metadata: { declaration_string: "github/codeql-action@51f77329afa6477de8c49fc9c7046c15b9a4e79d" }
515+
}],
516+
requirements: [{
517+
requirement: nil,
518+
groups: [],
519+
file: ".github/workflows/workflow.yml",
520+
source: {
521+
type: "git",
522+
url: "https://github.com/github/codeql-action",
523+
ref: new_ref,
524+
branch: nil
525+
},
526+
metadata: { declaration_string: "github/codeql-action@#{new_ref}" }
527+
}]
528+
)
529+
end
530+
531+
before do
532+
stub_request(:get, service_pack_url)
533+
.to_return(
534+
status: 200,
535+
body: fixture("git", "upload_packs", "codeql-action"),
536+
headers: {
537+
"content-type" => "application/x-git-upload-pack-advertisement"
538+
}
539+
)
540+
end
541+
542+
it "updates SHA and comment" do
543+
expect(updated_workflow_file.content).to match(/@#{new_ref}\s+#.*#{dependency.version}/)
544+
end
545+
end
546+
485547
context "with a path based tag with semver" do
486548
let(:workflow_file_body) do
487549
fixture("workflow_files", "workflow_monorepo_path_based_semver.yml")
234 KB
Binary file not shown.

github_actions/spec/fixtures/workflow_files/pinned_sources_version_comments.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ jobs:
3333
# This is pinned to the version before v2.1.0, so the comment is incorrect.
3434
# Rather than failing to update, it will just leave the comment as-is.
3535
- uses: actions/checkout@85b1f35505da871133b65f059e96210c65650a8b # v2.1.0
36+
37+
# The pinned SHA matches both v3.29.5 and v3.29.7. It will be still replaced
38+
# properly.
39+
- uses: github/codeql-action@51f77329afa6477de8c49fc9c7046c15b9a4e79d # v3.29.5

0 commit comments

Comments
 (0)