From e41a8c404bad53c66eac46395edf1deae2d1f751 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 20 Oct 2022 00:53:25 +0100 Subject: [PATCH 01/27] GitHubHelper: Access Key is a now a mandatory parameter to use gitbuh_client The use of the github_token under the hood was causing visibility problems about the token being mandatory and necessary to execute some actions on the API --- .../actions/common/close_milestone_action.rb | 3 +- .../common/removebranchprotection_action.rb | 3 +- .../common/setbranchprotection_action.rb | 3 +- .../actions/common/setfrozentag_action.rb | 3 +- .../wpmreleasetoolkit/helper/github_helper.rb | 30 +++++++++++-------- spec/github_helper_spec.rb | 10 ++++--- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index 217f7e834..9e2725b9a 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -13,7 +13,8 @@ def self.run(params) milestone = Fastlane::Helper::GithubHelper.get_milestone(repository, milestone_title) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? - Fastlane::Helper::GithubHelper.github_client().update_milestone(repository, milestone[:number], state: 'closed') + token = Fastlane::Helper::GithubHelper.github_token + Fastlane::Helper::GithubHelper.github_client(token).update_milestone(repository, milestone[:number], state: 'closed') end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb index defbfc207..9b72d03ec 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb @@ -14,7 +14,8 @@ def self.run(params) branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - Fastlane::Helper::GithubHelper.github_client().unprotect_branch(repository, branch_name, branch_prot) + token = Fastlane::Helper::GithubHelper.github_token + Fastlane::Helper::GithubHelper.github_client(token).unprotect_branch(repository, branch_name, branch_prot) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb index 8026b860d..c703233df 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb @@ -13,7 +13,8 @@ def self.run(params) branch_prot[:restrictions] = { url: "#{branch_url}/protection/restrictions", users_url: "#{branch_url}/protection/restrictions/users", teams_url: "#{branch_url}/protection/restrictions/teams", users: [], teams: [] } branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - Fastlane::Helper::GithubHelper.github_client().protect_branch(repository, branch_name, branch_prot) + token = Fastlane::Helper::GithubHelper.github_token + Fastlane::Helper::GithubHelper.github_client(token).protect_branch(repository, branch_name, branch_prot) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index ca145bcac..4f5a36cdf 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -27,7 +27,8 @@ def self.run(params) end UI.message("New milestone: #{mile_title}") - Fastlane::Helper::GithubHelper.github_client().update_milestone(repository, milestone[:number], title: mile_title) + token = Fastlane::Helper::GithubHelper.github_token + Fastlane::Helper::GithubHelper.github_client(token).update_milestone(repository, milestone[:number], title: mile_title) end def self.is_frozen(milestone) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 581b078f3..0593c8956 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -19,9 +19,13 @@ def self.github_token! token || UI.user_error!('Please provide a GitHub authentication token via the `GITHUB_TOKEN` environment variable') end - def self.github_client + # Creates a client for the GitHub API + # + # @param [String] accesstoken GitHub OAuth access token + # + def self.github_client(accesstoken) @@client ||= begin - client = Octokit::Client.new(access_token: github_token!) + client = Octokit::Client.new(access_token: accesstoken) # Fetch the current user user = client.user @@ -35,7 +39,7 @@ def self.github_client end def self.get_milestone(repository, release) - miles = github_client().list_milestones(repository) + miles = github_client(github_token!).list_milestones(repository) mile = nil miles&.each do |mm| @@ -52,14 +56,14 @@ def self.get_milestone(repository, release) # @return [] A list of the PRs for the given milestone, sorted by number # def self.get_prs_for_milestone(repository, milestone) - github_client.search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) + github_client(github_token!).search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) end def self.get_last_milestone(repository) options = {} options[:state] = 'open' - milestones = github_client().list_milestones(repository, options) + milestones = github_client(github_token!).list_milestones(repository, options) return nil if milestones.nil? last_stone = nil @@ -92,7 +96,7 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, options = {} options[:due_on] = newmilestone_duedate options[:description] = comment - github_client().create_milestone(repository, newmilestone_number, options) + github_client(github_token!).create_milestone(repository, newmilestone_number, options) end # Creates a Release on GitHub as a Draft @@ -106,8 +110,8 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, # @param [Array] assets List of file paths to attach as assets to the release # @param [TrueClass|FalseClass] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta) # - def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:) - release = github_client().create_release( + def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:, githubtoken: nil) + release = github_client(github_token!).create_release( repository, version, # tag name name: version, # release name @@ -117,7 +121,7 @@ def self.create_release(repository:, version:, target: nil, description:, assets body: description ) assets.each do |file_path| - github_client().upload_asset(release[:url], file_path, content_type: 'application/octet-stream') + github_client(github_token!).upload_asset(release[:url], file_path, content_type: 'application/octet-stream') end end @@ -135,9 +139,9 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) file_name = File.basename(file_path) download_path = File.join(download_folder, file_name) - download_url = github_client.contents(repository, - path: file_path, - ref: tag).download_url + download_url = github_client(github_token!).contents(repository, + path: file_path, + ref: tag).download_url begin uri = URI.parse(download_url) uri.open do |remote_file| @@ -152,7 +156,7 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) # Creates (or updates an existing) GitHub PR Comment def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid) - client = github_client + client = github_client(github_token!) comments = client.issue_comments(project_slug, pr_number) reuse_marker = "" diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index 18875fcbe..a299bb2ea 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -15,6 +15,7 @@ end before do + allow(described_class).to receive(:github_token!).and_return('') allow(described_class).to receive(:github_client).and_return(client) end @@ -72,7 +73,6 @@ end before do - allow(described_class).to receive(:github_token!).and_return('') allow(Octokit::Client).to receive(:new).and_return(client) end @@ -82,13 +82,13 @@ end it 'is not nil' do - expect(described_class.github_client).not_to be_nil + expect(described_class.github_client('')).not_to be_nil end it 'memoizes the client' do expect(Octokit::Client).to receive(:new).once - described_class.github_client - described_class.github_client + described_class.github_client('') + described_class.github_client('') end end @@ -103,6 +103,7 @@ end before do + allow(described_class).to receive(:github_token!).and_return('') allow(described_class).to receive(:github_client).and_return(client) end @@ -128,6 +129,7 @@ def mock_milestone(title) end before do + allow(described_class).to receive(:github_token!).and_return('') allow(described_class).to receive(:github_client).and_return(client) end From b78a5c00c8368ab932be9e2596ec3cf68913370d Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 20 Oct 2022 00:56:29 +0100 Subject: [PATCH 02/27] GitHubHelper: Add gihubToken as optional argument on create_release At the moment of the release creation, the githubtoken can be passed as a parameter. In the cases that the param is nil or empty, we fallback to using the github_token from the runtime environment. --- .../wpmreleasetoolkit/helper/github_helper.rb | 6 ++- spec/github_helper_spec.rb | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 0593c8956..2620779a2 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -109,9 +109,11 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, # @param [String] description The text to use as the release's body / description (typically the release notes) # @param [Array] assets List of file paths to attach as assets to the release # @param [TrueClass|FalseClass] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta) + # @param [String?] githubtoken GitHub OAuth access token # def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:, githubtoken: nil) - release = github_client(github_token!).create_release( + token = githubtoken || github_token! + release = github_client(token).create_release( repository, version, # tag name name: version, # release name @@ -121,7 +123,7 @@ def self.create_release(repository:, version:, target: nil, description:, assets body: description ) assets.each do |file_path| - github_client(github_token!).upload_asset(release[:url], file_path, content_type: 'application/octet-stream') + github_client(token).upload_asset(release[:url], file_path, content_type: 'application/octet-stream') end end diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index a299bb2ea..136152037 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -173,4 +173,55 @@ def mock_comment(body: ' Test', user_id: 1234) instance_double('Comment', id: 1234, body: body, user: instance_double('User', id: user_id)) end end + + describe 'create_release' do + let(:client) do + instance_double( + Octokit::Client, + user: instance_double('User', name: 'test') + ) + end + + before do + allow(described_class).to receive(:github_client).and_return(client) + allow(client).to receive(:create_release) + end + + after do + ENV['GITHUB_TOKEN'] = nil + end + + it 'use the githubtoken when is passed as argument' do + expect(described_class).to receive(:github_client).with('PARAM_TOKEN') + create_release('PARAM_TOKEN') + end + + it 'when githubtoken is nil uses the one from the environment' do + ENV['GITHUB_TOKEN'] = 'GITHUB_TOKEN' + expect(described_class).to receive(:github_client).with('GITHUB_TOKEN') + create_release(nil) + end + + it 'prioritizes argument `githubtoken` over env variable `GITHUB_TOKEN` if both are present' do + ENV['GITHUB_TOKEN'] = 'GITHUB_TOKEN' + expect(described_class).to receive(:github_client).with('PARAM_TOKEN') + create_release('PARAM_TOKEN') + end + + it 'prints an error if neither `githubtoken` nor `GITHUB_TOKEN` are present' do + expect { create_release(nil) }.to raise_error(FastlaneCore::Interface::FastlaneError) + end + + def create_release(token) + described_class.create_release( + repository: '', + version: '', + target: '', + description: '', + assets: [], + prerelease: false, + githubtoken: token + ) + end + end end From 4a8d7929044e8131eaf495072840cd3ae0a45173 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 20 Oct 2022 00:58:20 +0100 Subject: [PATCH 03/27] CreateReleaseAction: Add gihubToken as optional argument At the calling of the action the client has the possibility to use the githubtoken inside the create_relese action, the use is also documented in the ConfigItens --- .../actions/common/create_release_action.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb index d0e9094ed..d10033da8 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb @@ -27,7 +27,8 @@ def self.run(params) target: params[:target], description: release_notes, assets: assets, - prerelease: prerelease + prerelease: prerelease, + githubtoken: params[:githubtoken] ) UI.message('Done') end @@ -82,6 +83,11 @@ def self.available_options optional: true, default_value: false, is_string: false), + FastlaneCore::ConfigItem.new(key: :githubtoken, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: true, + type: String), ] end From 8946ad77f1c5411cae7255356f74288458df2dbc Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 21 Oct 2022 00:33:07 +0100 Subject: [PATCH 04/27] GitHubHelper: GithubToken is now mandatory on the class initialization The github_client was removed in favor of instantiating the GithubClient at every instantiation of the helper, in that way we expose the use of the token and making more difficult to miss it at the use of this helper --- .../wpmreleasetoolkit/helper/github_helper.rb | 63 ++++------ spec/github_helper_spec.rb | 116 +----------------- 2 files changed, 25 insertions(+), 154 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 2620779a2..4d11de1aa 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -8,38 +8,23 @@ module Fastlane module Helper class GithubHelper - def self.github_token! - token = [ - 'GHHELPER_ACCESS', # For historical reasons / backward compatibility - 'GITHUB_TOKEN', # Used by the `gh` CLI tool - ].map { |key| ENV[key] } - .compact - .first - - token || UI.user_error!('Please provide a GitHub authentication token via the `GITHUB_TOKEN` environment variable') - end - - # Creates a client for the GitHub API + # Helper for GitHub Actions # - # @param [String] accesstoken GitHub OAuth access token + # @param [String?] githubtoken GitHub OAuth access token # - def self.github_client(accesstoken) - @@client ||= begin - client = Octokit::Client.new(access_token: accesstoken) - - # Fetch the current user - user = client.user - UI.message("Logged in as: #{user.name}") + def initialize(github_token:) + @client = Octokit::Client.new(access_token: access_token) - # Auto-paginate to ensure we're not missing data - client.auto_paginate = true + # Fetch the current user + user = @client.user + UI.message("Logged in as: #{user.name}") - client - end + # Auto-paginate to ensure we're not missing data + @client.auto_paginate = true end def self.get_milestone(repository, release) - miles = github_client(github_token!).list_milestones(repository) + miles = @client.list_milestones(repository) mile = nil miles&.each do |mm| @@ -56,14 +41,14 @@ def self.get_milestone(repository, release) # @return [] A list of the PRs for the given milestone, sorted by number # def self.get_prs_for_milestone(repository, milestone) - github_client(github_token!).search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) + @client.search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) end def self.get_last_milestone(repository) options = {} options[:state] = 'open' - milestones = github_client(github_token!).list_milestones(repository, options) + milestones = @client.list_milestones(repository, options) return nil if milestones.nil? last_stone = nil @@ -96,7 +81,7 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, options = {} options[:due_on] = newmilestone_duedate options[:description] = comment - github_client(github_token!).create_milestone(repository, newmilestone_number, options) + @client.create_milestone(repository, newmilestone_number, options) end # Creates a Release on GitHub as a Draft @@ -109,11 +94,9 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, # @param [String] description The text to use as the release's body / description (typically the release notes) # @param [Array] assets List of file paths to attach as assets to the release # @param [TrueClass|FalseClass] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta) - # @param [String?] githubtoken GitHub OAuth access token # - def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:, githubtoken: nil) - token = githubtoken || github_token! - release = github_client(token).create_release( + def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:) + release = @client.create_release( repository, version, # tag name name: version, # release name @@ -123,7 +106,7 @@ def self.create_release(repository:, version:, target: nil, description:, assets body: description ) assets.each do |file_path| - github_client(token).upload_asset(release[:url], file_path, content_type: 'application/octet-stream') + @client.upload_asset(release[:url], file_path, content_type: 'application/octet-stream') end end @@ -141,9 +124,8 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) file_name = File.basename(file_path) download_path = File.join(download_folder, file_name) - download_url = github_client(github_token!).contents(repository, - path: file_path, - ref: tag).download_url + download_url = @client.contents(repository, path: file_path, ref: tag).download_url + begin uri = URI.parse(download_url) uri.open do |remote_file| @@ -158,22 +140,21 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) # Creates (or updates an existing) GitHub PR Comment def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid) - client = github_client(github_token!) - comments = client.issue_comments(project_slug, pr_number) + comments = @client.issue_comments(project_slug, pr_number) reuse_marker = "" existing_comment = comments.find do |comment| # Only match comments posted by the owner of the GitHub Token, and with the given reuse ID - comment.user.id == client.user.id and comment.body.include?(reuse_marker) + comment.user.id == @client.user.id and comment.body.include?(reuse_marker) end comment_body = reuse_marker + body if existing_comment.nil? - client.add_comment(project_slug, pr_number, comment_body) + @client.add_comment(project_slug, pr_number, comment_body) else - client.update_comment(project_slug, existing_comment.id, comment_body) + @client.update_comment(project_slug, existing_comment.id, comment_body) end reuse_identifier diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index 136152037..ea3819351 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -15,8 +15,7 @@ end before do - allow(described_class).to receive(:github_token!).and_return('') - allow(described_class).to receive(:github_client).and_return(client) + described_class.instance_variable_set(:@client, client) end it 'fails if it does not find the right release on GitHub' do @@ -36,62 +35,6 @@ end end - describe 'github_token' do - after do - ENV['GHHELPER_ACCESS'] = nil - ENV['GITHUB_TOKEN'] = nil - end - - it 'can use `GHHELPER_ACCESS`' do - ENV['GHHELPER_ACCESS'] = 'GHHELPER_ACCESS' - expect(described_class.github_token!).to eq('GHHELPER_ACCESS') - end - - it 'can use `GITHUB_TOKEN`' do - ENV['GITHUB_TOKEN'] = 'GITHUB_TOKEN' - expect(described_class.github_token!).to eq('GITHUB_TOKEN') - end - - it 'prioritizes GHHELPER_ACCESS` over `GITHUB_TOKEN` if both are present' do - ENV['GITHUB_TOKEN'] = 'GITHUB_TOKEN' - ENV['GHHELPER_ACCESS'] = 'GHHELPER_ACCESS' - expect(described_class.github_token!).to eq('GHHELPER_ACCESS') - end - - it 'prints an error if no environment variable is present' do - expect { described_class.github_token! }.to raise_error(FastlaneCore::Interface::FastlaneError) - end - end - - describe 'github_client' do - let(:client) do - instance_double( - Octokit::Client, - user: instance_double('User', name: 'test'), - 'auto_paginate=': nil - ) - end - - before do - allow(Octokit::Client).to receive(:new).and_return(client) - end - - after do - # Clean up the client memoization between runs to ensure it's re-initialized in each test - described_class.remove_class_variable(:@@client) if described_class.class_variable_defined?(:@@client) - end - - it 'is not nil' do - expect(described_class.github_client('')).not_to be_nil - end - - it 'memoizes the client' do - expect(Octokit::Client).to receive(:new).once - described_class.github_client('') - described_class.github_client('') - end - end - describe 'get_last_milestone' do let(:test_repo) { 'repo-test/project-test' } let(:last_stone) { mock_milestone('10.0') } @@ -103,8 +46,7 @@ end before do - allow(described_class).to receive(:github_token!).and_return('') - allow(described_class).to receive(:github_client).and_return(client) + described_class.instance_variable_set(:@client, client) end it 'returns correct milestone' do @@ -129,8 +71,7 @@ def mock_milestone(title) end before do - allow(described_class).to receive(:github_token!).and_return('') - allow(described_class).to receive(:github_client).and_return(client) + described_class.instance_variable_set(:@client, client) end it 'will create a new comment if an existing one is not found' do @@ -173,55 +114,4 @@ def mock_comment(body: ' Test', user_id: 1234) instance_double('Comment', id: 1234, body: body, user: instance_double('User', id: user_id)) end end - - describe 'create_release' do - let(:client) do - instance_double( - Octokit::Client, - user: instance_double('User', name: 'test') - ) - end - - before do - allow(described_class).to receive(:github_client).and_return(client) - allow(client).to receive(:create_release) - end - - after do - ENV['GITHUB_TOKEN'] = nil - end - - it 'use the githubtoken when is passed as argument' do - expect(described_class).to receive(:github_client).with('PARAM_TOKEN') - create_release('PARAM_TOKEN') - end - - it 'when githubtoken is nil uses the one from the environment' do - ENV['GITHUB_TOKEN'] = 'GITHUB_TOKEN' - expect(described_class).to receive(:github_client).with('GITHUB_TOKEN') - create_release(nil) - end - - it 'prioritizes argument `githubtoken` over env variable `GITHUB_TOKEN` if both are present' do - ENV['GITHUB_TOKEN'] = 'GITHUB_TOKEN' - expect(described_class).to receive(:github_client).with('PARAM_TOKEN') - create_release('PARAM_TOKEN') - end - - it 'prints an error if neither `githubtoken` nor `GITHUB_TOKEN` are present' do - expect { create_release(nil) }.to raise_error(FastlaneCore::Interface::FastlaneError) - end - - def create_release(token) - described_class.create_release( - repository: '', - version: '', - target: '', - description: '', - assets: [], - prerelease: false, - githubtoken: token - ) - end - end end From 6d1e690b2639c070d3fbfccd1f43fc9bfc7aa0f2 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 21 Oct 2022 00:53:54 +0100 Subject: [PATCH 05/27] Actions: All the instances of the GithubHelper require a GithubToken The need of the GitHub token to use the actions is now mandatory, so the ConfigItens and parameters were updated to reflect those changes --- .../android/android_download_file_by_version.rb | 11 ++++++++++- .../actions/common/close_milestone_action.rb | 15 +++++++++++---- .../actions/common/comment_on_pr.rb | 5 ++++- .../actions/common/create_new_milestone_action.rb | 13 +++++++++++-- .../actions/common/create_release_action.rb | 13 ++++++++----- .../actions/common/get_prs_list_action.rb | 11 ++++++++++- .../common/removebranchprotection_action.rb | 11 +++++++++-- .../actions/common/setbranchprotection_action.rb | 12 ++++++++++-- .../actions/common/setfrozentag_action.rb | 15 ++++++++++++--- 9 files changed, 85 insertions(+), 21 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb index 859862182..a4cfbf173 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb @@ -9,7 +9,10 @@ def self.run(params) UI.user_error!("Can't find any reference for key #{params[:import_key]}") if version.nil? UI.message "Downloading #{params[:file_path]} from #{params[:repository]} at version #{version} to #{params[:download_folder]}" - Fastlane::Helper::GithubHelper.download_file_from_tag( + token = params[:github_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + + github_helper.download_file_from_tag( repository: params[:repository], tag: "#{params[:github_release_prefix]}#{version}", file_path: params[:file_path], @@ -57,6 +60,12 @@ def self.available_options description: 'The prefix which is used in the GitHub release title', type: String, optional: true), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index 9e2725b9a..6bf1ed818 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -9,12 +9,13 @@ class CloseMilestoneAction < Action def self.run(params) repository = params[:repository] milestone_title = params[:milestone] + token = params[:github_token] - milestone = Fastlane::Helper::GithubHelper.get_milestone(repository, milestone_title) - UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) - token = Fastlane::Helper::GithubHelper.github_token - Fastlane::Helper::GithubHelper.github_client(token).update_milestone(repository, milestone[:number], state: 'closed') + milestone = github_helper.get_milestone(repository, milestone_title) + UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? + github_helper.update_milestone(repository, milestone[:number], state: 'closed') end def self.description @@ -46,6 +47,12 @@ def self.available_options description: 'The GitHub milestone', optional: false, type: String), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb index e3b82532b..1cc352e09 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb @@ -10,7 +10,10 @@ class CommentOnPrAction < Action def self.run(params) require_relative '../../helper/github_helper' - reuse_identifier = Fastlane::Helper::GithubHelper.comment_on_pr( + token = params[:access_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + + reuse_identifier = github_helper.comment_on_pr( project_slug: params[:project], pr_number: params[:pr_number], body: params[:body], diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb index 0155c0d82..b89f526c3 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb @@ -8,8 +8,11 @@ module Actions class CreateNewMilestoneAction < Action def self.run(params) repository = params[:repository] + token = params[:github_token] - last_stone = Fastlane::Helper::GithubHelper.get_last_milestone(repository) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + + last_stone = github_helper.get_last_milestone(repository) UI.message("Last detected milestone: #{last_stone[:title]} due on #{last_stone[:due_on]}.") milestone_duedate = last_stone[:due_on] milestone_duration = params[:milestone_duration] @@ -17,7 +20,7 @@ def self.run(params) newmilestone_number = Fastlane::Helper::Ios::VersionHelper.calc_next_release_version(last_stone[:title]) number_of_days_from_code_freeze_to_release = params[:number_of_days_from_code_freeze_to_release] UI.message("Next milestone: #{newmilestone_number} due on #{newmilestone_duedate}.") - Fastlane::Helper::GithubHelper.create_milestone(repository, newmilestone_number, newmilestone_duedate, milestone_duration, number_of_days_from_code_freeze_to_release, params[:need_appstore_submission]) + github_helper.create_milestone(repository, newmilestone_number, newmilestone_duedate, milestone_duration, number_of_days_from_code_freeze_to_release, params[:need_appstore_submission]) end def self.description @@ -62,6 +65,12 @@ def self.available_options optional: true, is_string: false, default_value: 14), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb index d10033da8..64f2f2f86 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb @@ -14,6 +14,7 @@ def self.run(params) # Replace full URLS to PRs/Issues with shorthand, because GitHub does not render them properly otherwise. release_notes.gsub!(%r{https://github.com/([^/]*/[^/]*)/(pulls?|issues?)/([0-9]*)}, '\1#\3') prerelease = params[:prerelease] + token = params[:github_token] UI.message("Creating draft release #{version} in #{repository}.") # Verify assets @@ -21,14 +22,15 @@ def self.run(params) UI.user_error!("Can't find file #{file_path}!") unless File.exist?(file_path) end - Fastlane::Helper::GithubHelper.create_release( + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + + github_helper.create_release( repository: repository, version: version, target: params[:target], description: release_notes, assets: assets, - prerelease: prerelease, - githubtoken: params[:githubtoken] + prerelease: prerelease ) UI.message('Done') end @@ -83,10 +85,11 @@ def self.available_options optional: true, default_value: false, is_string: false), - FastlaneCore::ConfigItem.new(key: :githubtoken, + FastlaneCore::ConfigItem.new(key: :github_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', - optional: true, + optional: false, + default_value: false, type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb index 1a2b2679e..d6252f50f 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb @@ -8,9 +8,12 @@ def self.run(params) repository = params[:repository] report_path = File.expand_path(params[:report_path]) milestone = params[:milestone] + token = params[:github_token] + + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) # Get commit list - pr_list = Fastlane::Helper::GithubHelper.get_prs_for_milestone(repository, milestone) + pr_list = github_helper.get_prs_for_milestone(repository, milestone) File.open(report_path, 'w') do |file| pr_list.each do |data| @@ -53,6 +56,12 @@ def self.available_options description: 'The name of the milestone we want to fetch the list of PRs for (e.g.: `16.9`)', optional: false, is_string: true), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb index 9b72d03ec..6eed183ae 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb @@ -14,8 +14,9 @@ def self.run(params) branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - token = Fastlane::Helper::GithubHelper.github_token - Fastlane::Helper::GithubHelper.github_client(token).unprotect_branch(repository, branch_name, branch_prot) + token = params[:github_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper.unprotect_branch(repository, branch_name, branch_prot) end def self.description @@ -47,6 +48,12 @@ def self.available_options description: 'The branch to unprotect', optional: false, type: String), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb index c703233df..9bd3f37fe 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb @@ -13,8 +13,10 @@ def self.run(params) branch_prot[:restrictions] = { url: "#{branch_url}/protection/restrictions", users_url: "#{branch_url}/protection/restrictions/users", teams_url: "#{branch_url}/protection/restrictions/teams", users: [], teams: [] } branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - token = Fastlane::Helper::GithubHelper.github_token - Fastlane::Helper::GithubHelper.github_client(token).protect_branch(repository, branch_name, branch_prot) + + token = params[:github_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper.protect_branch(repository, branch_name, branch_prot) end def self.description @@ -46,6 +48,12 @@ def self.available_options description: 'The branch to protect', optional: false, type: String), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index 4f5a36cdf..b148e4ffe 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -8,8 +8,11 @@ def self.run(params) repository = params[:repository] milestone_title = params[:milestone] freeze = params[:freeze] + token = params[:github_token] - milestone = Fastlane::Helper::GithubHelper.get_milestone(repository, milestone_title) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + + milestone = github_helper.get_milestone(repository, milestone_title) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? mile_title = milestone[:title] @@ -27,8 +30,8 @@ def self.run(params) end UI.message("New milestone: #{mile_title}") - token = Fastlane::Helper::GithubHelper.github_token - Fastlane::Helper::GithubHelper.github_client(token).update_milestone(repository, milestone[:number], title: mile_title) + + github_helper.update_milestone(repository, milestone[:number], title: mile_title) end def self.is_frozen(milestone) @@ -71,6 +74,12 @@ def self.available_options optional: false, default_value: true, is_string: false), + FastlaneCore::ConfigItem.new(key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + default_value: false, + type: String), ] end From 81d954a3a56745d47824d76b94e347ca7386c488 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 21 Oct 2022 15:10:58 +0100 Subject: [PATCH 06/27] GithubHelper: Correct the name of parameter at the initialization --- lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 4d11de1aa..598d2a25c 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -13,7 +13,7 @@ class GithubHelper # @param [String?] githubtoken GitHub OAuth access token # def initialize(github_token:) - @client = Octokit::Client.new(access_token: access_token) + @client = Octokit::Client.new(access_token: github_token) # Fetch the current user user = @client.user From 15099caddfa217e4d6b55ea8f26716da31ec201a Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 21 Oct 2022 15:29:04 +0100 Subject: [PATCH 07/27] Actions: The parameter name im the ConfigItens changed from github_token to access_token --- .../actions/android/android_download_file_by_version.rb | 6 +++--- .../actions/common/close_milestone_action.rb | 6 +++--- .../wpmreleasetoolkit/actions/common/comment_on_pr.rb | 4 ++-- .../actions/common/create_new_milestone_action.rb | 6 +++--- .../actions/common/create_release_action.rb | 6 +++--- .../wpmreleasetoolkit/actions/common/get_prs_list_action.rb | 6 +++--- .../actions/common/removebranchprotection_action.rb | 6 +++--- .../actions/common/setbranchprotection_action.rb | 6 +++--- .../wpmreleasetoolkit/actions/common/setfrozentag_action.rb | 6 +++--- .../plugin/wpmreleasetoolkit/helper/github_helper.rb | 2 +- 10 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb index a4cfbf173..a11221364 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb @@ -9,8 +9,8 @@ def self.run(params) UI.user_error!("Can't find any reference for key #{params[:import_key]}") if version.nil? UI.message "Downloading #{params[:file_path]} from #{params[:repository]} at version #{version} to #{params[:download_folder]}" - token = params[:github_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + access_token = params[:access_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) github_helper.download_file_from_tag( repository: params[:repository], @@ -60,7 +60,7 @@ def self.available_options description: 'The prefix which is used in the GitHub release title', type: String, optional: true), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index 6bf1ed818..33a69a203 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -9,9 +9,9 @@ class CloseMilestoneAction < Action def self.run(params) repository = params[:repository] milestone_title = params[:milestone] - token = params[:github_token] + access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) milestone = github_helper.get_milestone(repository, milestone_title) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? @@ -47,7 +47,7 @@ def self.available_options description: 'The GitHub milestone', optional: false, type: String), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb index 1cc352e09..c2ebbc445 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb @@ -10,8 +10,8 @@ class CommentOnPrAction < Action def self.run(params) require_relative '../../helper/github_helper' - token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + access_token = params[:access_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) reuse_identifier = github_helper.comment_on_pr( project_slug: params[:project], diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb index b89f526c3..b22199ead 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb @@ -8,9 +8,9 @@ module Actions class CreateNewMilestoneAction < Action def self.run(params) repository = params[:repository] - token = params[:github_token] + access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) last_stone = github_helper.get_last_milestone(repository) UI.message("Last detected milestone: #{last_stone[:title]} due on #{last_stone[:due_on]}.") @@ -65,7 +65,7 @@ def self.available_options optional: true, is_string: false, default_value: 14), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb index 64f2f2f86..fc6f76eae 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb @@ -14,7 +14,7 @@ def self.run(params) # Replace full URLS to PRs/Issues with shorthand, because GitHub does not render them properly otherwise. release_notes.gsub!(%r{https://github.com/([^/]*/[^/]*)/(pulls?|issues?)/([0-9]*)}, '\1#\3') prerelease = params[:prerelease] - token = params[:github_token] + access_token = params[:access_token] UI.message("Creating draft release #{version} in #{repository}.") # Verify assets @@ -22,7 +22,7 @@ def self.run(params) UI.user_error!("Can't find file #{file_path}!") unless File.exist?(file_path) end - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) github_helper.create_release( repository: repository, @@ -85,7 +85,7 @@ def self.available_options optional: true, default_value: false, is_string: false), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb index d6252f50f..672706eef 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb @@ -8,9 +8,9 @@ def self.run(params) repository = params[:repository] report_path = File.expand_path(params[:report_path]) milestone = params[:milestone] - token = params[:github_token] + access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) # Get commit list pr_list = github_helper.get_prs_for_milestone(repository, milestone) @@ -56,7 +56,7 @@ def self.available_options description: 'The name of the milestone we want to fetch the list of PRs for (e.g.: `16.9`)', optional: false, is_string: true), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb index 6eed183ae..aaa314503 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb @@ -14,8 +14,8 @@ def self.run(params) branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - token = params[:github_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + access_token = params[:access_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) github_helper.unprotect_branch(repository, branch_name, branch_prot) end @@ -48,7 +48,7 @@ def self.available_options description: 'The branch to unprotect', optional: false, type: String), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb index 9bd3f37fe..265fb0b0c 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb @@ -14,8 +14,8 @@ def self.run(params) branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - token = params[:github_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + access_token = params[:access_token] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) github_helper.protect_branch(repository, branch_name, branch_prot) end @@ -48,7 +48,7 @@ def self.available_options description: 'The branch to protect', optional: false, type: String), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index b148e4ffe..9d79063cd 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -8,9 +8,9 @@ def self.run(params) repository = params[:repository] milestone_title = params[:milestone] freeze = params[:freeze] - token = params[:github_token] + access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) milestone = github_helper.get_milestone(repository, milestone_title) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? @@ -74,7 +74,7 @@ def self.available_options optional: false, default_value: true, is_string: false), - FastlaneCore::ConfigItem.new(key: :github_token, + FastlaneCore::ConfigItem.new(key: :access_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', optional: false, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 598d2a25c..ddc500959 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -10,7 +10,7 @@ module Helper class GithubHelper # Helper for GitHub Actions # - # @param [String?] githubtoken GitHub OAuth access token + # @param [String?] github_token GitHub OAuth access token # def initialize(github_token:) @client = Octokit::Client.new(access_token: github_token) From e110a41de997230d28bb5ad4a22842390962b8bd Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sat, 22 Oct 2022 18:42:37 +0100 Subject: [PATCH 08/27] GithubHelper: Change the OktokitClient to be accessed by attr_reader from GithubClient --- .../wpmreleasetoolkit/helper/github_helper.rb | 24 ++++++++------- spec/github_helper_spec.rb | 30 +++++++++++++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index ddc500959..fbd99726e 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -8,6 +8,8 @@ module Fastlane module Helper class GithubHelper + attr_reader :client + # Helper for GitHub Actions # # @param [String?] github_token GitHub OAuth access token @@ -24,7 +26,7 @@ def initialize(github_token:) end def self.get_milestone(repository, release) - miles = @client.list_milestones(repository) + miles = client.list_milestones(repository) mile = nil miles&.each do |mm| @@ -41,14 +43,14 @@ def self.get_milestone(repository, release) # @return [] A list of the PRs for the given milestone, sorted by number # def self.get_prs_for_milestone(repository, milestone) - @client.search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) + client.search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) end def self.get_last_milestone(repository) options = {} options[:state] = 'open' - milestones = @client.list_milestones(repository, options) + milestones = client.list_milestones(repository, options) return nil if milestones.nil? last_stone = nil @@ -81,7 +83,7 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, options = {} options[:due_on] = newmilestone_duedate options[:description] = comment - @client.create_milestone(repository, newmilestone_number, options) + client.create_milestone(repository, newmilestone_number, options) end # Creates a Release on GitHub as a Draft @@ -96,7 +98,7 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, # @param [TrueClass|FalseClass] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta) # def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:) - release = @client.create_release( + release = client.create_release( repository, version, # tag name name: version, # release name @@ -106,7 +108,7 @@ def self.create_release(repository:, version:, target: nil, description:, assets body: description ) assets.each do |file_path| - @client.upload_asset(release[:url], file_path, content_type: 'application/octet-stream') + client.upload_asset(release[:url], file_path, content_type: 'application/octet-stream') end end @@ -124,7 +126,7 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) file_name = File.basename(file_path) download_path = File.join(download_folder, file_name) - download_url = @client.contents(repository, path: file_path, ref: tag).download_url + download_url = client.contents(repository, path: file_path, ref: tag).download_url begin uri = URI.parse(download_url) @@ -140,21 +142,21 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) # Creates (or updates an existing) GitHub PR Comment def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid) - comments = @client.issue_comments(project_slug, pr_number) + comments = client.issue_comments(project_slug, pr_number) reuse_marker = "" existing_comment = comments.find do |comment| # Only match comments posted by the owner of the GitHub Token, and with the given reuse ID - comment.user.id == @client.user.id and comment.body.include?(reuse_marker) + comment.user.id == client.user.id and comment.body.include?(reuse_marker) end comment_body = reuse_marker + body if existing_comment.nil? - @client.add_comment(project_slug, pr_number, comment_body) + client.add_comment(project_slug, pr_number, comment_body) else - @client.update_comment(project_slug, existing_comment.id, comment_body) + client.update_comment(project_slug, existing_comment.id, comment_body) end reuse_identifier diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index ea3819351..36038237f 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -2,6 +2,30 @@ require 'webmock/rspec' describe Fastlane::Helper::GithubHelper do + describe '#initialize' do + let(:client) do + instance_double( + Octokit::Client, + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil + ) + end + + before do + allow(Octokit::Client).to receive(:new).and_return(client) + end + + it 'with the correct github_token' do + expect(described_class).to receive(:new).with(github_token: 'GITHUB_TOKEN') + described_class.new(github_token: 'GITHUB_TOKEN') + end + + it 'Octokit client receives the correct github_token' do + expect(Octokit::Client).to receive(:new).with(access_token: 'GITHUB_TOKEN') + described_class.new(github_token: 'GITHUB_TOKEN') + end + end + describe 'download_file_from_tag' do let(:test_repo) { 'repo-test/project-test' } let(:test_tag) { '1.0' } @@ -15,7 +39,7 @@ end before do - described_class.instance_variable_set(:@client, client) + allow(described_class).to receive(:client).and_return(client) end it 'fails if it does not find the right release on GitHub' do @@ -46,7 +70,7 @@ end before do - described_class.instance_variable_set(:@client, client) + allow(described_class).to receive(:client).and_return(client) end it 'returns correct milestone' do @@ -71,7 +95,7 @@ def mock_milestone(title) end before do - described_class.instance_variable_set(:@client, client) + allow(described_class).to receive(:client).and_return(client) end it 'will create a new comment if an existing one is not found' do From fcacaa8c3885b52744f395c0612ead1de35df23c Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sun, 23 Oct 2022 00:19:16 +0100 Subject: [PATCH 09/27] GithubHelper: Add unit tests for not covered functions --- spec/github_helper_spec.rb | 115 +++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index 36038237f..b359d78c4 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -138,4 +138,119 @@ def mock_comment(body: ' Test', user_id: 1234) instance_double('Comment', id: 1234, body: body, user: instance_double('User', id: user_id)) end end + + describe 'get_milestone' do + let(:test_repo) { 'repo-test/project-test' } + let(:release_name) { '10.0' } + let(:client) do + instance_double( + Octokit::Client, + list_milestones: [] + ) + end + + before do + allow(described_class).to receive(:client).and_return(client) + end + + it 'receives the correct repository' do + expect(client).to receive(:list_milestones).with(test_repo) + described_class.get_milestone(test_repo, release_name) + end + + it 'returns nil when no milestone exists' do + expect(described_class.get_milestone(test_repo, release_name)).to be_nil + end + + it 'returns milestone from repo' do + allow(client).to receive(:list_milestones).and_return([{ title: 'release/10.0' }]) + expect(described_class.get_milestone(test_repo, release_name)).to be_nil + end + end + + describe 'create_milestone' do + let(:test_repo) { 'repo-test/project-test' } + let(:test_milestone_number) { '10.0' } + let(:test_milestone_duedate) { '2022-10-22T23:39:01Z' } + let(:client) do + instance_double( + Octokit::Client, + create_milestone: nil + ) + end + + before do + allow(described_class).to receive(:client).and_return(client) + end + + it 'has the correct dates to code freeze without submission' do + comment = 'Code freeze: October 22, 2022 App Store submission: November 15, 2022 Release: October 25, 2022' + options = { due_on: test_milestone_duedate, description: comment } + + expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options) + create_milestone(need_submission: false, milestone_duration: 24, days_code_freeze: 3) + end + + it 'has the correct dates to code freeze with submission' do + comment = 'Code freeze: October 22, 2022 App Store submission: October 22, 2022 Release: October 25, 2022' + options = { due_on: test_milestone_duedate, description: comment } + + expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options) + create_milestone(need_submission: true, milestone_duration: 19, days_code_freeze: 3) + end + + def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) + described_class.create_milestone( + test_repo, + test_milestone_number, + test_milestone_duedate, + milestone_duration, + days_code_freeze, + need_submission + ) + end + end + + describe 'create_release' do + let(:test_repo) { 'repo-test/project-test' } + let(:test_tag) { '1.0' } + let(:test_target) { 'dummysha123456' } + let(:test_description) { 'Hey Im a Test Description' } + let(:client) do + instance_double( + Octokit::Client, + create_release: nil + ) + end + + before do + allow(described_class).to receive(:client).and_return(client) + end + + it 'has the correct options' do + options = { body: test_description, draft: true, name: test_tag, prerelease: false, target_commitish: test_target } + expect(client).to receive(:create_release).with(test_repo, test_tag, options) + mockrelease + end + + it 'upload the assets to the correct location' do + test_assets = 'test-file.xml' + test_url = '/test/url' + + allow(client).to receive(:create_release).and_return({ url: test_url }) + expect(client).to receive(:upload_asset).with(test_url, test_assets, { content_type: 'application/octet-stream' }) + mockrelease([test_assets]) + end + + def mockrelease(assets = []) + described_class.create_release( + repository: test_repo, + version: test_tag, + target: test_target, + description: test_description, + assets: assets, + prerelease: false + ) + end + end end From 302e372f5a1ced26dc6db911907114933af4ad79 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sun, 23 Oct 2022 01:33:04 +0100 Subject: [PATCH 10/27] GithubHelper: Create Function to encapsulate the GithubToken Fastlane ConfigItem --- .../android_download_file_by_version.rb | 7 +------ .../actions/common/close_milestone_action.rb | 7 +------ .../actions/common/comment_on_pr.rb | 7 +------ .../common/create_new_milestone_action.rb | 7 +------ .../actions/common/create_release_action.rb | 7 +------ .../actions/common/get_prs_list_action.rb | 7 +------ .../common/removebranchprotection_action.rb | 7 +------ .../common/setbranchprotection_action.rb | 7 +------ .../actions/common/setfrozentag_action.rb | 7 +------ .../wpmreleasetoolkit/helper/github_helper.rb | 14 ++++++++++++++ spec/github_helper_spec.rb | 18 ++++++++++++++++++ 11 files changed, 41 insertions(+), 54 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb index a11221364..5d53939f6 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb @@ -60,12 +60,7 @@ def self.available_options description: 'The prefix which is used in the GitHub release title', type: String, optional: true), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index 33a69a203..177526d61 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -47,12 +47,7 @@ def self.available_options description: 'The GitHub milestone', optional: false, type: String), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb index c2ebbc445..40e11af37 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb @@ -44,12 +44,7 @@ def self.details def self.available_options [ - FastlaneCore::ConfigItem.new( - key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub token to use for posting the comment', - type: String - ), + Fastlane::Helper::GithubHelper.github_token_config_item, FastlaneCore::ConfigItem.new( key: :reuse_identifier, description: 'If provided, the reuse identifier can identify an existing comment to overwrite', diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb index b22199ead..cee5ec5d8 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb @@ -65,12 +65,7 @@ def self.available_options optional: true, is_string: false, default_value: 14), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb index fc6f76eae..900691182 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb @@ -85,12 +85,7 @@ def self.available_options optional: true, default_value: false, is_string: false), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb index 672706eef..443b93c8d 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb @@ -56,12 +56,7 @@ def self.available_options description: 'The name of the milestone we want to fetch the list of PRs for (e.g.: `16.9`)', optional: false, is_string: true), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb index aaa314503..32f36ab26 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb @@ -48,12 +48,7 @@ def self.available_options description: 'The branch to unprotect', optional: false, type: String), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb index 265fb0b0c..19fc05655 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb @@ -48,12 +48,7 @@ def self.available_options description: 'The branch to protect', optional: false, type: String), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index 9d79063cd..3fd25db80 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -74,12 +74,7 @@ def self.available_options optional: false, default_value: true, is_string: false), - FastlaneCore::ConfigItem.new(key: :access_token, - env_name: 'GITHUB_TOKEN', - description: 'The GitHub OAuth access token', - optional: false, - default_value: false, - type: String), + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index fbd99726e..886572237 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -161,6 +161,20 @@ def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: Secur reuse_identifier end + + # Creates a GithubToken Fastlane ConfigItem + # + # @return [FastlaneCore::ConfigItem] The Fastlane ConfigItem for GitHub OAuth access token + # + def self.github_token_config_item + return FastlaneCore::ConfigItem.new( + key: :github_token, + env_name: 'GITHUB_TOKEN', + description: 'The GitHub OAuth access token', + optional: false, + type: String + ) + end end end end diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index b359d78c4..d315f14ea 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -253,4 +253,22 @@ def mockrelease(assets = []) ) end end + + describe 'github_token_config_item' do + it 'has the correct key' do + expect(described_class.github_token_config_item.key).to eq(:github_token) + end + it 'has the correct env_name' do + expect(described_class.github_token_config_item.env_name).to eq('GITHUB_TOKEN') + end + it 'has the correct description' do + expect(described_class.github_token_config_item.description).to eq('The GitHub OAuth access token') + end + it 'is not optional' do + expect(described_class.github_token_config_item.optional).to eq(false) + end + it 'has String as data_type' do + expect(described_class.github_token_config_item.data_type).to eq(String) + end + end end From 461bcd8f435f3ae2a558d84564f05185aac9d935 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sun, 23 Oct 2022 01:49:15 +0100 Subject: [PATCH 11/27] Actions: Set the GithubToken param as github_client instead of access_token --- .../actions/android/android_download_file_by_version.rb | 4 +--- .../actions/common/close_milestone_action.rb | 7 ++----- .../wpmreleasetoolkit/actions/common/comment_on_pr.rb | 4 +--- .../actions/common/create_new_milestone_action.rb | 4 +--- .../actions/common/create_release_action.rb | 4 +--- .../actions/common/get_prs_list_action.rb | 3 +-- .../actions/common/removebranchprotection_action.rb | 5 ++--- .../actions/common/setbranchprotection_action.rb | 5 ++--- .../actions/common/setfrozentag_action.rb | 6 ++---- 9 files changed, 13 insertions(+), 29 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb index 5d53939f6..7d4eb24f8 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb @@ -9,9 +9,7 @@ def self.run(params) UI.user_error!("Can't find any reference for key #{params[:import_key]}") if version.nil? UI.message "Downloading #{params[:file_path]} from #{params[:repository]} at version #{version} to #{params[:download_folder]}" - access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) - + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) github_helper.download_file_from_tag( repository: params[:repository], tag: "#{params[:github_release_prefix]}#{version}", diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index 177526d61..c3acd4def 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -9,13 +9,10 @@ class CloseMilestoneAction < Action def self.run(params) repository = params[:repository] milestone_title = params[:milestone] - access_token = params[:access_token] - - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) - + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) milestone = github_helper.get_milestone(repository, milestone_title) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? - github_helper.update_milestone(repository, milestone[:number], state: 'closed') + github_helper.client.update_milestone(repository, milestone[:number], state: 'closed') end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb index 40e11af37..9335f37be 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/comment_on_pr.rb @@ -10,9 +10,7 @@ class CommentOnPrAction < Action def self.run(params) require_relative '../../helper/github_helper' - access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) - + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) reuse_identifier = github_helper.comment_on_pr( project_slug: params[:project], pr_number: params[:pr_number], diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb index cee5ec5d8..9f3e1ea32 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_new_milestone_action.rb @@ -8,10 +8,8 @@ module Actions class CreateNewMilestoneAction < Action def self.run(params) repository = params[:repository] - access_token = params[:access_token] - - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) last_stone = github_helper.get_last_milestone(repository) UI.message("Last detected milestone: #{last_stone[:title]} due on #{last_stone[:due_on]}.") milestone_duedate = last_stone[:due_on] diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb index 900691182..844d89c91 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_action.rb @@ -14,7 +14,6 @@ def self.run(params) # Replace full URLS to PRs/Issues with shorthand, because GitHub does not render them properly otherwise. release_notes.gsub!(%r{https://github.com/([^/]*/[^/]*)/(pulls?|issues?)/([0-9]*)}, '\1#\3') prerelease = params[:prerelease] - access_token = params[:access_token] UI.message("Creating draft release #{version} in #{repository}.") # Verify assets @@ -22,8 +21,7 @@ def self.run(params) UI.user_error!("Can't find file #{file_path}!") unless File.exist?(file_path) end - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) - + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) github_helper.create_release( repository: repository, version: version, diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb index 443b93c8d..3fcc3ac91 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb @@ -8,9 +8,8 @@ def self.run(params) repository = params[:repository] report_path = File.expand_path(params[:report_path]) milestone = params[:milestone] - access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) # Get commit list pr_list = github_helper.get_prs_for_milestone(repository, milestone) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb index 32f36ab26..9c2726b80 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb @@ -14,9 +14,8 @@ def self.run(params) branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) - github_helper.unprotect_branch(repository, branch_name, branch_prot) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) + github_helper.client.unprotect_branch(repository, branch_name, branch_prot) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb index 19fc05655..c47a94d54 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb @@ -14,9 +14,8 @@ def self.run(params) branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } - access_token = params[:access_token] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) - github_helper.protect_branch(repository, branch_name, branch_prot) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) + github_helper.client.protect_branch(repository, branch_name, branch_prot) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index 3fd25db80..bdfe0a1ac 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -8,10 +8,8 @@ def self.run(params) repository = params[:repository] milestone_title = params[:milestone] freeze = params[:freeze] - access_token = params[:access_token] - - github_helper = Fastlane::Helper::GithubHelper.new(github_token: access_token) + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) milestone = github_helper.get_milestone(repository, milestone_title) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? @@ -31,7 +29,7 @@ def self.run(params) UI.message("New milestone: #{mile_title}") - github_helper.update_milestone(repository, milestone[:number], title: mile_title) + github_helper.client.update_milestone(repository, milestone[:number], title: mile_title) end def self.is_frozen(milestone) From 75c420ec10c86650591e0231cb70cea9494b5494 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sun, 23 Oct 2022 01:55:44 +0100 Subject: [PATCH 12/27] Actions: Correct the line order and spacing --- .../wpmreleasetoolkit/actions/common/close_milestone_action.rb | 3 +++ .../wpmreleasetoolkit/actions/common/get_prs_list_action.rb | 3 +-- .../wpmreleasetoolkit/actions/common/setfrozentag_action.rb | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index c3acd4def..89020a603 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -9,9 +9,12 @@ class CloseMilestoneAction < Action def self.run(params) repository = params[:repository] milestone_title = params[:milestone] + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) milestone = github_helper.get_milestone(repository, milestone_title) + UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? + github_helper.client.update_milestone(repository, milestone[:number], state: 'closed') end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb index 3fcc3ac91..7b9a40e55 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_list_action.rb @@ -9,9 +9,8 @@ def self.run(params) report_path = File.expand_path(params[:report_path]) milestone = params[:milestone] - github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) - # Get commit list + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) pr_list = github_helper.get_prs_for_milestone(repository, milestone) File.open(report_path, 'w') do |file| diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index bdfe0a1ac..d77942dd0 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -11,6 +11,7 @@ def self.run(params) github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) milestone = github_helper.get_milestone(repository, milestone_title) + UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? mile_title = milestone[:title] @@ -28,7 +29,6 @@ def self.run(params) end UI.message("New milestone: #{mile_title}") - github_helper.client.update_milestone(repository, milestone[:number], title: mile_title) end From 76f5dcc0b9f743c8a6d6123a047785217d48a4dd Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Mon, 24 Oct 2022 23:45:34 +0100 Subject: [PATCH 13/27] Actions: All Acess to OktoClient is done by GithubHelper The GithubHelper must be the only entry point to access the OktoClient, with that we have more control about the correct parameters and configurations being set and centralizes all the logic on a single point --- .../actions/common/close_milestone_action.rb | 2 +- .../common/removebranchprotection_action.rb | 4 +- .../common/setbranchprotection_action.rb | 4 +- .../actions/common/setfrozentag_action.rb | 2 +- .../wpmreleasetoolkit/helper/github_helper.rb | 33 ++++++++ spec/close_milestone_action_spec.rb | 63 +++++++++++++++ spec/removebranchprotection_action_spec.rb | 74 ++++++++++++++++++ spec/setbranchprotection_action_spec.rb | 74 ++++++++++++++++++ spec/setfrozentag_action_spec.rb | 78 +++++++++++++++++++ 9 files changed, 328 insertions(+), 6 deletions(-) create mode 100644 spec/close_milestone_action_spec.rb create mode 100644 spec/removebranchprotection_action_spec.rb create mode 100644 spec/setbranchprotection_action_spec.rb create mode 100644 spec/setfrozentag_action_spec.rb diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb index 89020a603..f85d18dcd 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/close_milestone_action.rb @@ -15,7 +15,7 @@ def self.run(params) UI.user_error!("Milestone #{milestone_title} not found.") if milestone.nil? - github_helper.client.update_milestone(repository, milestone[:number], state: 'closed') + github_helper.update_milestone(repository: repository, number: milestone[:number], options: { state: 'closed' }) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb index 9c2726b80..9d33e5083 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/removebranchprotection_action.rb @@ -7,15 +7,15 @@ class RemovebranchprotectionAction < Action def self.run(params) repository = params[:repository] branch_name = params[:branch] - branch_prot = {} + branch_prot = {} branch_url = "https://api.github.com/repos/#{repository}/branches/#{branch_name}" branch_prot[:restrictions] = { url: "#{branch_url}/protection/restrictions", users_url: "#{branch_url}/protection/restrictions/users", teams_url: "#{branch_url}/protection/restrictions/teams", users: [], teams: [] } branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) - github_helper.client.unprotect_branch(repository, branch_name, branch_prot) + github_helper.remove_branch_protection(repository: repository, branch: branch_name, options: branch_prot) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb index c47a94d54..41a11e5bd 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setbranchprotection_action.rb @@ -7,15 +7,15 @@ class SetbranchprotectionAction < Action def self.run(params) repository = params[:repository] branch_name = params[:branch] - branch_prot = {} + branch_prot = {} branch_url = "https://api.github.com/repos/#{repository}/branches/#{branch_name}" branch_prot[:restrictions] = { url: "#{branch_url}/protection/restrictions", users_url: "#{branch_url}/protection/restrictions/users", teams_url: "#{branch_url}/protection/restrictions/teams", users: [], teams: [] } branch_prot[:enforce_admins] = nil branch_prot[:required_pull_request_reviews] = { url: "#{branch_url}/protection/required_pull_request_reviews", dismiss_stale_reviews: false, require_code_owner_reviews: false } github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) - github_helper.client.protect_branch(repository, branch_name, branch_prot) + github_helper.set_branch_protection(repository: repository, branch: branch_name, options: branch_prot) end def self.description diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb index d77942dd0..780ffede1 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/setfrozentag_action.rb @@ -29,7 +29,7 @@ def self.run(params) end UI.message("New milestone: #{mile_title}") - github_helper.client.update_milestone(repository, milestone[:number], title: mile_title) + github_helper.update_milestone(repository: repository, number: milestone[:number], options: { title: mile_title }) end def self.is_frozen(milestone) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 886572237..8b2461cd9 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -162,6 +162,39 @@ def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: Secur reuse_identifier end + # Update a milestone for a repository + # + # @param [String] repository The repository name (including the organization) + # @param [String] number The number of the milestone we want to fetch + # @param options [Hash] A customizable set of options. + # @option options [String] :title A unique title. + # @option options [String] :state + # @option options [String] :description A meaningful description + # @option options [Time] :due_on Set if the milestone has a due date + # @return [Milestone] A single milestone object + # + def self.update_milestone(repository:, number:, options: {}) + client.update_milestone(repository, number, options) + end + + # Unlock a single branch from a repository + # + # @param [String] repository The repository name (including the organization) + # @param [String] number The branch name + # + def self.remove_branch_protection(repository:, branch:, options:) + client.unprotect_branch(repository, branch_name, options) + end + + # Lock a single branch from a repository + # + # @param [String] repository The repository name (including the organization) + # @param [String] number The branch name + # + def self.set_branch_protection(repository:, branch:, options:) + client.protect_branch(repository, branch_name, options) + end + # Creates a GithubToken Fastlane ConfigItem # # @return [FastlaneCore::ConfigItem] The Fastlane ConfigItem for GitHub OAuth access token diff --git a/spec/close_milestone_action_spec.rb b/spec/close_milestone_action_spec.rb new file mode 100644 index 000000000..be9fe63d2 --- /dev/null +++ b/spec/close_milestone_action_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +describe Fastlane::Actions::CloseMilestoneAction do + describe 'run' do + let(:test_repository) { 'test/test' } + let(:test_milestone_title) { 'milestone_title' } + let(:test_milestone) { { number: 1234 } } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:update_milestone) + allow(github_helper).to receive(:get_milestone).and_return(test_milestone) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the get_milestone' do + expect(github_helper).to receive(:get_milestone).with(test_repository, test_milestone_title) + described_class.run(mock_params) + end + + it 'calls the update_milestone' do + expect(github_helper).to receive(:update_milestone).with( + repository: test_repository, + number: 1234, + options: { state: 'closed' } + ) + + described_class.run(mock_params) + end + + def mock_params + { + repository: test_repository, + milestone: test_milestone_title, + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(3) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem milestone' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/removebranchprotection_action_spec.rb b/spec/removebranchprotection_action_spec.rb new file mode 100644 index 000000000..46c1b9695 --- /dev/null +++ b/spec/removebranchprotection_action_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +describe Fastlane::Actions::RemovebranchprotectionAction do + describe 'run' do + let(:test_repository) { 'test/test' } + let(:test_branch) { 'test_branch' } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:remove_branch_protection) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the remove_branch_protection' do + expect(github_helper).to receive(:remove_branch_protection).with( + repository: test_repository, + branch: test_branch, + options: branch_options + ) + + described_class.run(mock_params) + end + + def mock_params + { + repository: test_repository, + branch: test_branch, + github_token: test_token + } + end + + def branch_options + { + enforce_admins: nil, + required_pull_request_reviews: { + dismiss_stale_reviews: false, + require_code_owner_reviews: false, + url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/required_pull_request_reviews' + }, + restrictions: { + teams: [], + teams_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/teams', + url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions', + users: [], + users_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/users' + } + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(3) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem branch' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :branch)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/setbranchprotection_action_spec.rb b/spec/setbranchprotection_action_spec.rb new file mode 100644 index 000000000..3f5bbaa0a --- /dev/null +++ b/spec/setbranchprotection_action_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +describe Fastlane::Actions::SetbranchprotectionAction do + describe 'run' do + let(:test_repository) { 'test/test' } + let(:test_branch) { 'test_branch' } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:set_branch_protection) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the set_branch_protection' do + expect(github_helper).to receive(:set_branch_protection).with( + repository: test_repository, + branch: test_branch, + options: branch_options + ) + + described_class.run(mock_params) + end + + def mock_params + { + repository: test_repository, + branch: test_branch, + github_token: test_token + } + end + + def branch_options + { + enforce_admins: nil, + required_pull_request_reviews: { + dismiss_stale_reviews: false, + require_code_owner_reviews: false, + url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/required_pull_request_reviews' + }, + restrictions: { + teams: [], + teams_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/teams', + url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions', + users: [], + users_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/users' + } + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(3) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem branch' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :branch)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/setfrozentag_action_spec.rb b/spec/setfrozentag_action_spec.rb new file mode 100644 index 000000000..2d1c41328 --- /dev/null +++ b/spec/setfrozentag_action_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper' + +describe Fastlane::Actions::SetfrozentagAction do + describe 'run' do + let(:test_repository) { 'test/test' } + let(:test_milestone_title) { 'milestone_title' } + let(:test_milestone) { { title: 'milestone', number: 1234 } } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:update_milestone) + allow(github_helper).to receive(:get_milestone).and_return(test_milestone) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the get_milestone' do + expect(github_helper).to receive(:get_milestone).with(test_repository, test_milestone_title) + described_class.run(mock_params) + end + + it 'calls the update_milestone' do + expect(github_helper).to receive(:update_milestone).with( + repository: test_repository, + number: 1234, + options: { title: test_milestone_title } + ) + + described_class.run(mock_params) + end + + it 'calls the update_milestone with freeze' do + expect(github_helper).to receive(:update_milestone).with( + repository: test_repository, + number: 1234, + options: { title: 'milestone ❄️' } + ) + + described_class.run(mock_params(freeze: true)) + end + + def mock_params(freeze: false) + { + repository: test_repository, + milestone: test_milestone_title, + freeze: freeze, + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(4) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem milestone' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone)) + end + + it 'has the ConfigItem freeze' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :freeze)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end From e95557b22c344c0acade86d1060e5909de9f053e Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 25 Oct 2022 01:12:20 +0100 Subject: [PATCH 14/27] Actions: Add Unit Tests to Specs without them --- spec/android_download_file_by_version_spec.rb | 79 ++++++++++++++++++ spec/comment_on_pr_spec.rb | 69 ++++++++++++++++ spec/create_new_milestone_action_spec.rb | 81 +++++++++++++++++++ spec/create_release_action_spec.rb | 80 ++++++++++++++++++ spec/get_prs_list_action_spec.rb | 58 +++++++++++++ 5 files changed, 367 insertions(+) create mode 100644 spec/android_download_file_by_version_spec.rb create mode 100644 spec/comment_on_pr_spec.rb create mode 100644 spec/create_new_milestone_action_spec.rb create mode 100644 spec/create_release_action_spec.rb create mode 100644 spec/get_prs_list_action_spec.rb diff --git a/spec/android_download_file_by_version_spec.rb b/spec/android_download_file_by_version_spec.rb new file mode 100644 index 000000000..2e0c97885 --- /dev/null +++ b/spec/android_download_file_by_version_spec.rb @@ -0,0 +1,79 @@ +require 'spec_helper' + +describe Fastlane::Actions::AndroidDownloadFileByVersionAction do + describe 'run' do + let(:test_repo) { 'repo-test/project-test' } + let(:test_tag) { 'release/10.0' } + let(:test_version) { '10.0' } + let(:test_file) { 'test-file.xml' } + let(:test_folder) { 'test-folder' } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:download_file_from_tag) # .and_return(nil) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + allow(Fastlane::Helper::Android::VersionHelper).to receive(:get_library_version_from_gradle_config).and_return(test_version) + end + + it 'includes the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the download_file_from_tag' do + expect(github_helper).to receive(:download_file_from_tag).with( + repository: test_repo, + tag: test_tag, + file_path: test_file, + download_folder: test_folder + ) + + described_class.run(mock_params) + end + + def mock_params + { + repository: test_repo, + github_release_prefix: 'release/', + file_path: test_file, + download_folder: test_folder, + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(7) + end + + it 'has the ConfigItem library_name' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :library_name)) + end + + it 'has the ConfigItem import_key' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :import_key)) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem file_path' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :file_path)) + end + + it 'has the ConfigItem download_folder' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :download_folder)) + end + + it 'has the ConfigItem github_release_prefix' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_release_prefix)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/comment_on_pr_spec.rb b/spec/comment_on_pr_spec.rb new file mode 100644 index 000000000..becfe436b --- /dev/null +++ b/spec/comment_on_pr_spec.rb @@ -0,0 +1,69 @@ +require 'spec_helper' + +describe Fastlane::Actions::CommentOnPrAction do + describe 'run' do + let(:test_project) { 'test/test' } + let(:test_pr_number) { 1234 } + let(:test_body) { 'Test' } + let(:test_reuse_identifier) { 'test-id' } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:comment_on_pr).and_return('') + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the comment_on_pr' do + expect(github_helper).to receive(:comment_on_pr).with( + project_slug: test_project, + pr_number: test_pr_number, + body: test_body, + reuse_identifier: test_reuse_identifier + ) + + described_class.run(mock_params) + end + + def mock_params + { + project: test_project, + pr_number: test_pr_number, + body: test_body, + reuse_identifier: test_reuse_identifier, + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(5) + end + + it 'has the ConfigItem reuse_identifier' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :reuse_identifier)) + end + + it 'has the ConfigItem project' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :project)) + end + + it 'has the ConfigItem pr_number' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :pr_number)) + end + + it 'has the ConfigItem body' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :body)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/create_new_milestone_action_spec.rb b/spec/create_new_milestone_action_spec.rb new file mode 100644 index 000000000..ecd33c8f5 --- /dev/null +++ b/spec/create_new_milestone_action_spec.rb @@ -0,0 +1,81 @@ +require 'spec_helper' + +describe Fastlane::Actions::CreateNewMilestoneAction do + describe 'run' do + let(:test_repository) { 'test/test' } + let(:test_milestone_duration) { 10 } + let(:test_freeze_days) { 5 } + let(:test_submit_appstore) { false } + let(:test_milestone_number) { 1234 } + let(:test_milestone_duedate) { '2022-11-01T23:39:01Z'.to_time.utc } + let(:test_milestone) { { title: 'milestone', due_on: '2022-10-22T23:39:01Z' } } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:create_milestone) + allow(github_helper).to receive(:get_last_milestone).and_return(test_milestone) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + allow(Fastlane::Helper::Ios::VersionHelper).to receive(:calc_next_release_version).and_return(test_milestone_number) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the get_last_milestone' do + expect(github_helper).to receive(:get_last_milestone).with(test_repository) + described_class.run(mock_params) + end + + it 'calls the create_milestone' do + expect(github_helper).to receive(:create_milestone).with( + test_repository, + test_milestone_number, + test_milestone_duedate, + test_milestone_duration, + test_freeze_days, + test_submit_appstore + ) + + described_class.run(mock_params) + end + + def mock_params(freeze: false) + { + repository: test_repository, + milestone_duration: test_milestone_duration, + number_of_days_from_code_freeze_to_release: test_freeze_days, + need_appstore_submission: test_submit_appstore, + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(5) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem need_appstore_submission' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :need_appstore_submission)) + end + + it 'has the ConfigItem milestone_duration' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone_duration)) + end + + it 'has the ConfigItem number_of_days_from_code_freeze_to_release' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :number_of_days_from_code_freeze_to_release)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/create_release_action_spec.rb b/spec/create_release_action_spec.rb new file mode 100644 index 000000000..54b4ebe8d --- /dev/null +++ b/spec/create_release_action_spec.rb @@ -0,0 +1,80 @@ +require 'spec_helper' + +describe Fastlane::Actions::CreateReleaseAction do + describe 'run' do + let(:test_repo) { 'repo-test/project-test' } + let(:test_version) { 'release/10.0' } + let(:test_target) { 'dummy_target' } + let(:test_prerelease) { false } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(github_helper).to receive(:create_release) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the create_release' do + expect(github_helper).to receive(:create_release).with( + repository: test_repo, + version: test_version, + target: test_target, + description: '', + assets: [], + prerelease: test_prerelease + ) + + described_class.run(mock_params) + end + + def mock_params + { + repository: test_repo, + version: test_version, + release_assets: [], + target: test_target, + prerelease: test_prerelease, + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(7) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem version' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :version)) + end + + it 'has the ConfigItem target' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :target)) + end + + it 'has the ConfigItem release_notes_file_path' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :release_notes_file_path)) + end + + it 'has the ConfigItem release_assets' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :release_assets)) + end + + it 'has the ConfigItem prerelease' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :prerelease)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end diff --git a/spec/get_prs_list_action_spec.rb b/spec/get_prs_list_action_spec.rb new file mode 100644 index 000000000..4e759aff1 --- /dev/null +++ b/spec/get_prs_list_action_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe Fastlane::Actions::GetPrsListAction do + describe 'run' do + let(:test_repo) { 'repo-test/project-test' } + let(:test_milestone) { 'release/10.0' } + let(:file_like_object) { double('file like object') } + let(:test_token) { 'GITHUB_TOKEN' } + let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } + + before do + allow(File).to receive(:open).and_return(file_like_object) + allow(github_helper).to receive(:get_prs_for_milestone).and_return([]) + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + it 'sets the github_token' do + expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) + described_class.run(mock_params) + end + + it 'calls the get_prs_for_milestone' do + expect(github_helper).to receive(:get_prs_for_milestone).with(test_repo, test_milestone) + described_class.run(mock_params) + end + + def mock_params + { + repository: test_repo, + milestone: test_milestone, + report_path: '', + github_token: test_token + } + end + end + + describe 'available_options' do + it 'has the correct length' do + expect(described_class.available_options.length).to be(4) + end + + it 'has the ConfigItem repository' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) + end + + it 'has the ConfigItem report_path' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :report_path)) + end + + it 'has the ConfigItem milestone' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone)) + end + + it 'has the ConfigItem github_token' do + expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) + end + end +end From 2576c5135933624568b5e9b45e19a4abf459f76b Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 25 Oct 2022 01:56:16 +0100 Subject: [PATCH 15/27] Actions: Delete Unit Tests to Move for Another PR --- spec/android_download_file_by_version_spec.rb | 79 ------------------ spec/close_milestone_action_spec.rb | 63 --------------- spec/comment_on_pr_spec.rb | 69 ---------------- spec/create_new_milestone_action_spec.rb | 81 ------------------- spec/create_release_action_spec.rb | 80 ------------------ spec/get_prs_list_action_spec.rb | 58 ------------- spec/removebranchprotection_action_spec.rb | 74 ----------------- spec/setbranchprotection_action_spec.rb | 74 ----------------- spec/setfrozentag_action_spec.rb | 78 ------------------ 9 files changed, 656 deletions(-) delete mode 100644 spec/android_download_file_by_version_spec.rb delete mode 100644 spec/close_milestone_action_spec.rb delete mode 100644 spec/comment_on_pr_spec.rb delete mode 100644 spec/create_new_milestone_action_spec.rb delete mode 100644 spec/create_release_action_spec.rb delete mode 100644 spec/get_prs_list_action_spec.rb delete mode 100644 spec/removebranchprotection_action_spec.rb delete mode 100644 spec/setbranchprotection_action_spec.rb delete mode 100644 spec/setfrozentag_action_spec.rb diff --git a/spec/android_download_file_by_version_spec.rb b/spec/android_download_file_by_version_spec.rb deleted file mode 100644 index 2e0c97885..000000000 --- a/spec/android_download_file_by_version_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::AndroidDownloadFileByVersionAction do - describe 'run' do - let(:test_repo) { 'repo-test/project-test' } - let(:test_tag) { 'release/10.0' } - let(:test_version) { '10.0' } - let(:test_file) { 'test-file.xml' } - let(:test_folder) { 'test-folder' } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:download_file_from_tag) # .and_return(nil) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - allow(Fastlane::Helper::Android::VersionHelper).to receive(:get_library_version_from_gradle_config).and_return(test_version) - end - - it 'includes the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the download_file_from_tag' do - expect(github_helper).to receive(:download_file_from_tag).with( - repository: test_repo, - tag: test_tag, - file_path: test_file, - download_folder: test_folder - ) - - described_class.run(mock_params) - end - - def mock_params - { - repository: test_repo, - github_release_prefix: 'release/', - file_path: test_file, - download_folder: test_folder, - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(7) - end - - it 'has the ConfigItem library_name' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :library_name)) - end - - it 'has the ConfigItem import_key' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :import_key)) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem file_path' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :file_path)) - end - - it 'has the ConfigItem download_folder' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :download_folder)) - end - - it 'has the ConfigItem github_release_prefix' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_release_prefix)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/close_milestone_action_spec.rb b/spec/close_milestone_action_spec.rb deleted file mode 100644 index be9fe63d2..000000000 --- a/spec/close_milestone_action_spec.rb +++ /dev/null @@ -1,63 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::CloseMilestoneAction do - describe 'run' do - let(:test_repository) { 'test/test' } - let(:test_milestone_title) { 'milestone_title' } - let(:test_milestone) { { number: 1234 } } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:update_milestone) - allow(github_helper).to receive(:get_milestone).and_return(test_milestone) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the get_milestone' do - expect(github_helper).to receive(:get_milestone).with(test_repository, test_milestone_title) - described_class.run(mock_params) - end - - it 'calls the update_milestone' do - expect(github_helper).to receive(:update_milestone).with( - repository: test_repository, - number: 1234, - options: { state: 'closed' } - ) - - described_class.run(mock_params) - end - - def mock_params - { - repository: test_repository, - milestone: test_milestone_title, - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(3) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem milestone' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/comment_on_pr_spec.rb b/spec/comment_on_pr_spec.rb deleted file mode 100644 index becfe436b..000000000 --- a/spec/comment_on_pr_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::CommentOnPrAction do - describe 'run' do - let(:test_project) { 'test/test' } - let(:test_pr_number) { 1234 } - let(:test_body) { 'Test' } - let(:test_reuse_identifier) { 'test-id' } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:comment_on_pr).and_return('') - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the comment_on_pr' do - expect(github_helper).to receive(:comment_on_pr).with( - project_slug: test_project, - pr_number: test_pr_number, - body: test_body, - reuse_identifier: test_reuse_identifier - ) - - described_class.run(mock_params) - end - - def mock_params - { - project: test_project, - pr_number: test_pr_number, - body: test_body, - reuse_identifier: test_reuse_identifier, - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(5) - end - - it 'has the ConfigItem reuse_identifier' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :reuse_identifier)) - end - - it 'has the ConfigItem project' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :project)) - end - - it 'has the ConfigItem pr_number' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :pr_number)) - end - - it 'has the ConfigItem body' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :body)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/create_new_milestone_action_spec.rb b/spec/create_new_milestone_action_spec.rb deleted file mode 100644 index ecd33c8f5..000000000 --- a/spec/create_new_milestone_action_spec.rb +++ /dev/null @@ -1,81 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::CreateNewMilestoneAction do - describe 'run' do - let(:test_repository) { 'test/test' } - let(:test_milestone_duration) { 10 } - let(:test_freeze_days) { 5 } - let(:test_submit_appstore) { false } - let(:test_milestone_number) { 1234 } - let(:test_milestone_duedate) { '2022-11-01T23:39:01Z'.to_time.utc } - let(:test_milestone) { { title: 'milestone', due_on: '2022-10-22T23:39:01Z' } } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:create_milestone) - allow(github_helper).to receive(:get_last_milestone).and_return(test_milestone) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - allow(Fastlane::Helper::Ios::VersionHelper).to receive(:calc_next_release_version).and_return(test_milestone_number) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the get_last_milestone' do - expect(github_helper).to receive(:get_last_milestone).with(test_repository) - described_class.run(mock_params) - end - - it 'calls the create_milestone' do - expect(github_helper).to receive(:create_milestone).with( - test_repository, - test_milestone_number, - test_milestone_duedate, - test_milestone_duration, - test_freeze_days, - test_submit_appstore - ) - - described_class.run(mock_params) - end - - def mock_params(freeze: false) - { - repository: test_repository, - milestone_duration: test_milestone_duration, - number_of_days_from_code_freeze_to_release: test_freeze_days, - need_appstore_submission: test_submit_appstore, - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(5) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem need_appstore_submission' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :need_appstore_submission)) - end - - it 'has the ConfigItem milestone_duration' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone_duration)) - end - - it 'has the ConfigItem number_of_days_from_code_freeze_to_release' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :number_of_days_from_code_freeze_to_release)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/create_release_action_spec.rb b/spec/create_release_action_spec.rb deleted file mode 100644 index 54b4ebe8d..000000000 --- a/spec/create_release_action_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::CreateReleaseAction do - describe 'run' do - let(:test_repo) { 'repo-test/project-test' } - let(:test_version) { 'release/10.0' } - let(:test_target) { 'dummy_target' } - let(:test_prerelease) { false } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:create_release) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the create_release' do - expect(github_helper).to receive(:create_release).with( - repository: test_repo, - version: test_version, - target: test_target, - description: '', - assets: [], - prerelease: test_prerelease - ) - - described_class.run(mock_params) - end - - def mock_params - { - repository: test_repo, - version: test_version, - release_assets: [], - target: test_target, - prerelease: test_prerelease, - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(7) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem version' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :version)) - end - - it 'has the ConfigItem target' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :target)) - end - - it 'has the ConfigItem release_notes_file_path' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :release_notes_file_path)) - end - - it 'has the ConfigItem release_assets' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :release_assets)) - end - - it 'has the ConfigItem prerelease' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :prerelease)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/get_prs_list_action_spec.rb b/spec/get_prs_list_action_spec.rb deleted file mode 100644 index 4e759aff1..000000000 --- a/spec/get_prs_list_action_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::GetPrsListAction do - describe 'run' do - let(:test_repo) { 'repo-test/project-test' } - let(:test_milestone) { 'release/10.0' } - let(:file_like_object) { double('file like object') } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(File).to receive(:open).and_return(file_like_object) - allow(github_helper).to receive(:get_prs_for_milestone).and_return([]) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the get_prs_for_milestone' do - expect(github_helper).to receive(:get_prs_for_milestone).with(test_repo, test_milestone) - described_class.run(mock_params) - end - - def mock_params - { - repository: test_repo, - milestone: test_milestone, - report_path: '', - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(4) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem report_path' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :report_path)) - end - - it 'has the ConfigItem milestone' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/removebranchprotection_action_spec.rb b/spec/removebranchprotection_action_spec.rb deleted file mode 100644 index 46c1b9695..000000000 --- a/spec/removebranchprotection_action_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::RemovebranchprotectionAction do - describe 'run' do - let(:test_repository) { 'test/test' } - let(:test_branch) { 'test_branch' } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:remove_branch_protection) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the remove_branch_protection' do - expect(github_helper).to receive(:remove_branch_protection).with( - repository: test_repository, - branch: test_branch, - options: branch_options - ) - - described_class.run(mock_params) - end - - def mock_params - { - repository: test_repository, - branch: test_branch, - github_token: test_token - } - end - - def branch_options - { - enforce_admins: nil, - required_pull_request_reviews: { - dismiss_stale_reviews: false, - require_code_owner_reviews: false, - url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/required_pull_request_reviews' - }, - restrictions: { - teams: [], - teams_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/teams', - url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions', - users: [], - users_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/users' - } - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(3) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem branch' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :branch)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/setbranchprotection_action_spec.rb b/spec/setbranchprotection_action_spec.rb deleted file mode 100644 index 3f5bbaa0a..000000000 --- a/spec/setbranchprotection_action_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::SetbranchprotectionAction do - describe 'run' do - let(:test_repository) { 'test/test' } - let(:test_branch) { 'test_branch' } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:set_branch_protection) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the set_branch_protection' do - expect(github_helper).to receive(:set_branch_protection).with( - repository: test_repository, - branch: test_branch, - options: branch_options - ) - - described_class.run(mock_params) - end - - def mock_params - { - repository: test_repository, - branch: test_branch, - github_token: test_token - } - end - - def branch_options - { - enforce_admins: nil, - required_pull_request_reviews: { - dismiss_stale_reviews: false, - require_code_owner_reviews: false, - url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/required_pull_request_reviews' - }, - restrictions: { - teams: [], - teams_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/teams', - url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions', - users: [], - users_url: 'https://api.github.com/repos/test/test/branches/test_branch/protection/restrictions/users' - } - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(3) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem branch' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :branch)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end diff --git a/spec/setfrozentag_action_spec.rb b/spec/setfrozentag_action_spec.rb deleted file mode 100644 index 2d1c41328..000000000 --- a/spec/setfrozentag_action_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'spec_helper' - -describe Fastlane::Actions::SetfrozentagAction do - describe 'run' do - let(:test_repository) { 'test/test' } - let(:test_milestone_title) { 'milestone_title' } - let(:test_milestone) { { title: 'milestone', number: 1234 } } - let(:test_token) { 'GITHUB_TOKEN' } - let(:github_helper) { class_double(Fastlane::Helper::GithubHelper) } - - before do - allow(github_helper).to receive(:update_milestone) - allow(github_helper).to receive(:get_milestone).and_return(test_milestone) - allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) - end - - it 'sets the github_token' do - expect(Fastlane::Helper::GithubHelper).to receive(:new).with(github_token: test_token) - described_class.run(mock_params) - end - - it 'calls the get_milestone' do - expect(github_helper).to receive(:get_milestone).with(test_repository, test_milestone_title) - described_class.run(mock_params) - end - - it 'calls the update_milestone' do - expect(github_helper).to receive(:update_milestone).with( - repository: test_repository, - number: 1234, - options: { title: test_milestone_title } - ) - - described_class.run(mock_params) - end - - it 'calls the update_milestone with freeze' do - expect(github_helper).to receive(:update_milestone).with( - repository: test_repository, - number: 1234, - options: { title: 'milestone ❄️' } - ) - - described_class.run(mock_params(freeze: true)) - end - - def mock_params(freeze: false) - { - repository: test_repository, - milestone: test_milestone_title, - freeze: freeze, - github_token: test_token - } - end - end - - describe 'available_options' do - it 'has the correct length' do - expect(described_class.available_options.length).to be(4) - end - - it 'has the ConfigItem repository' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :repository)) - end - - it 'has the ConfigItem milestone' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :milestone)) - end - - it 'has the ConfigItem freeze' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :freeze)) - end - - it 'has the ConfigItem github_token' do - expect(described_class.available_options).to include(an_object_having_attributes(key: :github_token)) - end - end -end From fb94b4713a19eee0ea6f1122397de3f4628e3983 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 25 Oct 2022 15:39:41 +0100 Subject: [PATCH 16/27] GithubActions: Fix Failing Unit Tests --- spec/github_helper_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index d315f14ea..b2ba187d4 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -184,16 +184,16 @@ def mock_comment(body: ' Test', user_id: 1234) end it 'has the correct dates to code freeze without submission' do - comment = 'Code freeze: October 22, 2022 App Store submission: November 15, 2022 Release: October 25, 2022' - options = { due_on: test_milestone_duedate, description: comment } + comment = "Code freeze: October 22, 2022\nApp Store submission: November 15, 2022\nRelease: October 25, 2022\n" + options = { due_on: '2022-10-22T12:00:00Z', description: comment } expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options) create_milestone(need_submission: false, milestone_duration: 24, days_code_freeze: 3) end it 'has the correct dates to code freeze with submission' do - comment = 'Code freeze: October 22, 2022 App Store submission: October 22, 2022 Release: October 25, 2022' - options = { due_on: test_milestone_duedate, description: comment } + comment = "Code freeze: October 22, 2022\nApp Store submission: October 22, 2022\nRelease: October 25, 2022\n" + options = { due_on: '2022-10-22T12:00:00Z', description: comment } expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options) create_milestone(need_submission: true, milestone_duration: 19, days_code_freeze: 3) @@ -203,7 +203,7 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) described_class.create_milestone( test_repo, test_milestone_number, - test_milestone_duedate, + test_milestone_duedate.to_time.utc, milestone_duration, days_code_freeze, need_submission From 126a46c59c49fbcd04403330e7de3522accdea99 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 25 Oct 2022 22:58:58 +0100 Subject: [PATCH 17/27] GithubActions: Fix Issues Pointed on PR Review --- .../wpmreleasetoolkit/helper/github_helper.rb | 9 +++- spec/github_helper_spec.rb | 44 ++++++++++++------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 315fd5497..8ea5f398b 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -187,8 +187,9 @@ def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: Secur # @option options [String] :description A meaningful description # @option options [Time] :due_on Set if the milestone has a due date # @return [Milestone] A single milestone object + # @see http://developer.github.com/v3/issues/milestones/#update-a-milestone # - def self.update_milestone(repository:, number:, options: {}) + def self.update_milestone(repository:, number:, options:) client.update_milestone(repository, number, options) end @@ -196,6 +197,8 @@ def self.update_milestone(repository:, number:, options: {}) # # @param [String] repository The repository name (including the organization) # @param [String] number The branch name + # @param [Hash] options A customizable set of options. + # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # def self.remove_branch_protection(repository:, branch:, options:) client.unprotect_branch(repository, branch_name, options) @@ -205,6 +208,8 @@ def self.remove_branch_protection(repository:, branch:, options:) # # @param [String] repository The repository name (including the organization) # @param [String] number The branch name + # @param options [Hash] A customizable set of options. + # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # def self.set_branch_protection(repository:, branch:, options:) client.protect_branch(repository, branch_name, options) @@ -215,7 +220,7 @@ def self.set_branch_protection(repository:, branch:, options:) # @return [FastlaneCore::ConfigItem] The Fastlane ConfigItem for GitHub OAuth access token # def self.github_token_config_item - return FastlaneCore::ConfigItem.new( + FastlaneCore::ConfigItem.new( key: :github_token, env_name: 'GITHUB_TOKEN', description: 'The GitHub OAuth access token', diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index b2ba187d4..542a57407 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -15,14 +15,9 @@ allow(Octokit::Client).to receive(:new).and_return(client) end - it 'with the correct github_token' do - expect(described_class).to receive(:new).with(github_token: 'GITHUB_TOKEN') - described_class.new(github_token: 'GITHUB_TOKEN') - end - - it 'Octokit client receives the correct github_token' do - expect(Octokit::Client).to receive(:new).with(access_token: 'GITHUB_TOKEN') - described_class.new(github_token: 'GITHUB_TOKEN') + it 'properly passes the token all the way down to the Octokit::Client' do + expect(Octokit::Client).to receive(:new).with(access_token: 'Fake-GitHubToken-123') + described_class.new(github_token: 'Fake-GitHubToken-123') end end @@ -141,7 +136,7 @@ def mock_comment(body: ' Test', user_id: 1234) describe 'get_milestone' do let(:test_repo) { 'repo-test/project-test' } - let(:release_name) { '10.0' } + let(:test_milestone) { [{title: 'release/10.0'}, {title: 'release/10.1'}, {title: 'hotfix/10.2'}] } let(:client) do instance_double( Octokit::Client, @@ -153,19 +148,30 @@ def mock_comment(body: ' Test', user_id: 1234) allow(described_class).to receive(:client).and_return(client) end - it 'receives the correct repository' do + it 'properly passes the repository all the way down to the Octokit::Client' do expect(client).to receive(:list_milestones).with(test_repo) - described_class.get_milestone(test_repo, release_name) + described_class.get_milestone(test_repo, 'test') + end + + it 'returns nil when no milestone is returned from the api' do + expect(described_class.get_milestone(test_repo, 'release')).to be_nil + end + + it 'returns nil when no milestone title starts with the searched term' do + allow(client).to receive(:list_milestones).and_return(test_milestone) + expect(described_class.get_milestone(test_repo, '10.0')).to be_nil end - it 'returns nil when no milestone exists' do - expect(described_class.get_milestone(test_repo, release_name)).to be_nil + it 'returns a milestone when the milestone title starts with search term' do + allow(client).to receive(:list_milestones).and_return(test_milestone) + expect(described_class.get_milestone(test_repo, 'hotfix')).to eq({title: 'hotfix/10.2'}) end - it 'returns milestone from repo' do - allow(client).to receive(:list_milestones).and_return([{ title: 'release/10.0' }]) - expect(described_class.get_milestone(test_repo, release_name)).to be_nil + it 'returns the newest of milestones where the title matches with search term' do + allow(client).to receive(:list_milestones).and_return(test_milestone) + expect(described_class.get_milestone(test_repo, 'release')).to eq({title: 'release/10.1'}) end + end describe 'create_milestone' do @@ -258,15 +264,19 @@ def mockrelease(assets = []) it 'has the correct key' do expect(described_class.github_token_config_item.key).to eq(:github_token) end + it 'has the correct env_name' do expect(described_class.github_token_config_item.env_name).to eq('GITHUB_TOKEN') end + it 'has the correct description' do expect(described_class.github_token_config_item.description).to eq('The GitHub OAuth access token') end + it 'is not optional' do - expect(described_class.github_token_config_item.optional).to eq(false) + expect(described_class.github_token_config_item.optional).to be(false) end + it 'has String as data_type' do expect(described_class.github_token_config_item.data_type).to eq(String) end From 2b049f68c9621a7cb5c461c8780a6261b2aaefea Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 25 Oct 2022 23:30:54 +0100 Subject: [PATCH 18/27] Changelog: Add Breaking Changes Entry --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b74c6826..165b89fd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ ### Breaking Changes -_None_ +- Deprecate the use of `GHHELPER_ACCESS` in favor of `GITHUB_TOKEN` as the default environment variable to set the GitHub API token. [#420] +- The `github_client:` parameter (aka `ConfigItem`) is now mandatory for all Fastlane actions that use the GitHub API. [#420] +- The Fastlane action `CommentOnPrAction` has the parameter `access_key:` replaced by `github_token:`. [#420] ### New Features From 738c6b012a0a7f61b311305449a52e462f0e4245 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 25 Oct 2022 23:34:02 +0100 Subject: [PATCH 19/27] Fix Formmating and LineSpacing --- spec/github_helper_spec.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index 542a57407..b121b53b4 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -136,7 +136,7 @@ def mock_comment(body: ' Test', user_id: 1234) describe 'get_milestone' do let(:test_repo) { 'repo-test/project-test' } - let(:test_milestone) { [{title: 'release/10.0'}, {title: 'release/10.1'}, {title: 'hotfix/10.2'}] } + let(:test_milestone) { [{ title: 'release/10.0' }, { title: 'release/10.1' }, { title: 'hotfix/10.2' }] } let(:client) do instance_double( Octokit::Client, @@ -164,14 +164,13 @@ def mock_comment(body: ' Test', user_id: 1234) it 'returns a milestone when the milestone title starts with search term' do allow(client).to receive(:list_milestones).and_return(test_milestone) - expect(described_class.get_milestone(test_repo, 'hotfix')).to eq({title: 'hotfix/10.2'}) + expect(described_class.get_milestone(test_repo, 'hotfix')).to eq({ title: 'hotfix/10.2' }) end it 'returns the newest of milestones where the title matches with search term' do allow(client).to receive(:list_milestones).and_return(test_milestone) - expect(described_class.get_milestone(test_repo, 'release')).to eq({title: 'release/10.1'}) + expect(described_class.get_milestone(test_repo, 'release')).to eq({ title: 'release/10.1' }) end - end describe 'create_milestone' do From 04a1e430bee6cc135c8d2cd9ed9701f1756f69c8 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 27 Oct 2022 12:34:22 +0100 Subject: [PATCH 20/27] GithubHelperSpec: Fix Issues Pointed on PR Review --- CHANGELOG.md | 2 +- spec/github_helper_spec.rb | 37 ++++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 165b89fd4..278d5330f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - Deprecate the use of `GHHELPER_ACCESS` in favor of `GITHUB_TOKEN` as the default environment variable to set the GitHub API token. [#420] - The `github_client:` parameter (aka `ConfigItem`) is now mandatory for all Fastlane actions that use the GitHub API. [#420] -- The Fastlane action `CommentOnPrAction` has the parameter `access_key:` replaced by `github_token:`. [#420] +- The Fastlane action `comment_on_pr` has the parameter `access_key:` replaced by `github_token:`. [#420] ### New Features diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index b121b53b4..b6ef70bf9 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -39,7 +39,8 @@ it 'fails if it does not find the right release on GitHub' do stub = stub_request(:get, content_url).to_return(status: [404, 'Not Found']) - expect(described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: './')).to be_nil + donwloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: './') + expect(donwloaded_file).to be_nil expect(stub).to have_been_made.once end @@ -47,7 +48,8 @@ stub = stub_request(:get, content_url).to_return(status: 200, body: 'my-test-content') Dir.mktmpdir('a8c-download-repo-file-') do |tmpdir| dst_file = File.join(tmpdir, 'test-file.xml') - expect(described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: tmpdir)).to eq(dst_file) + donwloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: tmpdir) + expect(donwloaded_file).to eq(dst_file) expect(stub).to have_been_made.once expect(File.read(dst_file)).to eq('my-test-content') end @@ -70,7 +72,8 @@ it 'returns correct milestone' do expect(client).to receive(:list_milestones) - expect(described_class.get_last_milestone(repository: test_repo)).to eq(last_stone) + last_milestone = described_class.get_last_milestone(repository: test_repo) + expect(last_milestone).to eq(last_stone) end def mock_milestone(title) @@ -136,7 +139,7 @@ def mock_comment(body: ' Test', user_id: 1234) describe 'get_milestone' do let(:test_repo) { 'repo-test/project-test' } - let(:test_milestone) { [{ title: 'release/10.0' }, { title: 'release/10.1' }, { title: 'hotfix/10.2' }] } + let(:test_milestones) { [{ title: '9.8' }, { title: '10.1' }, { title: '10.1.3 ❄️' }] } let(:client) do instance_double( Octokit::Client, @@ -154,22 +157,26 @@ def mock_comment(body: ' Test', user_id: 1234) end it 'returns nil when no milestone is returned from the api' do - expect(described_class.get_milestone(test_repo, 'release')).to be_nil + milestone = described_class.get_milestone(test_repo, '10') + expect(milestone).to be_nil end it 'returns nil when no milestone title starts with the searched term' do - allow(client).to receive(:list_milestones).and_return(test_milestone) - expect(described_class.get_milestone(test_repo, '10.0')).to be_nil + allow(client).to receive(:list_milestones).and_return(test_milestones) + milestone = described_class.get_milestone(test_repo, '8.5') + expect(milestone).to be_nil end it 'returns a milestone when the milestone title starts with search term' do - allow(client).to receive(:list_milestones).and_return(test_milestone) - expect(described_class.get_milestone(test_repo, 'hotfix')).to eq({ title: 'hotfix/10.2' }) + allow(client).to receive(:list_milestones).and_return(test_milestones) + milestone = described_class.get_milestone(test_repo, '9') + expect(milestone).to eq({ title: '9.8' }) end - it 'returns the newest of milestones where the title matches with search term' do - allow(client).to receive(:list_milestones).and_return(test_milestone) - expect(described_class.get_milestone(test_repo, 'release')).to eq({ title: 'release/10.1' }) + it 'returns the milestone with the latest due date matching the search term when there are more than one' do + allow(client).to receive(:list_milestones).and_return(test_milestones) + milestone = described_class.get_milestone(test_repo, '10.1') + expect(milestone).to eq({ title: '10.1.3 ❄️' }) end end @@ -235,7 +242,7 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) it 'has the correct options' do options = { body: test_description, draft: true, name: test_tag, prerelease: false, target_commitish: test_target } expect(client).to receive(:create_release).with(test_repo, test_tag, options) - mockrelease + create_release end it 'upload the assets to the correct location' do @@ -244,10 +251,10 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) allow(client).to receive(:create_release).and_return({ url: test_url }) expect(client).to receive(:upload_asset).with(test_url, test_assets, { content_type: 'application/octet-stream' }) - mockrelease([test_assets]) + create_release(assets: [test_assets]) end - def mockrelease(assets = []) + def create_release(assets: []) described_class.create_release( repository: test_repo, version: test_tag, From 4527067b2df3c50beec39d4344031a4b1aa576e8 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 27 Oct 2022 12:36:01 +0100 Subject: [PATCH 21/27] GithubHelper: Update Documentation of branch_protection methods --- lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 8ea5f398b..bbc28f4f2 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -193,7 +193,7 @@ def self.update_milestone(repository:, number:, options:) client.update_milestone(repository, number, options) end - # Unlock a single branch from a repository + # Remove the protection of a single branch from a repository # # @param [String] repository The repository name (including the organization) # @param [String] number The branch name @@ -204,7 +204,7 @@ def self.remove_branch_protection(repository:, branch:, options:) client.unprotect_branch(repository, branch_name, options) end - # Lock a single branch from a repository + # Protects a single branch from a repository # # @param [String] repository The repository name (including the organization) # @param [String] number The branch name From eaf6ac7cb59344b430a3d1d27b76d8a4e0931a3c Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 27 Oct 2022 18:15:34 +0100 Subject: [PATCH 22/27] Fix Typos and Rubocop Issues --- .../actions/android/android_download_file_by_version.rb | 2 +- .../plugin/wpmreleasetoolkit/helper/github_helper.rb | 4 ++-- spec/github_helper_spec.rb | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb index 7d4eb24f8..6dbbc8762 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb @@ -58,7 +58,7 @@ def self.available_options description: 'The prefix which is used in the GitHub release title', type: String, optional: true), - Fastlane::Helper::GithubHelper.github_token_config_item + Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index bbc28f4f2..aa9d2d4a0 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -196,7 +196,7 @@ def self.update_milestone(repository:, number:, options:) # Remove the protection of a single branch from a repository # # @param [String] repository The repository name (including the organization) - # @param [String] number The branch name + # @param [String] branch The branch name # @param [Hash] options A customizable set of options. # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # @@ -207,7 +207,7 @@ def self.remove_branch_protection(repository:, branch:, options:) # Protects a single branch from a repository # # @param [String] repository The repository name (including the organization) - # @param [String] number The branch name + # @param [String] branch The branch name # @param options [Hash] A customizable set of options. # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index b6ef70bf9..af254415d 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -39,8 +39,8 @@ it 'fails if it does not find the right release on GitHub' do stub = stub_request(:get, content_url).to_return(status: [404, 'Not Found']) - donwloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: './') - expect(donwloaded_file).to be_nil + downloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: './') + expect(downloaded_file).to be_nil expect(stub).to have_been_made.once end @@ -48,8 +48,8 @@ stub = stub_request(:get, content_url).to_return(status: 200, body: 'my-test-content') Dir.mktmpdir('a8c-download-repo-file-') do |tmpdir| dst_file = File.join(tmpdir, 'test-file.xml') - donwloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: tmpdir) - expect(donwloaded_file).to eq(dst_file) + downloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: tmpdir) + expect(downloaded_file).to eq(dst_file) expect(stub).to have_been_made.once expect(File.read(dst_file)).to eq('my-test-content') end From 8ed0e7eb5f70d87c5ceafdb8f6fd83c45a7b4280 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 28 Oct 2022 17:06:55 +0100 Subject: [PATCH 23/27] 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. --- .../wpmreleasetoolkit/helper/github_helper.rb | 20 ++--- spec/github_helper_spec.rb | 75 +++++++++++++------ 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index aa9d2d4a0..27b5f92e5 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -25,7 +25,7 @@ def initialize(github_token:) @client.auto_paginate = true end - def self.get_milestone(repository, release) + def get_milestone(repository, release) miles = client.list_milestones(repository) mile = nil @@ -42,11 +42,11 @@ def self.get_milestone(repository, release) # @param [String] milestone The name of the milestone we want to fetch the list of PRs for (e.g.: `16.9`) # @return [] A list of the PRs for the given milestone, sorted by number # - def self.get_prs_for_milestone(repository, milestone) + def get_prs_for_milestone(repository, milestone) client.search_issues(%(type:pr milestone:"#{milestone}" repo:#{repository}))[:items].sort_by(&:number) end - def self.get_last_milestone(repository) + def get_last_milestone(repository) options = {} options[:state] = 'open' @@ -71,7 +71,7 @@ def self.get_last_milestone(repository) last_stone end - def self.create_milestone(repository, newmilestone_number, newmilestone_duedate, newmilestone_duration, number_of_days_from_code_freeze_to_release, need_submission) + def create_milestone(repository, newmilestone_number, newmilestone_duedate, newmilestone_duration, number_of_days_from_code_freeze_to_release, need_submission) # If there is a review process, we want to submit the binary 3 days before its release # # 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, # @param [Array] assets List of file paths to attach as assets to the release # @param [TrueClass|FalseClass] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta) # - def self.create_release(repository:, version:, target: nil, description:, assets:, prerelease:) + def create_release(repository:, version:, target: nil, description:, assets:, prerelease:) release = client.create_release( repository, version, # tag name @@ -135,7 +135,7 @@ def self.create_release(repository:, version:, target: nil, description:, assets # @param [String] download_folder The folder which the file should be downloaded into # @return [String] The path of the downloaded file, or nil if something went wrong # - def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) + def download_file_from_tag(repository:, tag:, file_path:, download_folder:) repository = repository.delete_prefix('/').chomp('/') file_path = file_path.delete_prefix('/').chomp('/') file_name = File.basename(file_path) @@ -156,7 +156,7 @@ def self.download_file_from_tag(repository:, tag:, file_path:, download_folder:) end # Creates (or updates an existing) GitHub PR Comment - def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid) + def comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: SecureRandom.uuid) comments = client.issue_comments(project_slug, pr_number) reuse_marker = "" @@ -189,7 +189,7 @@ def self.comment_on_pr(project_slug:, pr_number:, body:, reuse_identifier: Secur # @return [Milestone] A single milestone object # @see http://developer.github.com/v3/issues/milestones/#update-a-milestone # - def self.update_milestone(repository:, number:, options:) + def update_milestone(repository:, number:, options:) client.update_milestone(repository, number, options) end @@ -200,7 +200,7 @@ def self.update_milestone(repository:, number:, options:) # @param [Hash] options A customizable set of options. # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # - def self.remove_branch_protection(repository:, branch:, options:) + def remove_branch_protection(repository:, branch:, options:) client.unprotect_branch(repository, branch_name, options) end @@ -211,7 +211,7 @@ def self.remove_branch_protection(repository:, branch:, options:) # @param options [Hash] A customizable set of options. # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # - def self.set_branch_protection(repository:, branch:, options:) + def set_branch_protection(repository:, branch:, options:) client.protect_branch(repository, branch_name, options) end diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index af254415d..cd2b82ba3 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -29,17 +29,19 @@ let(:client) do instance_double( Octokit::Client, - contents: double(download_url: content_url) # rubocop:disable RSpec/VerifiedDoubles + contents: double(download_url: content_url), # rubocop:disable RSpec/VerifiedDoubles + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil ) end before do - allow(described_class).to receive(:client).and_return(client) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'fails if it does not find the right release on GitHub' do stub = stub_request(:get, content_url).to_return(status: [404, 'Not Found']) - downloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: './') + downloaded_file = download_file_from_tag(download_folder: './') expect(downloaded_file).to be_nil expect(stub).to have_been_made.once end @@ -48,12 +50,17 @@ stub = stub_request(:get, content_url).to_return(status: 200, body: 'my-test-content') Dir.mktmpdir('a8c-download-repo-file-') do |tmpdir| dst_file = File.join(tmpdir, 'test-file.xml') - downloaded_file = described_class.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: tmpdir) + downloaded_file = download_file_from_tag(download_folder: tmpdir) expect(downloaded_file).to eq(dst_file) expect(stub).to have_been_made.once expect(File.read(dst_file)).to eq('my-test-content') end end + + def download_file_from_tag(download_folder:) + helper = described_class.new(github_token: 'Fake-GitHubToken-123') + helper.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: download_folder) + end end describe 'get_last_milestone' do @@ -62,23 +69,30 @@ let(:client) do instance_double( Octokit::Client, - list_milestones: ['9.8 ❄️', '9.9'].map { |title| mock_milestone(title) }.append(last_stone) + list_milestones: ['9.8 ❄️', '9.9'].map { |title| mock_milestone(title) }.append(last_stone), + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil ) end before do - allow(described_class).to receive(:client).and_return(client) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'returns correct milestone' do expect(client).to receive(:list_milestones) - last_milestone = described_class.get_last_milestone(repository: test_repo) + last_milestone = get_last_milestone(repository: test_repo) expect(last_milestone).to eq(last_stone) end def mock_milestone(title) { title: title } end + + def get_last_milestone(repository:) + helper = described_class.new(github_token: 'Fake-GitHubToken-123') + helper.get_last_milestone(repository: repository) + end end describe 'comment_on_pr' do @@ -88,12 +102,13 @@ def mock_milestone(title) issue_comments: [], add_comment: nil, update_comment: nil, - user: instance_double('User', id: 1234) + user: instance_double('User', id: 1234, name: 'test'), + 'auto_paginate=': nil ) end before do - allow(described_class).to receive(:client).and_return(client) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'will create a new comment if an existing one is not found' do @@ -124,7 +139,8 @@ def mock_milestone(title) end def comment_on_pr - described_class.comment_on_pr( + helper = described_class.new(github_token: 'Fake-GitHubToken-123') + helper.comment_on_pr( project_slug: 'test/test', pr_number: 1234, body: 'Test', @@ -143,41 +159,48 @@ def mock_comment(body: ' Test', user_id: 1234) let(:client) do instance_double( Octokit::Client, - list_milestones: [] + list_milestones: [], + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil ) end before do - allow(described_class).to receive(:client).and_return(client) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'properly passes the repository all the way down to the Octokit::Client' do expect(client).to receive(:list_milestones).with(test_repo) - described_class.get_milestone(test_repo, 'test') + get_milestone(milestone_name: 'test') end it 'returns nil when no milestone is returned from the api' do - milestone = described_class.get_milestone(test_repo, '10') + milestone = get_milestone(milestone_name: '10') expect(milestone).to be_nil end it 'returns nil when no milestone title starts with the searched term' do allow(client).to receive(:list_milestones).and_return(test_milestones) - milestone = described_class.get_milestone(test_repo, '8.5') + milestone = get_milestone(milestone_name: '8.5') expect(milestone).to be_nil end it 'returns a milestone when the milestone title starts with search term' do allow(client).to receive(:list_milestones).and_return(test_milestones) - milestone = described_class.get_milestone(test_repo, '9') + milestone = get_milestone(milestone_name: '9') expect(milestone).to eq({ title: '9.8' }) end it 'returns the milestone with the latest due date matching the search term when there are more than one' do allow(client).to receive(:list_milestones).and_return(test_milestones) - milestone = described_class.get_milestone(test_repo, '10.1') + milestone = get_milestone(milestone_name: '10.1') expect(milestone).to eq({ title: '10.1.3 ❄️' }) end + + def get_milestone(milestone_name:) + helper = described_class.new(github_token: 'Fake-GitHubToken-123') + helper.get_milestone(test_repo, milestone_name) + end end describe 'create_milestone' do @@ -187,12 +210,14 @@ def mock_comment(body: ' Test', user_id: 1234) let(:client) do instance_double( Octokit::Client, - create_milestone: nil + create_milestone: nil, + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil ) end before do - allow(described_class).to receive(:client).and_return(client) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'has the correct dates to code freeze without submission' do @@ -212,7 +237,8 @@ def mock_comment(body: ' Test', user_id: 1234) end def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) - described_class.create_milestone( + helper = described_class.new(github_token: 'Fake-GitHubToken-123') + helper.create_milestone( test_repo, test_milestone_number, test_milestone_duedate.to_time.utc, @@ -231,12 +257,14 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) let(:client) do instance_double( Octokit::Client, - create_release: nil + create_release: nil, + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil ) end before do - allow(described_class).to receive(:client).and_return(client) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'has the correct options' do @@ -255,7 +283,8 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) end def create_release(assets: []) - described_class.create_release( + helper = described_class.new(github_token: 'Fake-GitHubToken-123') + helper.create_release( repository: test_repo, version: test_tag, target: test_target, From 8882fbd8e470123d13aebd264fe399691260a12c Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 28 Oct 2022 17:30:49 +0100 Subject: [PATCH 24/27] GithubHelperSpec: Change Order of Methods to Improve Diff --- spec/github_helper_spec.rb | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index cd2b82ba3..4d723a5b1 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -2,25 +2,6 @@ require 'webmock/rspec' describe Fastlane::Helper::GithubHelper do - describe '#initialize' do - let(:client) do - instance_double( - Octokit::Client, - user: instance_double('User', name: 'test'), - 'auto_paginate=': nil - ) - end - - before do - allow(Octokit::Client).to receive(:new).and_return(client) - end - - it 'properly passes the token all the way down to the Octokit::Client' do - expect(Octokit::Client).to receive(:new).with(access_token: 'Fake-GitHubToken-123') - described_class.new(github_token: 'Fake-GitHubToken-123') - end - end - describe 'download_file_from_tag' do let(:test_repo) { 'repo-test/project-test' } let(:test_tag) { '1.0' } @@ -153,6 +134,25 @@ def mock_comment(body: ' Test', user_id: 1234) end end + describe '#initialize' do + let(:client) do + instance_double( + Octokit::Client, + user: instance_double('User', name: 'test'), + 'auto_paginate=': nil + ) + end + + before do + allow(Octokit::Client).to receive(:new).and_return(client) + end + + it 'properly passes the token all the way down to the Octokit::Client' do + expect(Octokit::Client).to receive(:new).with(access_token: 'Fake-GitHubToken-123') + described_class.new(github_token: 'Fake-GitHubToken-123') + end + end + describe 'get_milestone' do let(:test_repo) { 'repo-test/project-test' } let(:test_milestones) { [{ title: '9.8' }, { title: '10.1' }, { title: '10.1.3 ❄️' }] } From f41bcc740d528720d02b4b36bfe2d75ed963380d Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sun, 30 Oct 2022 20:16:11 +0000 Subject: [PATCH 25/27] GithubHelper: Fix Named Parameters for warpper methods The invocation of the actions protect/unprotect branch was with the wrong parameters at the call of the method on the cliend --- lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index 27b5f92e5..e8d35e754 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -201,7 +201,7 @@ def update_milestone(repository:, number:, options:) # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # def remove_branch_protection(repository:, branch:, options:) - client.unprotect_branch(repository, branch_name, options) + client.unprotect_branch(repository, branch, options) end # Protects a single branch from a repository @@ -212,7 +212,7 @@ def remove_branch_protection(repository:, branch:, options:) # @see https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection # def set_branch_protection(repository:, branch:, options:) - client.protect_branch(repository, branch_name, options) + client.protect_branch(repository, branch, options) end # Creates a GithubToken Fastlane ConfigItem From 27c20dc733f22a4f9859b57d7525ecc71b7b986d Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Mon, 31 Oct 2022 20:44:21 +0000 Subject: [PATCH 26/27] Fix Issues Pointed on PR Review --- CHANGELOG.md | 2 +- spec/github_helper_spec.rb | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 278d5330f..58caf5205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ ### Breaking Changes -- Deprecate the use of `GHHELPER_ACCESS` in favor of `GITHUB_TOKEN` as the default environment variable to set the GitHub API token. [#420] +- Removed support for the deprecated `GHHELPER_ACCESS` in favor of `GITHUB_TOKEN` as the default environment variable to set the GitHub API token. [#420] - The `github_client:` parameter (aka `ConfigItem`) is now mandatory for all Fastlane actions that use the GitHub API. [#420] - The Fastlane action `comment_on_pr` has the parameter `access_key:` replaced by `github_token:`. [#420] diff --git a/spec/github_helper_spec.rb b/spec/github_helper_spec.rb index 4d723a5b1..d6051da3c 100644 --- a/spec/github_helper_spec.rb +++ b/spec/github_helper_spec.rb @@ -143,11 +143,8 @@ def mock_comment(body: ' Test', user_id: 1234) ) end - before do - allow(Octokit::Client).to receive(:new).and_return(client) - end - it 'properly passes the token all the way down to the Octokit::Client' do + allow(Octokit::Client).to receive(:new).and_return(client) expect(Octokit::Client).to receive(:new).with(access_token: 'Fake-GitHubToken-123') described_class.new(github_token: 'Fake-GitHubToken-123') end @@ -273,7 +270,7 @@ def create_milestone(need_submission:, milestone_duration:, days_code_freeze:) create_release end - it 'upload the assets to the correct location' do + it 'uploads the assets to the correct location' do test_assets = 'test-file.xml' test_url = '/test/url' From 6adfe25a3be7aae0eaf616a7b71fbc88c4af5b37 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Wed, 2 Nov 2022 18:39:08 +0000 Subject: [PATCH 27/27] Changelog: Fix Typos and make new changes more descriptive --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58caf5205..831e8d9d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ ### Breaking Changes - Removed support for the deprecated `GHHELPER_ACCESS` in favor of `GITHUB_TOKEN` as the default environment variable to set the GitHub API token. [#420] -- The `github_client:` parameter (aka `ConfigItem`) is now mandatory for all Fastlane actions that use the GitHub API. [#420] +- The `github_token:` parameter (aka `ConfigItem`)–or using the corresponding `GITHUB_TOKEN` env var to provide it a value–is now mandatory for all Fastlane actions that use the GitHub API. [#420] - The Fastlane action `comment_on_pr` has the parameter `access_key:` replaced by `github_token:`. [#420] ### New Features