Module: Carson::Runtime::Abandon

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

Instance Method Summary collapse

Instance Method Details

#abandon!(target:, json_output: false) ⇒ Object



5
6
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
# File 'lib/carson/runtime/abandon.rb', line 5

def abandon!( target:, json_output: false )
	result = { command: "abandon", target: target }

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

	resolution = resolve_abandon_target( target: target )
	if resolution.nil?
		result[ :error ] = "no branch or pull request found for #{target}"
		result[ :recovery ] = "use a PR number, PR URL, or existing branch name"
		return abandon_finish( result: result, exit_code: EXIT_ERROR, json_output: json_output )
	end

	branch = resolution.fetch( :branch )
	pull_request = resolution.fetch( :pull_request )
	worktree = resolution.fetch( :worktree )

	result[ :branch ] = branch
	result[ :pr_number ] = pull_request&.fetch( :number, nil )
	result[ :pr_url ] = pull_request&.fetch( :url, nil )
	result[ :worktree_path ] = worktree&.path

	preflight = abandon_preflight_issue( branch: branch, worktree: worktree )
	unless preflight.nil?
		result[ :error ] = preflight.fetch( :error )
		result[ :recovery ] = preflight.fetch( :recovery )
		return abandon_finish( result: result, exit_code: preflight.fetch( :exit_code ), json_output: json_output )
	end

	if pull_request&.fetch( :state ) == "OPEN"
		close_exit = close_pull_request!( number: pull_request.fetch( :number ), result: result )
		return abandon_finish( result: result, exit_code: close_exit, json_output: json_output ) unless close_exit == EXIT_OK
		result[ :pull_request_closed ] = true
	else
		result[ :pull_request_closed ] = false
	end

	if worktree
		remove_exit = with_captured_output do
			worktree_remove!( worktree_path: worktree.path, skip_unpushed: true, json_output: false )
		end
		unless remove_exit == EXIT_OK
			result[ :error ] = "worktree cleanup failed for #{worktree.path}"
			result[ :recovery ] = "run carson worktree remove #{File.basename( worktree.path )}"
			return abandon_finish( result: result, exit_code: remove_exit, json_output: json_output )
		end

		result[ :worktree_removed ] = true
		result[ :branch_deleted ] = !local_branch_exists?( branch: branch )
		result[ :remote_deleted ] = !remote_branch_exists?( branch: branch )
	else
		branch_deleted, remote_deleted = delete_branch_refs!( branch: branch )
		result[ :worktree_removed ] = false
		result[ :branch_deleted ] = branch_deleted
		result[ :remote_deleted ] = remote_deleted
	end

	mark_delivery_abandoned!( branch: branch )
	result[ :summary ] = "abandoned delivery cleaned up"
	abandon_finish( result: result, exit_code: EXIT_OK, json_output: json_output )
end