Class: GitReview::Github

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGithub

Returns a new instance of Github.



28
29
30
# File 'lib/git-review/github.rb', line 28

def initialize
  configure_github_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

delegate methods that interact with Github to Octokit client



182
183
184
185
186
187
188
# File 'lib/git-review/github.rb', line 182

def method_missing(method, *args)
  if @github.respond_to?(method)
    @github.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#githubObject (readonly)

Returns the value of attribute github.



19
20
21
# File 'lib/git-review/github.rb', line 19

def github
  @github
end

#source_repoString

Returns the source repo.

Returns:

  • (String)

    the source repo



103
104
105
# File 'lib/git-review/github.rb', line 103

def source_repo
  @source_repo
end

Class Method Details

.instanceObject

acts like a singleton class but it’s actually not use ::GitReview::Github.instance everywhere except in tests



24
25
26
# File 'lib/git-review/github.rb', line 24

def self.instance
  @instance ||= new
end

Instance Method Details

#commit_discussion(number) ⇒ Object



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

def commit_discussion(number)
  pull_commits = @github.pull_commits(source_repo, number)
  repo = @github.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 #{format_time(commit.commit.committer.date)}"
    output << ":\n#{''.rjust(output.length + 1, "-")}\n"
    output << "#{commit.commit.message}"
    output << "\n\n"
    result = [output]

    # comments on commit
    comments = @github.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 #{format_time(comment.created_at)}"
      unless comment.created_at == comment.updated_at
        output << " (updated on #{format_time(comment.updated_at)})"
      end
      output << ":\n#{''.rjust(output.length + 1, "-")}\n"
      output << comment.body
      output << "\n\n"
    }
  }
  discussion.compact.flatten unless discussion.empty?
end

#configure_github_accessString

setup connection with Github via OAuth

Returns:

  • (String)

    the username logged in



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/git-review/github.rb', line 34

def configure_github_access
  settings = ::GitReview::Settings.instance
  if settings.oauth_token && settings.username
    @github = Octokit::Client.new(
      :login          => settings.username,
      :access_token    => settings.oauth_token,
      :auto_traversal => true
    )
    @github.
  else
    configure_oauth
    configure_github_access
  end
end

#current_requests(repo = source_repo) ⇒ Object

an alias to pull_requests



68
69
70
# File 'lib/git-review/github.rb', line 68

def current_requests(repo=source_repo)
  @github.pull_requests(repo)
end

#current_requests_full(repo = source_repo) ⇒ Object

a more detailed collection of requests



73
74
75
76
77
# File 'lib/git-review/github.rb', line 73

def current_requests_full(repo=source_repo)
  @github.pull_requests(repo).collect { |request|
    @github.pull_request(repo, request.number)
  }
end

#discussion(number) ⇒ Object

show discussion for a request



165
166
167
168
# File 'lib/git-review/github.rb', line 165

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

#issue_discussion(number) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/git-review/github.rb', line 146

def issue_discussion(number)
  comments = @github.issue_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 #{format_time(comment.created_at)}"
    unless comment.created_at == comment.updated_at
      output << " (updated on #{format_time(comment.updated_at)})"
    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



171
172
173
# File 'lib/git-review/github.rb', line 171

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

#repo_info_from_configArray(String, String)

Returns user and repo name from local git config.

Returns:

  • (Array(String, String))

    user and repo name from local git config

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/git-review/github.rb', line 84

def repo_info_from_config
  git_config = local.config
  url = git_config['remote.origin.url']
  raise ::GitReview::InvalidGitRepositoryError if url.nil?

  user, project = github_url_matching(url)
  # if there are no results yet, look for 'insteadof' substitutions
  #   in URL and try again
  unless user && project
    insteadof_url, true_url = github_insteadof_matching(git_config, url)
    if insteadof_url and true_url
      url = url.sub(insteadof_url, true_url)
      user, project = github_url_matching(url)
    end
  end
  [user, project]
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.



52
53
54
55
56
57
58
# File 'lib/git-review/github.rb', line 52

def request_exists?(number, state='open')
  return false if number.nil?
  request = @github.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)


60
61
62
63
64
65
# File 'lib/git-review/github.rb', line 60

def request_exists_for_branch?(upstream=false, branch=local.source_branch)
  target_repo = local.target_repo(upstream)
  @github.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



176
177
178
179
# File 'lib/git-review/github.rb', line 176

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/git-review/github.rb', line 190

def respond_to?(method)
  @github.respond_to?(method) || super
end

#updateObject



79
80
81
# File 'lib/git-review/github.rb', line 79

def update
  git_call('fetch origin')
end