Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Add support for add/create/remove
Browse files Browse the repository at this point in the history
Also some misc license and readme update.
  • Loading branch information
Kylo Ginsberg committed Sep 1, 2016
1 parent 46d031e commit 74a4e5b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@
/spec/reports/
/tmp/
/MAINTAINERS
/.byebug_history
13 changes: 13 additions & 0 deletions CHANGELOG
@@ -0,0 +1,13 @@
# Change Log
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## 0.1.0 - 2016-09-01
### Added
- Initial support for a json format for MAINTAINERS

[Unreleased]: https://github.com/puppetlabs/maintainers/compare/v0.1.0...HEAD
2 changes: 2 additions & 0 deletions LICENSE
@@ -1,5 +1,7 @@
Maintainers - a gem for maintaining MAINTAINERS files

Copyright (C) 2016 Puppet, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
14 changes: 2 additions & 12 deletions README.md
Expand Up @@ -4,19 +4,9 @@ This is a gem for maintaining MAINTAINERS files.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'maintainers'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install maintainers
```

## Usage

Expand Down Expand Up @@ -83,5 +73,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kylog/maintainers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
Bug reports and pull requests are welcome on GitHub at https://github.com/puppetlabs/maintainers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

10 changes: 10 additions & 0 deletions lib/maintainers/cli.rb
Expand Up @@ -70,6 +70,16 @@ def parse(args)

subcommands[subcommand].order!

if subcommand == 'create' && options[:issues].nil?
$stderr.puts "Please specify --issues"
usage
end

if subcommand == 'add' && options[:github].nil?
$stderr.puts "Please specify --github"
usage
end

if args.count > 0
$stderr.puts "Unexpected additional args #{args}"
usage
Expand Down
84 changes: 76 additions & 8 deletions lib/maintainers/runner.rb
@@ -1,6 +1,9 @@
# encoding: utf-8
# frozen_string_literal: true

require 'json'
require 'json-schema'

module Maintainers
# Runner entry point
class Runner
Expand All @@ -16,21 +19,86 @@ def initialize(options)
def run
case @options[:subcommand]
when 'create'
create(@options[:filename], @options[:issues])
create(@options)
when 'add'
add(@options[:filename], @options[:github], @options[:email], @options[:name])
add(@options)
when 'remove'
remove(@options[:filename], @options[:github])
remove(@options)
end
end

def validate(maintainers)
maintainers_schema = JSON.parse(File.read('schema/maintainers.json'))

JSON::Validator.validate(maintainers_schema, maintainers)
end

def write_file(filename, maintainers)
maintainers_json = JSON.pretty_generate(maintainers)

if !validate(maintainers_json)
$stderr.puts "Invalid maintainers string!"
exit 1
end

File.open(filename, 'w') { |f| f.write(maintainers_json) }
end

def create(options)
filename = options[:filename]
if File.exist?(filename)
$stderr.puts "#{filename} already exists. Remove it and then re-run this command."
exit 1
end

# minimum content for a maintainers file
maintainers = {}
maintainers["version"] = 1
maintainers["issues"] = options[:issues]
maintainers["people"] = []

write_file(filename, maintainers)
end

# Create a new file given the file name and an optional issues url
def create(filename, issues_url = nil)
File.open(filename, 'w')
def add(options)
filename = options[:filename]
if !File.exist?(filename)
$stderr.puts "No #{filename} file exists yet. You can use the 'create' subcommand to create one."
exit 1
end

maintainers = JSON.load(File.read(filename))
new_maintainer = { "github" => options[:github] }
new_maintainer["email"] = options[:email] if options[:email]
new_maintainer["name"] = options[:name] if options[:name]
index = maintainers["people"].index { |person| person["github"] == options[:github] }
if index
current = maintainers["people"][index]
new_maintainer.merge! current
maintainers["people"][index] = new_maintainer
else
maintainers["people"] << new_maintainer
end

write_file(filename, maintainers)
end

# add a contributor given the filename and contributor attributes
def add(filename, github, email, name, comment=nil, section=nil)
def remove(options)
filename = options[:filename]
if !File.exist?(filename)
$stderr.puts "No #{filename} file exists yet. You can use the 'create' subcommand to create one."
exit 1
end

maintainers = JSON.load(File.read(filename))
index = maintainers["people"].index { |person| person["github"] == options[:github] }
if index
maintainers["people"].slice!(index)
else
puts "I didn't find #{options[:github]} in the file #{filename}"
end

write_file(filename, maintainers)
end
end
end
6 changes: 3 additions & 3 deletions maintainers.gemspec
Expand Up @@ -6,11 +6,10 @@ require 'maintainers/version'
Gem::Specification.new do |spec|
spec.name = "maintainers"
spec.version = Maintainers::VERSION
spec.authors = ["Kylo Ginsberg"]
spec.email = ["kylo@puppet.com"]
spec.authors = ["Puppet, Inc"]

spec.summary = %q{A gem for maintaining MAINTAINERS files}
spec.homepage = "https://github.com/kylog/maintainers"
spec.homepage = "https://github.com/puppetlabs/maintainers"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = 'bin'
Expand All @@ -20,6 +19,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "byebug", "~> 9.0"

spec.add_runtime_dependency 'json-schema', '~> 2.6'
end
1 change: 0 additions & 1 deletion spec/schema_spec.rb
Expand Up @@ -7,7 +7,6 @@
maintainers_schema = JSON.parse(File.read('schema/maintainers.json'))

JSON::Validator.validate!(json_meta_schema, maintainers_schema)
# JSON::Validator.validate!(@schema, json)
end
end

Expand Down

0 comments on commit 74a4e5b

Please sign in to comment.