-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.rb
More file actions
executable file
·176 lines (147 loc) · 4.4 KB
/
run.rb
File metadata and controls
executable file
·176 lines (147 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'octokit'
require 'pry'
require 'semverse'
module Semverse
# Monkey patching the Version class for two things we'll use
class Version
# returns a new Version array suitable for bumping
def to_a
[major, minor, patch, pre_release, build]
end
# returns a new Version with the bump reflected
def bump!(type)
version_array = to_a
case type
when :major
version_array[0] += 1
when :minor
version_array[1] += 1
when :patch
version_array[2] += 1
end
self.class.new(version_array)
end
end
end
module Rubyists
module Github
module Action
# Tag the repo with the next version
class AutoTag
DEFAULT_BUMP_TYPE = ENV.fetch('DEFAULT_BUMP_TYPE', :minor).to_sym
DEFAULT_VERSION = Semverse::Version.new('0.0.0')
TOKEN = ENV.fetch('GITHUB_TOKEN', nil)
def with_v
@with_v ||= ENV.fetch('WITH_V', 'v') # default to leading v
end
def self.run!
new.run!
end
def org_name
@org_name ||= ENV['GITHUB_ORG'] || 'rubyists'
end
def repo_name
@repo_name ||= ENV['GITHUB_REPOSITORY'] || 'rubyists/github-semver-autotag-action'
end
def client
@client ||= if TOKEN
Octokit::Client.new(auto_paginate: true, access_token: TOKEN)
else
Octokit::Client.new(auto_paginate: true)
end
end
def repo
@repo ||= client.repository repo_name
rescue Octokit::NotFound
warn ''
warn "Yo, that repository (#{repo_name}) doesn't seem to exist, or you don't have accees"
warn 'Bailing and stuff'
end
def run!
puts "Running for #{repo_name}"
return self unless repo
tag_it!
self
end
def latest_version
@latest_version ||= Semverse::Version.new(latest_tag.to_s)
end
def latest_tag
@latest_tag ||= repo.rels[:tags].get.data.first || DEFAULT_VERSION
end
def this_commit
@this_commit ||= repo.rels[:commits].get.data.first
end
def this_commit_message
@this_commit_message ||= this_commit.commit.message
end
def last_tag_commit_message
@last_tag_commit_message ||= ''
end
def bump_from_regex
case this_commit_message
when /#(?:major|breaking)\b/i
:major
when /#(?:minor|normal)\b/i
:minor
when /#(?:patch|trivial)\b/i
:patch
end
end
def pull_request
false
end
def bump_from_label
return unless pull_request
case pull_request.labels
when /(?:major|breaking)\b/i
:major
when /(?:minor|normal)\b/i
:minor
when /(?:patch|trivial)\b/i
:patch
end
end
def bump_type
@bump_type ||= bump_from_regex || bump_from_label || DEFAULT_BUMP_TYPE
end
def next_tag
@next_tag ||= with_v + latest_version.bump!(bump_type).to_s
end
def logged_in?
if client.login.nil?
logger.error 'You are not logged in, you must be logged in to push a tag'
logger.error 'Please set the GITHUB_TOKEN environment variable'
return false
end
true
end
def create_ref!
client.create_ref(repo_name, "tags/#{next_tag}", this_commit.sha)
rescue Octokit::NotFound => e
if logged_in?
logger.error "Failed to tag #{repo_name} with #{next_tag}, #{e.message}"
else
logger.error "Can't push a tag when you are not logged in. Would have tagged #{repo_name} with #{next_tag}"
end
false
end
def tag_it!
logger.debug "Tagging with #{next_tag}"
if create_ref!
logger.debug "Tagged #{repo_name} with #{next_tag}"
else
logger.debug "Unable to #{repo_name} with #{next_tag}"
exit 1
end
end
def logger
@logger ||= Logger.new($stderr)
end
end
end
end
end
Rubyists::Github::Action::AutoTag.run! if $PROGRAM_NAME == __FILE__