forked from integrity/integrity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrity.rb
175 lines (153 loc) · 4.85 KB
/
integrity.rb
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
if Object.const_defined?(:Encoding) && Encoding.respond_to?(:default_internal=)
# ruby 1.9
# Internal encoding is what is used on ruby strings.
Encoding.default_internal = Encoding::UTF_8
# External encoding is what is used on pipes used to communicate with
# launched command runners and is the default encoding used when opening
# e.g. the log file.
Encoding.default_external = Encoding::UTF_8
end
require "yaml"
require "logger"
require "digest/sha1"
require "timeout"
require "ostruct"
require "pathname"
require "sinatra/base"
require "sinatra/authorization"
require "json"
require "haml"
require "sass"
require "dm-core"
require "dm-validations"
require "dm-types"
require "dm-timestamps"
require "dm-aggregates"
require "dm-migrations"
require "addressable/uri"
require "chronic_duration"
require "bcat/ansi"
require "app/app"
require "integrity/configuration"
require "integrity/bootstrapper"
require "integrity/project"
require "integrity/project_finder"
require "integrity/author"
require "integrity/commit"
require "integrity/build"
require "integrity/builder"
require "integrity/notifier"
require "integrity/notifier/base"
require "integrity/payload"
require "integrity/payload_builder"
require "integrity/checkout"
require "integrity/command_runner"
require "integrity/builder"
DataMapper.finalize
# TODO
Addressable::URI.class_eval { def gsub(*a); to_s.gsub(*a); end }
module Integrity
class CannotFindEncoding < StandardError
end
autoload :ThreadedBuilder, "integrity/builders/threaded_builder"
autoload :DelayedBuilder, "integrity/builders/delayed_builder"
autoload :ResqueBuilder, "integrity/builders/resque_builder"
autoload :ExplicitBuilder, "integrity/builders/explicit_builder"
Repository = Struct.new(:uri, :branch)
def self.config
@config ||= Configuration.new
end
def self.configure
yield(config)
end
def self.bootstrap(&block)
Bootstrapper.new(&block)
end
def self.logger
config.logger
end
def self.app
unless config.base_url
warn "the base_url setting isn't set"
end
Integrity::App
end
def self.datetime_to_utc_time(datetime)
if datetime.offset != 0
# This converts to utc
# borrowed from activesupport DateTime#to_utc
datetime = datetime.new_offset(0)
end
# This is what DateTime#to_time does some of the time.
# Our offset is always 0 and therefore we always produce a Time
::Time.utc(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min, datetime.sec)
end
def self.human_duration(delta)
if delta > 0
ChronicDuration.output(delta, :format => :micro)
else
'0s'
end
end
# Replace or delete invalid UTF-8 characters from text, which is assumed
# to be in UTF-8.
#
# The text is expected to come from external to Integrity sources such as
# commit messages or build output.
#
# On ruby 1.9, invalid UTF-8 characters are replaced with question marks.
# On ruby 1.8, if iconv extension is present, invalid UTF-8 characters
# are removed.
# On ruby 1.8, if iconv extension is not present, the string is unmodified.
def self.clean_utf8(text)
# http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
# http://stackoverflow.com/questions/9126782/how-to-change-deprecated-iconv-to-stringencode-for-invalid-utf8-correction
if text.respond_to?(:encoding)
# ruby 1.9
text = text.force_encoding('utf-8').encode(intermediate_encoding, :invalid => :replace, :replace => '?').encode('utf-8')
else
# ruby 1.8
# As no encoding checks are done, any string will be accepted.
# But delete invalid utf-8 characters anyway for consistency with 1.9.
iconv, iconv_fallback = clean_utf8_iconv
if iconv
begin
output = iconv.iconv(text)
rescue Iconv::IllegalSequence
output = iconv_fallback.iconv(text)
end
end
end
text
end
def self.clean_utf8_iconv
unless @iconv_loaded
begin
require 'iconv'
rescue LoadError
@iconv = nil
else
@iconv = Iconv.new('utf-8//translit//ignore', 'utf-8')
# On some systems (Linux appears to be vulnerable, FreeBSD not)
# iconv chokes on invalid utf-8 with //translit//ignore.
@iconv_fallback = Iconv.new('utf-8//ignore', 'utf-8')
end
@iconv_loaded = true
end
[@iconv, @iconv_fallback]
end
# Apparently utf-16 is not available everywhere, in particular not on travis.
# Try to find a usable encoding.
def self.intermediate_encoding
map = {}
Encoding.list.each do |encoding|
map[encoding.name.downcase] = true
end
%w(utf-16 utf-16be utf-16le utf-7 utf-32 utf-32le utf-32be).each do |candidate|
if map[candidate]
return candidate
end
end
raise CannotFindEncoding, 'Cannot find an intermediate encoding for conversion to UTF-8'
end
end