-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2b3aa2c
Showing
10 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.* | ||
!.gitignore | ||
*.gem | ||
*.lock |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |