Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisroberts committed Feb 3, 2015
0 parents commit 2b3aa2c
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.*
!.gitignore
*.gem
*.lock
Empty file added CHANGELOG.md
Empty file.
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Contributing

## Branches

### `master` branch

The master branch is the current stable released version.

### `develop` branch

The develop branch is the current edge of development.

## Pull requests

* https://github.com/carnivore-rb/jackal-code-fetcher/pulls

Please base all pull requests of the `develop` branch. Merges to
`master` only occur through the `develop` branch. Pull requests
based on `master` will likely be cherry picked.

## Issues

Need to report an issue? Use the github issues:

* https://github.com/carnivore-rb/jackal-code-fetcher/issues
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2015 Chris Roberts

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Jackal Code Fetcher

Fetch code from a repository and stash reference in the asset store.

## Configuration

The code fetcher uses the asset store to persist compressed code
asset. Configure:

```json
{
"jackal": {
"assets": {
"provider": PROVIDER,
"credentials": {
CREDENTIALS
}
}
}
}
```

## Supported Remotes

### GitHub

Access tokens are used for fetching private repositories. Token can
be provided via direct configuration:

```json
{
"jackal": {
"code_fetcher": {
"config": {
"github": {
"access_token": TOKEN
}
}
}
}
}
```

or it can be provided via application level configuration:

```json
{
"jackal": {
"github": {
"access_token": ACCESS_TOKEN
}
}
}
```

# Info

* Repository: https://github.com/carnivore-rb/jackal-code-fetcher
* IRC: Freenode @ #carnivore
16 changes: 16 additions & 0 deletions jackal-code-fetcher.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
require 'jackal-code-fetcher/version'
Gem::Specification.new do |s|
s.name = 'jackal-code-fetcher'
s.version = Jackal::CodeFetcher::VERSION.version
s.summary = 'Message processing helper'
s.author = 'Chris Roberts'
s.email = '[email protected]'
s.homepage = 'https://github.com/carnivore-rb/jackal-code-fetcher'
s.description = 'Code fetching helper'
s.require_path = 'lib'
s.license = 'Apache 2.0'
s.add_dependency 'jackal'
s.add_dependency 'git'
s.files = Dir['lib/**/*'] + %w(jackal-code-fetcher.gemspec README.md CHANGELOG.md CONTRIBUTING.md LICENSE)
end
9 changes: 9 additions & 0 deletions lib/jackal-code-fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'jackal'

module Jackal
module CodeFetcher
autoload :GitHub, 'jackal-code-fetcher/git_hub'
end
end

require 'jackal-code-fetcher/version'
134 changes: 134 additions & 0 deletions lib/jackal-code-fetcher/git_hub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require 'jackal-code-fetcher'

module Jackal
module CodeFetcher
# GitHub code fetcher
class GitHub < Callback

# Setup callback
def setup(*_)
require 'uri'
require 'git'
require 'fileutils'
require 'tmpdir'
FileUtils.mkdir_p(config.fetch(:working_directory, '/tmp'))
end

# Determine validity of message
#
# @param message [Carnivore::Message]
# @return [Truthy, Falsey]
def valid?(message)
super do |payload|
payload.get(:data, :github) &&
!payload.get(:data, :code_fetcher, :asset)
end
end

# Fetch code and push to asset store
#
# @param message [Carnivore::Message]
def execute(message)
failure_wrap(message) do |payload|
payload.set(:data, :code_fetcher, :info,
Smash.new(
:owner => payload.get(:data, :github, :repository, :name),
:name => payload.get(:data, :github, :repository, :owner, :name),
:reference => payload.get(:data, :github, :ref),
:private => payload.get(:data, :github, :private),
:url => payload.get(:data, :github, :repository, :url)
)
)
store_reference(payload)
job_completed(:code_fetcher, payload, message)
end

# Fetch reference from GitHub repository and store compressed
# copy in the asset store
#
# @param payload [Smash]
# @return [TrueClass]
def store_reference(payload)
repo_dir = fetch_repository(payload)
pack_and_store(repo_dir, payload)
end

# Build github URL for fetching
#
# @param payload [Smash]
# @return [String]
def github_url(payload)
if(payload.get(:data, :code_fetcher, :info, :private))
uri = URI.parse(payload.get(:data, :code_fetcher, :info, :url))
uri.scheme = 'https'
uri.user = config.fetch(:github, :access_token,
app_config.get(:github, :access_token)
)
uri.to_s
else
payload.get(:data, :code_fetcher, :info, :url)
end
end

# Generate local path
#
# @return [String] path
def repository_path(payload)
File.join(
config.fetch(:working_directory, '/tmp'),
payload.get(:data, :code_fetcher, :info, :owner),
payload.get(:data, :code_fetcher, :info, :name)
)
end

# Fetch repository from GitHub
#
# @param payload [Smash]
# @return [String] path to repository directory
def fetch_repository(payload)
repo_path = repository_path(payload)
if(File.directory?(repo_path))
debug "Pulling changes to: #{repo_path}"
repo = Git.open(repo_path)
repo.checkout('master')
repo.pull
repo.fetch
else
debug "Initiating repository clone to: #{repo_path}"
Git.clone(github_url(payload))
end
repo_path
end

# Store reference in asset store
#
# @param path [String] local path to repository
# @param payload [Smash]
# @return [TrueClass]
def pack_and_store(path, payload)
repo = Git.open(path)
repo.checkout(
payload.get(:data, :code_fetcher, :info, :reference)
)
asset_key = [
payload.get(:data, :code_fetcher, :info, :owner),
payload.get(:data, :code_fetcher, :info, :name),
payload.get(:data, :code_fetcher, :info, :reference)
].join('-')
Dir.mktmpdir(asset_key) do |_path|
tmp_path = File.join(_path, asset_key)
FileUtils.mkdir_p(tmp_path)
FileUtils.cp_r(File.join(repository_path(payload), '.'), tmp_path)
FileUtils.rm_rf(File.join(tmp_path, '.git'))
tarball = asset_store.pack(tmp_path)
asset_store.put(asset_key, tarball)
end
payload.set(:data, :code_fetcher, :asset, asset_key)
true
end

end

end
end
end
6 changes: 6 additions & 0 deletions lib/jackal-code-fetcher/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Jackal
module CodeFetcher
# Current library version
VERSION = Gem::Version.new('0.1.0')
end
end

0 comments on commit 2b3aa2c

Please sign in to comment.