From c1aaeeee32fc5b5b890ca0e3777c7cff24ea38e4 Mon Sep 17 00:00:00 2001 From: Henrik Hodne Date: Mon, 3 Dec 2012 23:09:57 -0600 Subject: [PATCH 1/2] Add a way to add responses to documentation You can add a predefined JSON response by entering "json(:resource_name)" in the docstring. This will then be replaced with the resource with the same name, found in lib/travis/api/app/endpoint/documentation/resources.rb. --- lib/travis/api/app/endpoint/documentation.rb | 2 + .../app/endpoint/documentation/resources.rb | 95 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 lib/travis/api/app/endpoint/documentation/resources.rb diff --git a/lib/travis/api/app/endpoint/documentation.rb b/lib/travis/api/app/endpoint/documentation.rb index 18653990..569906da 100644 --- a/lib/travis/api/app/endpoint/documentation.rb +++ b/lib/travis/api/app/endpoint/documentation.rb @@ -1,4 +1,5 @@ require 'travis/api/app' +require 'travis/api/app/endpoint/documentation/resources' class Travis::Api::App class Endpoint @@ -44,6 +45,7 @@ class Travis::Api::App def with_code_highlighting(str) str. + gsub(/json\(:([^)]+)\)/) { "
" + Resources::Helpers.json($1) + "
" }. gsub('/, ''). gsub(/TODO:?/, 'TODO') diff --git a/lib/travis/api/app/endpoint/documentation/resources.rb b/lib/travis/api/app/endpoint/documentation/resources.rb new file mode 100644 index 00000000..7026a6fe --- /dev/null +++ b/lib/travis/api/app/endpoint/documentation/resources.rb @@ -0,0 +1,95 @@ +require 'json' + +class Travis::Api::App::Endpoint + module Resources + module Helpers + def self.json(key) + JSON.pretty_generate(Resources.const_get(key.to_s.upcase)) + end + end + + REPOSITORY_KEY = { + "public_key" => "-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBAOcx131amMqIzm5+FbZz+DhIgSDbFzjKKpzaN5UWVCrLSc57z64xxTV6\nkaOTZmjCWz6WpaPkFZY+czfL7lmuZ/Y6UNm0vupvdZ6t27SytFFGd1/RJlAe89tu\nGcIrC1vtEvQu2frMLvHqFylnGd5Gy64qkQT4KRhMsfZctX4z5VzTAgMBAAE=\n-----END RSA PUBLIC KEY-----\n", + } + + REPOSITORY = { + "id" => 59, + "slug" => "travis-ci/travis-ci", + "description" => "A distributed build system for the open source community.", + "public_key" => REPOSITORY_KEY["public_key"], + "last_build_id" => 3373911, + "last_build_number" => "2188", + "last_build_status" => 0, + "last_build_result" => 0, + "last_build_duration" => 221, + "last_build_language" => nil, + "last_build_started_at" => "2012-11-27T01:01:28Z", + "last_build_finished_at" => "2012-11-27T01:05:09Z", + } + + REPOSITORIES = [REPOSITORY] + + SHORT_BUILD = { + "id" => 3373911, + "repository_id" => 59, + "number" => "2188", + "state" => "finished", + "result" => 0, + "started_at" => "2012-11-27T01:01:28Z", + "finished_at" => "2012-11-27T01:05:09Z", + "duration" => 221, + "commit" => "a0e4dada7eb30b41817d9d3c5222b519502ef87a", + "branch" => "master", + "message" => "no need to set up services", + "event_type" => "push", + } + + BUILDS = [ + SHORT_BUILD, + ] + + CONFIG = { + "language" => "ruby", + "rvm" => [ + "1.9.3", + ], + "bundler_args" => "--without development", + "before_install" => [ + "gem install bundler --pre", + ], + "before_script" => [ + "cp config/database.example.yml config/database.yml" + ], + "script" => "RAILS_ENV=test bundle exec rake test:ci --trace", + "notifications" => { + "irc" => "irc.freenode.org#travis", + "campfire" => { + "secure" => "JJezWGD9KJY/LC2aznI3Zyohy31VTIhcTKX7RWR4C/C8YKbW9kZv3xV6Vn11\nSHxJTeZo6st2Bpv6tjlWZ+HCR09kyCNavIChedla3+oHOiuL0D4gSo+gkTNW\nUKYZz9mcQUd9RoQpTeyxvdvX+l7z62/7JwFA7txHOqxbTS8jrjc=" + } + }, + ".result" => "configured" + } + + BUILD = SHORT_BUILD.merge({ + "config" => CONFIG, + "committed_at" => "2012-11-27T01:01:06Z", + "author_name" => "Sven Fuchs", + "author_email" => "me@svenfuchs.com", + "committer_name" => "Sven Fuchs", + "committer_email" => "me@svenfuchs.com", + "compare_url" => "https://github.com/travis-ci/travis-ci/compare/18b6874865f2...a0e4dada7eb3", + "matrix" => [ + { + "id" => 3373912, + "repository_id" => 59, + "number" => "2188.1", + "config" => CONFIG, + "result" => 0, + "started_at" => "2012-11-27T01:01:28Z", + "finished_at" => "2012-11-27T01:05:09Z", + "allow_failure" => false + } + ] + }) + end +end From 6f5f7d0073431c3b52728bfea95735ce3e1a5378 Mon Sep 17 00:00:00 2001 From: Henrik Hodne Date: Mon, 3 Dec 2012 23:11:01 -0600 Subject: [PATCH 2/2] Add responses to repository endpoints --- lib/travis/api/app/endpoint/repos.rb | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/travis/api/app/endpoint/repos.rb b/lib/travis/api/app/endpoint/repos.rb index 7e9eb3fe..40c430ce 100644 --- a/lib/travis/api/app/endpoint/repos.rb +++ b/lib/travis/api/app/endpoint/repos.rb @@ -8,10 +8,19 @@ class Travis::Api::App # You can filter the repositories by adding parameters to the request. For example, you can get all repositories # owned by johndoe by adding `owner_name=johndoe`, or all repositories that johndoe has access to by adding # `member=johndoe`. The parameter names correspond to the keys of the response hash. + # + # ### Response + # + # json(:repositories) get '/' do respond_with service(:find_repos, params) end + # Gets the repository with the given id. + # + # ### Response + # + # json(:repository) get '/:id' do respond_with service(:find_repo, params) end @@ -20,6 +29,15 @@ class Travis::Api::App respond_with service(:find_repo, params.merge(schema: 'cc')) end + # Get the public key for the repository with the given id. + # + # This can be used to encrypt secure variables in the build configuration. See + # [the encryption keys](http://about.travis-ci.org/docs/user/encryption-keys/) documentation page for more + # information. + # + # ### Response + # + # json(:repository_key) get '/:id/key' do respond_with service(:find_repo_key, params), version: :v2 end @@ -28,14 +46,29 @@ class Travis::Api::App respond_with service(:regenerate_repo_key, params), version: :v2 end + # Gets the repository with the given name. + # + # ### Response + # + # json(:repository) get '/:owner_name/:name' do respond_with service(:find_repo, params) end + # Gets the builds for the repository with the given name. + # + # ### Response + # + # json(:builds) get '/:owner_name/:name/builds' do respond_with service(:find_builds, params) end + # Get a build with the given id in the repository with the given name. + # + # ### Response + # + # json(:build) get '/:owner_name/:name/builds/:id' do respond_with service(:find_build, params) end @@ -44,6 +77,15 @@ class Travis::Api::App respond_with service(:find_repo, params.merge(schema: 'cc')) end + # Get the public key for a given repository. + # + # This can be used to encrypt secure variables in the build configuration. See + # [the encryption keys](http://about.travis-ci.org/docs/user/encryption-keys/) documentation page for more + # information. + # + # ### Response + # + # json(:repository_key) get '/:owner_name/:name/key' do respond_with service(:find_repo_key, params), version: :v2 end