forked from dependabot/dependabot-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency_file.rb
206 lines (168 loc) · 5.03 KB
/
dependency_file.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# typed: strong
# frozen_string_literal: true
require "pathname"
require "sorbet-runtime"
module Dependabot
class DependencyFile
extend T::Sig
sig { returns(String) }
attr_accessor :name
sig { returns(T.nilable(String)) }
attr_accessor :content
# This is the directory of the job source, not the directory of the file itself.
# The name actually contains the relative path from the job directory.
sig { returns(String) }
attr_accessor :directory
sig { returns(String) }
attr_accessor :type
sig { returns(T::Boolean) }
attr_accessor :support_file
sig { returns(T::Boolean) }
attr_accessor :vendored_file
sig { returns(T.nilable(String)) }
attr_accessor :symlink_target
sig { returns(String) }
attr_accessor :content_encoding
sig { returns(String) }
attr_accessor :operation
sig { returns(T.nilable(String)) }
attr_accessor :mode
class ContentEncoding
UTF_8 = "utf-8"
BASE64 = "base64"
end
class Operation
UPDATE = "update"
CREATE = "create"
DELETE = "delete"
end
class Mode
FILE = "100644"
SUBMODULE = "160000"
end
sig do
params(
name: String,
content: T.nilable(String),
directory: String,
type: String,
support_file: T::Boolean,
vendored_file: T::Boolean,
symlink_target: T.nilable(String),
content_encoding: String,
deleted: T::Boolean,
operation: String,
mode: T.nilable(String)
)
.void
end
def initialize(name:, content:, directory: "/", type: "file",
support_file: false, vendored_file: false, symlink_target: nil,
content_encoding: ContentEncoding::UTF_8, deleted: false,
operation: Operation::UPDATE, mode: nil)
@name = name
@content = content
@directory = T.let(clean_directory(directory), String)
@symlink_target = symlink_target
@support_file = support_file
@vendored_file = vendored_file
@content_encoding = content_encoding
@operation = operation
# Make deleted override the operation. Deleted is kept when operation
# was introduced to keep compatibility with downstream dependants.
@operation = Operation::DELETE if deleted
# Type is used *very* sparingly. It lets the git_modules updater know that
# a "file" is actually a submodule, and lets our Go updaters know which
# file represents the main.go.
# New use cases should be avoided if at all possible (and use the
# support_file flag instead)
@type = type
begin
@mode = T.let(File.stat(realpath).mode.to_s(8), T.nilable(String))
rescue StandardError
@mode = mode
end
return unless (type == "symlink") ^ symlink_target
raise "Symlinks must specify a target!" unless symlink_target
raise "Only symlinked files must specify a target!" if symlink_target
end
sig { returns(T::Hash[String, T.untyped]) }
def to_h
details = {
"name" => name,
"content" => content,
"directory" => directory,
"type" => type,
"support_file" => support_file,
"content_encoding" => content_encoding,
"deleted" => deleted,
"operation" => operation,
"mode" => mode
}
details["symlink_target"] = symlink_target if symlink_target
details
end
sig { returns(String) }
def path
Pathname.new(File.join(directory, name)).cleanpath.to_path
end
sig { returns(String) }
def realpath
(symlink_target || path).sub(%r{^/}, "")
end
sig { params(other: BasicObject).returns(T::Boolean) }
def ==(other)
case other
when DependencyFile
my_hash = to_h.reject { |k| k == "support_file" }
their_hash = other.to_h.reject { |k| k == "support_file" }
my_hash == their_hash
else
false
end
end
sig { returns(Integer) }
def hash
to_h.hash
end
sig { params(other: BasicObject).returns(T::Boolean) }
def eql?(other)
self == other
end
sig { returns(T::Boolean) }
def support_file?
@support_file
end
sig { returns(T::Boolean) }
def vendored_file?
@vendored_file
end
sig { returns(T::Boolean) }
def deleted
@operation == Operation::DELETE
end
sig { params(deleted: T::Boolean).void }
def deleted=(deleted)
@operation = deleted ? Operation::DELETE : Operation::UPDATE
end
sig { returns(T::Boolean) }
def deleted?
deleted
end
sig { returns(T::Boolean) }
def binary?
content_encoding == ContentEncoding::BASE64
end
sig { returns(String) }
def decoded_content
return Base64.decode64(T.must(content)) if binary?
T.must(content)
end
private
sig { params(directory: String).returns(String) }
def clean_directory(directory)
# Directory should always start with a `/`
directory.sub(%r{^/*}, "/")
end
end
end