Class: GithubMergeSign::PullRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/github_merge_sign.rb

Constant Summary collapse

BASE_URL =
'https://api.github.com/repos'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(pr_number) ⇒ PullRequest

Returns a new instance of PullRequest.



11
12
13
# File 'lib/github_merge_sign.rb', line 11

def initialize(pr_number)
  @pr_number = pr_number
end

Instance Method Details

#commit_messageObject



48
49
50
51
52
53
54
# File 'lib/github_merge_sign.rb', line 48

def commit_message
  <<-EOT
Merge pull request ##{@pr_number} from #{details.fetch('head').fetch('user').fetch('login')}/#{head_ref}

#{details.fetch('title')}
  EOT
end

#external_pr?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/github_merge_sign.rb', line 31

def external_pr?
  details.fetch("base").fetch("repo").fetch("full_name") !=
    details.fetch("head").fetch("repo").fetch("full_name")
end

#head_commit_idObject



40
41
42
# File 'lib/github_merge_sign.rb', line 40

def head_commit_id
  details.fetch("head").fetch("sha")
end

#head_refObject



44
45
46
# File 'lib/github_merge_sign.rb', line 44

def head_ref
  details.fetch("head").fetch("ref")
end

#merge!Object



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
# File 'lib/github_merge_sign.rb', line 56

def merge!
  raise "PR is not open" unless open?
  raise "PR is not mergeable" unless mergeable?
  raise "PR has not passed all status checks" unless status_checks_passed?

  # Check working directorty is clean
  unless Kernel.system('git diff --quiet --exit-code HEAD')
    raise "Working directory is not clean. Aborting..."
  end

  info "Checking out and updating #{target_branch}"
  execute_command("git checkout #{target_branch}")
  execute_command("git pull --ff-only origin #{target_branch}")

  if external_pr?
    info "Fetching commits from PR #{@pr_number}"
    execute_command("git fetch origin refs/pull/#{@pr_number}/head")
  end

  info "Merging PR with commit message:\n#{commit_message}"
  execute_command('git', 'merge', '--no-ff', '-S', '-m', commit_message, head_commit_id)

  info "Pushing to origin"
  execute_command("git push origin #{target_branch}")

  unless external_pr?
    info "Deleting remote branch #{head_ref}"
    execute_command("git push origin :#{head_ref}")
  end

  info "Done."
end

#mergeable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/github_merge_sign.rb', line 23

def mergeable?
  !! details.fetch("mergeable")
end

#open?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/github_merge_sign.rb', line 19

def open?
  details.fetch("state") == "open"
end

#repoObject



15
16
17
# File 'lib/github_merge_sign.rb', line 15

def repo
  @repo ||= read_repo_from_git
end

#status_checks_passed?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/github_merge_sign.rb', line 27

def status_checks_passed?
  status.fetch("state") == "success" || status.fetch("statuses").empty?
end

#target_branchObject



36
37
38
# File 'lib/github_merge_sign.rb', line 36

def target_branch
  details.fetch("base").fetch("ref")
end