Module: Theoj::GitHub

Included in:
ReviewIssue, Submission
Defined in:
lib/theoj/github.rb

Instance Method Summary collapse

Instance Method Details

#can_be_assignee?(repo, username) ⇒ Boolean

Uses the GitHub API to determine if a user is already a collaborator of the repo

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/theoj/github.rb', line 42

def can_be_assignee?(repo, username)
  username = (username)
  github_client.check_assignee(repo, username)
end

#github_access_tokenObject

GitHub access token



14
15
16
# File 'lib/theoj/github.rb', line 14

def github_access_token
  @github_access_token ||= (ENV["GH_ACCESS_TOKEN"] || ENV["GITHUB_TOKEN"])
end

#github_clientObject

Authenticated Octokit



9
10
11
# File 'lib/theoj/github.rb', line 9

def github_client
  @github_client ||= Octokit::Client.new(access_token: github_access_token, auto_paginate: true)
end

#github_headersObject

GitHub API headers



19
20
21
22
23
# File 'lib/theoj/github.rb', line 19

def github_headers
  @github_headers ||= { "Authorization" => "token #{github_access_token}",
                        "Content-Type" => "application/json",
                        "Accept" => "application/vnd.github.v3+json" }
end

#is_collaborator?(repo, username) ⇒ Boolean

Uses the GitHub API to determine if a user is already a collaborator of the repo

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/theoj/github.rb', line 36

def is_collaborator?(repo, username)
  username = (username)
  github_client.collaborator?(repo, username)
end

#is_invited?(repo, username) ⇒ Boolean

Uses the GitHub API to determine if a user has a pending invitation

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/theoj/github.rb', line 48

def is_invited?(repo, username)
  username = (username).downcase
  github_client.repository_invitations(repo).any? { |i| i.invitee..downcase == username }
end

#issue(repo, issue_id) ⇒ Object

Return an Octokit GitHub Issue



26
27
28
# File 'lib/theoj/github.rb', line 26

def issue(repo, issue_id)
  @issue ||= github_client.issue(repo, issue_id)
end

#issue_labels(repo, issue_id) ⇒ Object

List labels of a GitHub issue



31
32
33
# File 'lib/theoj/github.rb', line 31

def issue_labels(repo, issue_id)
  github_client.labels_for_issue(repo, issue_id).map { |l| l[:name] }
end

#user_login(username) ⇒ Object

Returns the user login (removes the @ from the username)



54
55
56
# File 'lib/theoj/github.rb', line 54

def (username)
  username.to_s.strip.sub(/^@/, "")
end

#username?(username) ⇒ Boolean

Returns true if the string is a valid GitHub isername (starts with @)

Returns:

  • (Boolean)


59
60
61
# File 'lib/theoj/github.rb', line 59

def username?(username)
  username.match?(/\A@/)
end