Module: Carson::Runtime::Recover

Included in:
Carson::Runtime
Defined in:
lib/carson/runtime/recover.rb

Constant Summary collapse

GOVERNANCE_SURFACE_PREFIXES =
%w[ .github/ config/.github/ ].freeze

Instance Method Summary collapse

Instance Method Details

#recover!(check_name:, json_output: false) ⇒ Object



7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/carson/runtime/recover.rb', line 7

def recover!( check_name:, json_output: false )
	result = {
		command: "recover",
		branch: current_branch,
		check: check_name,
		main_branch: config.main_branch
	}

	if current_branch == config.main_branch
		result[ :error ] = "cannot recover from #{config.main_branch}"
		result[ :recovery ] = "carson worktree create <name>"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	if working_tree_dirty?
		result[ :error ] = "working tree is dirty"
		result[ :recovery ] = "commit or discard local changes, then rerun carson recover --check #{check_name.inspect}"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	unless gh_available?
		result[ :error ] = "gh CLI is required for carson recover"
		result[ :recovery ] = "install and authenticate gh, then retry"
		return recover_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end

	delivery = ledger.active_delivery( repo_path: repository_record.path, branch_name: current_branch )
	if delivery.nil?
		result[ :error ] = "no active delivery found for #{current_branch}"
		result[ :recovery ] = "carson deliver"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	result[ :pr_number ] = delivery.pull_request_number
	result[ :pr_url ] = delivery.pull_request_url

	pull_request = recover_pull_request_details( number: delivery.pull_request_number )
	result[ :pr_url ] = pull_request.fetch( :url )

	if pull_request.fetch( :state ) != "OPEN"
		result[ :error ] = "pull request ##{delivery.pull_request_number} is not open"
		result[ :recovery ] = "carson status"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	if pull_request.fetch( :branch ) != current_branch
		result[ :error ] = "pull request ##{delivery.pull_request_number} belongs to #{pull_request.fetch( :branch )}, not #{current_branch}"
		result[ :recovery ] = "checkout #{pull_request.fetch( :branch )} or rerun carson deliver for #{current_branch}"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	if pull_request.fetch( :head_sha ) != current_head
		result[ :error ] = "pull request ##{delivery.pull_request_number} head no longer matches local #{current_branch}"
		result[ :recovery ] = "push the current branch with carson deliver, then retry"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	relation = recovery_governance_surface_report( base_branch: pull_request.fetch( :base_branch ) )
	if relation.fetch( :status ) == "error"
		result[ :error ] = relation.fetch( :error )
		result[ :recovery ] = "git diff --name-only #{pull_request.fetch( :base_branch )}...HEAD"
		return recover_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end
	result[ :changed_files ] = relation.fetch( :files )

	unless relation.fetch( :related )
		result[ :error ] = "branch does not touch the governance surface for #{check_name}"
		result[ :recovery ] = "update the branch to repair .github/ or config/.github/, then rerun carson recover --check #{check_name.inspect}"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	baseline = default_branch_ci_baseline_report
	result[ :baseline ] = {
		default_branch: baseline.fetch( :default_branch, config.main_branch ),
		head_sha: baseline[ :head_sha ],
		status: baseline.fetch( :status ),
		check_name: check_name
	}
	if baseline.fetch( :status ) == "skipped"
		result[ :error ] = "unable to verify the default-branch baseline: #{baseline.fetch( :skip_reason )}"
		result[ :recovery ] = "run carson audit after fixing GitHub access"
		return recover_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end

	baseline_entry = recovery_baseline_entry( baseline: baseline, check_name: check_name )
	if baseline_entry.nil?
		result[ :error ] = "#{check_name} is not red on #{baseline.fetch( :default_branch, config.main_branch )}"
		result[ :recovery ] = "run carson audit to confirm the baseline check state"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	review = check_pr_review(
		number: delivery.pull_request_number,
		branch: current_branch,
		pr_url: delivery.pull_request_url
	)
	result[ :review ] = review
	review_issue = recovery_review_issue( review: review, check_name: check_name )
	unless review_issue.nil?
		result[ :error ] = review_issue.fetch( :error )
		result[ :recovery ] = review_issue.fetch( :recovery )
		return recover_finish(
			result: result,
			exit_code: review_issue.fetch( :exit_code ),
			json_output: json_output
		)
	end

	checks = recover_required_pr_checks_report( number: delivery.pull_request_number )
	result[ :checks ] = checks
	if checks.fetch( :status ) == "error"
		result[ :error ] = checks.fetch( :error )
		result[ :recovery ] = "gh pr checks #{delivery.pull_request_number} --required"
		return recover_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end

	other_gate_issue = recovery_other_required_check_issue( checks: checks, check_name: check_name )
	unless other_gate_issue.nil?
		result[ :error ] = other_gate_issue.fetch( :error )
		result[ :recovery ] = other_gate_issue.fetch( :recovery )
		return recover_finish(
			result: result,
			exit_code: other_gate_issue.fetch( :exit_code ),
			json_output: json_output
		)
	end

	pr_state = pull_request_state( number: delivery.pull_request_number )
	merge_issue = recover_mergeability_issue( pr_state: pr_state )
	unless merge_issue.nil?
		result[ :error ] = merge_issue
		result[ :recovery ] = "resolve the merge conflict, then rerun carson recover --check #{check_name.inspect}"
		return recover_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	end

	prepared = ledger.update_delivery(
		delivery: delivery,
		status: "integrating",
		summary: "recovering #{check_name} into #{config.main_branch}"
	)
	merge_exit = recover_merge_pr!(
		number: prepared.pull_request_number,
		owner: pull_request.fetch( :owner ),
		repo: pull_request.fetch( :repo ),
		head_sha: pull_request.fetch( :head_sha ),
		result: result
	)

	if merge_exit == EXIT_OK
		event = ledger.send(
			:record_recovery_event,
			repository: repository_record,
			branch_name: current_branch,
			pr_number: prepared.pull_request_number,
			pr_url: prepared.pull_request_url,
			check_name: check_name,
			default_branch: baseline.fetch( :default_branch, config.main_branch ),
			default_branch_sha: baseline.fetch( :head_sha ),
			pr_sha: pull_request.fetch( :head_sha ),
			actor: recovery_actor,
			merge_method: result.fetch( :merge_method ),
			status: "integrated",
			summary: "recovered #{check_name} into #{config.main_branch}"
		)

		integrated = ledger.update_delivery(
			delivery: prepared,
			status: "integrated",
			integrated_at: Time.now.utc.iso8601,
			summary: "recovered #{check_name} into #{config.main_branch}"
		)
		sync_after_merge!( remote: config.git_remote, main: config.main_branch, result: result )
		result[ :delivery ] = delivery_payload( delivery: integrated )
		result[ :recovery_event ] = event
		result[ :summary ] = integrated.summary
		result[ :next_step ] = deliver_next_step( delivery: integrated, result: result )
		return recover_finish( result: result, exit_code: EXIT_OK, json_output: json_output )
	end

	gated = ledger.update_delivery(
		delivery: prepared,
		status: "gated",
		cause: "policy",
		summary: result.fetch( :error, "recovery merge failed" )
	)
	result[ :delivery ] = delivery_payload( delivery: gated )
	result[ :summary ] = gated.summary
	result[ :next_step ] = "carson status"
	recover_finish( result: result, exit_code: merge_exit, json_output: json_output )
rescue StandardError => exception
	result[ :error ] = exception.message
	result[ :recovery ] = "carson status"
	recover_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
end