codeinventory 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d36a885396b768230bc4748b7b18dfcf6da7fb8
4
+ data.tar.gz: e178759f92968f5fa78c53fabf508a87debbca35
5
+ SHA512:
6
+ metadata.gz: 66f579ac72173b3d8e2a79ffc664b9b982930a2a6f903d5daf7ed60c0cbd7e2d71d4339c58d257a4861946d8ccc255167d2da6d87f72583a7eb63ded95101d40
7
+ data.tar.gz: 7878947ca4b20cc8d99ba5596eedae88b2932688289b4578fa1cdebae15a65b6becae9d369b4656d51d8edf49e43896c8a1ae8ce9d434f11279f1f4aa349e53f
@@ -0,0 +1,9 @@
1
+ name: CodeInventory
2
+ description: A Ruby gem that harvests project metadata from an agency's repositories.
3
+ license: https://github.com/GSA/codeinventory/blob/master/LICENSE.md
4
+ openSourceProject: 1
5
+ governmentWideReuseProject: 1
6
+ tags:
7
+ - code.gov
8
+ contact:
9
+ email: jeffrey.fredrickson@gsa.gov
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codeinventory.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,3 @@
1
+ # License
2
+
3
+ As a work of the United States Government, this project is in the public domain within the United States.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # CodeInventory: The People's Code
2
+
3
+ *_This is an experimental gem that is currently in an alpha stage. The features and interface are unstable and may change at any time._*
4
+
5
+ The `codeinventory` gem is a tool to harvest project metadata from an agency's repositories. The harvested metadata can be used to produce a [code.json](https://code.gov/#/policy-guide/docs/compliance/inventory-code) file for [Code.gov](https://code.gov/). The gem includes the ability to pull project metadata from these sources:
6
+
7
+ * JSON files
8
+ * CSV files
9
+ * GitHub
10
+
11
+ More sources can be added.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'codeinventory'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install codeinventory
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ json_source = CodeInventory::Source::JSONFile.new(File.new("some_projects.json"))
33
+ csv_source = CodeInventory::Source::CSVFile.new(File.new("more_projects.csv"))
34
+ github_source = CodeInventory::Source::GitHub.new(access_token: "GITHUB_ACCESS_TOKEN", org: "github_org_name")
35
+
36
+ inventory = CodeInventory::Inventory.new(json_source, csv_source, github_source)
37
+ inventory.projects # Returns an array of all projects
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GSA/codeinventory.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "spec"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "codeinventory"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codeinventory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codeinventory"
8
+ spec.version = CodeInventory::VERSION
9
+ spec.authors = ["Jeff Fredrickson"]
10
+ spec.email = ["jeffrey.fredrickson@gsa.gov"]
11
+
12
+ spec.summary = %q{Harvests project metadata from an agency's repositories to build a code inventory.}
13
+ spec.description = %q{Harvests project metadata from an agency's repositories to build a code inventory. This helps agencies comply with the Federal Source Code Policy.}
14
+ spec.homepage = "https://github.com/GSA/codeinventory"
15
+ spec.license = "CC0-1.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ spec.add_development_dependency "webmock", "~> 2.1"
28
+ spec.add_development_dependency "pry", "~> 0.10"
29
+
30
+ spec.add_runtime_dependency "octokit", "~> 4.6"
31
+ end
@@ -0,0 +1,8 @@
1
+ require "codeinventory/version"
2
+ require "codeinventory/inventory"
3
+ require "codeinventory/source/json_file"
4
+ require "codeinventory/source/csv_file"
5
+ require "codeinventory/source/github"
6
+
7
+ module CodeInventory
8
+ end
@@ -0,0 +1,13 @@
1
+ module CodeInventory
2
+ class Inventory
3
+ attr_accessor :sources
4
+
5
+ def initialize(*sources)
6
+ @sources = [sources].flatten
7
+ end
8
+
9
+ def projects
10
+ @sources.collect { |source| source.projects }.flatten
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ require "csv"
2
+
3
+ module CodeInventory
4
+ module Source
5
+ class CSVFile
6
+ attr_reader :csv
7
+
8
+ def initialize(csv_file)
9
+ @csv = CSV.read(csv_file, { headers: true, converters: [ :integer ] })
10
+ validate_headers
11
+ end
12
+
13
+ def projects
14
+ @csv.collect do |row|
15
+ csv_data = row.to_hash
16
+ csv_data.inject({}) do |memo, pair|
17
+ csv_header, csv_value = pair
18
+ case
19
+ when csv_header == "tags"
20
+ new_pair = { "tags" => csv_value.split(",").collect { |tag| tag.strip } }
21
+ when csv_header.include?(".")
22
+ new_pair = dotted_to_nested(csv_header, csv_value)
23
+ else
24
+ new_pair = { csv_header => csv_value }
25
+ end
26
+ memo.merge(new_pair)
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def dotted_to_nested(path, value)
34
+ path.split(".").reverse.inject(value) do |hash, element|
35
+ { element => hash }
36
+ end
37
+ end
38
+
39
+ def validate_headers
40
+ required_headers = [ "name", "description", "license", "openSourceProject", "governmentWideReuseProject", "tags", "contact.email" ]
41
+ required_headers.each do |required|
42
+ raise FileFormatError unless @csv.headers.include? required
43
+ end
44
+ end
45
+ end
46
+
47
+ class FileFormatError < StandardError
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ require "octokit"
2
+ require "yaml"
3
+ require "base64"
4
+
5
+ module CodeInventory
6
+ module Source
7
+ class GitHub
8
+ attr_accessor :org
9
+
10
+ def initialize(access_token:, org:)
11
+ Octokit.auto_paginate = true
12
+ @access_token = access_token
13
+ @org = org
14
+ end
15
+
16
+ def projects
17
+ repos = client.organization_repositories(@org)
18
+ projects = []
19
+ repos.each do |repo|
20
+ begin
21
+ contents_metadata = client.contents(repo[:full_name], path: ".codeinventory.yml")
22
+ type = :yaml
23
+ raw_content = Base64.decode64(contents_metadata[:content])
24
+ rescue Octokit::NotFound
25
+ begin
26
+ contents_metadata = client.contents(repo[:full_name], path: ".codeinventory.json")
27
+ type = :json
28
+ raw_content = Base64.decode64(contents_metadata[:content])
29
+ rescue Octokit::NotFound
30
+ # Ignore repositories that don't have a CodeInventory metadata file
31
+ end
32
+ end
33
+ if type == :yaml
34
+ projects << YAML.load(raw_content).to_hash
35
+ elsif type == :json
36
+ projects << JSON.parse(raw_content)
37
+ end
38
+ end
39
+ projects
40
+ end
41
+
42
+ def client
43
+ @client ||= Octokit::Client.new(access_token: @access_token)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ require "json"
2
+
3
+ module CodeInventory
4
+ module Source
5
+ class JSONFile
6
+ attr_accessor :projects
7
+
8
+ def initialize(json_file)
9
+ @projects = JSON.load(json_file)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module CodeInventory
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codeinventory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Fredrickson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: octokit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.6'
97
+ description: Harvests project metadata from an agency's repositories to build a code
98
+ inventory. This helps agencies comply with the Federal Source Code Policy.
99
+ email:
100
+ - jeffrey.fredrickson@gsa.gov
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".codeinventory.yml"
106
+ - ".gitignore"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.md
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - codeinventory.gemspec
115
+ - lib/codeinventory.rb
116
+ - lib/codeinventory/inventory.rb
117
+ - lib/codeinventory/source/csv_file.rb
118
+ - lib/codeinventory/source/github.rb
119
+ - lib/codeinventory/source/json_file.rb
120
+ - lib/codeinventory/version.rb
121
+ homepage: https://github.com/GSA/codeinventory
122
+ licenses:
123
+ - CC0-1.0
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.5.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Harvests project metadata from an agency's repositories to build a code inventory.
145
+ test_files: []