Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Better reflect updates
Browse files Browse the repository at this point in the history
Now Portus will be able to track better updates on repositories and tags.
Moreover, updates will create activities too, and this will also be the case
for crono updates (whenever it makes sense).

Fixes #553

Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
  • Loading branch information
mssola committed Aug 12, 2016
1 parent d03cbb9 commit 89b9964
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
26 changes: 23 additions & 3 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def self.add_repo(event, namespace, repo, tag)
elsif repository.tags.exists?(name: tag)
# Update digest if the given tag already exists.
_, digest = Repository.id_and_digest_from_event(event, repository.full_name)
repository.tags.find_by(name: tag).update!(digest: digest)
tag = repository.tags.find_by(name: tag)
tag.update!(digest: digest, updated_at: Time.current)
repository.create_activity(:push, owner: actor, recipient: tag)
return
end

Expand Down Expand Up @@ -207,6 +209,8 @@ def self.create_or_update!(repo)

# Update digest of already existing tags.
def self.update_tags(client, repository, tags)
portus = User.find_by(username: "portus")

tags.each do |tag|
# Try to fetch the manifest digest of the tag.
begin
Expand All @@ -218,12 +222,21 @@ def self.update_tags(client, repository, tags)
end
next
end
repository.tags.find_by(name: tag).update!(digest: digest)

# Let's update the tag, if it really changed,
t = repository.tags.find_by(name: tag)
t.digest = digest
if t.changed.any?
t.save!
repository.create_activity(:push, owner: portus, recipient: t)
end
end
end

# Create new tags.
def self.create_tags(client, repository, author, tags)
portus = User.find_by(username: "portus")

tags.each do |tag|
# Try to fetch the manifest digest of the tag.
begin
Expand All @@ -233,7 +246,14 @@ def self.create_tags(client, repository, author, tags)
digest = ""
end

Tag.create!(name: tag, repository: repository, author: author, digest: digest, image_id: id)
t = Tag.create!(
name: tag,
repository: repository,
author: author,
digest: digest,
image_id: id
)
repository.create_activity(:push, owner: portus, recipient: t)
logger.tagged("catalog") { logger.info "Created the tag '#{tag}'." }
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/repositories/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
- else
span[title="sha256:#{tag.first.image_id}"]
= tag.first.image_id[0, 12]
td= tag.first.created_at
td= tag.first.updated_at
- else
tr
td
Expand All @@ -128,7 +128,7 @@
- else
span[title="sha256:#{tag.first.image_id}"]
= tag.first.image_id[0, 12]
td= tag.first.created_at
td= tag.first.updated_at

- if can_destroy?(@repository)
td
Expand Down
8 changes: 6 additions & 2 deletions spec/helpers/repositories_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ def update_registry!(catalog)

idx = 0

na = "<strong>Someone pushed </strong><a href=\"/namespaces/#{global}\">registry:5000</a> / "\
"<a href=\"/repositories/#{repo.id}\">repo:0.1</a>"
expectations = expectations.unshift na

# Changes because of the Catalog job.
expectations[3] = "<strong>#{nameo} pushed </strong><a href=\"/namespaces/#{global}\">"\
expectations[4] = "<strong>#{nameo} pushed </strong><a href=\"/namespaces/#{global}\">"\
"registry:5000</a> / <span>repo2:0.3</span>"
expectations[4] = "<strong>#{nameo} pushed </strong><span>namespace</span> / <span>repo"\
expectations[5] = "<strong>#{nameo} pushed </strong><span>namespace</span> / <span>repo"\
"3:0.4</span>"

# Push activities
Expand Down
6 changes: 4 additions & 2 deletions spec/jobs/catalog_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ def update_registry!(catalog)
job = CatalogJobMock.new
job.update_registry!([{ "name" => "repo", "tags" => ["0.1"] }])

# Two activities: push and then delete.
expect(PublicActivity::Activity.count).to eq 2
# Three activities: the original one, one more push for 0.1 and then the
# delete for latest.
expect(PublicActivity::Activity.count).to eq 3
expect(PublicActivity::Activity.first.parameters[:tag_name]).to eq "latest"
expect(PublicActivity::Activity.all[1].recipient.name).to eq "0.1"
expect(PublicActivity::Activity.last.key).to eq "repository.delete"
end
end
Expand Down
15 changes: 11 additions & 4 deletions spec/models/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,35 @@ def get_url(repo, tag)
end

context "adding an existing repo/tag" do
it "does not add a new activity when an already existing repo/tag already existed" do
it "adds a new activity when an already existing repo/tag already existed" do
event = { "actor" => { "name" => user.username } }

# First we create it, and make sure that it creates the activity.
expect do
Repository.add_repo(event, registry.global_namespace, repository_name, tag_name)
end.to change(PublicActivity::Activity, :count).by(1)

# And now it shouldn't create more activities.
# And now it should create another activities.
expect do
Repository.add_repo(event, registry.global_namespace, repository_name, tag_name)
end.to change(PublicActivity::Activity, :count).by(0)
end.to change(PublicActivity::Activity, :count).by(1)
end

it "updates the digest of an already existing tag" do
event = { "actor" => { "name" => user.username }, "target" => { "digest" => "foo" } }
Repository.add_repo(event, registry.global_namespace, repository_name, tag_name)
expect(Repository.find_by(name: repository_name).tags.first.digest).to eq("foo")

# Making sure that the updated_at column is set in the past.
tag = Repository.find_by(name: repository_name).tags.first
tag.update!(updated_at: 2.hours.ago)
ua = tag.updated_at

event["target"]["digest"] = "bar"
Repository.add_repo(event, registry.global_namespace, repository_name, tag_name)
expect(Repository.find_by(name: repository_name).tags.first.digest).to eq("bar")
tag = Repository.find_by(name: repository_name).tags.first
expect(tag.digest).to eq("bar")
expect(tag.updated_at).to_not eq(ua)
end
end

Expand Down

0 comments on commit 89b9964

Please sign in to comment.