Class: Henry::Deploy

Inherits:
Object
  • Object
show all
Defined in:
lib/henry/deploy.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Deploy

Returns a new instance of Deploy.



4
5
6
7
8
# File 'lib/henry/deploy.rb', line 4

def initialize(path)
  @user, @repo, @commit = ENV['TRAVIS_REPO_SLUG'].split('/')[0], ENV['TRAVIS_REPO_SLUG'].split('/')[1], ENV['TRAVIS_COMMIT']
  @path = path
  @github = Github.new oauth_token: ENV['GITHUB_OUATH_TOKEN']
end

Instance Method Details

#blob(content) ⇒ Object



94
95
96
97
# File 'lib/henry/deploy.rb', line 94

def blob(content)
  blob = @github.git_data.blobs.create @user, @repo, "content" => content, "encoding" => "utf-8"
  blob['sha']
end

#changelog_contentObject



33
34
35
36
# File 'lib/henry/deploy.rb', line 33

def changelog_content
  generator = GitHubChangelogGenerator::Generator.new(changelog_options)
  generator.compound_changelog
end

#changelog_optionsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/henry/deploy.rb', line 42

def changelog_options
  {
   :tag1=>nil,
   :tag2=>nil,
   :date_format=>"%Y-%m-%d",
   :output=>"CHANGELOG.md",
   :base=>@path,
   :issues=>true,
   :add_issues_wo_labels=>true,
   :add_pr_wo_labels=>true,
   :pulls=>true,
   :filter_issues_by_milestone=>true,
   :author=>true,
   :unreleased=>true,
   :unreleased_label=>"Unreleased",
   :compare_link=>true,
   :enhancement_labels=>["enhancement", "Enhancement"],
   :bug_labels=>["bug", "Bug"],
   :exclude_labels=>
    ["duplicate",
     "question",
     "invalid",
     "wontfix",
     "Duplicate",
     "Question",
     "Invalid",
     "Wontfix"],
   :max_issues=>nil,
   :simple_list=>false,
   :verbose=>true,
   :header=>"# Change Log",
   :merge_prefix=>"**Merged pull requests:**",
   :issue_prefix=>"**Closed issues:**",
   :bug_prefix=>"**Fixed bugs:**",
   :enhancement_prefix=>"**Implemented enhancements:**",
   :git_remote=>"origin",
   :user=>@user,
   :project=>@repo,
   :token=>ENV['GITHUB_OUATH_TOKEN']
  }
end

#commit(tree_sha, filename) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/henry/deploy.rb', line 110

def commit(tree_sha, filename)
  branch_data = @github.repos.branch @user, @repo, 'master'
  latest_commit = branch_data['commit']['sha']
  commit = @github.git_data.commits.create @user, @repo, "message" => "Updated #{filename} [ci skip]",
          "parents" => [latest_commit],
          "tree" => tree_sha
  commit['sha']
end

#delete_tagsObject



29
30
31
# File 'lib/henry/deploy.rb', line 29

def delete_tags
  @github.git_data.references.delete(@user, @repo, "tags/#{version}")
end

#deployable?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/henry/deploy.rb', line 119

def deployable?
  ENV['TRAVIS_BRANCH'] == 'master' && ENV['TRAVIS_PULL_REQUEST'] == "false"
end

#generate_changelogObject



38
39
40
# File 'lib/henry/deploy.rb', line 38

def generate_changelog
  File.open(File.join(@path, 'CHANGELOG.md'), "w") { |file| file.write(changelog_content) }
end

#performObject



14
15
16
17
18
19
# File 'lib/henry/deploy.rb', line 14

def perform
  if deployable? && push_tags === true
    generate_changelog
    push_changelog
  end
end

#push_changelogObject



84
85
86
87
88
89
90
91
92
# File 'lib/henry/deploy.rb', line 84

def push_changelog
  filename = 'CHANGELOG.md'
  file_path = File.join(@path, filename)
  blob_sha = blob(File.open(file_path).read)
  tree_sha = tree(filename, blob_sha)
  commit_sha = commit(tree_sha, filename)

  @github.git_data.references.update @user, @repo, "heads/master", "sha" => commit_sha
end

#push_tagsObject



21
22
23
24
25
26
27
# File 'lib/henry/deploy.rb', line 21

def push_tags
  begin
    @github.git_data.references.create(@user, @repo, ref: "refs/tags/#{version}", sha: @commit).success?
  rescue Github::Error::UnprocessableEntity
    false
  end
end

#tree(filename, blob_sha) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/henry/deploy.rb', line 99

def tree(filename, blob_sha)
  tree = @github.git_data.trees.get @user, @repo, 'master'
  new_tree = @github.git_data.trees.create @user, @repo, "base_tree" => tree['sha'], "tree" => [
    "path" => filename,
    "mode" => "100644",
    "type" => "blob",
    "sha" => blob_sha
  ]
  new_tree['sha']
end

#versionObject



10
11
12
# File 'lib/henry/deploy.rb', line 10

def version
  @version ||= Gem::Specification.load(Dir["#{@path}/*.gemspec"].first).version.to_s
end