Module: Carson::Runtime::Deliver

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

Instance Method Summary collapse

Instance Method Details

#deliver!(merge: false, title: nil, body_file: nil, json_output: false) ⇒ Object

Entry point for ‘carson deliver`. Pushes current branch, creates a PR if needed, reports the PR URL. With merge: true, also merges if CI passes and cleans up.



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
# File 'lib/carson/runtime/deliver.rb', line 12

def deliver!( merge: false, title: nil, body_file: nil, json_output: false )
	branch = current_branch
	main = config.main_branch
	remote = config.git_remote
	result = { command: "deliver", branch: branch }

	# Guard: cannot deliver from main.
	if branch == main
		result[ :error ] = "cannot deliver from #{main}"
		result[ :recovery ] = "git checkout -b <branch-name>"
		return deliver_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end

	# Step 1: push the branch.
	push_exit = push_branch!( branch: branch, remote: remote, result: result )
	return deliver_finish( result: result, exit_code: push_exit, json_output: json_output ) unless push_exit == EXIT_OK

	# Step 2: find or create the PR.
	pr_number, pr_url = find_or_create_pr!(
		branch: branch, title: title, body_file: body_file, result: result
	)
	if pr_number.nil?
		return deliver_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end

	result[ :pr_number ] = pr_number
	result[ :pr_url ] = pr_url

	# Without --merge, we are done.
	unless merge
		return deliver_finish( result: result, exit_code: EXIT_OK, json_output: json_output )
	end

	# Step 3: check CI status.
	ci_status = check_pr_ci( number: pr_number )
	result[ :ci ] = ci_status.to_s

	case ci_status
	when :pass
		# Continue to merge.
	when :pending
		result[ :recovery ] = "gh pr checks #{pr_number} --watch && carson deliver --merge"
		return deliver_finish( result: result, exit_code: EXIT_OK, json_output: json_output )
	when :fail
		result[ :recovery ] = "gh pr checks #{pr_number} — fix failures, push, then `carson deliver --merge`"
		return deliver_finish( result: result, exit_code: EXIT_BLOCK, json_output: json_output )
	else
		result[ :recovery ] = "gh pr checks #{pr_number}"
		return deliver_finish( result: result, exit_code: EXIT_OK, json_output: json_output )
	end

	# Step 4: merge.
	merge_exit = merge_pr!( number: pr_number, result: result )
	return deliver_finish( result: result, exit_code: merge_exit, json_output: json_output ) unless merge_exit == EXIT_OK

	result[ :merged ] = true

	# Step 5: sync main.
	sync_after_merge!( remote: remote, main: main )

	deliver_finish( result: result, exit_code: EXIT_OK, json_output: json_output )
end