Skip to content

Commit 8ed0e7e

Browse files
committed
GithubHelper: Change all methods to be instance methods
In the past all the methods use the self to be accessed as class methods, as is now mandatory to instanciate the GithubHelper, the methods are now converted in instance methods.
1 parent eaf6ac7 commit 8ed0e7e

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(github_token:)
2525
@client.auto_paginate = true
2626
end
2727

28-
def self.get_milestone(repository, release)
28+
def get_milestone(repository, release)
2929
miles = client.list_milestones(repository)
3030
mile = nil
3131

@@ -42,11 +42,11 @@ def self.get_milestone(repository, release)
4242
# @param [String] milestone The name of the milestone we want to fetch the list of PRs for (e.g.: `16.9`)
4343
# @return [<Sawyer::Resource>] A list of the PRs for the given milestone, sorted by number
4444
#
45-
def self.get_prs_for_milestone(repository, milestone)
45+
def get_prs_for_milestone(repository, milestone)
4646
client.search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number)
4747
end
4848

49-
def self.get_last_milestone(repository)
49+
def get_last_milestone(repository)
5050
options = {}
5151
options[:state] = 'open'
5252

@@ -71,7 +71,7 @@ def self.get_last_milestone(repository)
7171
last_stone
7272
end
7373

74-
def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, newmilestone_duration, number_of_days_from_code_freeze_to_release, need_submission)
74+
def create_milestone(repository, newmilestone_number, newmilestone_duedate, newmilestone_duration, number_of_days_from_code_freeze_to_release, need_submission)
7575
# If there is a review process, we want to submit the binary 3 days before its release
7676
#
7777
# Using 3 days is mostly for historical reasons where we release the apps on Monday and submit them on Friday.
@@ -112,7 +112,7 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate,
112112
# @param [Array<String>] assets List of file paths to attach as assets to the release
113113
# @param [TrueClass|FalseClass] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta)
114114
#
115-
def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:)
115+
def create_release(repository:, version:, target: nil, description:, assets:, prerelease:)
116116
release = client.create_release(
117117
repository,
118118
version, # tag name
@@ -135,7 +135,7 @@ def self.create_release(repository:, version:, target: nil, description:, assets
135135
# @param [String] download_folder The folder which the file should be downloaded into
136136
# @return [String] The path of the downloaded file, or nil if something went wrong
137137
#
138-
def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:)
138+
def download_file_from_tag(repository:, tag:, file_path:, download_folder:)
139139
repository = repository.delete_prefix('/').chomp('/')
140140
file_path = file_path.delete_prefix('/').chomp('/')
141141
file_name = File.basename(file_path)
@@ -156,7 +156,7 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:)
156156
end
157157

158158
# Creates (or updates an existing) GitHub PR Comment
159-
def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid)
159+
def comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid)
160160
comments = client.issue_comments(project_slug, pr_number)
161161

162162
reuse_marker = "<!-- REUSE_ID: #{reuse_identifier} -->"
@@ -189,7 +189,7 @@ def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: Secur
189189
# @return [Milestone] A single milestone object
190190
# @see http://developer.github.com/v3/issues/milestones/#update-a-milestone
191191
#
192-
def self.update_milestone(repository:, number:, options:)
192+
def update_milestone(repository:, number:, options:)
193193
client.update_milestone(repository, number, options)
194194
end
195195

@@ -200,7 +200,7 @@ def self.update_milestone(repository:, number:, options:)
200200
# @param [Hash] options A customizable set of options.
201201
# @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection
202202
#
203-
def self.remove_branch_protection(repository:, branch:, options:)
203+
def remove_branch_protection(repository:, branch:, options:)
204204
client.unprotect_branch(repository, branch_name, options)
205205
end
206206

@@ -211,7 +211,7 @@ def self.remove_branch_protection(repository:, branch:, options:)
211211
# @param options [Hash] A customizable set of options.
212212
# @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection
213213
#
214-
def self.set_branch_protection(repository:, branch:, options:)
214+
def set_branch_protection(repository:, branch:, options:)
215215
client.protect_branch(repository, branch_name, options)
216216
end
217217

spec/github_helper_spec.rb

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@
2929
let(:client) do
3030
instance_double(
3131
Octokit::Client,
32-
contents: double(download_url: content_url) # rubocop:disable RSpec/VerifiedDoubles
32+
contents: double(download_url: content_url), # rubocop:disable RSpec/VerifiedDoubles
33+
user: instance_double('User', name: 'test'),
34+
'auto_paginate=': nil
3335
)
3436
end
3537

3638
before do
37-
allow(described_class).to receive(:client).and_return(client)
39+
allow(Octokit::Client).to receive(:new).and_return(client)
3840
end
3941

4042
it 'fails if it does not find the right release on GitHub' do
4143
stub = stub_request(:get, content_url).to_return(status: [404, 'Not Found'])
42-
downloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: './')
44+
downloaded_file = download_file_from_tag(download_folder: './')
4345
expect(downloaded_file).to be_nil
4446
expect(stub).to have_been_made.once
4547
end
@@ -48,12 +50,17 @@
4850
stub = stub_request(:get, content_url).to_return(status: 200, body: 'my-test-content')
4951
Dir.mktmpdir('a8c-download-repo-file-') do |tmpdir|
5052
dst_file = File.join(tmpdir, 'test-file.xml')
51-
downloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: tmpdir)
53+
downloaded_file = download_file_from_tag(download_folder: tmpdir)
5254
expect(downloaded_file).to eq(dst_file)
5355
expect(stub).to have_been_made.once
5456
expect(File.read(dst_file)).to eq('my-test-content')
5557
end
5658
end
59+
60+
def download_file_from_tag(download_folder:)
61+
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
62+
helper.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: download_folder)
63+
end
5764
end
5865

5966
describe 'get_last_milestone' do
@@ -62,23 +69,30 @@
6269
let(:client) do
6370
instance_double(
6471
Octokit::Client,
65-
list_milestones: ['9.8 ❄️', '9.9'].map { |title| mock_milestone(title) }.append(last_stone)
72+
list_milestones: ['9.8 ❄️', '9.9'].map { |title| mock_milestone(title) }.append(last_stone),
73+
user: instance_double('User', name: 'test'),
74+
'auto_paginate=': nil
6675
)
6776
end
6877

6978
before do
70-
allow(described_class).to receive(:client).and_return(client)
79+
allow(Octokit::Client).to receive(:new).and_return(client)
7180
end
7281

7382
it 'returns correct milestone' do
7483
expect(client).to receive(:list_milestones)
75-
last_milestone = described_class.get_last_milestone(repository: test_repo)
84+
last_milestone = get_last_milestone(repository: test_repo)
7685
expect(last_milestone).to eq(last_stone)
7786
end
7887

7988
def mock_milestone(title)
8089
{ title: title }
8190
end
91+
92+
def get_last_milestone(repository:)
93+
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
94+
helper.get_last_milestone(repository: repository)
95+
end
8296
end
8397

8498
describe 'comment_on_pr' do
@@ -88,12 +102,13 @@ def mock_milestone(title)
88102
issue_comments: [],
89103
add_comment: nil,
90104
update_comment: nil,
91-
user: instance_double('User', id: 1234)
105+
user: instance_double('User', id: 1234, name: 'test'),
106+
'auto_paginate=': nil
92107
)
93108
end
94109

95110
before do
96-
allow(described_class).to receive(:client).and_return(client)
111+
allow(Octokit::Client).to receive(:new).and_return(client)
97112
end
98113

99114
it 'will create a new comment if an existing one is not found' do
@@ -124,7 +139,8 @@ def mock_milestone(title)
124139
end
125140

126141
def comment_on_pr
127-
described_class.comment_on_pr(
142+
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
143+
helper.comment_on_pr(
128144
project_slug: 'test/test',
129145
pr_number: 1234,
130146
body: 'Test',
@@ -143,41 +159,48 @@ def mock_comment(body: '<!-- REUSE_ID: test-id --> Test', user_id: 1234)
143159
let(:client) do
144160
instance_double(
145161
Octokit::Client,
146-
list_milestones: []
162+
list_milestones: [],
163+
user: instance_double('User', name: 'test'),
164+
'auto_paginate=': nil
147165
)
148166
end
149167

150168
before do
151-
allow(described_class).to receive(:client).and_return(client)
169+
allow(Octokit::Client).to receive(:new).and_return(client)
152170
end
153171

154172
it 'properly passes the repository all the way down to the Octokit::Client' do
155173
expect(client).to receive(:list_milestones).with(test_repo)
156-
described_class.get_milestone(test_repo, 'test')
174+
get_milestone(milestone_name: 'test')
157175
end
158176

159177
it 'returns nil when no milestone is returned from the api' do
160-
milestone = described_class.get_milestone(test_repo, '10')
178+
milestone = get_milestone(milestone_name: '10')
161179
expect(milestone).to be_nil
162180
end
163181

164182
it 'returns nil when no milestone title starts with the searched term' do
165183
allow(client).to receive(:list_milestones).and_return(test_milestones)
166-
milestone = described_class.get_milestone(test_repo, '8.5')
184+
milestone = get_milestone(milestone_name: '8.5')
167185
expect(milestone).to be_nil
168186
end
169187

170188
it 'returns a milestone when the milestone title starts with search term' do
171189
allow(client).to receive(:list_milestones).and_return(test_milestones)
172-
milestone = described_class.get_milestone(test_repo, '9')
190+
milestone = get_milestone(milestone_name: '9')
173191
expect(milestone).to eq({ title: '9.8' })
174192
end
175193

176194
it 'returns the milestone with the latest due date matching the search term when there are more than one' do
177195
allow(client).to receive(:list_milestones).and_return(test_milestones)
178-
milestone = described_class.get_milestone(test_repo, '10.1')
196+
milestone = get_milestone(milestone_name: '10.1')
179197
expect(milestone).to eq({ title: '10.1.3 ❄️' })
180198
end
199+
200+
def get_milestone(milestone_name:)
201+
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
202+
helper.get_milestone(test_repo, milestone_name)
203+
end
181204
end
182205

183206
describe 'create_milestone' do
@@ -187,12 +210,14 @@ def mock_comment(body: '<!-- REUSE_ID: test-id --> Test', user_id: 1234)
187210
let(:client) do
188211
instance_double(
189212
Octokit::Client,
190-
create_milestone: nil
213+
create_milestone: nil,
214+
user: instance_double('User', name: 'test'),
215+
'auto_paginate=': nil
191216
)
192217
end
193218

194219
before do
195-
allow(described_class).to receive(:client).and_return(client)
220+
allow(Octokit::Client).to receive(:new).and_return(client)
196221
end
197222

198223
it 'has the correct dates to code freeze without submission' do
@@ -212,7 +237,8 @@ def mock_comment(body: '<!-- REUSE_ID: test-id --> Test', user_id: 1234)
212237
end
213238

214239
def create_milestone(need_submission:, milestone_duration:, days_code_freeze:)
215-
described_class.create_milestone(
240+
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
241+
helper.create_milestone(
216242
test_repo,
217243
test_milestone_number,
218244
test_milestone_duedate.to_time.utc,
@@ -231,12 +257,14 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:)
231257
let(:client) do
232258
instance_double(
233259
Octokit::Client,
234-
create_release: nil
260+
create_release: nil,
261+
user: instance_double('User', name: 'test'),
262+
'auto_paginate=': nil
235263
)
236264
end
237265

238266
before do
239-
allow(described_class).to receive(:client).and_return(client)
267+
allow(Octokit::Client).to receive(:new).and_return(client)
240268
end
241269

242270
it 'has the correct options' do
@@ -255,7 +283,8 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:)
255283
end
256284

257285
def create_release(assets: [])
258-
described_class.create_release(
286+
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
287+
helper.create_release(
259288
repository: test_repo,
260289
version: test_tag,
261290
target: test_target,

0 commit comments

Comments
 (0)