diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac3bff..2cfbf8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/bitbucket/bitbucket.rb b/lib/bitbucket/bitbucket.rb index 069adfb..0ac8cf4 100644 --- a/lib/bitbucket/bitbucket.rb +++ b/lib/bitbucket/bitbucket.rb @@ -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 diff --git a/lib/tasks/bitbucket.rake b/lib/tasks/bitbucket.rake index 222e413..2b661b5 100644 --- a/lib/tasks/bitbucket.rake +++ b/lib/tasks/bitbucket.rake @@ -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