Skip to content

Commit

Permalink
Added more logic around skipping repos without new changes
Browse files Browse the repository at this point in the history
mr-exz committed Dec 6, 2024
1 parent c243550 commit 87178dd
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## 0.16.0
### Improvements
- Added more logic around skipping repos without new changes

## 0.15.0
### Improvements
- Added skip repo sync if last import was less 1 day ago
22 changes: 22 additions & 0 deletions lib/bitbucket/bitbucket.rb
Original file line number Diff line number Diff line change
@@ -225,4 +225,26 @@ def all_commits(project_key, repo_slug)
all_commits
end

def last_branch_created_date(project_key, repo_slug)
branches = branches(project_key, repo_slug)
return Time.at(0).utc if branches.empty?

last_branch = branches.max_by { |branch| branch['latestCommit'] }
commit_author_timestamp(project_key, repo_slug, last_branch['latestCommit'])
end

def last_pull_request_created_date(project_key, repo_slug)
pull_requests = pull_requests(project_key, repo_slug)
return Time.at(0).utc if pull_requests.empty?

last_pr = pull_requests.max_by { |pr| pr['createdDate'] }
Time.at(last_pr['createdDate'] / 1000).utc
end

def commit_author_timestamp(project_key, repo_slug, commit_id)
commit = fetch_data("/rest/api/1.0/projects/#{project_key}/repos/#{repo_slug}/commits/#{commit_id}")
return Time.at(0).utc if commit.empty? || !commit['authorTimestamp']

Time.at(commit['authorTimestamp'] / 1000).utc
end
end
22 changes: 19 additions & 3 deletions lib/tasks/bitbucket.rake
Original file line number Diff line number Diff line change
@@ -15,13 +15,29 @@ namespace :bitbucket do
logger.info("Discovery commits of git repo #{project['key']}:#{repository['slug']}")

# Get the most recent commit from the database
last_commit = BitbucketCommit.where(project_key: project['key'],
last_commit_created = BitbucketCommit.where(project_key: project['key'],
repo_slug: repository['slug']).order(created_at: :desc).first

last_commit_by_date = BitbucketCommit.where(project_key: project['key'],
repo_slug: repository['slug']).order(date: :desc).first

# Skip sync if the last commit was added less than 24 hours ago
if last_commit && last_commit.created_at > 24.hours.ago
logger.info("Skipping sync for repository: #{project['key']}:#{repository['slug']} as the last commit was added less than 24 hours ago")
if last_commit_created && last_commit_created.created_at < 24.hours.ago
logger.info("Skipping sync for repository: #{project['key']}:#{repository['slug']} as the last commit was added #{last_commit_created.created_at} less than 24 hours ago")
next
end

last_branch_date=bitbucket.last_branch_created_date(project['key'], repository['slug'])
logger.info("Last branch of repository: #{project['key']}:#{repository['slug']} is #{last_branch_date}")

last_pr_date=bitbucket.last_pull_request_created_date(project['key'], repository['slug'])
logger.info("Last pull request of repository: #{project['key']}:#{repository['slug']} is #{last_pr_date}")

if last_commit_by_date && (last_commit_by_date.date > last_branch_date || last_commit_by_date.date > last_pr_date)
logger.info("Skipping sync for repository: #{project['key']}:#{repository['slug']} as the last commit was added #{last_commit_by_date.date} after the last branch or pull request")
next
else
logger.info("Syncing commits for repository: #{project['key']}:#{repository['slug']} synce last commit was added #{last_commit_by_date.date} before the last branch or pull request")
end

# Get all existing commit IDs from the database

0 comments on commit 87178dd

Please sign in to comment.