Class: CVEList::Repository

Inherits:
Directory show all
Includes:
Enumerable
Defined in:
lib/cvelist/repository.rb

Constant Summary collapse

URL =

The default git URI for the cvelist repository

'https://github.com/CVEProject/cvelist.git'
REMOTE =

The default git remote.

'origin'
BRANCH =

The default git branch.

'master'
GLOB =

Dir.glob for year directories.

'[1-2][0-9][0-9][0-9]'

Instance Attribute Summary

Attributes inherited from Directory

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Directory

#directory?, #file?, #glob, #initialize, #join, #to_s

Constructor Details

This class inherits a constructor from CVEList::Directory

Class Method Details

.clone(path, url: URL, depth: 1) ⇒ Object Also known as: download

Clones a new repository.

Parameters:

  • path (#to_s)

    The path to where the cvelist repository will be cloned to.

  • url (#to_s) (defaults to: URL)

    The URL for the cvelist repository.

  • depth (#to_s) (defaults to: 1)

    The depth of the git clone.

Raises:

  • (CloneFailedError)

    The git clone command failed.



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

def self.clone(path, url: URL, depth: 1)
  unless system 'git', 'clone', '--depth', depth.to_s, url.to_s, path.to_s
    raise(GitCloneFailed,"failed to clone #{url.inspect} into #{path.inspect}")
  end

  return new(path)
end

Instance Method Details

#[](cve_id) ⇒ CVE?

Accesses a CVE from the repository with the given CVE ID.

Parameters:

  • cve_id (String)

    The given CVE ID.

Returns:

  • (CVE, nil)

    The CVE with the given ID. If no CVE with the given ID could be found, nil will be returned.

Raises:



216
217
218
219
220
221
222
# File 'lib/cvelist/repository.rb', line 216

def [](cve_id)
  year_number = cve_to_year(cve_id)

  if has_year?(year_number)
    year(year_number)[cve_id]
  end
end

#directoriesArray<String>

The year directories within the repository.

Returns:

  • (Array<String>)

    The paths to the year directories.



110
111
112
# File 'lib/cvelist/repository.rb', line 110

def directories
  glob(GLOB).sort
end

#each {|cve| ... } ⇒ Enumerator

Enumerates over every CVE withing the year directories.

Yields:

  • (cve)

    The given block will be passed each CVE from within the repository.

Yield Parameters:

  • cve (CVE)

    A CVE from the repository.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



160
161
162
163
164
165
166
# File 'lib/cvelist/repository.rb', line 160

def each(&block)
  return enum_for(__method__) unless block_given?

  years.each do |year_dir|
    year_dir.each(&block)
  end
end

#each_malformed {|malformed_cve| ... } ⇒ Enumerator

Enumerates over every malformed CVE within the repository.

Yields:

  • (malformed_cve)

    The given block will be passed each malformed CVE from within the repository.

Yield Parameters:

  • malformed_cve (MalformedCVE)

    A malformed CVE from within the repository.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



181
182
183
184
185
186
187
# File 'lib/cvelist/repository.rb', line 181

def each_malformed(&block)
  return enum_for(__method__) unless block_given?

  years.each do |year_dir|
    year_dir.each_malformed(&block)
  end
end

#git?Boolean

Determines whether the repository is a git repository.

Returns:

  • (Boolean)

    Specifies whether the repository is a git repository or not.



48
49
50
# File 'lib/cvelist/repository.rb', line 48

def git?
  directory?('.git')
end

#has_cve?(cve_id) ⇒ Boolean

Determines whether the repository contains the given CVE ID.

Parameters:

  • cve_id (String)

    The given CVE ID.

Returns:

  • (Boolean)


197
198
199
200
201
# File 'lib/cvelist/repository.rb', line 197

def has_cve?(cve_id)
  year_number = cve_to_year(cve_id)

  return has_year?(year_number) && year(year_number).has_cve?(cve_id)
end

#has_year?(year) ⇒ Boolean

Determines if the repository contains a directory for the given year.

Parameters:

  • year (#to_s)

    The given year.

Returns:

  • (Boolean)

    Specifies whether the repository contains the directory for the year.



97
98
99
# File 'lib/cvelist/repository.rb', line 97

def has_year?(year)
  directory?(year.to_s)
end

#pull!(remote: REMOTE, branch: BRANCH) ⇒ true, false Also known as: update!

Pulls down new commits from the given remote/branch.

Parameters:

  • remote (#to_s) (defaults to: REMOTE)

    The git remote.

  • branch (#to_s) (defaults to: BRANCH)

    The git branch.

Returns:

  • (true, false)

    Returns true if the git pull succeeds. Returns false if the repository is not a git repository.

Raises:

  • (PullFailedError)

    The git pull command faild.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cvelist/repository.rb', line 74

def pull!(remote: REMOTE, branch: BRANCH)
  return false unless git?

  Dir.chdir(@path) do
    unless system('git', 'pull', remote.to_s, branch.to_s)
      raise(GitPullFailed,"failed to pull from remote #{remote.inspect} branch #{branch.inspect}")
    end
  end

  return true
end

#sizeInteger

Calculates the total number of CVEs in the repository.

Returns:

  • (Integer)


229
230
231
# File 'lib/cvelist/repository.rb', line 229

def size
  Dir[join(GLOB,YearDir::GLOB,RangeDir::GLOB)].length
end

#year(year_number) ⇒ YearDir Also known as: /

Requests a year directory from the repository.

Parameters:

  • year_number (#to_s)

    The given year number.

Returns:

  • (YearDir)

    The year directory.

Raises:

  • (YearNotFound)

    There is no year directory within the repository for the given year.



136
137
138
139
140
141
142
143
144
# File 'lib/cvelist/repository.rb', line 136

def year(year_number)
  year_dir = join(year_number.to_s)

  unless File.directory?(year_dir)
    raise(YearDirNotFound,"year #{year_number.inspect} not found within #{@path.inspect}")
  end

  return YearDir.new(year_dir)
end

#years(&block) ⇒ Array<YearDir>

The year directories contained within the repository.

Returns:

  • (Array<YearDir>)

    The year directories within the repository.



120
121
122
# File 'lib/cvelist/repository.rb', line 120

def years(&block)
  directories.map { |dir| YearDir.new(dir) }
end