Class: GitReview::Provider::Github

Inherits:
Base
  • Object
show all
Includes:
Helpers
Defined in:
lib/git-review/provider/github.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #source_repo

Instance Method Summary collapse

Methods inherited from Base

#get_request_by_number, #initialize, instance, #login, #method_missing, #repo_info_from_config, #respond_to?, #update

Constructor Details

This class inherits a constructor from GitReview::Provider::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GitReview::Provider::Base

Instance Method Details

#comments_count(request) ⇒ Object

get the number of comments, including comments on commits



143
144
145
146
147
148
# File 'lib/git-review/provider/github.rb', line 143

def comments_count(request)
  issue_c = request.comments + request.review_comments
  commits_c = client.pull_commits(source_repo, request.number).
      inject(0) { |sum, c| sum + c.commit.comment_count }
  issue_c + commits_c
end

#commit_discussion(number) ⇒ Object



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
# File 'lib/git-review/provider/github.rb', line 90

def commit_discussion(number)
  pull_commits = client.pull_commits(source_repo, number)
  repo = client.pull_request(source_repo, number).head.repo.full_name
  discussion = ["Commits on pull request:\n\n"]
  discussion += pull_commits.collect { |commit|
    # commit message
    name = commit.committer.
    output = "\e[35m#{name}\e[m "
    output << "committed \e[36m#{commit.sha[0..6]}\e[m "
    output << "on #{commit.commit.committer.date.review_time}"
    output << ":\n#{''.rjust(output.length + 1, "-")}\n"
    output << "#{commit.commit.message}"
    output << "\n\n"
    result = [output]

    # comments on commit
    comments = client.commit_comments(repo, commit.sha)
    result + comments.collect { |comment|
      name = comment.user.
      output = "\e[35m#{name}\e[m "
      output << "added a comment to \e[36m#{commit.sha[0..6]}\e[m"
      output << " on #{comment.created_at.review_time}"
      unless comment.created_at == comment.updated_at
        output << " (updated on #{comment.updated_at.review_time})"
      end
      output << ":\n#{''.rjust(output.length + 1, "-")}\n"
      output << comment.body
      output << "\n\n"
    }
  }
  discussion.compact.flatten unless discussion.empty?
end

#configure_accessString

Returns Authenticated username.

Returns:

  • (String)

    Authenticated username



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/git-review/provider/github.rb', line 17

def configure_access
  if settings.oauth_token && settings.username
    @client = Octokit::Client.new(
      login: settings.username,
      access_token: settings.oauth_token,
      auto_traversal: true
    )

    @client.
  else
    configure_oauth
    configure_access
  end
end

#current_requests(repo = source_repo) ⇒ Object

an alias to pull_requests



49
50
51
# File 'lib/git-review/provider/github.rb', line 49

def current_requests(repo=source_repo)
  client.pull_requests(repo)
end

#current_requests_full(repo = source_repo) ⇒ Object

a more detailed collection of requests



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/git-review/provider/github.rb', line 54

def current_requests_full(repo=source_repo)
  threads = []
  requests = []
  client.pull_requests(repo).each do |req|
    threads << Thread.new {
      requests << client.pull_request(repo, req.number)
    }
  end
  threads.each { |t| t.join }
  requests
end

#discussion(number) ⇒ Object

show discussion for a request



151
152
153
154
# File 'lib/git-review/provider/github.rb', line 151

def discussion(number)
  commit_discussion(number) +
  issue_discussion(number)
end

#issue_discussion(number) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/git-review/provider/github.rb', line 123

def issue_discussion(number)
  comments = client.issue_comments(source_repo, number) +
      client.review_comments(source_repo, number)
  discussion = ["\nComments on pull request:\n\n"]
  discussion += comments.collect { |comment|
    name = comment.user.
    output = "\e[35m#{name}\e[m "
    output << "added a comment to \e[36m#{comment.id}\e[m"
    output << " on #{comment.created_at.review_time}"
    unless comment.created_at == comment.updated_at
      output << " (updated on #{comment.updated_at.review_time})"
    end
    output << ":\n#{''.rjust(output.length + 1, "-")}\n"
    output << comment.body
    output << "\n\n"
  }
  discussion.compact.flatten unless discussion.empty?
end

#latest_request_number(repo = source_repo) ⇒ Object

show latest pull request number



157
158
159
# File 'lib/git-review/provider/github.rb', line 157

def latest_request_number(repo=source_repo)
  current_requests(repo).collect(&:number).sort.last.to_i
end

#remote_url_for(user_name, repo_name = repo_info_from_config.last) ⇒ Object

FIXME: Needs to be moved into Server class, as its result is dependent of the actual provider (i.e. GitHub or BitBucket).



174
175
176
# File 'lib/git-review/provider/github.rb', line 174

def remote_url_for(user_name, repo_name = repo_info_from_config.last)
  "[email protected]:#{user_name}/#{repo_name}.git"
end

#request_exists?(number, state = 'open') ⇒ Boolean, Hash

Returns the specified request if exists, otherwise false. Instead of true, the request itself is returned, so another round-trip of pull_request can be avoided.

Returns:

  • (Boolean, Hash)

    the specified request if exists, otherwise false. Instead of true, the request itself is returned, so another round-trip of pull_request can be avoided.



35
36
37
38
39
40
41
# File 'lib/git-review/provider/github.rb', line 35

def request_exists?(number, state='open')
  return false if number.nil?
  request = client.pull_request(source_repo, number)
  request.state == state ? request : false
rescue Octokit::NotFound
  false
end

#request_exists_for_branch?(upstream = false, branch = local.source_branch) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/git-review/provider/github.rb', line 43

def request_exists_for_branch?(upstream = false, branch = local.source_branch)
  target_repo = local.target_repo(upstream)
  client.pull_requests(target_repo).any? { |r| r.head.ref == branch }
end

#request_number_by_title(title, repo = source_repo) ⇒ Object

get the number of the request that matches the title



162
163
164
165
# File 'lib/git-review/provider/github.rb', line 162

def request_number_by_title(title, repo=source_repo)
  request = current_requests(repo).find { |r| r.title == title }
  request.number if request
end

#request_url_for(target_repo, request_number) ⇒ Object

FIXME: Remove this method after merging create_pull_request from commands.rb, currently no specs



168
169
170
# File 'lib/git-review/provider/github.rb', line 168

def request_url_for(target_repo, request_number)
  "https://github.com/#{target_repo}/pull/#{request_number}"
end

#send_pull_request(to_upstream = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/git-review/provider/github.rb', line 66

def send_pull_request(to_upstream = false)
  target_repo = local.target_repo(to_upstream)
  head = local.head
  base = local.target_branch
  title, body = local.create_title_and_body(base)

  # gather information before creating pull request
  latest_number = latest_request_number(target_repo)

  # create the actual pull request
  create_pull_request(target_repo, base, head, title, body)
  # switch back to target_branch and check for success
  git_call "checkout #{base}"

  # make sure the new pull request is indeed created
  new_number = request_number_by_title(title, target_repo)
  if new_number && new_number > latest_number
    puts "Successfully created new request ##{new_number}"
    puts request_url_for target_repo, new_number
  else
    puts "Pull request was not created for #{target_repo}."
  end
end