Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make explicit the use of the parameter GITHUB_TOKEN when use create_release #420

Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e41a8c4
GitHubHelper: Access Key is a now a mandatory parameter to use gitbuh…
raafaelima Oct 19, 2022
b78a5c0
GitHubHelper: Add gihubToken as optional argument on create_release
raafaelima Oct 19, 2022
4a8d792
CreateReleaseAction: Add gihubToken as optional argument
raafaelima Oct 19, 2022
8946ad7
GitHubHelper: GithubToken is now mandatory on the class initialization
raafaelima Oct 20, 2022
6d1e690
Actions: All the instances of the GithubHelper require a GithubToken
raafaelima Oct 20, 2022
81d954a
GithubHelper: Correct the name of parameter at the initialization
raafaelima Oct 21, 2022
15099ca
Actions: The parameter name im the ConfigItens changed from github_to…
raafaelima Oct 21, 2022
e110a41
GithubHelper: Change the OktokitClient to be accessed by attr_reader …
raafaelima Oct 22, 2022
fcacaa8
GithubHelper: Add unit tests for not covered functions
raafaelima Oct 22, 2022
302e372
GithubHelper: Create Function to encapsulate the GithubToken Fastlane…
raafaelima Oct 23, 2022
461bcd8
Actions: Set the GithubToken param as github_client instead of access…
raafaelima Oct 23, 2022
75c420e
Actions: Correct the line order and spacing
raafaelima Oct 23, 2022
76f5dcc
Actions: All Acess to OktoClient is done by GithubHelper
raafaelima Oct 24, 2022
e95557b
Actions: Add Unit Tests to Specs without them
raafaelima Oct 25, 2022
2576c51
Actions: Delete Unit Tests to Move for Another PR
raafaelima Oct 25, 2022
c894e5e
Merge branch 'trunk' into fix/github-token-required-at-github-helper
raafaelima Oct 25, 2022
fb94b47
GithubActions: Fix Failing Unit Tests
raafaelima Oct 25, 2022
126a46c
GithubActions: Fix Issues Pointed on PR Review
raafaelima Oct 25, 2022
2b049f6
Changelog: Add Breaking Changes Entry
raafaelima Oct 25, 2022
738c6b0
Fix Formmating and LineSpacing
raafaelima Oct 25, 2022
04a1e43
GithubHelperSpec: Fix Issues Pointed on PR Review
raafaelima Oct 27, 2022
4527067
GithubHelper: Update Documentation of branch_protection methods
raafaelima Oct 27, 2022
9031f81
Merge branch 'wordpress-mobile:trunk' into fix/github-token-required-…
raafaelima Oct 27, 2022
eaf6ac7
Fix Typos and Rubocop Issues
raafaelima Oct 27, 2022
8ed0e7e
GithubHelper: Change all methods to be instance methods
raafaelima Oct 28, 2022
8882fbd
GithubHelperSpec: Change Order of Methods to Improve Diff
raafaelima Oct 28, 2022
f41bcc7
GithubHelper: Fix Named Parameters for warpper methods
raafaelima Oct 30, 2022
27c20dc
Fix Issues Pointed on PR Review
raafaelima Oct 31, 2022
6adfe25
Changelog: Fix Typos and make new changes more descriptive
raafaelima Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
- The `github_client:` parameter (aka `ConfigItem`) is now mandatory for all Fastlane actions that use the GitHub API. [#420]
Copy link
Contributor

@AliSoftware AliSoftware Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Typo fix
  • Adjusting wording, as in practice, many clients won't provide github_token: … as part of the parameters used in their call sites and will instead just rely on GITHUB_TOKEN env var being set (e.g. this call site won't require any change for it to continue working)
Suggested change
- 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]

PS: Btw, one good news to note is that, despite those items indeed being "breaking changes" (for any call site of any of those actions that relied on GHHELPER_ACCESS env var instead of GITHUB_TOKEN, and/or for call sites of comment_on_pr that used access_token: parameter explicitly), in practice I doubt that many client app relied on any of those, because most of our client apps and CI rely on the GITHUB_TOKEN env var already (as opposed to access_token: param or deprecated GHHELPER_ACCESS env var) to provide the token, which is something that will continue working as before.

So while this is correct for those changes to be under "Breaking Changes", the good news is in practice most clients will have nothing to do to adopt it 🙂 🎉

- The Fastlane action `comment_on_pr` has the parameter `access_key:` replaced by `github_token:`. [#420]

### New Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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]}"

Fastlane::Helper::GithubHelper.download_file_from_tag(
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}",
file_path: params[:file_path],
Expand Down Expand Up @@ -57,6 +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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just triggered CI on your PR (FYI, because you're working on a fork, CI isn't run automatically—for security reasons— like it is for usual PRs), and got a rubocop warning on this line (see here):

lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_download_file_by_version.rb:61:11: C: [Correctable] Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array.
          Fastlane::Helper::GithubHelper.github_token_config_item
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have access to see that buildkite link 😢
I fixed the warning, hope everything pass now 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've retriggered the CI on your latest commit, let's see if CI checks go green on your PR after that 🙂

]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ def self.run(params)
repository = params[:repository]
milestone_title = params[:milestone]

milestone = Fastlane::Helper::GithubHelper.get_milestone(repository, milestone_title)
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?

Fastlane::Helper::GithubHelper.github_client().update_milestone(repository, milestone[:number], state: 'closed')
github_helper.update_milestone(repository: repository, number: milestone[:number], options: { state: 'closed' })
end

def self.description
Expand Down Expand Up @@ -45,6 +47,7 @@ def self.available_options
description: 'The GitHub milestone',
optional: false,
type: String),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class CommentOnPrAction < Action
def self.run(params)
require_relative '../../helper/github_helper'

reuse_identifier = Fastlane::Helper::GithubHelper.comment_on_pr(
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],
body: params[:body],
Expand Down Expand Up @@ -41,12 +42,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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ class CreateNewMilestoneAction < Action
def self.run(params)
repository = params[:repository]

last_stone = Fastlane::Helper::GithubHelper.get_last_milestone(repository)
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]
milestone_duration = params[:milestone_duration]
newmilestone_duedate = (milestone_duedate.to_datetime.next_day(milestone_duration).to_time).utc
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
Expand Down Expand Up @@ -62,6 +63,7 @@ def self.available_options
optional: true,
is_string: false,
default_value: 14),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ 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: params[:github_token])
github_helper.create_release(
repository: repository,
version: version,
target: params[:target],
Expand Down Expand Up @@ -82,6 +83,7 @@ def self.available_options
optional: true,
default_value: false,
is_string: false),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def self.run(params)
milestone = params[:milestone]

# Get commit list
pr_list = Fastlane::Helper::GithubHelper.get_prs_for_milestone(repository, milestone)
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|
pr_list.each do |data|
Expand Down Expand Up @@ -53,6 +54,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),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +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 }

Fastlane::Helper::GithubHelper.github_client().unprotect_branch(repository, branch_name, branch_prot)
github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token])
github_helper.remove_branch_protection(repository: repository, branch: branch_name, options: branch_prot)
end

def self.description
Expand Down Expand Up @@ -46,6 +47,7 @@ def self.available_options
description: 'The branch to unprotect',
optional: false,
type: String),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +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 }
Fastlane::Helper::GithubHelper.github_client().protect_branch(repository, branch_name, branch_prot)

github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token])
github_helper.set_branch_protection(repository: repository, branch: branch_name, options: branch_prot)
end

def self.description
Expand Down Expand Up @@ -45,6 +47,7 @@ def self.available_options
description: 'The branch to protect',
optional: false,
type: String),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def self.run(params)
milestone_title = params[:milestone]
freeze = params[:freeze]

milestone = Fastlane::Helper::GithubHelper.get_milestone(repository, milestone_title)
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]
Expand All @@ -27,7 +29,7 @@ def self.run(params)
end

UI.message("New milestone: #{mile_title}")
Fastlane::Helper::GithubHelper.github_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)
Expand Down Expand Up @@ -70,6 +72,7 @@ def self.available_options
optional: false,
default_value: true,
is_string: false),
Fastlane::Helper::GithubHelper.github_token_config_item,
]
end

Expand Down
103 changes: 72 additions & 31 deletions lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,25 @@ 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
Comment on lines -11 to -20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! Bye bye GithubHelper#github_token! method and the need for the helper to know or look at the env vars 🎉


def self.github_client
@@client ||= begin
client = Octokit::Client.new(access_token: github_token!)
attr_reader :client

# Fetch the current user
user = client.user
UI.message("Logged in as: #{user.name}")
# Helper for GitHub Actions
#
# @param [String?] github_token GitHub OAuth access token
#
def initialize(github_token:)
@client = Octokit::Client.new(access_token: github_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().list_milestones(repository)
miles = client.list_milestones(repository)
mile = nil

miles&.each do |mm|
Expand All @@ -52,14 +43,14 @@ def self.get_milestone(repository, release)
# @return [<Sawyer::Resource>] 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)
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().list_milestones(repository, options)
milestones = client.list_milestones(repository, options)
return nil if milestones.nil?

last_stone = nil
Expand Down Expand Up @@ -107,7 +98,7 @@ def self.create_milestone(repository, newmilestone_number, newmilestone_duedate,
# To solve this, we trick it by forcing the time component of the ISO date we send to be `12:00:00Z`.
options[:due_on] = newmilestone_duedate.strftime('%Y-%m-%dT12:00:00Z')
options[:description] = comment
github_client().create_milestone(repository, newmilestone_number, options)
client.create_milestone(repository, newmilestone_number, options)
end

# Creates a Release on GitHub as a Draft
Expand All @@ -122,7 +113,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 = github_client().create_release(
release = client.create_release(
repository,
version, # tag name
name: version, # release name
Expand All @@ -132,7 +123,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')
client.upload_asset(release[:url], file_path, content_type: 'application/octet-stream')
end
end

Expand All @@ -150,9 +141,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.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|
Expand All @@ -167,7 +157,6 @@ 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
comments = client.issue_comments(project_slug, pr_number)

reuse_marker = "<!-- REUSE_ID: #{reuse_identifier} -->"
Expand All @@ -187,6 +176,58 @@ 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
# @see http://developer.github.com/v3/issues/milestones/#update-a-milestone
#
def self.update_milestone(repository:, number:, options:)
client.update_milestone(repository, number, options)
end

# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# @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
#
def self.remove_branch_protection(repository:, branch:, options:)
client.unprotect_branch(repository, branch_name, options)
end

# Protects a single branch from a repository
#
# @param [String] repository The repository name (including the organization)
# @param [String] number The branch name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# @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
#
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
#
def self.github_token_config_item
FastlaneCore::ConfigItem.new(
key: :github_token,
env_name: 'GITHUB_TOKEN',
description: 'The GitHub OAuth access token',
optional: false,
type: String
)
end
end
end
end
Loading