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

Commit

Permalink
Merge pull request #1 from kylog/json-example
Browse files Browse the repository at this point in the history
Move to json
  • Loading branch information
Kylo Ginsberg committed Aug 30, 2016
2 parents 726e13d + adb59b1 commit 46d031e
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
/pkg/
/spec/reports/
/tmp/
MAINTAINERS
/MAINTAINERS
52 changes: 21 additions & 31 deletions MAINTAINERS-example
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
# This project, topsikrit, has top-secret awesome sauce.
#
# Btw, note that this comment was human-edited and will be preserved
# even if you add/remove maintainers using the gem. Nifty!

maintainers: {
version: 1
issues: "https://github.com/csprojects/topsikrit/issues"
# This is a very interesting comment. It will also be preserved.
roles: {
core: {
"ada": {},
"turing": {},
},
subsystem: {
"gracehopper": { subsystem: "ENIAC" },
},
}
people: {
"ada": {
name: "Ada Lovelace"
email: "ada@example.com"
},
"gracehopper": {
name: "Grace Hopper"
email: "admiral.grace.hopper@usnavy.gov"
},
"turing": {
name: "Alan Turing"
email: "at@cambridge.edu"
},
{
"version": 1,
"issues": "https://github.com/graceland/issues",
"people": [
{
"github": "gracehopper",
"email": "admiral.grace.hopper@usnavy.gov",
"name": "Grace Hopper",
"comment": "Maintains ENIAC subsystem"
},
{
"github": "turing",
"email": "at@cambridge.edu",
"name": "Alan Turing",
"comment": "Passed his own test"
},
{
"github": "ada",
"email": "ada@example.com",
"name": "Ada Lovelace"
}
]
}
6 changes: 6 additions & 0 deletions MAINTAINERS-unmaintained_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 1,
"maintained": false,
"issues": "https://github.com/graceland/issues",
"people": []
}
22 changes: 0 additions & 22 deletions lib/maintainers/runner.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# encoding: utf-8
# frozen_string_literal: true

require 'hocon/parser/config_document_factory'
require 'hocon/config_value_factory'

module Maintainers
# Runner entry point
class Runner
Expand All @@ -30,29 +27,10 @@ def run
# Create a new file given the file name and an optional issues url
def create(filename, issues_url = nil)
File.open(filename, 'w')

doc = Hocon::Parser::ConfigDocumentFactory.parse_file(filename)
doc = doc.set_config_value('maintainers.version',
Hocon::ConfigValueFactory.from_any_ref(1))

if issues_url
doc = doc.set_value('maintainers.issues', issues_url)
end
File.open(filename, 'w') do |file|
file.puts(doc.render)
end
end

# add a contributor given the filename and contributor attributes
def add(filename, github, email, name, comment=nil, section=nil)
ro_doc = Hocon::ConfigFactory.parse_file(filename)
rw_doc = Hocon::Parser::ConfigDocumentFactory.parse_file(filename)

people = ro_doc.get_value('maintainers.people') if ro_doc.has_path?('maintainers.people')
# modify people to add this person and then add that to rw_doc. i think.
File.open(filename, 'w') do |file|
file.puts(rw_doc.render)
end
end
end
end
2 changes: 1 addition & 1 deletion maintainers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"

spec.add_runtime_dependency 'hocon', '~> 1.1'
spec.add_runtime_dependency 'json-schema', '~> 2.6'
end
49 changes: 49 additions & 0 deletions schema/MAINTAINERS.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Maintainers file format",
"type": "object",
"additionalProperties": true,
"required": [ "version", "issues", "people" ],
"properties": {
"version": {
"description": "Version of this file format. Initially only: 1",
"type": "number",
"minimum": 1,
"maximum": 1
},
"issues": {
"description": "Where to file issues",
"type": "string"
},
"maintained": {
"description": "Is this repo maintained? Optional field, but set false to make clear that a repo is not maintained.",
"type": "boolean"
},
"people": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": [ "github" ],
"properties": {
"github": {
"description": "The maintainer's github id. Note that this is the only requied field.",
"type": "string"
},
"email": {
"description": "The maintainer's email address. Optional, and if present, no format is enforced, so uses like 'fry at puppet' are fine.",
"type": "string"
},
"name": {
"description": "The maintainer's name. Optional.",
"type": "string"
},
"comment": {
"description": "Optional comment field. May be handy for identifying subsystem ownership or just general awesomeness.",
"type": "string"
}
}
}
}
}
}
150 changes: 150 additions & 0 deletions schema/json-meta-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"positiveInteger": {
"type": "integer",
"minimum": 0
},
"positiveIntegerDefault0": {
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
},
"simpleTypes": {
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
}
},
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uri"
},
"$schema": {
"type": "string",
"format": "uri"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"multipleOf": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "boolean",
"default": false
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "boolean",
"default": false
},
"maxLength": { "$ref": "#/definitions/positiveInteger" },
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": {}
},
"maxItems": { "$ref": "#/definitions/positiveInteger" },
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"dependencies": {
"exclusiveMaximum": [ "maximum" ],
"exclusiveMinimum": [ "minimum" ]
},
"default": {}
}
30 changes: 30 additions & 0 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'json'
require 'json-schema'

describe 'the maintainers schema' do
it 'validates against the json meta schema' do
json_meta_schema = JSON.parse(File.read('schema/json-meta-schema.json'))
maintainers_schema = JSON.parse(File.read('schema/maintainers.json'))

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

describe 'the maintainers example' do
it 'validates against the maintainers schema' do
maintainers_schema = JSON.parse(File.read('schema/maintainers.json'))
maintainers_example = JSON.parse(File.read('MAINTAINERS-example'))

JSON::Validator.validate!(maintainers_schema, maintainers_example)
end
end

describe 'the maintainers unmaintained example' do
it 'validates against the maintainers schema' do
maintainers_schema = JSON.parse(File.read('schema/maintainers.json'))
maintainers_example = JSON.parse(File.read('MAINTAINERS-unmaintained_example'))

JSON::Validator.validate!(maintainers_schema, maintainers_example)
end
end

0 comments on commit 46d031e

Please sign in to comment.