Skip to content

Instantly share code, notes, and snippets.

@yurinnick
Last active January 20, 2016 08:52
Show Gist options
  • Save yurinnick/9d5f9c0658d7a1c52054 to your computer and use it in GitHub Desktop.
Save yurinnick/9d5f9c0658d7a1c52054 to your computer and use it in GitHub Desktop.
require 'git'
module Metadata
class SharedContext
attr_reader :state
def initialize
@state ||= []
end
def get_state(state_name)
end
def append_state(state)
@state << state
end
end
end
module Action
module Type
Base = 'base'
Git = 'checkout.git'
end
module Status
Success = 'execute.success'
Failed = 'execute.failed'
RuntimeError = 'runtime.error'
end
class BaseAction
attr_reader :shared_context
def initialize(shared_context)
@shared_context = shared_context
end
def execute
@shared_context.append_state({
type: Type::Base,
status: Status::Success,
data: {}
})
end
end
class GitAction < BaseAction
attr_reader :repository
attr_reader :origin
attr_reader :branch
def initialize(shared_context, repository, remote = 'origin', branch = 'master')
@repository = repository
@branch = branch
@name = File.basename(repository, '.*')
super(shared_context)
end
def execute
begin
repo = Git.clone(@repository, @name, :branch => @branch)
revision = repo.log.first
author = {
name: revision.author.name,
email: revision.author.email
}
status = Status::Success
data = {
branch: repo.branch.full,
author: author,
message: revision.message,
date: revision.date,
sha: revision.sha
}
rescue Git::GitExecuteError => e
status = Status::Failed
data = {
message: e.message
}
rescue Exception => e
status = Status::RuntimeError
data = {
message: e.message
}
ensure
@shared_context.append_state(
type: Type::Git,
status: status,
data: data
)
end
end
end
end
context = Metadata::SharedContext.new
Action::GitAction.new(context, 'https://github.com/rbenv/ruby-build.git').execute
Action::GitAction.new(context, 'https://github.com/rbenv/ruby-build.git').execute
p context.state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment