rename api/v[0-2] to api/serialize/v[0-2]
This commit is contained in:
parent
4e858e3949
commit
81d8c64e98
|
@ -26,10 +26,14 @@ require 'metriks/reporter/logger'
|
|||
require 'metriks/librato_metrics_reporter'
|
||||
require 'travis/support/log_subscriber/active_record_metrics'
|
||||
require 'fileutils'
|
||||
|
||||
module Travis::Api
|
||||
end
|
||||
|
||||
require 'travis/api/app/endpoint'
|
||||
require 'travis/api/app/middleware'
|
||||
require 'travis/api/instruments'
|
||||
require 'travis/api/v2/http'
|
||||
require 'travis/api/serialize/v2'
|
||||
require 'travis/api/v3'
|
||||
require 'travis/api/app/stack_instrumentation'
|
||||
require 'travis/api/app/error_handling'
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'travis/api/serialize'
|
||||
|
||||
class Travis::Api::App
|
||||
module Responders
|
||||
class Json < Base
|
||||
|
@ -46,7 +48,7 @@ class Travis::Api::App
|
|||
if defined?(@builder)
|
||||
@builder
|
||||
else
|
||||
@builder = Travis::Api.builder(resource, { :version => version }.merge(options))
|
||||
@builder = Travis::Api::Serialize.builder(resource, { :version => version }.merge(options))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
1
lib/travis/api/serialize/v2.rb
Normal file
1
lib/travis/api/serialize/v2.rb
Normal file
|
@ -0,0 +1 @@
|
|||
require 'travis/api/serialize/v2/http'
|
24
lib/travis/api/serialize/v2/http.rb
Normal file
24
lib/travis/api/serialize/v2/http.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
require 'travis/api/serializer'
|
||||
require 'travis/api/serialize/v2/http/accounts'
|
||||
require 'travis/api/serialize/v2/http/annotations'
|
||||
require 'travis/api/serialize/v2/http/broadcasts'
|
||||
require 'travis/api/serialize/v2/http/branch'
|
||||
require 'travis/api/serialize/v2/http/branches'
|
||||
require 'travis/api/serialize/v2/http/build'
|
||||
require 'travis/api/serialize/v2/http/builds'
|
||||
require 'travis/api/serialize/v2/http/caches'
|
||||
require 'travis/api/serialize/v2/http/hooks'
|
||||
require 'travis/api/serialize/v2/http/job'
|
||||
require 'travis/api/serialize/v2/http/jobs'
|
||||
require 'travis/api/serialize/v2/http/log'
|
||||
require 'travis/api/serialize/v2/http/permissions'
|
||||
require 'travis/api/serialize/v2/http/repositories'
|
||||
require 'travis/api/serialize/v2/http/repository'
|
||||
require 'travis/api/serialize/v2/http/requests'
|
||||
require 'travis/api/serialize/v2/http/request'
|
||||
require 'travis/api/serialize/v2/http/ssl_key'
|
||||
require 'travis/api/serialize/v2/http/env_var'
|
||||
require 'travis/api/serialize/v2/http/env_vars'
|
||||
require 'travis/api/serialize/v2/http/user'
|
||||
require 'travis/api/serialize/v2/http/validation_error'
|
||||
require 'travis/api/serialize/v2/http/ssh_key'
|
44
lib/travis/api/serialize/v2/http/accounts.rb
Normal file
44
lib/travis/api/serialize/v2/http/accounts.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Accounts
|
||||
include Formats
|
||||
|
||||
attr_reader :accounts, :options
|
||||
|
||||
def initialize(accounts, options = {})
|
||||
@accounts = accounts
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
:accounts => accounts.map { |account| account_data(account) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def account_data(account)
|
||||
data = {
|
||||
'id' => account.id,
|
||||
'name' => account.name,
|
||||
'login' => account.login,
|
||||
'type' => account.type.underscore,
|
||||
'repos_count' => account.repos_count
|
||||
}
|
||||
|
||||
data['avatar_url'] = account.avatar_url if account.respond_to?(:avatar_url)
|
||||
|
||||
data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
38
lib/travis/api/serialize/v2/http/annotations.rb
Normal file
38
lib/travis/api/serialize/v2/http/annotations.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Annotations
|
||||
include Formats
|
||||
|
||||
def initialize(annotations, options = {})
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
"annotations" => @annotations.map { |annotation| build_annotation(annotation) },
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_annotation(annotation)
|
||||
{
|
||||
"id" => annotation.id,
|
||||
"job_id" => annotation.job_id,
|
||||
"description" => annotation.description,
|
||||
"url" => annotation.url,
|
||||
"status" => annotation.status,
|
||||
"provider_name" => annotation.annotation_provider.name,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
lib/travis/api/serialize/v2/http/branch.rb
Normal file
31
lib/travis/api/serialize/v2/http/branch.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
require 'travis/api/serialize/v2/http/branches'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Branch < Branches
|
||||
include Formats
|
||||
|
||||
attr_reader :build, :commit, :options
|
||||
|
||||
def initialize(build, options = {})
|
||||
@build = build
|
||||
@commit = build.commit
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'branch' => build_data(build),
|
||||
'commit' => commit_data(commit)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
64
lib/travis/api/serialize/v2/http/branches.rb
Normal file
64
lib/travis/api/serialize/v2/http/branches.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Branches
|
||||
include Formats
|
||||
|
||||
attr_reader :builds, :commits, :options
|
||||
|
||||
def initialize(builds, options = {})
|
||||
builds = builds.last_finished_builds_by_branches if builds.is_a?(Repository) # TODO remove, bc
|
||||
@builds = builds
|
||||
@commits = builds.map(&:commit)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'branches' => builds.map { |build| build_data(build) },
|
||||
'commits' => commits.map { |commit| commit_data(commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'config' => build.obfuscated_config.stringify_keys,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => build.matrix.map { |job| job.id },
|
||||
'pull_request' => build.pull_request?
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
33
lib/travis/api/serialize/v2/http/broadcasts.rb
Normal file
33
lib/travis/api/serialize/v2/http/broadcasts.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Broadcasts
|
||||
attr_reader :broadcasts, :options
|
||||
|
||||
def initialize(broadcasts, options = {})
|
||||
@broadcasts = broadcasts
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'broadcasts' => broadcasts.map { |broadcast| broadcast_data(broadcast) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def broadcast_data(broadcast)
|
||||
{
|
||||
'id' => broadcast.id,
|
||||
'message' => broadcast.message
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
97
lib/travis/api/serialize/v2/http/build.rb
Normal file
97
lib/travis/api/serialize/v2/http/build.rb
Normal file
|
@ -0,0 +1,97 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Build
|
||||
include Formats
|
||||
|
||||
attr_reader :build, :options
|
||||
|
||||
def initialize(build, options = {})
|
||||
options[:include_jobs] = true unless options.key?(:include_jobs)
|
||||
|
||||
@build = build
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'build' => build_data(build),
|
||||
'commit' => commit_data(build.commit, build.repository),
|
||||
'jobs' => options[:include_jobs] ? build.matrix.map { |job| job_data(job) } : [],
|
||||
'annotations' => options[:include_jobs] ? Annotations.new(annotations(build), @options).data["annotations"] : [],
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'event_type' => build.event_type,
|
||||
'pull_request' => build.pull_request?,
|
||||
'pull_request_title' => build.pull_request_title,
|
||||
'pull_request_number' => build.pull_request_number,
|
||||
'config' => build.obfuscated_config.stringify_keys,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => build.matrix_ids
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit, repository)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'branch_is_default' => branch_is_default(commit, repository),
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
|
||||
def job_data(job)
|
||||
{
|
||||
'id' => job.id,
|
||||
'repository_id' => job.repository_id,
|
||||
'build_id' => job.source_id,
|
||||
'commit_id' => job.commit_id,
|
||||
'log_id' => job.log_id,
|
||||
'state' => job.state.to_s,
|
||||
'number' => job.number,
|
||||
'config' => job.obfuscated_config.stringify_keys,
|
||||
'started_at' => format_date(job.started_at),
|
||||
'finished_at' => format_date(job.finished_at),
|
||||
'queue' => job.queue,
|
||||
'allow_failure' => job.allow_failure,
|
||||
'tags' => job.tags,
|
||||
'annotation_ids' => job.annotation_ids,
|
||||
}
|
||||
end
|
||||
|
||||
def branch_is_default(commit, repository)
|
||||
repository.default_branch == commit.branch
|
||||
end
|
||||
|
||||
def annotations(build)
|
||||
build.matrix.map(&:annotations).flatten
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
71
lib/travis/api/serialize/v2/http/builds.rb
Normal file
71
lib/travis/api/serialize/v2/http/builds.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Builds
|
||||
include Formats
|
||||
|
||||
attr_reader :builds, :commits, :options
|
||||
|
||||
def initialize(builds, options = {})
|
||||
@builds = builds
|
||||
@commits = builds.map(&:commit)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'builds' => builds.map { |build| build_data(build) },
|
||||
'commits' => commits.map { |commit| commit_data(commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'event_type' => build.event_type,
|
||||
'pull_request' => build.pull_request?,
|
||||
'pull_request_title' => build.pull_request_title,
|
||||
'pull_request_number' => build.pull_request_number,
|
||||
'config' => build.obfuscated_config.stringify_keys,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => matrix_ids(build)
|
||||
}
|
||||
end
|
||||
|
||||
def matrix_ids(build)
|
||||
build.cached_matrix_ids || build.matrix_ids
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
'pull_request_number' => commit.pull_request_number
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
37
lib/travis/api/serialize/v2/http/caches.rb
Normal file
37
lib/travis/api/serialize/v2/http/caches.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Caches
|
||||
include Formats
|
||||
attr_reader :caches, :options
|
||||
|
||||
def initialize(caches, options = {})
|
||||
@caches = caches
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{ 'caches' => caches.map { |cache| cache_data(cache) } }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cache_data(cache)
|
||||
{
|
||||
'repository_id' => cache.repository.id,
|
||||
'size' => cache.size,
|
||||
'slug' => cache.slug,
|
||||
'branch' => cache.branch,
|
||||
'last_modified' => format_date(cache.last_modified)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
lib/travis/api/serialize/v2/http/env_var.rb
Normal file
25
lib/travis/api/serialize/v2/http/env_var.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class EnvVar < Travis::Api::Serializer
|
||||
attributes :id, :name, :value, :public, :repository_id
|
||||
|
||||
def value
|
||||
if object.public?
|
||||
object.value.decrypt
|
||||
end
|
||||
end
|
||||
|
||||
def serializable_hash
|
||||
hash = super
|
||||
hash.delete :value unless object.public?
|
||||
hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
2
lib/travis/api/serialize/v2/http/env_vars.rb
Normal file
2
lib/travis/api/serialize/v2/http/env_vars.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
class Travis::Api::Serialize::V2::Http::EnvVars < Travis::Api::ArraySerializer
|
||||
end
|
38
lib/travis/api/serialize/v2/http/hooks.rb
Normal file
38
lib/travis/api/serialize/v2/http/hooks.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Hooks
|
||||
attr_reader :hooks, :options
|
||||
|
||||
def initialize(hooks, options = {})
|
||||
@hooks = hooks
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'hooks' => hooks.map { |hook| hook_data(hook) },
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def hook_data(hook)
|
||||
{
|
||||
'id' => hook.id,
|
||||
'name' => hook.name,
|
||||
'owner_name' => hook.owner_name,
|
||||
'description' => hook.description,
|
||||
'active' => hook.active,
|
||||
'private' => hook.private,
|
||||
'admin' => hook.admin?
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
72
lib/travis/api/serialize/v2/http/job.rb
Normal file
72
lib/travis/api/serialize/v2/http/job.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Job
|
||||
include Formats
|
||||
|
||||
attr_reader :job, :options
|
||||
|
||||
def initialize(job, options = {})
|
||||
@job = job
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'job' => job_data(job),
|
||||
'commit' => commit_data(job.commit, job.repository),
|
||||
'annotations' => Annotations.new(job.annotations, @options).data["annotations"],
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def job_data(job)
|
||||
{
|
||||
'id' => job.id,
|
||||
'repository_id' => job.repository_id,
|
||||
'repository_slug' => job.repository.slug,
|
||||
'build_id' => job.source_id,
|
||||
'commit_id' => job.commit_id,
|
||||
'log_id' => job.log_id,
|
||||
'number' => job.number,
|
||||
'config' => job.obfuscated_config.stringify_keys,
|
||||
'state' => job.state.to_s,
|
||||
'started_at' => format_date(job.started_at),
|
||||
'finished_at' => format_date(job.finished_at),
|
||||
'queue' => job.queue,
|
||||
'allow_failure' => job.allow_failure,
|
||||
'tags' => job.tags,
|
||||
'annotation_ids' => job.annotation_ids,
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit, repository)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'branch_is_default' => branch_is_default(commit, repository),
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
|
||||
def branch_is_default(commit, repository)
|
||||
repository.default_branch == commit.branch
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
65
lib/travis/api/serialize/v2/http/jobs.rb
Normal file
65
lib/travis/api/serialize/v2/http/jobs.rb
Normal file
|
@ -0,0 +1,65 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Jobs
|
||||
include Formats
|
||||
|
||||
attr_reader :jobs, :options
|
||||
|
||||
def initialize(jobs, options = {})
|
||||
@jobs = jobs
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'jobs' => jobs.map { |job| job_data(job) },
|
||||
'commits' => jobs.map { |job| commit_data(job.commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def job_data(job)
|
||||
{
|
||||
'id' => job.id,
|
||||
'repository_id' => job.repository_id,
|
||||
'repository_slug' => job.repository.slug,
|
||||
'build_id' => job.source_id,
|
||||
'commit_id' => job.commit_id,
|
||||
'log_id' => job.log_id,
|
||||
'number' => job.number,
|
||||
'config' => job.obfuscated_config.stringify_keys,
|
||||
'state' => job.state.to_s,
|
||||
'started_at' => format_date(job.started_at),
|
||||
'finished_at' => format_date(job.finished_at),
|
||||
'queue' => job.queue,
|
||||
'allow_failure' => job.allow_failure,
|
||||
'tags' => job.tags
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
80
lib/travis/api/serialize/v2/http/log.rb
Normal file
80
lib/travis/api/serialize/v2/http/log.rb
Normal file
|
@ -0,0 +1,80 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Log
|
||||
attr_reader :log, :options
|
||||
|
||||
def initialize(log, options = {})
|
||||
@log = log
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
log_hash = options[:chunked] ? chunked_log_data : log_data
|
||||
if log.removed_at
|
||||
log_hash['removed_at'] = log.removed_at
|
||||
log_hash['removed_by'] = log.removed_by.name || log.removed_by.login
|
||||
end
|
||||
|
||||
{
|
||||
'log' => log_hash,
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log_data
|
||||
{
|
||||
'id' => log.id,
|
||||
'job_id' => log.job_id,
|
||||
'type' => log.class.name.demodulize,
|
||||
'body' => log.content
|
||||
}
|
||||
end
|
||||
|
||||
def chunked_log_data
|
||||
{
|
||||
'id' => log.id,
|
||||
'job_id' => log.job_id,
|
||||
'type' => log.class.name.demodulize,
|
||||
'parts' => log_parts
|
||||
}
|
||||
end
|
||||
|
||||
def log_parts
|
||||
if log.removed_at
|
||||
# if log is removed we don't have actual parts
|
||||
parts = [{ 'number' => 1, 'content' => log.content, 'final' => true }]
|
||||
else
|
||||
parts = log.parts
|
||||
parts = parts.where(number: part_numbers) if part_numbers
|
||||
parts = parts.where(["number > ?", after]) if after
|
||||
parts.sort_by(&:number).map do |part|
|
||||
{
|
||||
'id' => part.id,
|
||||
'number' => part.number,
|
||||
'content' => part.content,
|
||||
'final' => part.final
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def after
|
||||
after = options['after'].to_i
|
||||
after == 0 ? nil : after
|
||||
end
|
||||
|
||||
def part_numbers
|
||||
if numbers = options['part_numbers']
|
||||
numbers.is_a?(String) ? numbers.split(',').map(&:to_i) : numbers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
52
lib/travis/api/serialize/v2/http/permissions.rb
Normal file
52
lib/travis/api/serialize/v2/http/permissions.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Permissions
|
||||
attr_reader :permissions, :options
|
||||
|
||||
def initialize(permissions, options = {})
|
||||
@permissions = permissions
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'permissions' => repo_ids,
|
||||
'admin' => admin_ids,
|
||||
'pull' => pull_ids,
|
||||
'push' => push_ids
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
def filtered_ids(perm = nil)
|
||||
if perm
|
||||
permissions.find_all { |p| p.send("#{perm}?") }.map { |permission| permission.repository_id }
|
||||
else
|
||||
permissions.map { |permission| permission.repository_id }
|
||||
end
|
||||
end
|
||||
|
||||
def repo_ids
|
||||
filtered_ids
|
||||
end
|
||||
|
||||
def admin_ids
|
||||
filtered_ids(:admin)
|
||||
end
|
||||
|
||||
def pull_ids
|
||||
filtered_ids(:pull)
|
||||
end
|
||||
|
||||
def push_ids
|
||||
filtered_ids(:push)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
47
lib/travis/api/serialize/v2/http/repositories.rb
Normal file
47
lib/travis/api/serialize/v2/http/repositories.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Repositories
|
||||
include Formats
|
||||
|
||||
attr_reader :repositories, :options
|
||||
|
||||
def initialize(repositories, options = {})
|
||||
@repositories = repositories
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'repos' => repositories.map { |repository| repository_data(repository) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def repository_data(repository)
|
||||
{
|
||||
'id' => repository.id,
|
||||
'slug' => repository.slug,
|
||||
'description' => repository.description,
|
||||
'last_build_id' => repository.last_build_id,
|
||||
'last_build_number' => repository.last_build_number,
|
||||
'last_build_state' => repository.last_build_state.to_s,
|
||||
'last_build_duration' => repository.last_build_duration,
|
||||
'last_build_language' => nil,
|
||||
'last_build_started_at' => format_date(repository.last_build_started_at),
|
||||
'last_build_finished_at' => format_date(repository.last_build_finished_at),
|
||||
'active' => repository.active,
|
||||
'github_language' => repository.github_language
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
47
lib/travis/api/serialize/v2/http/repository.rb
Normal file
47
lib/travis/api/serialize/v2/http/repository.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Repository
|
||||
include Formats
|
||||
|
||||
attr_reader :repository, :options
|
||||
|
||||
def initialize(repository, options = {})
|
||||
@repository = repository
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'repo' => repository_data(repository)
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# TODO why does this not include the last build? (i.e. 'builds' => { last build here })
|
||||
def repository_data(repository)
|
||||
{
|
||||
'id' => repository.id,
|
||||
'slug' => repository.slug,
|
||||
'active' => repository.active,
|
||||
'description' => repository.description,
|
||||
'last_build_id' => repository.last_build_id,
|
||||
'last_build_number' => repository.last_build_number,
|
||||
'last_build_state' => repository.last_build_state.to_s,
|
||||
'last_build_duration' => repository.last_build_duration,
|
||||
'last_build_language' => nil,
|
||||
'last_build_started_at' => format_date(repository.last_build_started_at),
|
||||
'last_build_finished_at' => format_date(repository.last_build_finished_at),
|
||||
'github_language' => repository.github_language
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
75
lib/travis/api/serialize/v2/http/request.rb
Normal file
75
lib/travis/api/serialize/v2/http/request.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Request
|
||||
include Formats
|
||||
attr_reader :request, :commit, :options
|
||||
|
||||
def initialize(request, options = {})
|
||||
@request = request
|
||||
@commit = request.commit
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
data = {
|
||||
'request' => request_data
|
||||
}
|
||||
if commit
|
||||
data['commit'] = commit_data
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_data
|
||||
data = {
|
||||
'id' => request.id,
|
||||
'repository_id' => request.repository_id,
|
||||
'commit_id' => request.commit_id,
|
||||
'created_at' => format_date(request.created_at),
|
||||
'owner_id' => request.owner_id,
|
||||
'owner_type' => request.owner_type,
|
||||
'event_type' => request.event_type,
|
||||
'base_commit' => request.base_commit,
|
||||
'head_commit' => request.head_commit,
|
||||
'result' => request.result,
|
||||
'message' => request.message,
|
||||
'pull_request' => request.pull_request?,
|
||||
'pull_request_number' => request.pull_request_number,
|
||||
'pull_request_title' => request.pull_request_title,
|
||||
'branch' => request.branch_name,
|
||||
'tag' => request.tag_name
|
||||
}
|
||||
|
||||
data['build_id'] = request.builds.first.id if request.builds.present?
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def commit_data
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
'pull_request_number' => commit.pull_request_number
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
72
lib/travis/api/serialize/v2/http/requests.rb
Normal file
72
lib/travis/api/serialize/v2/http/requests.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class Requests
|
||||
include Formats
|
||||
attr_reader :requests, :commits, :options
|
||||
|
||||
def initialize(requests, options = {})
|
||||
@requests = requests
|
||||
@commits = requests.map(&:commit)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'requests' => requests.map { |request| request_data(request) },
|
||||
'commits' => commits.compact.map { |commit| commit_data(commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_data(request)
|
||||
data = {
|
||||
'id' => request.id,
|
||||
'repository_id' => request.repository_id,
|
||||
'commit_id' => request.commit_id,
|
||||
'created_at' => format_date(request.created_at),
|
||||
'owner_id' => request.owner_id,
|
||||
'owner_type' => request.owner_type,
|
||||
'event_type' => request.event_type,
|
||||
'base_commit' => request.base_commit,
|
||||
'head_commit' => request.head_commit,
|
||||
'result' => request.result,
|
||||
'message' => request.message,
|
||||
'pull_request' => request.pull_request?,
|
||||
'pull_request_number' => request.pull_request_number,
|
||||
'pull_request_title' => request.pull_request_title,
|
||||
'branch' => request.branch_name,
|
||||
'tag' => request.tag_name
|
||||
}
|
||||
|
||||
data['build_id'] = request.builds.first.id if request.builds.present?
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
'pull_request_number' => commit.pull_request_number
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
28
lib/travis/api/serialize/v2/http/ssh_key.rb
Normal file
28
lib/travis/api/serialize/v2/http/ssh_key.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require 'openssl'
|
||||
require 'travis/private_key'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class SshKey < Travis::Api::Serializer
|
||||
attributes :id, :description, :fingerprint
|
||||
|
||||
def id
|
||||
object.repository_id
|
||||
end
|
||||
|
||||
def fingerprint
|
||||
value = object.value.decrypt
|
||||
return unless value
|
||||
PrivateKey.new(value).fingerprint
|
||||
rescue OpenSSL::PKey::RSAError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
28
lib/travis/api/serialize/v2/http/ssl_key.rb
Normal file
28
lib/travis/api/serialize/v2/http/ssl_key.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class SslKey
|
||||
attr_reader :key
|
||||
|
||||
def initialize(key, options = {})
|
||||
@key = key
|
||||
end
|
||||
|
||||
def fingerprint
|
||||
PrivateKey.new(key.private_key).fingerprint
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'key' => key.public_key,
|
||||
'fingerprint' => fingerprint
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
50
lib/travis/api/serialize/v2/http/user.rb
Normal file
50
lib/travis/api/serialize/v2/http/user.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class User
|
||||
include Formats
|
||||
|
||||
attr_reader :user, :options
|
||||
|
||||
def initialize(user, options = {})
|
||||
@user = user
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'user' => user_data,
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_data
|
||||
{
|
||||
'id' => user.id,
|
||||
'name' => user.name,
|
||||
'login' => user.login,
|
||||
'email' => user.email,
|
||||
'gravatar_id' => user.email ? Digest::MD5.hexdigest(user.email) : "",
|
||||
'locale' => user.locale,
|
||||
'is_syncing' => user.syncing?,
|
||||
'synced_at' => format_date(user.synced_at),
|
||||
'correct_scopes' => user.correct_scopes?,
|
||||
'created_at' => format_date(user.created_at),
|
||||
'channels' => channels
|
||||
}
|
||||
end
|
||||
|
||||
def channels
|
||||
["user-#{user.id}"] + user.repository_ids.map { |id| "repo-#{id}" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
40
lib/travis/api/serialize/v2/http/validation_error.rb
Normal file
40
lib/travis/api/serialize/v2/http/validation_error.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V2
|
||||
module Http
|
||||
class ValidationError
|
||||
attr_reader :resource
|
||||
|
||||
def initialize(resource, options = {})
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def data
|
||||
response = {
|
||||
message: 'Validation failed'
|
||||
}
|
||||
resource.errors.to_hash.each do |name, errors|
|
||||
response['errors'] ||= []
|
||||
errors.each do |error_code|
|
||||
response['errors'] << { field: name, code: code(error_code) }
|
||||
end
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
def code(error_code)
|
||||
case error_code
|
||||
when :blank
|
||||
'missing_field'
|
||||
else
|
||||
error_code.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -14,7 +14,7 @@ module Travis
|
|||
end
|
||||
|
||||
def initialize(resource, options)
|
||||
options[:each_serializer] ||= Travis::Api::V2::Http.const_get(options[:root].to_s.singularize.camelize)
|
||||
options[:each_serializer] ||= Travis::Api::Serialize::V2::Http.const_get(options[:root].to_s.singularize.camelize)
|
||||
super(resource, options)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
require 'travis/api/v2/http'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
require 'travis/api/serializer'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
require 'travis/api/v2/http/accounts'
|
||||
require 'travis/api/v2/http/annotations'
|
||||
require 'travis/api/v2/http/broadcasts'
|
||||
require 'travis/api/v2/http/branch'
|
||||
require 'travis/api/v2/http/branches'
|
||||
require 'travis/api/v2/http/build'
|
||||
require 'travis/api/v2/http/builds'
|
||||
require 'travis/api/v2/http/caches'
|
||||
require 'travis/api/v2/http/hooks'
|
||||
require 'travis/api/v2/http/job'
|
||||
require 'travis/api/v2/http/jobs'
|
||||
require 'travis/api/v2/http/log'
|
||||
require 'travis/api/v2/http/permissions'
|
||||
require 'travis/api/v2/http/repositories'
|
||||
require 'travis/api/v2/http/repository'
|
||||
require 'travis/api/v2/http/requests'
|
||||
require 'travis/api/v2/http/request'
|
||||
require 'travis/api/v2/http/ssl_key'
|
||||
require 'travis/api/v2/http/env_var'
|
||||
require 'travis/api/v2/http/env_vars'
|
||||
require 'travis/api/v2/http/user'
|
||||
require 'travis/api/v2/http/validation_error'
|
||||
require 'travis/api/v2/http/ssh_key'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,42 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Accounts
|
||||
include Formats
|
||||
|
||||
attr_reader :accounts, :options
|
||||
|
||||
def initialize(accounts, options = {})
|
||||
@accounts = accounts
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
:accounts => accounts.map { |account| account_data(account) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def account_data(account)
|
||||
data = {
|
||||
'id' => account.id,
|
||||
'name' => account.name,
|
||||
'login' => account.login,
|
||||
'type' => account.type.underscore,
|
||||
'repos_count' => account.repos_count
|
||||
}
|
||||
|
||||
data['avatar_url'] = account.avatar_url if account.respond_to?(:avatar_url)
|
||||
|
||||
data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Annotations
|
||||
include Formats
|
||||
|
||||
def initialize(annotations, options = {})
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
"annotations" => @annotations.map { |annotation| build_annotation(annotation) },
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_annotation(annotation)
|
||||
{
|
||||
"id" => annotation.id,
|
||||
"job_id" => annotation.job_id,
|
||||
"description" => annotation.description,
|
||||
"url" => annotation.url,
|
||||
"status" => annotation.status,
|
||||
"provider_name" => annotation.annotation_provider.name,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,28 +0,0 @@
|
|||
require 'travis/api/v2/http/branches'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Branch < Branches
|
||||
include Formats
|
||||
|
||||
attr_reader :build, :commit, :options
|
||||
|
||||
def initialize(build, options = {})
|
||||
@build = build
|
||||
@commit = build.commit
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'branch' => build_data(build),
|
||||
'commit' => commit_data(commit)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,60 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Branches
|
||||
include Formats
|
||||
|
||||
attr_reader :builds, :commits, :options
|
||||
|
||||
def initialize(builds, options = {})
|
||||
builds = builds.last_finished_builds_by_branches if builds.is_a?(Repository) # TODO remove, bc
|
||||
@builds = builds
|
||||
@commits = builds.map(&:commit)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'branches' => builds.map { |build| build_data(build) },
|
||||
'commits' => commits.map { |commit| commit_data(commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'config' => build.obfuscated_config.stringify_keys,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => build.matrix.map { |job| job.id },
|
||||
'pull_request' => build.pull_request?
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,32 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Broadcasts
|
||||
attr_reader :broadcasts, :options
|
||||
|
||||
def initialize(broadcasts, options = {})
|
||||
@broadcasts = broadcasts
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'broadcasts' => broadcasts.map { |broadcast| broadcast_data(broadcast) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def broadcast_data(broadcast)
|
||||
{
|
||||
'id' => broadcast.id,
|
||||
'message' => broadcast.message
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Build
|
||||
include Formats
|
||||
|
||||
attr_reader :build, :options
|
||||
|
||||
def initialize(build, options = {})
|
||||
options[:include_jobs] = true unless options.key?(:include_jobs)
|
||||
|
||||
@build = build
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'build' => build_data(build),
|
||||
'commit' => commit_data(build.commit, build.repository),
|
||||
'jobs' => options[:include_jobs] ? build.matrix.map { |job| job_data(job) } : [],
|
||||
'annotations' => options[:include_jobs] ? Annotations.new(annotations(build), @options).data["annotations"] : [],
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'event_type' => build.event_type,
|
||||
'pull_request' => build.pull_request?,
|
||||
'pull_request_title' => build.pull_request_title,
|
||||
'pull_request_number' => build.pull_request_number,
|
||||
'config' => build.obfuscated_config.stringify_keys,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => build.matrix_ids
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit, repository)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'branch_is_default' => branch_is_default(commit, repository),
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
|
||||
def job_data(job)
|
||||
{
|
||||
'id' => job.id,
|
||||
'repository_id' => job.repository_id,
|
||||
'build_id' => job.source_id,
|
||||
'commit_id' => job.commit_id,
|
||||
'log_id' => job.log_id,
|
||||
'state' => job.state.to_s,
|
||||
'number' => job.number,
|
||||
'config' => job.obfuscated_config.stringify_keys,
|
||||
'started_at' => format_date(job.started_at),
|
||||
'finished_at' => format_date(job.finished_at),
|
||||
'queue' => job.queue,
|
||||
'allow_failure' => job.allow_failure,
|
||||
'tags' => job.tags,
|
||||
'annotation_ids' => job.annotation_ids,
|
||||
}
|
||||
end
|
||||
|
||||
def branch_is_default(commit, repository)
|
||||
repository.default_branch == commit.branch
|
||||
end
|
||||
|
||||
def annotations(build)
|
||||
build.matrix.map(&:annotations).flatten
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,67 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Builds
|
||||
include Formats
|
||||
|
||||
attr_reader :builds, :commits, :options
|
||||
|
||||
def initialize(builds, options = {})
|
||||
@builds = builds
|
||||
@commits = builds.map(&:commit)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'builds' => builds.map { |build| build_data(build) },
|
||||
'commits' => commits.map { |commit| commit_data(commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'event_type' => build.event_type,
|
||||
'pull_request' => build.pull_request?,
|
||||
'pull_request_title' => build.pull_request_title,
|
||||
'pull_request_number' => build.pull_request_number,
|
||||
'config' => build.obfuscated_config.stringify_keys,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => matrix_ids(build)
|
||||
}
|
||||
end
|
||||
|
||||
def matrix_ids(build)
|
||||
build.cached_matrix_ids || build.matrix_ids
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
'pull_request_number' => commit.pull_request_number
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,33 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Caches
|
||||
include Formats
|
||||
attr_reader :caches, :options
|
||||
|
||||
def initialize(caches, options = {})
|
||||
@caches = caches
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{ 'caches' => caches.map { |cache| cache_data(cache) } }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cache_data(cache)
|
||||
{
|
||||
'repository_id' => cache.repository.id,
|
||||
'size' => cache.size,
|
||||
'slug' => cache.slug,
|
||||
'branch' => cache.branch,
|
||||
'last_modified' => format_date(cache.last_modified)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,23 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class EnvVar < Travis::Api::Serializer
|
||||
attributes :id, :name, :value, :public, :repository_id
|
||||
|
||||
def value
|
||||
if object.public?
|
||||
object.value.decrypt
|
||||
end
|
||||
end
|
||||
|
||||
def serializable_hash
|
||||
hash = super
|
||||
hash.delete :value unless object.public?
|
||||
hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,2 +0,0 @@
|
|||
class Travis::Api::V2::Http::EnvVars < Travis::Api::ArraySerializer
|
||||
end
|
|
@ -1,37 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Hooks
|
||||
attr_reader :hooks, :options
|
||||
|
||||
def initialize(hooks, options = {})
|
||||
@hooks = hooks
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'hooks' => hooks.map { |hook| hook_data(hook) },
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def hook_data(hook)
|
||||
{
|
||||
'id' => hook.id,
|
||||
'name' => hook.name,
|
||||
'owner_name' => hook.owner_name,
|
||||
'description' => hook.description,
|
||||
'active' => hook.active,
|
||||
'private' => hook.private,
|
||||
'admin' => hook.admin?
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Job
|
||||
include Formats
|
||||
|
||||
attr_reader :job, :options
|
||||
|
||||
def initialize(job, options = {})
|
||||
@job = job
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'job' => job_data(job),
|
||||
'commit' => commit_data(job.commit, job.repository),
|
||||
'annotations' => Annotations.new(job.annotations, @options).data["annotations"],
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def job_data(job)
|
||||
{
|
||||
'id' => job.id,
|
||||
'repository_id' => job.repository_id,
|
||||
'repository_slug' => job.repository.slug,
|
||||
'build_id' => job.source_id,
|
||||
'commit_id' => job.commit_id,
|
||||
'log_id' => job.log_id,
|
||||
'number' => job.number,
|
||||
'config' => job.obfuscated_config.stringify_keys,
|
||||
'state' => job.state.to_s,
|
||||
'started_at' => format_date(job.started_at),
|
||||
'finished_at' => format_date(job.finished_at),
|
||||
'queue' => job.queue,
|
||||
'allow_failure' => job.allow_failure,
|
||||
'tags' => job.tags,
|
||||
'annotation_ids' => job.annotation_ids,
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit, repository)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'branch_is_default' => branch_is_default(commit, repository),
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
|
||||
def branch_is_default(commit, repository)
|
||||
repository.default_branch == commit.branch
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,79 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Log
|
||||
attr_reader :log, :options
|
||||
|
||||
def initialize(log, options = {})
|
||||
@log = log
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
log_hash = options[:chunked] ? chunked_log_data : log_data
|
||||
if log.removed_at
|
||||
log_hash['removed_at'] = log.removed_at
|
||||
log_hash['removed_by'] = log.removed_by.name || log.removed_by.login
|
||||
end
|
||||
|
||||
{
|
||||
'log' => log_hash,
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log_data
|
||||
{
|
||||
'id' => log.id,
|
||||
'job_id' => log.job_id,
|
||||
'type' => log.class.name.demodulize,
|
||||
'body' => log.content
|
||||
}
|
||||
end
|
||||
|
||||
def chunked_log_data
|
||||
{
|
||||
'id' => log.id,
|
||||
'job_id' => log.job_id,
|
||||
'type' => log.class.name.demodulize,
|
||||
'parts' => log_parts
|
||||
}
|
||||
end
|
||||
|
||||
def log_parts
|
||||
if log.removed_at
|
||||
# if log is removed we don't have actual parts
|
||||
parts = [{ 'number' => 1, 'content' => log.content, 'final' => true }]
|
||||
else
|
||||
parts = log.parts
|
||||
parts = parts.where(number: part_numbers) if part_numbers
|
||||
parts = parts.where(["number > ?", after]) if after
|
||||
parts.sort_by(&:number).map do |part|
|
||||
{
|
||||
'id' => part.id,
|
||||
'number' => part.number,
|
||||
'content' => part.content,
|
||||
'final' => part.final
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def after
|
||||
after = options['after'].to_i
|
||||
after == 0 ? nil : after
|
||||
end
|
||||
|
||||
def part_numbers
|
||||
if numbers = options['part_numbers']
|
||||
numbers.is_a?(String) ? numbers.split(',').map(&:to_i) : numbers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Permissions
|
||||
attr_reader :permissions, :options
|
||||
|
||||
def initialize(permissions, options = {})
|
||||
@permissions = permissions
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'permissions' => repo_ids,
|
||||
'admin' => admin_ids,
|
||||
'pull' => pull_ids,
|
||||
'push' => push_ids
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
def filtered_ids(perm = nil)
|
||||
if perm
|
||||
permissions.find_all { |p| p.send("#{perm}?") }.map { |permission| permission.repository_id }
|
||||
else
|
||||
permissions.map { |permission| permission.repository_id }
|
||||
end
|
||||
end
|
||||
|
||||
def repo_ids
|
||||
filtered_ids
|
||||
end
|
||||
|
||||
def admin_ids
|
||||
filtered_ids(:admin)
|
||||
end
|
||||
|
||||
def pull_ids
|
||||
filtered_ids(:pull)
|
||||
end
|
||||
|
||||
def push_ids
|
||||
filtered_ids(:push)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,71 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Request
|
||||
include Formats
|
||||
attr_reader :request, :commit, :options
|
||||
|
||||
def initialize(request, options = {})
|
||||
@request = request
|
||||
@commit = request.commit
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
data = {
|
||||
'request' => request_data
|
||||
}
|
||||
if commit
|
||||
data['commit'] = commit_data
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_data
|
||||
data = {
|
||||
'id' => request.id,
|
||||
'repository_id' => request.repository_id,
|
||||
'commit_id' => request.commit_id,
|
||||
'created_at' => format_date(request.created_at),
|
||||
'owner_id' => request.owner_id,
|
||||
'owner_type' => request.owner_type,
|
||||
'event_type' => request.event_type,
|
||||
'base_commit' => request.base_commit,
|
||||
'head_commit' => request.head_commit,
|
||||
'result' => request.result,
|
||||
'message' => request.message,
|
||||
'pull_request' => request.pull_request?,
|
||||
'pull_request_number' => request.pull_request_number,
|
||||
'pull_request_title' => request.pull_request_title,
|
||||
'branch' => request.branch_name,
|
||||
'tag' => request.tag_name
|
||||
}
|
||||
|
||||
data['build_id'] = request.builds.first.id if request.builds.present?
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def commit_data
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
'pull_request_number' => commit.pull_request_number
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,68 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class Requests
|
||||
include Formats
|
||||
attr_reader :requests, :commits, :options
|
||||
|
||||
def initialize(requests, options = {})
|
||||
@requests = requests
|
||||
@commits = requests.map(&:commit)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'requests' => requests.map { |request| request_data(request) },
|
||||
'commits' => commits.compact.map { |commit| commit_data(commit) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_data(request)
|
||||
data = {
|
||||
'id' => request.id,
|
||||
'repository_id' => request.repository_id,
|
||||
'commit_id' => request.commit_id,
|
||||
'created_at' => format_date(request.created_at),
|
||||
'owner_id' => request.owner_id,
|
||||
'owner_type' => request.owner_type,
|
||||
'event_type' => request.event_type,
|
||||
'base_commit' => request.base_commit,
|
||||
'head_commit' => request.head_commit,
|
||||
'result' => request.result,
|
||||
'message' => request.message,
|
||||
'pull_request' => request.pull_request?,
|
||||
'pull_request_number' => request.pull_request_number,
|
||||
'pull_request_title' => request.pull_request_title,
|
||||
'branch' => request.branch_name,
|
||||
'tag' => request.tag_name
|
||||
}
|
||||
|
||||
data['build_id'] = request.builds.first.id if request.builds.present?
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
'pull_request_number' => commit.pull_request_number
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,26 +0,0 @@
|
|||
require 'openssl'
|
||||
require 'travis/private_key'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class SshKey < Travis::Api::Serializer
|
||||
attributes :id, :description, :fingerprint
|
||||
|
||||
def id
|
||||
object.repository_id
|
||||
end
|
||||
|
||||
def fingerprint
|
||||
value = object.value.decrypt
|
||||
return unless value
|
||||
PrivateKey.new(value).fingerprint
|
||||
rescue OpenSSL::PKey::RSAError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,27 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class SslKey
|
||||
attr_reader :key
|
||||
|
||||
def initialize(key, options = {})
|
||||
@key = key
|
||||
end
|
||||
|
||||
def fingerprint
|
||||
PrivateKey.new(key.private_key).fingerprint
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'key' => key.public_key,
|
||||
'fingerprint' => fingerprint
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class User
|
||||
include Formats
|
||||
|
||||
attr_reader :user, :options
|
||||
|
||||
def initialize(user, options = {})
|
||||
@user = user
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'user' => user_data,
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_data
|
||||
{
|
||||
'id' => user.id,
|
||||
'name' => user.name,
|
||||
'login' => user.login,
|
||||
'email' => user.email,
|
||||
'gravatar_id' => user.email ? Digest::MD5.hexdigest(user.email) : "",
|
||||
'locale' => user.locale,
|
||||
'is_syncing' => user.syncing?,
|
||||
'synced_at' => format_date(user.synced_at),
|
||||
'correct_scopes' => user.correct_scopes?,
|
||||
'created_at' => format_date(user.created_at),
|
||||
'channels' => channels
|
||||
}
|
||||
end
|
||||
|
||||
def channels
|
||||
["user-#{user.id}"] + user.repository_ids.map { |id| "repo-#{id}" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module V2
|
||||
module Http
|
||||
class ValidationError
|
||||
attr_reader :resource
|
||||
|
||||
def initialize(resource, options = {})
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def data
|
||||
response = {
|
||||
message: 'Validation failed'
|
||||
}
|
||||
resource.errors.to_hash.each do |name, errors|
|
||||
response['errors'] ||= []
|
||||
errors.each do |error_code|
|
||||
response['errors'] << { field: name, code: code(error_code) }
|
||||
end
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
def code(error_code)
|
||||
case error_code
|
||||
when :blank
|
||||
'missing_field'
|
||||
else
|
||||
error_code.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -22,16 +22,16 @@ describe Travis::Api::App::SettingsEndpoint do
|
|||
serializer_class = Class.new(Travis::Api::Serializer) do
|
||||
attributes :id, :name
|
||||
end
|
||||
Travis::Api::V2::Http.const_set(:Item, serializer_class)
|
||||
Travis::Api::V2::Http.const_set(:Items, Travis::Api::ArraySerializer)
|
||||
Travis::Api::Serialize::V2::Http.const_set(:Item, serializer_class)
|
||||
Travis::Api::Serialize::V2::Http.const_set(:Items, Travis::Api::ArraySerializer)
|
||||
|
||||
add_settings_endpoint :items
|
||||
end
|
||||
|
||||
after do
|
||||
Travis::Api::App::Endpoint.send :remove_const, :Items
|
||||
Travis::Api::V2::Http.send :remove_const, :Items
|
||||
Travis::Api::V2::Http.send :remove_const, :Item
|
||||
Travis::Api::Serialize::V2::Http.send :remove_const, :Items
|
||||
Travis::Api::Serialize::V2::Http.send :remove_const, :Item
|
||||
end
|
||||
|
||||
describe 'with authenticated user' do
|
||||
|
|
|
@ -18,14 +18,14 @@ describe Travis::Api::App::SettingsEndpoint do
|
|||
serializer_class = Class.new(Travis::Api::Serializer) do
|
||||
attributes :name
|
||||
end
|
||||
Travis::Api::V2::Http.const_set(:Item, serializer_class)
|
||||
Travis::Api::Serialize::V2::Http.const_set(:Item, serializer_class)
|
||||
|
||||
add_settings_endpoint :item, singleton: true
|
||||
end
|
||||
|
||||
after do
|
||||
Travis::Api::App::Endpoint.send :remove_const, :Item
|
||||
Travis::Api::V2::Http.send :remove_const, :Item
|
||||
Travis::Api::Serialize::V2::Http.send :remove_const, :Item
|
||||
end
|
||||
|
||||
describe 'with authenticated user' do
|
||||
|
|
|
@ -10,7 +10,7 @@ describe 'App' do
|
|||
end
|
||||
|
||||
it 'uses version from current accept header' do
|
||||
Travis::Api.expects(:builder).with { |r, options| options[:version] == 'v1' }
|
||||
Travis::Api::Serialize.expects(:builder).with { |r, options| options[:version] == 'v1' }
|
||||
|
||||
Travis::Api::App::Responders::Json.any_instance.stubs(:apply?).
|
||||
returns(false).then.returns(true)
|
||||
|
@ -20,7 +20,7 @@ describe 'App' do
|
|||
end
|
||||
|
||||
it 'uses v1 by default' do
|
||||
Travis::Api.expects(:builder).with { |r, options| options[:version] == 'v1' }
|
||||
Travis::Api::Serialize.expects(:builder).with { |r, options| options[:version] == 'v1' }
|
||||
get '/foo', {}, 'HTTP_ACCEPT' => 'application/json'
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ RSpec::Matchers.define :deliver_json_for do |resource, options = {}|
|
|||
match do |response|
|
||||
if response.status == 200
|
||||
actual = parse(response.body)
|
||||
expected = resource.is_a?(Hash) ? resource : Travis::Api.data(resource, options)
|
||||
expected = resource.is_a?(Hash) ? resource : Travis::Api::Serialize.data(resource, options)
|
||||
|
||||
failure_message_for_should do
|
||||
"expected\n\n#{actual}\n\nto equal\n\n#{expected}"
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Accounts do
|
||||
describe Travis::Api::Serialize::V2::Http::Accounts do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:user) { { 'id' => 1, 'type' => 'User', 'login' => 'sven', 'name' => 'Sven', 'repos_count' => 2 } }
|
||||
let(:org) { { 'id' => 1, 'type' => 'Organization', 'login' => 'travis', 'name' => 'Travis', 'repos_count' => 1, 'avatar_url' => 'https://example.org/avatar.png' } }
|
||||
|
||||
let(:accounts) { [Account.new(user), Account.new(org)] }
|
||||
let(:data) { Travis::Api::V2::Http::Accounts.new(accounts).data }
|
||||
let(:data) { described_class.new(accounts).data }
|
||||
|
||||
it 'accounts' do
|
||||
data[:accounts].should == [
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Annotations do
|
||||
describe Travis::Api::Serialize::V2::Http::Annotations do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { described_class.new([annotation]).data }
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Branch do
|
||||
describe Travis::Api::Serialize::V2::Http::Branch do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Branch.new(branch).data }
|
||||
let(:data) { described_class.new(branch).data }
|
||||
let(:branch) { build }
|
||||
|
||||
specify 'branch' do
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Branches do
|
||||
describe Travis::Api::Serialize::V2::Http::Branches do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Branches.new(branches).data }
|
||||
let(:data) { described_class.new(branches).data }
|
||||
let(:branches) { [build] }
|
||||
|
||||
it 'branches' do
|
|
@ -1,10 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Broadcasts do
|
||||
describe Travis::Api::Serialize::V2::Http::Broadcasts do
|
||||
include Support::Formats
|
||||
|
||||
let(:broadcast) { stub(:id => 1, :message => 'yo hey!') }
|
||||
let(:data) { Travis::Api::V2::Http::Broadcasts.new([broadcast]).data }
|
||||
let(:data) { described_class.new([broadcast]).data }
|
||||
|
||||
it 'broadcasts' do
|
||||
data['broadcasts'].first.should == {
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Build do
|
||||
describe Travis::Api::Serialize::V2::Http::Build do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Build.new(build).data }
|
||||
let(:data) { described_class.new(build).data }
|
||||
|
||||
it 'build' do
|
||||
data['build'].should == {
|
||||
|
@ -46,7 +46,7 @@ describe Travis::Api::V2::Http::Build do
|
|||
pull_request_title: 'A pull request',
|
||||
pull_request_number: 44
|
||||
end
|
||||
let(:data) { Travis::Api::V2::Http::Build.new(build).data }
|
||||
let(:data) { described_class.new(build).data }
|
||||
|
||||
it 'returns pull request data' do
|
||||
data['build']['pull_request'].should == true
|
||||
|
@ -75,10 +75,10 @@ describe Travis::Api::V2::Http::Build do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Travis::Api::V2::Http::Build using Travis::Services::Builds::FindOne' do
|
||||
describe Travis::Api::Serialize::V2::Http::Build, 'using Travis::Services::Builds::FindOne' do
|
||||
let!(:record) { Factory(:build) }
|
||||
let(:build) { Travis.run_service(:find_build, nil, :id => record.id) }
|
||||
let(:data) { Travis::Api::V2::Http::Build.new(build).data }
|
||||
let(:data) { described_class.new(build).data }
|
||||
|
||||
it 'queries' do
|
||||
lambda { data }.should issue_queries(8)
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Builds do
|
||||
describe Travis::Api::Serialize::V2::Http::Builds do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Builds.new([build]).data }
|
||||
let(:data) { described_class.new([build]).data }
|
||||
|
||||
it 'builds' do
|
||||
data['builds'].first.should == {
|
||||
|
@ -43,7 +43,7 @@ describe Travis::Api::V2::Http::Builds do
|
|||
it 'uses uses cached_matrix_ids if the column exists in DB' do
|
||||
build = stub_build
|
||||
build.expects(:cached_matrix_ids).returns([1, 2, 3])
|
||||
data = Travis::Api::V2::Http::Builds.new([build]).data
|
||||
data = described_class.new([build]).data
|
||||
data['builds'].first['job_ids'].should == [1, 2, 3]
|
||||
end
|
||||
|
||||
|
@ -61,10 +61,10 @@ describe Travis::Api::V2::Http::Builds do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Travis::Api::V2::Http::Builds using Travis::Services::Builds::FindAll' do
|
||||
describe Travis::Api::Serialize::V2::Http::Builds, 'using Travis::Services::Builds::FindAll' do
|
||||
let!(:repo) { Factory(:repository) }
|
||||
let(:builds) { Travis.run_service(:find_builds, nil, :event_type => 'push', :repository_id => repo.id) }
|
||||
let(:data) { Travis::Api::V2::Http::Builds.new(builds).data }
|
||||
let(:data) { described_class.new(builds).data }
|
||||
|
||||
before :each do
|
||||
3.times { Factory(:build, :repository => repo) }
|
|
@ -1,8 +1,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Branch do
|
||||
describe Travis::Api::Serialize::V2::Http::Caches do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
let(:data) { Travis::Api::V2::Http::Caches.new([cache]).data }
|
||||
let(:data) { described_class.new([cache]).data }
|
||||
|
||||
specify 'caches' do
|
||||
data['caches'].should be == [{
|
||||
|
@ -13,4 +13,4 @@ describe Travis::Api::V2::Http::Branch do
|
|||
"last_modified" => "1970-01-01T00:00:00Z"
|
||||
}]
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,8 +1,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::EnvVar do
|
||||
describe Travis::Api::Serialize::V2::Http::EnvVar do
|
||||
let(:env_var) { Repository::Settings::EnvVar.new(name: 'FOO', value: 'bar', public: true) }
|
||||
let(:data) { Travis::Api::V2::Http::EnvVar.new(env_var) }
|
||||
let(:data) { described_class.new(env_var) }
|
||||
|
||||
it 'returns value' do
|
||||
data.as_json['env_var'][:value].should == 'bar'
|
|
@ -1,12 +1,12 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Hooks do
|
||||
describe Travis::Api::Serialize::V2::Http::Hooks do
|
||||
include Travis::Testing::Stubs
|
||||
|
||||
let(:data) {
|
||||
r = repository
|
||||
r.stubs(:admin?).returns(true)
|
||||
Travis::Api::V2::Http::Hooks.new([r]).data
|
||||
described_class.new([r]).data
|
||||
}
|
||||
|
||||
it 'hooks' do
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Job do
|
||||
describe Travis::Api::Serialize::V2::Http::Job do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Job.new(test).data }
|
||||
let(:data) { described_class.new(test).data }
|
||||
|
||||
it 'job' do
|
||||
data['job'].should == {
|
||||
|
@ -63,10 +63,10 @@ describe Travis::Api::V2::Http::Job do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Travis::Api::V2::Http::Job using Travis::Services::Jobs::FindOne' do
|
||||
describe Travis::Api::Serialize::V2::Http::Job, 'using Travis::Services::Jobs::FindOne' do
|
||||
let!(:record) { Factory(:test) }
|
||||
let(:job) { Travis.run_service(:find_job, nil, :id => record.id) }
|
||||
let(:data) { Travis::Api::V2::Http::Job.new(job).data }
|
||||
let(:data) { described_class.new(job).data }
|
||||
|
||||
it 'queries' do
|
||||
lambda { data }.should issue_queries(5)
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Jobs do
|
||||
describe Travis::Api::Serialize::V2::Http::Jobs do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Jobs.new([test]).data }
|
||||
let(:data) { described_class.new([test]).data }
|
||||
|
||||
it 'jobs' do
|
||||
data['jobs'].first.should == {
|
||||
|
@ -40,9 +40,9 @@ describe Travis::Api::V2::Http::Jobs do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Travis::Api::V2::Http::Jobs using Travis::Services::Jobs::FindAll' do
|
||||
describe Travis::Api::Serialize::V2::Http::Jobs, 'using Travis::Services::Jobs::FindAll' do
|
||||
let(:jobs) { Travis.run_service(:find_jobs, nil) }
|
||||
let(:data) { Travis::Api::V2::Http::Jobs.new(jobs).data }
|
||||
let(:data) { described_class.new(jobs).data }
|
||||
|
||||
before :each do
|
||||
3.times { Factory(:test) }
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Log do
|
||||
describe Travis::Api::Serialize::V2::Http::Log do
|
||||
include Travis::Testing::Stubs
|
||||
|
||||
let(:log) {
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Permissions do
|
||||
describe Travis::Api::Serialize::V2::Http::Permissions do
|
||||
include Travis::Testing::Stubs
|
||||
|
||||
let(:permissions) do
|
||||
|
@ -11,7 +11,7 @@ describe Travis::Api::V2::Http::Permissions do
|
|||
]
|
||||
end
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Permissions.new(permissions).data }
|
||||
let(:data) { described_class.new(permissions).data }
|
||||
|
||||
it 'permissions' do
|
||||
data['permissions'].should == [1, 2, 3]
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Repositories do
|
||||
describe Travis::Api::Serialize::V2::Http::Repositories do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Repositories.new([repository]).data }
|
||||
let(:data) { described_class.new([repository]).data }
|
||||
|
||||
it 'repositories' do
|
||||
data['repos'].first.should == {
|
||||
|
@ -23,9 +23,9 @@ describe Travis::Api::V2::Http::Repositories do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Travis::Api::V2::Http::Repositories using Travis::Services::FindRepos' do
|
||||
describe Travis::Api::Serialize::V2::Http::Repositories, 'using Travis::Services::FindRepos' do
|
||||
let(:repos) { Travis.run_service(:find_repos) }
|
||||
let(:data) { Travis::Api::V2::Http::Repositories.new(repos).data }
|
||||
let(:data) { described_class.new(repos).data }
|
||||
|
||||
before :each do
|
||||
3.times { |i| Factory(:repository, :name => i) }
|
|
@ -1,10 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Repository do
|
||||
describe Travis::Api::Serialize::V2::Http::Repository do
|
||||
include Travis::Testing::Stubs
|
||||
include Support::Formats
|
||||
|
||||
let(:data) { Travis::Api::V2::Http::Repository.new(repository).data }
|
||||
let(:data) { described_class.new(repository).data }
|
||||
|
||||
it 'repository' do
|
||||
data['repo'].should == {
|
||||
|
@ -24,10 +24,10 @@ describe Travis::Api::V2::Http::Repository do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Travis::Api::V2::Http::Repository using Travis::Services::FindRepo' do
|
||||
describe Travis::Api::Serialize::V2::Http::Repository, 'using Travis::Services::FindRepo' do
|
||||
let!(:record) { Factory(:repository) }
|
||||
let(:repo) { Travis.run_service(:find_repo, :id => record.id) }
|
||||
let(:data) { Travis::Api::V2::Http::Repository.new(repo).data }
|
||||
let(:data) { described_class.new(repo).data }
|
||||
|
||||
it 'queries' do
|
||||
lambda { data }.should issue_queries(1)
|
|
@ -1,13 +1,13 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Request do
|
||||
describe Travis::Api::Serialize::V2::Http::Request do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:data) {
|
||||
request = stub_request
|
||||
request.stubs(:builds).returns([build])
|
||||
request.stubs(:tag_name).returns(nil)
|
||||
Travis::Api::V2::Http::Request.new(request).data
|
||||
described_class.new(request).data
|
||||
}
|
||||
|
||||
it 'returns request data' do
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Requests do
|
||||
describe Travis::Api::Serialize::V2::Http::Requests do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
before do
|
||||
|
@ -11,7 +11,7 @@ describe Travis::Api::V2::Http::Requests do
|
|||
request = stub_request
|
||||
request.stubs(:builds).returns([build])
|
||||
request.stubs(:tag_name).returns(nil)
|
||||
Travis::Api::V2::Http::Requests.new([request]).data
|
||||
described_class.new([request]).data
|
||||
}
|
||||
|
||||
it 'returns requests data' do
|
||||
|
@ -59,7 +59,7 @@ describe Travis::Api::V2::Http::Requests do
|
|||
request = stub_request
|
||||
request.stubs(:commit).returns(nil)
|
||||
request.stubs(:builds).returns([build])
|
||||
Travis::Api::V2::Http::Requests.new([request]).data
|
||||
described_class.new([request]).data
|
||||
}
|
||||
|
||||
it "doesn't fail if there is no commit data for a given request" do
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::Repository do
|
||||
describe Travis::Api::Serialize::V2::Http::SslKey do
|
||||
include Travis::Testing::Stubs
|
||||
include Support::Formats
|
||||
|
||||
|
@ -9,7 +9,7 @@ describe Travis::Api::V2::Http::Repository do
|
|||
key.stubs(:private_key).returns(TEST_PRIVATE_KEY)
|
||||
key
|
||||
}
|
||||
let(:data) { Travis::Api::V2::Http::SslKey.new(key).data }
|
||||
let(:data) { described_class.new(key).data }
|
||||
|
||||
it 'returns data' do
|
||||
data['key'].should == '-----BEGIN PUBLIC KEY-----'
|
|
@ -1,10 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::Api::V2::Http::User do
|
||||
describe Travis::Api::Serialize::V2::Http::User do
|
||||
include Travis::Testing::Stubs, Support::Formats
|
||||
|
||||
let(:user) { stub_user(repository_ids: [1, 4, 8]) }
|
||||
let(:data) { Travis::Api::V2::Http::User.new(user).data }
|
||||
let(:data) { described_class.new(user).data }
|
||||
|
||||
it 'user' do
|
||||
data['user'].should == {
|
2
vendor/travis-core/lib/travis.rb
vendored
2
vendor/travis-core/lib/travis.rb
vendored
|
@ -42,7 +42,7 @@ module Travis
|
|||
require 'travis/task'
|
||||
require 'travis/event'
|
||||
require 'travis/addons'
|
||||
require 'travis/api'
|
||||
require 'travis/api/serialize'
|
||||
require 'travis/config/defaults'
|
||||
require 'travis/features'
|
||||
require 'travis/github'
|
||||
|
|
|
@ -16,7 +16,7 @@ module Travis
|
|||
|
||||
def initialize(*)
|
||||
super
|
||||
@payload = Api.data(object, for: 'event', version: 'v0', params: data)
|
||||
@payload = Serializer.data(object, for: 'event', version: 'v0', params: data)
|
||||
end
|
||||
|
||||
def handle?
|
||||
|
|
|
@ -16,7 +16,7 @@ module Travis
|
|||
|
||||
def initialize(*)
|
||||
super
|
||||
@pusher_payload = Api.data(object, :for => 'pusher', :type => type, :params => data) if handle?
|
||||
@pusher_payload = Serializer.data(object, :for => 'pusher', :type => type, :params => data) if handle?
|
||||
end
|
||||
|
||||
def handle?
|
||||
|
|
|
@ -25,7 +25,7 @@ module Travis
|
|||
end
|
||||
|
||||
def webhook_payload
|
||||
Api.data(object, :for => 'webhook', :type => 'build/finished', :version => 'v1')
|
||||
Serializer.data(object, :for => 'webhook', :type => 'build/finished', :version => 'v1')
|
||||
end
|
||||
|
||||
def targets
|
||||
|
|
67
vendor/travis-core/lib/travis/api.rb
vendored
67
vendor/travis-core/lib/travis/api.rb
vendored
|
@ -1,67 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
require 'travis/api/formats'
|
||||
require 'travis/api/v0'
|
||||
require 'travis/api/v1'
|
||||
require 'travis/api/v2'
|
||||
|
||||
DEFAULT_VERSION = 'v2'
|
||||
|
||||
class << self
|
||||
def data(resource, options = {})
|
||||
new(resource, options).data
|
||||
end
|
||||
|
||||
def builder(resource, options = {})
|
||||
target = (options[:for] || 'http').to_s.camelize
|
||||
version = (options[:version] || default_version(options)).to_s.camelize
|
||||
type = (options[:type] || type_for(resource)).to_s.camelize.split('::')
|
||||
([version, target] + type).inject(Travis::Api) do |const, name|
|
||||
begin
|
||||
if const && const.const_defined?(name.to_s.camelize, false)
|
||||
const.const_get(name, false)
|
||||
else
|
||||
nil
|
||||
end
|
||||
rescue NameError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def new(resource, options = {})
|
||||
builder = builder(resource, options) || raise(ArgumentError, "cannot serialize #{resource.inspect}, options: #{options.inspect}")
|
||||
builder.new(resource, options[:params] || {})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def type_for(resource)
|
||||
if arel_relation?(resource)
|
||||
type = resource.klass.name.pluralize
|
||||
else
|
||||
type = resource.class
|
||||
type = type.base_class if active_record?(type)
|
||||
type = type.name
|
||||
end
|
||||
type.split('::').last
|
||||
end
|
||||
|
||||
def arel_relation?(object)
|
||||
object.respond_to?(:klass)
|
||||
end
|
||||
|
||||
def active_record?(object)
|
||||
object.respond_to?(:base_class)
|
||||
end
|
||||
|
||||
def default_version(options)
|
||||
if options[:for].to_s.downcase == "pusher"
|
||||
"v0"
|
||||
else
|
||||
DEFAULT_VERSION
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,10 +0,0 @@
|
|||
This directory contains serializers for events and models.
|
||||
|
||||
- `v0/event`: Payloads used by [`Travis::Event::Handler`](../event/handler.rb). These are the payloads that the [addons](../addons) will get.
|
||||
- `v0/pusher`: Payloads used to send events to the web UI using Pusher.
|
||||
- `v0/worker`: Payloads sent to [travis-worker](https://github.com/travis-ci/travis-worker).
|
||||
|
||||
- `v1/http`: Payloads for the v1 [API](https://github.com/travis-ci/travis-api).
|
||||
- `v1/webhook`: Payloads for the webhook notifications.
|
||||
|
||||
- `v2/http`: Payloads for the v2 [API](https://github.com/travis-ci/travis-api).
|
9
vendor/travis-core/lib/travis/api/formats.rb
vendored
9
vendor/travis-core/lib/travis/api/formats.rb
vendored
|
@ -1,9 +0,0 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Formats
|
||||
def format_date(date)
|
||||
date && date.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
68
vendor/travis-core/lib/travis/api/serialize.rb
vendored
Normal file
68
vendor/travis-core/lib/travis/api/serialize.rb
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
require 'travis/api/serialize/formats'
|
||||
require 'travis/api/serialize/v0'
|
||||
require 'travis/api/serialize/v1'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
DEFAULT_VERSION = 'v2'
|
||||
|
||||
class << self
|
||||
def data(resource, options = {})
|
||||
new(resource, options).data
|
||||
end
|
||||
|
||||
def builder(resource, options = {})
|
||||
target = (options[:for] || 'http').to_s.camelize
|
||||
version = (options[:version] || default_version(options)).to_s.camelize
|
||||
type = (options[:type] || type_for(resource)).to_s.camelize.split('::')
|
||||
([version, target] + type).inject(self) do |const, name|
|
||||
begin
|
||||
if const && const.const_defined?(name.to_s.camelize, false)
|
||||
const.const_get(name, false)
|
||||
else
|
||||
nil
|
||||
end
|
||||
rescue NameError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def new(resource, options = {})
|
||||
builder = builder(resource, options) || raise(ArgumentError, "cannot serialize #{resource.inspect}, options: #{options.inspect}")
|
||||
builder.new(resource, options[:params] || {})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def type_for(resource)
|
||||
if arel_relation?(resource)
|
||||
type = resource.klass.name.pluralize
|
||||
else
|
||||
type = resource.class
|
||||
type = type.base_class if active_record?(type)
|
||||
type = type.name
|
||||
end
|
||||
type.split('::').last
|
||||
end
|
||||
|
||||
def arel_relation?(object)
|
||||
object.respond_to?(:klass)
|
||||
end
|
||||
|
||||
def active_record?(object)
|
||||
object.respond_to?(:base_class)
|
||||
end
|
||||
|
||||
def default_version(options)
|
||||
if options[:for].to_s.downcase == "pusher"
|
||||
"v0"
|
||||
else
|
||||
DEFAULT_VERSION
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
11
vendor/travis-core/lib/travis/api/serialize/formats.rb
vendored
Normal file
11
vendor/travis-core/lib/travis/api/serialize/formats.rb
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module Formats
|
||||
def format_date(date)
|
||||
date && date.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
6
vendor/travis-core/lib/travis/api/serialize/v0.rb
vendored
Normal file
6
vendor/travis-core/lib/travis/api/serialize/v0.rb
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
# V0 is an internal api that we can change at any time
|
||||
|
||||
require 'travis/api/serialize/v0/event'
|
||||
require 'travis/api/serialize/v0/notification'
|
||||
require 'travis/api/serialize/v0/pusher'
|
||||
require 'travis/api/serialize/v0/worker'
|
2
vendor/travis-core/lib/travis/api/serialize/v0/event.rb
vendored
Normal file
2
vendor/travis-core/lib/travis/api/serialize/v0/event.rb
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
require 'travis/api/serialize/v0/event/build'
|
||||
require 'travis/api/serialize/v0/event/job'
|
96
vendor/travis-core/lib/travis/api/serialize/v0/event/build.rb
vendored
Normal file
96
vendor/travis-core/lib/travis/api/serialize/v0/event/build.rb
vendored
Normal file
|
@ -0,0 +1,96 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Event
|
||||
class Build
|
||||
include Formats
|
||||
|
||||
attr_reader :build, :repository, :request, :commit, :options
|
||||
|
||||
def initialize(build, options = {})
|
||||
@build = build
|
||||
@repository = build.repository
|
||||
@request = build.request
|
||||
@commit = build.commit
|
||||
# @options = options
|
||||
end
|
||||
|
||||
def data(extra = {})
|
||||
{
|
||||
'repository' => repository_data,
|
||||
'request' => request_data,
|
||||
'commit' => commit_data,
|
||||
'build' => build_data,
|
||||
'jobs' => build.matrix.map { |job| job_data(job) }
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'pull_request' => build.pull_request?,
|
||||
'pull_request_number' => build.pull_request_number,
|
||||
'config' => build.config.try(:except, :source_key),
|
||||
'state' => build.state.to_s,
|
||||
'previous_state' => build.previous_state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => build.matrix_ids
|
||||
}
|
||||
end
|
||||
|
||||
def repository_data
|
||||
{
|
||||
'id' => repository.id,
|
||||
'key' => repository.key.try(:public_key),
|
||||
'slug' => repository.slug,
|
||||
'name' => repository.name,
|
||||
'owner_email' => repository.owner_email,
|
||||
'owner_avatar_url' => repository.owner.try(:avatar_url)
|
||||
}
|
||||
end
|
||||
|
||||
def request_data
|
||||
{
|
||||
'token' => request.token,
|
||||
'head_commit' => (request.head_commit || '')
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
|
||||
def job_data(job)
|
||||
{
|
||||
'id' => job.id,
|
||||
'number' => job.number,
|
||||
'state' => job.state.to_s,
|
||||
'tags' => job.tags
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
37
vendor/travis-core/lib/travis/api/serialize/v0/event/job.rb
vendored
Normal file
37
vendor/travis-core/lib/travis/api/serialize/v0/event/job.rb
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Event
|
||||
class Job
|
||||
include Formats
|
||||
|
||||
attr_reader :job
|
||||
|
||||
def initialize(job, options = {})
|
||||
@job = job
|
||||
# @options = options
|
||||
end
|
||||
|
||||
def data(extra = {})
|
||||
{
|
||||
'job' => job_data,
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def job_data
|
||||
{
|
||||
'queue' => job.queue,
|
||||
'created_at' => job.created_at,
|
||||
'started_at' => job.started_at,
|
||||
'finished_at' => job.finished_at,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
3
vendor/travis-core/lib/travis/api/serialize/v0/notification.rb
vendored
Normal file
3
vendor/travis-core/lib/travis/api/serialize/v0/notification.rb
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
require 'travis/api/serialize/v0/notification/build'
|
||||
require 'travis/api/serialize/v0/notification/repository'
|
||||
require 'travis/api/serialize/v0/notification/user'
|
29
vendor/travis-core/lib/travis/api/serialize/v0/notification/build.rb
vendored
Normal file
29
vendor/travis-core/lib/travis/api/serialize/v0/notification/build.rb
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Notification
|
||||
class Build
|
||||
attr_reader :build
|
||||
|
||||
def initialize(build, options = {})
|
||||
@build = build
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'build' => build_data
|
||||
}
|
||||
end
|
||||
|
||||
def build_data
|
||||
{
|
||||
'id' => build.id
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
30
vendor/travis-core/lib/travis/api/serialize/v0/notification/repository.rb
vendored
Normal file
30
vendor/travis-core/lib/travis/api/serialize/v0/notification/repository.rb
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Notification
|
||||
class Repository
|
||||
attr_reader :repository
|
||||
|
||||
def initialize(repository, options = {})
|
||||
@repository = repository
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'repository' => repository_data
|
||||
}
|
||||
end
|
||||
|
||||
def repository_data
|
||||
{
|
||||
'id' => repository.id,
|
||||
'slug' => repository.slug
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
30
vendor/travis-core/lib/travis/api/serialize/v0/notification/user.rb
vendored
Normal file
30
vendor/travis-core/lib/travis/api/serialize/v0/notification/user.rb
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Notification
|
||||
class User
|
||||
attr_reader :user
|
||||
|
||||
def initialize(user, options = {})
|
||||
@user = user
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'user' => user_data
|
||||
}
|
||||
end
|
||||
|
||||
def user_data
|
||||
{
|
||||
'id' => user.id,
|
||||
'login' => user.login
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
3
vendor/travis-core/lib/travis/api/serialize/v0/pusher.rb
vendored
Normal file
3
vendor/travis-core/lib/travis/api/serialize/v0/pusher.rb
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
require 'travis/api/serialize/v0/pusher/annotation'
|
||||
require 'travis/api/serialize/v0/pusher/build'
|
||||
require 'travis/api/serialize/v0/pusher/job'
|
35
vendor/travis-core/lib/travis/api/serialize/v0/pusher/annotation.rb
vendored
Normal file
35
vendor/travis-core/lib/travis/api/serialize/v0/pusher/annotation.rb
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
require 'travis/api/serialize/v0/pusher/annotation/created'
|
||||
require 'travis/api/serialize/v0/pusher/annotation/updated'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Annotation
|
||||
include Formats
|
||||
|
||||
attr_reader :annotation
|
||||
|
||||
def initialize(annotation, options = {})
|
||||
@annotation = annotation
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
"annotation" => {
|
||||
"id" => annotation.id,
|
||||
"job_id" => annotation.job_id,
|
||||
"description" => annotation.description,
|
||||
"url" => annotation.url,
|
||||
"status" => annotation.status,
|
||||
"provider_name" => annotation.annotation_provider.name,
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/annotation/created.rb
vendored
Normal file
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/annotation/created.rb
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Annotation
|
||||
class Created < Annotation
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/annotation/updated.rb
vendored
Normal file
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/annotation/updated.rb
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Annotation
|
||||
class Updated < Annotation
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
112
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build.rb
vendored
Normal file
112
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build.rb
vendored
Normal file
|
@ -0,0 +1,112 @@
|
|||
require 'travis/api/serialize/v0/pusher/build/canceled'
|
||||
require 'travis/api/serialize/v0/pusher/build/created'
|
||||
require 'travis/api/serialize/v0/pusher/build/received'
|
||||
require 'travis/api/serialize/v0/pusher/build/started'
|
||||
require 'travis/api/serialize/v0/pusher/build/finished'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Build
|
||||
include Formats
|
||||
|
||||
attr_reader :build, :options
|
||||
|
||||
def initialize(build, options = {})
|
||||
@build = build
|
||||
@options = options
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'build' => build_data(build),
|
||||
'commit' => commit_data(build.commit),
|
||||
'repository' => repository_data(build.repository)
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_data(build)
|
||||
commit = build.commit
|
||||
{
|
||||
'id' => build.id,
|
||||
'repository_id' => build.repository_id,
|
||||
'commit_id' => build.commit_id,
|
||||
'number' => build.number,
|
||||
'pull_request' => build.pull_request?,
|
||||
'pull_request_title' => build.pull_request_title,
|
||||
'pull_request_number' => build.pull_request_number,
|
||||
'state' => build.state.to_s,
|
||||
'started_at' => format_date(build.started_at),
|
||||
'finished_at' => format_date(build.finished_at),
|
||||
'duration' => build.duration,
|
||||
'job_ids' => build.matrix_ids,
|
||||
'event_type' => build.event_type,
|
||||
|
||||
# this is a legacy thing, we should think about removing it
|
||||
'commit' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'compare_url' => commit.compare_url,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email
|
||||
}
|
||||
end
|
||||
|
||||
def commit_data(commit)
|
||||
{
|
||||
'id' => commit.id,
|
||||
'sha' => commit.commit,
|
||||
'branch' => commit.branch,
|
||||
'message' => commit.message,
|
||||
'committed_at' => format_date(commit.committed_at),
|
||||
'author_name' => commit.author_name,
|
||||
'author_email' => commit.author_email,
|
||||
'committer_name' => commit.committer_name,
|
||||
'committer_email' => commit.committer_email,
|
||||
'compare_url' => commit.compare_url,
|
||||
}
|
||||
end
|
||||
|
||||
def repository_data(repository)
|
||||
{
|
||||
'id' => repository.id,
|
||||
'slug' => repository.slug,
|
||||
'description' => repository.description,
|
||||
'private' => repository.private,
|
||||
'last_build_id' => repository.last_build_id,
|
||||
'last_build_number' => repository.last_build_number,
|
||||
'last_build_state' => repository.last_build_state.to_s,
|
||||
'last_build_duration' => repository.last_build_duration,
|
||||
'last_build_language' => nil,
|
||||
'last_build_started_at' => format_date(repository.last_build_started_at),
|
||||
'last_build_finished_at' => format_date(repository.last_build_finished_at),
|
||||
'github_language' => repository.github_language,
|
||||
'default_branch' => {
|
||||
'name' => repository.default_branch,
|
||||
'last_build_id' => last_build_on_default_branch_id(repository)
|
||||
},
|
||||
'active' => repository.active,
|
||||
'current_build_id' => repository.current_build_id
|
||||
}
|
||||
end
|
||||
|
||||
def last_build_on_default_branch_id(repository)
|
||||
default_branch = Branch.where(repository_id: repository.id, name: repository.default_branch).first
|
||||
|
||||
if default_branch
|
||||
default_branch.last_build_id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build/canceled.rb
vendored
Normal file
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build/canceled.rb
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Build
|
||||
class Canceled < Build
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build/created.rb
vendored
Normal file
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build/created.rb
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Build
|
||||
class Created < Build
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build/finished.rb
vendored
Normal file
14
vendor/travis-core/lib/travis/api/serialize/v0/pusher/build/finished.rb
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Travis
|
||||
module Api
|
||||
module Serialize
|
||||
module V0
|
||||
module Pusher
|
||||
class Build
|
||||
class Finished < Build
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user