Skip to content

Commit

Permalink
add branches discovery (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-exz authored Dec 3, 2024
1 parent 46f7731 commit ffaa6fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 24 additions & 1 deletion lib/bitbucket/bitbucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -72,4 +92,7 @@ def commits(project_key, repo_slug, since_commit = nil)

commits
end
def commit_count(commits)
commits.size
end
end
36 changes: 21 additions & 15 deletions lib/tasks/bitbucket.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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
Expand Down

0 comments on commit ffaa6fa

Please sign in to comment.