Class: Metior::GitHub::Repository

Inherits:
Repository show all
Defined in:
lib/metior/github/repository.rb

Overview

Represents a GitHub source code repository

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Attributes inherited from Repository

#path

Instance Method Summary collapse

Methods inherited from Repository

#actor, #authors, #branches, #build_commits, #cached_commits, #commits, #committers, #description, #file_stats, #line_history, #name, #parse_range, #significant_authors, #significant_commits, #tags, #top_authors

Methods included from AutoIncludeVCS

included

Constructor Details

#initialize(user, project = nil) ⇒ Repository

Creates a new GitHub repository based on the given user and project names

Parameters:

  •  user (String)

    The GitHub username of repository's owner

  •  project (String)

    The name of the project



30
31
32
33
34
35
36
37
# File 'lib/metior/github/repository.rb', line 30

def initialize(user, project = nil)
  user, project = user.split('/') if user.include? '/'

  super "#{user}/#{project}"

  @project     = project
  @user        = user
end

Instance Attribute Details

#projectString (readonly)

Returns The project name of the repository.

Returns:

  • (String)

    The project name of the repository



20
21
22
# File 'lib/metior/github/repository.rb', line 20

def project
  @project
end

#userString (readonly)

Returns The GitHub username of the repository's owner.

Returns:

  • (String)

    The GitHub username of the repository's owner



23
24
25
# File 'lib/metior/github/repository.rb', line 23

def user
  @user
end

Instance Method Details

#id_for_ref(ref) ⇒ String (private)

Returns the unique identifier for the commit the given reference – like a branch name – is pointing to

Returns the given ref name immediately if it is a full SHA1 commit ID.

Parameters:

  • ref (String)

    A symbolic reference name

Returns:

  • (String)

    The SHA1 ID of the commit the reference is pointing to



48
49
50
51
52
# File 'lib/metior/github/repository.rb', line 48

def id_for_ref(ref)
  return ref if ref.match(/[0-9a-f]{40}/)
  @refs[ref] = Octokit.commit(@path, ref).id unless @refs.key? ref
  @refs[ref]
end

#load_branchesHash<String, String> (private)

Loads all branches and the corresponding commit IDs of this repository

Returns:

  • (Hash<String, String>)

    The names of all branches and the corresponding commit IDs

See Also:

  • Octokit#branches


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

def load_branches
  Octokit.branches(@path)
end

#load_commits(range) ⇒ Hashie::Mash, ... (private)

Note:

GitHub API is currently limited to 60 calls a minute, so you won't be able to query branches with more than 2100 commits (35 commits per call).

This method uses Octokit to load all commits from the given commit range

Parameters:

  • range (String, Range)

    The range of commits for which the commits should be loaded. This may be given as a string ('master..development'), a range ('master'..'development') or as a single ref ('master'). A single ref name means all commits reachable from that ref.

Returns:

  • (Hashie::Mash, nil)

    The base commit of the requested range or nil if the the range starts at the beginning of the history

  • (Array<Hashie::Mash>)

    All commits in the given commit range

See Also:

  • Octokit::Commits#commits


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/metior/github/repository.rb', line 78

def load_commits(range)
  base_commit = nil
  commits = []
  page = 1
  begin
    loop do
      new_commits = Octokit.commits(@path, range.last, :page => page)
      base_commit_index = new_commits.find_index do |commit|
        commit.id == range.first
      end
      unless base_commit_index.nil?
        commits += new_commits[0..base_commit_index-1]
        base_commit = new_commits[base_commit_index]
        break
      end
      commits += new_commits
      page += 1
    end
  rescue Octokit::NotFound
  end

  [base_commit, commits]
end

#load_name_and_descriptionObject (private) Also known as: load_description, load_name

Loads both the name and description of the project contained in the repository from GitHub

See Also:



108
109
110
111
112
# File 'lib/metior/github/repository.rb', line 108

def load_name_and_description
  github_repo  = Octokit.repo @path
  @description = github_repo.description
  @name        = github_repo.name
end

#load_tagsHash<String, String> (private)

Loads all tags and the corresponding commit IDs of this repository

Returns:

  • (Hash<String, String>)

    The names of all tags and the corresponding commit IDs

See Also:

  • Octokit#tags


121
122
123
# File 'lib/metior/github/repository.rb', line 121

def load_tags
  Octokit.tags @path
end