Class: Makit::V1::Git::GitRepositoryServiceImpl

Inherits:
Makit::V1::Git::GitRepositoryService::Service show all
Defined in:
lib/makit/v1/git/git_repository_service_impl.rb

Overview

Implementation of the GitRepositoryService gRPC service

Instance Method Summary collapse

Instance Method Details

#create_repository_state(request, _unused_call) ⇒ Object

Create a new repository state



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 107

def create_repository_state(request, _unused_call)
  model = GitRepositoryModel.new
  
  model.is_git_repo = request.is_git_repo
  model.is_ci = request.is_ci
  model.is_detached = request.is_detached
  model.is_read_only = request.is_read_only
  model.is_clean = request.is_clean
  model.branch = request.branch
  model.commit_sha = request.commit_sha
  model.commit_message = request.commit_message
  model.commit_date = request.commit_date
  model.commit_author = request.commit_author
  model.commit_email = request.commit_email
  model.remote_url = request.remote_url
  
  model
end

#get_repository_state(request, _unused_call) ⇒ Object

Get current repository state



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 11

def get_repository_state(request, _unused_call)
  # Get the current repository state using direct git commands
  model = GitRepositoryModel.new
  
  # Basic repository checks
  model.is_git_repo = Dir.exist?(".git")
  model.is_ci = ENV["CI"] == "true"
  model.is_detached = `git status`.include?("detached")
  model.is_read_only = !model.is_git_repo || model.is_detached
  model.is_clean = `git status --porcelain`.empty?
  
  # File information
  model.unstaged_files.concat(`git status --porcelain`.split("\n"))
  model.untracked_files.concat(`git ls-files --others --exclude-standard`.split("\n"))
  model.tracked_file_infos.concat(get_file_infos_proto)
  model.untracked_file_infos.concat(get_untracked_file_infos_proto)
  
  # Commit information
  model.branch = `git branch --show-current`.strip
  model.commit_sha = `git rev-parse HEAD`.strip
  model.commit_message = `git log -1 --pretty=%B`.strip
  model.commit_date = `git log -1 --pretty=%cd`.strip
  model.commit_author = `git log -1 --pretty=%an`.strip
  model.commit_email = `git log -1 --pretty=%ae`.strip
  model.remote_url = `git remote get-url origin`.strip
  
  model
end

#load_repository_state(request, _unused_call) ⇒ Object

Load repository state from file

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 53

def load_repository_state(request, _unused_call)
  raise ArgumentError, "File does not exist: #{request.path}" unless File.exist?(request.path)
  
  content = File.read(request.path)
  json_data = JSON.parse(content, symbolize_names: true)
  convert_json_to_proto(json_data)
end

#merge_repository_states(request, _unused_call) ⇒ Object

Merge two repository states



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 79

def merge_repository_states(request, _unused_call)
  base = request.base_repository
  override = request.override_repository
  
  merged = GitRepositoryModel.new
  
  # Use override values if present and not empty, otherwise use base values
  merged.is_git_repo = override.is_git_repo || base.is_git_repo
  merged.is_ci = override.is_ci || base.is_ci
  merged.is_detached = override.is_detached || base.is_detached
  merged.is_read_only = override.is_read_only || base.is_read_only
  merged.is_clean = override.is_clean || base.is_clean
  merged.unstaged_files.concat(override.unstaged_files.any? ? override.unstaged_files.to_a : base.unstaged_files.to_a)
  merged.untracked_files.concat(override.untracked_files.any? ? override.untracked_files.to_a : base.untracked_files.to_a)
  merged.tracked_file_infos.concat(override.tracked_file_infos.any? ? override.tracked_file_infos.to_a : base.tracked_file_infos.to_a)
  merged.untracked_file_infos.concat(override.untracked_file_infos.any? ? override.untracked_file_infos.to_a : base.untracked_file_infos.to_a)
  merged.branch = (override.branch && !override.branch.empty?) ? override.branch : base.branch
  merged.commit_sha = (override.commit_sha && !override.commit_sha.empty?) ? override.commit_sha : base.commit_sha
  merged.commit_message = (override.commit_message && !override.commit_message.empty?) ? override.commit_message : base.commit_message
  merged.commit_date = (override.commit_date && !override.commit_date.empty?) ? override.commit_date : base.commit_date
  merged.commit_author = (override.commit_author && !override.commit_author.empty?) ? override.commit_author : base.commit_author
  merged.commit_email = (override.commit_email && !override.commit_email.empty?) ? override.commit_email : base.commit_email
  merged.remote_url = (override.remote_url && !override.remote_url.empty?) ? override.remote_url : base.remote_url
  
  merged
end

#save_repository_state(request, _unused_call) ⇒ Object

Save repository state to file

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 41

def save_repository_state(request, _unused_call)
  raise ArgumentError, "Model cannot be nil" if request.repository.nil?
  raise ArgumentError, "Path cannot be empty" if request.path.nil? || request.path.strip.empty?
  
  # Convert protobuf model to JSON and save
  json_data = convert_proto_to_json(request.repository)
  File.write(request.path, JSON.pretty_generate(json_data))
  
  Google::Protobuf::Empty.new
end

#update_repository_state(request, _unused_call) ⇒ Object

Update repository state

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 127

def update_repository_state(request, _unused_call)
  raise ArgumentError, "Model cannot be nil" if request.repository.nil?
  raise ArgumentError, "Updates cannot be nil" if request.updates.nil?
  
  model = GitRepositoryModel.new
  model.is_git_repo = request.repository.is_git_repo
  model.is_ci = request.repository.is_ci
  model.is_detached = request.repository.is_detached
  model.is_read_only = request.repository.is_read_only
  model.is_clean = request.repository.is_clean
  model.unstaged_files.concat(request.repository.unstaged_files.to_a)
  model.untracked_files.concat(request.repository.untracked_files.to_a)
  model.tracked_file_infos.concat(request.repository.tracked_file_infos.to_a)
  model.untracked_file_infos.concat(request.repository.untracked_file_infos.to_a)
  model.branch = request.repository.branch
  model.commit_sha = request.repository.commit_sha
  model.commit_message = request.repository.commit_message
  model.commit_date = request.repository.commit_date
  model.commit_author = request.repository.commit_author
  model.commit_email = request.repository.commit_email
  model.remote_url = request.repository.remote_url
  
  # Apply updates
  request.updates.each do |key, value|
    case key.to_sym
    when :is_git_repo
      model.is_git_repo = value == "true"
    when :is_ci
      model.is_ci = value == "true"
    when :is_detached
      model.is_detached = value == "true"
    when :is_read_only
      model.is_read_only = value == "true"
    when :is_clean
      model.is_clean = value == "true"
    when :branch
      model.branch = value
    when :commit_sha
      model.commit_sha = value
    when :commit_message
      model.commit_message = value
    when :commit_date
      model.commit_date = value
    when :commit_author
      model.commit_author = value
    when :commit_email
      model.commit_email = value
    when :remote_url
      model.remote_url = value
    end
  end
  
  model
end

#validate_repository_state(request, _unused_call) ⇒ Object

Validate repository state



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/makit/v1/git/git_repository_service_impl.rb', line 62

def validate_repository_state(request, _unused_call)
  errors = []
  
  if request.repository.nil?
    errors << "Model cannot be nil"
  else
    # Basic validation
    if request.repository.is_git_repo
      errors << "Branch cannot be empty for git repository" if request.repository.branch.nil? || request.repository.branch.strip.empty?
      errors << "Commit SHA cannot be empty for git repository" if request.repository.commit_sha.nil? || request.repository.commit_sha.strip.empty?
    end
  end
  
  ValidateRepositoryStateResponse.new(errors: errors)
end