-
Notifications
You must be signed in to change notification settings - Fork 942
/
pull_request_creator.rb
168 lines (147 loc) 路 5.49 KB
/
pull_request_creator.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
# frozen_string_literal: true
require "dependabot/metadata_finders"
module Dependabot
class PullRequestCreator
require "dependabot/pull_request_creator/github"
require "dependabot/pull_request_creator/gitlab"
require "dependabot/pull_request_creator/message_builder"
require "dependabot/pull_request_creator/branch_namer"
require "dependabot/pull_request_creator/labeler"
class RepoNotFound < StandardError; end
class RepoArchived < StandardError; end
class NoHistoryInCommon < StandardError; end
attr_reader :source, :dependencies, :files, :base_commit,
:credentials, :pr_message_footer, :custom_labels,
:author_details, :signature_key, :vulnerabilities_fixed,
:reviewers, :assignees, :milestone, :branch_name_separator,
:branch_name_prefix, :github_redirection_service
def initialize(source:, base_commit:, dependencies:, files:, credentials:,
pr_message_footer: nil, custom_labels: nil,
author_details: nil, signature_key: nil,
reviewers: nil, assignees: nil, milestone: nil,
vulnerabilities_fixed: {}, branch_name_separator: "/",
branch_name_prefix: "dependabot",
label_language: false, automerge_candidate: false,
github_redirection_service: "github-redirect.dependabot.com")
@dependencies = dependencies
@source = source
@base_commit = base_commit
@files = files
@credentials = credentials
@pr_message_footer = pr_message_footer
@author_details = author_details
@signature_key = signature_key
@custom_labels = custom_labels
@reviewers = reviewers
@assignees = assignees
@milestone = milestone
@vulnerabilities_fixed = vulnerabilities_fixed
@branch_name_separator = branch_name_separator
@branch_name_prefix = branch_name_prefix
@label_language = label_language
@automerge_candidate = automerge_candidate
@github_redirection_service = github_redirection_service
check_dependencies_have_previous_version
end
def check_dependencies_have_previous_version
return if library? && dependencies.all? { |d| requirements_changed?(d) }
return if dependencies.all?(&:previous_version)
raise "Dependencies must have a previous version or changed " \
"requirement to have a pull request created for them!"
end
def create
case source.provider
when "github" then github_creator.create
when "gitlab" then gitlab_creator.create
else raise "Unsupported provider #{source.provider}"
end
end
private
def label_language?
@label_language
end
def automerge_candidate?
@automerge_candidate
end
def github_creator
Github.new(
source: source,
branch_name: branch_namer.new_branch_name,
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
author_details: author_details,
signature_key: signature_key,
labeler: labeler,
reviewers: reviewers,
assignees: assignees,
milestone: milestone
)
end
def gitlab_creator
Gitlab.new(
source: source,
branch_name: branch_namer.new_branch_name,
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
author_details: author_details,
labeler: labeler,
approvers: reviewers,
assignee: assignees&.first,
milestone: milestone
)
end
def message_builder
@message_builder ||
MessageBuilder.new(
source: source,
dependencies: dependencies,
files: files,
credentials: credentials,
author_details: author_details,
pr_message_footer: pr_message_footer,
vulnerabilities_fixed: vulnerabilities_fixed,
github_redirection_service: github_redirection_service
)
end
def branch_namer
@branch_namer ||=
BranchNamer.new(
dependencies: dependencies,
files: files,
target_branch: source.branch,
separator: branch_name_separator,
prefix: branch_name_prefix
)
end
def labeler
@labeler ||=
Labeler.new(
source: source,
custom_labels: custom_labels,
credentials: credentials,
includes_security_fixes: includes_security_fixes?,
dependencies: dependencies,
label_language: label_language?,
automerge_candidate: automerge_candidate?
)
end
def library?
return true if files.any? { |file| file.name.end_with?(".gemspec") }
dependencies.none?(&:appears_in_lockfile?)
end
def includes_security_fixes?
vulnerabilities_fixed.values.flatten.any?
end
def requirements_changed?(dependency)
(dependency.requirements - dependency.previous_requirements).any?
end
end
end