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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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),
raafaelima marked this conversation as resolved.
Show resolved Hide resolved
raafaelima marked this conversation as resolved.
Show resolved Hide resolved
]
end

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

Fastlane::Helper::GithubHelper.github_client().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')
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
end

def self.description
Expand Down Expand Up @@ -45,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
raafaelima marked this conversation as resolved.
Show resolved Hide resolved
project_slug: params[:project],
pr_number: params[:pr_number],
body: params[:body],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ 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]
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 +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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ 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
assets.each do |file_path|
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],
Expand Down Expand Up @@ -82,6 +85,12 @@ def self.available_options
optional: true,
default_value: false,
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

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

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

Fastlane::Helper::GithubHelper.github_client().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)
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
end

def self.description
Expand Down Expand Up @@ -46,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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 }
Fastlane::Helper::GithubHelper.github_client().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)
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
end

def self.description
Expand Down Expand Up @@ -45,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -27,7 +30,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)

github_helper.update_milestone(repository, milestone[:number], title: mile_title)
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
end

def self.is_frozen(milestone)
Expand Down Expand Up @@ -70,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

Expand Down
59 changes: 23 additions & 36 deletions lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +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
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!)

# Fetch the current user
user = client.user
UI.message("Logged in as: #{user.name}")
# Helper for GitHub Actions
#
# @param [String?] githubtoken GitHub OAuth access token
#
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().list_milestones(repository)
miles = @client.list_milestones(repository)
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
mile = nil

miles&.each do |mm|
Expand All @@ -52,14 +41,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 @@ -92,7 +81,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)
@client.create_milestone(repository, newmilestone_number, options)
end

# Creates a Release on GitHub as a Draft
Expand All @@ -107,7 +96,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 @@ -117,7 +106,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 @@ -135,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.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 @@ -152,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
comments = client.issue_comments(project_slug, pr_number)
comments = @client.issue_comments(project_slug, pr_number)

reuse_marker = "<!-- REUSE_ID: #{reuse_identifier} -->"

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
Expand Down
Loading