From ffaa6fa7af597894e44730b37538d545dfdc4365 Mon Sep 17 00:00:00 2001 From: Nik Il Date: Tue, 3 Dec 2024 12:58:11 +0300 Subject: [PATCH] add branches discovery (#190) --- CHANGELOG.md | 5 +++++ lib/bitbucket/bitbucket.rb | 25 ++++++++++++++++++++++++- lib/tasks/bitbucket.rake | 36 +++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9efc44d..dc7206d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # Changelog +## 0.13.0 +### Improvements +- Better logging of sync git repos +- Added sync of non default branches + ## 0.12.1 ### Bugfixes - Layout and constraint fixes diff --git a/lib/bitbucket/bitbucket.rb b/lib/bitbucket/bitbucket.rb index 95d0cd8..89c7ab9 100644 --- a/lib/bitbucket/bitbucket.rb +++ b/lib/bitbucket/bitbucket.rb @@ -51,12 +51,32 @@ def repositories(project_key) repositories end + def branches(project_key, repo_slug) + branches = [] + start = 0 + limit = 25 + + loop do + response = @client.get("/rest/api/1.0/projects/#{project_key}/repos/#{repo_slug}/branches", start: start, limit: limit) + break unless response.success? + + data = JSON.parse(response.body) + branches.concat(data['values']) + + break if data['isLastPage'] + + start = data['nextPageStart'] + end + + branches + end + def commits(project_key, repo_slug, since_commit = nil) commits = [] start = 0 limit = 25 params = { start: start, limit: limit } - params[:since] = since_commit if since_commit + params[:until] = since_commit if since_commit loop do response = @client.get("/rest/api/1.0/projects/#{project_key}/repos/#{repo_slug}/commits", params) @@ -72,4 +92,7 @@ def commits(project_key, repo_slug, since_commit = nil) commits end + def commit_count(commits) + commits.size + end end diff --git a/lib/tasks/bitbucket.rake b/lib/tasks/bitbucket.rake index 6228914..e2ee611 100644 --- a/lib/tasks/bitbucket.rake +++ b/lib/tasks/bitbucket.rake @@ -10,20 +10,25 @@ namespace :bitbucket do print "Discovery repos of project: #{project['key']}\n" repositories = bitbucket.repositories(project['key']) repositories.each do |repository| - last_commit = BitbucketCommit.where(project_key: project['key'], - repo_slug: repository['slug']).order(date: :desc).first - if last_commit - print "Last commit of repository: #{repository['slug']} is #{last_commit.commit_id}\n" - commits = bitbucket.commits(project['key'], repository['slug'], last_commit.commit_id) - else - print "Discovery all commits of repository: #{repository['slug']}\n" - commits = bitbucket.commits(project['key'], repository['slug']) - end - commits.each do |commit| - if last_commit && commit['id'] == last_commit.commit_id - print "Commit in database: #{last_commit.commit_id} and #{commit['id']} equal, skip adding\n" - else - print "Saving commit #{commit['id']} of repository: #{project['key']} #{repository['slug']}\n" + print "Discovery commits of git repo #{project['key']}:#{repository['slug']}\n" + + branches = bitbucket.branches(project['key'], repository['slug']) + + branches.each do |branch| + print "Discovery all commits #{project['key']}:#{repository['slug']}:#{branch['id']}\n" + commits = bitbucket.commits(project['key'], repository['slug'], branch['id']) + + print "Discovered #{commits.count} commits, trying to save.\n" + + # Get all existing commit IDs from the database + existing_commit_ids = BitbucketCommit.where(project_key: project['key'], repo_slug: repository['slug']).pluck(:commit_id) + + # Filter out commits that already exist in the database + new_commits = commits.reject { |commit| existing_commit_ids.include?(commit['id']) } + + print "#{new_commits.count} new commits to save\n" + + new_commits.each do |commit| begin BitbucketCommit.create( commit_id: commit['id'], @@ -33,8 +38,9 @@ namespace :bitbucket do project_key: project['key'], repo_slug: repository['slug'] ) + print "Commit #{commit['id']} of repository: #{project['key']}:#{repository['slug']} saved\n" rescue ActiveRecord::RecordNotUnique - print "Commit #{commit['id']} already exists, skipping\n" + print "Commit #{commit['id']} of repository: #{project['key']}:#{repository['slug']} already exist\n" end end end