Skip to content

Commit

Permalink
refactor commit users for local_git extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 4, 2023
1 parent e3e0900 commit ad72ac6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
74 changes: 50 additions & 24 deletions lib/datadog/ci/ext/environment/local_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ def git_commit_message
end

def git_commit_author_name
git_commit_users[:author_name]
author.name
end

def git_commit_author_email
git_commit_users[:author_email]
author.email
end

def git_commit_author_date
git_commit_users[:author_date]
author.date
end

def git_commit_committer_name
git_commit_users[:committer_name]
committer.name
end

def git_commit_committer_email
git_commit_users[:committer_email]
committer.email
end

def git_commit_committer_date
git_commit_users[:committer_date]
committer.date
end

def workspace_path
Expand All @@ -104,37 +104,63 @@ def exec_git_command(cmd)
out
end

def git_commit_users
return @commit_users if defined?(@commit_users)
def author
return @author if defined?(@author)

set_git_commit_users
@author
end

def committer
return @committer if defined?(@committer)

set_git_commit_users
@committer
end

def set_git_commit_users
# Get committer and author information in one command.
output = exec_git_command("git show -s --format='%an\t%ae\t%at\t%cn\t%ce\t%ct'")
unless output
Datadog.logger.debug(
"Unable to read git commit users: git command output is nil"
)
return @commit_users = {}
@author = @committer = NilUser.new
return
end

fields = output.split("\t").each(&:strip!)

@commit_users = {
author_name: fields[0],
author_email: fields[1],
# Because we can't get a reliable UTC time from all recent versions of git
# We have to rely on converting the date to UTC ourselves.
author_date: Time.at(fields[2].to_i).utc.to_datetime.iso8601,
committer_name: fields[3],
committer_email: fields[4],
# Because we can't get a reliable UTC time from all recent versions of git
# We have to rely on converting the date to UTC ourselves.
committer_date: Time.at(fields[5].to_i).utc.to_datetime.iso8601
}
author_name, author_email, author_timestamp,
committer_name, committer_email, committer_timestamp = output.split("\t").each(&:strip!)

@author = GitUser.new(author_name, author_email, author_timestamp)
@committer = GitUser.new(committer_name, committer_email, committer_timestamp)
rescue => e
Datadog.logger.debug(
"Unable to read git commit users: #{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
)
@commit_users = {}
@author = @committer = NilUser.new
end

class GitUser
attr_reader :name, :email, :timestamp

def initialize(name, email, timestamp)
@name = name
@email = email
@timestamp = timestamp
end

def date
return nil if timestamp.nil?

Time.at(timestamp.to_i).utc.to_datetime.iso8601
end
end

class NilUser < GitUser
def initialize
super(nil, nil, nil)
end
end
end
end
Expand Down
27 changes: 25 additions & 2 deletions sig/datadog/ci/ext/environment/local_git.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@ module Datadog
module Ext
module Environment
class LocalGit < Extractor
class GitUser
attr_reader name: String?
attr_reader email: String?
attr_reader timestamp: String?

@name: String?
@email: String?
@timestamp: String?

def initialize: (String? name, String? email, String? timestamp) -> void

def date: () -> String?
end

class NilUser < GitUser
def initialize: () -> void
end

private

@commit_users: Hash[Symbol, String?]
@author: GitUser
@committer: GitUser

def git_repository_url: () -> String?

Expand Down Expand Up @@ -33,7 +52,11 @@ module Datadog

def exec_git_command: (String cmd) -> String?

def git_commit_users: () -> Hash[Symbol, String?]
def author: () -> GitUser

def committer: () -> GitUser

def set_git_commit_users: () -> void
end
end
end
Expand Down

0 comments on commit ad72ac6

Please sign in to comment.