Skip to content

Commit

Permalink
Merge branch 'master' into latest-codegen-master
Browse files Browse the repository at this point in the history
  • Loading branch information
helenye-stripe authored Jan 27, 2025
2 parents 106a2ae + 5304640 commit b56aa56
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 23 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ on:
jobs:
build:
name: Build
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

steps:
- uses: extractions/setup-just@v2
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
- name: Lint
run: bundle install && bundle exec rake rubocop
run: just lint
- name: Build
run: gem build stripe.gemspec
- name: 'Upload Artifact'
Expand All @@ -40,19 +41,21 @@ jobs:

test:
name: Test (${{ matrix.ruby-version }})
runs-on: ubuntu-latest
# this version of jruby isn't available in the new latest (24.04) so we have to pin (or update jruby)
runs-on: ubuntu-22.04
strategy:
matrix:
ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2, '3.3', jruby-9.4.0.0, truffleruby-head]
steps:
- uses: extractions/setup-just@v2
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- uses: stripe/openapi/actions/stripe-mock@master
- name: test
run: make ci-test
run: just test typecheck
env:
GITHUB_TOKEN: ${{ secrets.github_token }}

Expand All @@ -63,7 +66,7 @@ jobs:
startsWith(github.ref, 'refs/tags/v') &&
endsWith(github.actor, '-stripe')
needs: [build, test]
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Download all workflow run artifacts
uses: actions/download-artifact@v4
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog
## 13.3.1 - 2025-01-13
* [#1512](https://github.com/stripe/stripe-ruby/pull/1512) Import global configuration for options not available on StripeClient options
* Fixes bug where `StripeClient` was not falling back to global options for options that are not available to be set per-client
* [#1516](https://github.com/stripe/stripe-ruby/pull/1516) ThinEvent reason and livemode
- Add `livemode` and optional `reason` fields to ThinEvent
* [#1518](https://github.com/stripe/stripe-ruby/pull/1518) Pin ubuntu version in Test action
* [#1508](https://github.com/stripe/stripe-ruby/pull/1508) Added pull request template

## 13.3.0 - 2024-12-18
* [#1500](https://github.com/stripe/stripe-ruby/pull/1500) This release changes the pinned API version to `2024-12-18.acacia`.

Expand Down
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# Contributing

We welcome bug reports, feature requests, and code contributions in a pull request.

For most pull requests, we request that you identify or create an associated issue that has the necessary context. We use these issues to reach agreement on an approach and save the PR author from having to redo work. Fixing typos or documentation issues likely do not need an issue; for any issue that introduces substantial code changes, changes the public interface, or if you aren't sure, please find or [create an issue](https://www.github.com/stripe/stripe-ruby/issues/new/choose).

## Contributor License Agreement

All contributors must sign the Contributor License Agreement (CLA) before we can accept their contribution. If you have not yet signed the agreement, you will be given an option to do so when you open a pull request. You can then sign by clicking on the badge in the comment from @CLAassistant.

## Generated code

This project has a combination of manually maintained code and code generated from our private code generator. If your contribution involves changes to generated code, please call this out in the issue or pull request as we will likely need to make a change to our code generator before accepting the contribution.

To identify files with purely generated code, look for the comment `File generated from our OpenAPI spec.` at the start of the file. Generated blocks of code within hand-written files will be between comments that say `The beginning of the section generated from our OpenAPI spec` and `The end of the section generated from our OpenAPI spec`.

## Compatibility with supported language and runtime versions

This project supports [many different langauge and runtime versions](README.md#requirements) and we are unable to accept any contribution that does not work on _all_ supported versions. If, after discussing the approach in the associated issue, your change must use an API / feature that isn't available in all supported versions, please call this out explicitly in the issue or pull request so we can help figure out the best way forward.

## Set up your dev environment

Please refer to this project's [README.md](README.md#development) for instructions on how to set up your development environment.

2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NOTE: this file is deprecated and slated for deletion; prefer using the equivalent `just` commands.

.PHONY: update-version codegen-format test ci-test
update-version:
@echo "$(VERSION)" > VERSION
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,22 @@ New features and bug fixes are released on the latest major version of the Strip

## Development

The test suite depends on [stripe-mock], so make sure to fetch and run it from a
background terminal ([stripe-mock's README][stripe-mock] also contains
instructions for installing via Homebrew and other methods):
[Contribution guidelines for this project](CONTRIBUTING.md)

The test suite depends on [stripe-mock], so make sure to fetch and run it from a background terminal ([stripe-mock's README][stripe-mock] also contains instructions for installing via Homebrew and other methods):

```sh
go install github.com/stripe/stripe-mock@latest
stripe-mock
```

We use [just](https://github.com/casey/just) for common development tasks. You can install it or run the underlying commands directly (by copying them from the `justfile`). Common tasks include:

Run all tests:

```sh
bundle exec rake test
just test
# or: bundle exec rake test
```

Run a single test suite:
Expand All @@ -392,13 +395,15 @@ bundle exec ruby -Ilib/ test/stripe/util_test.rb -n /should.convert.names.to.sym
Run the linter:

```sh
bundle exec rake rubocop
just lint
# or: bundle exec rubocop
```

Update bundled CA certificates from the [Mozilla cURL release][curl]:

```sh
bundle exec rake update_certs
just update-certs
# or: bundle exec rake update_certs
```

Update the bundled [stripe-mock] by editing the version number found in
Expand Down
5 changes: 0 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ Rake::TestTask.new do |t|
t.pattern = "./test/**/*_test.rb"
end

if RUBY_VERSION >= "2.7.0"
require "rubocop/rake_task"
RuboCop::RakeTask.new
end

desc "Update bundled certs"
task :update_certs do
require "net/http"
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13.3.0
13.3.1
43 changes: 43 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
set quiet

import? '../sdk-codegen/utils.just'

_default:
just --list --unsorted

install *args:
bundle install {{ if is_dependency() == "true" {"--quiet"} else {""} }} {{ args }}

# ⭐ run all unit tests
test: install
bundle exec rake test

# check linting / formatting status of files
format-check *args: install
bundle exec rubocop {{ args }}
alias lint-check := format-check

# ⭐ check style & formatting for all files, fixing what we can
lint: (format-check "--autocorrect")

# NOTE: "-o /dev/null" is vital - rubocop has super noisy output and codegen will crash when formatting ruby if everything gets printed
# so, we send all its output to the void
# copy of `lint` with less output
format: (format-check "-o /dev/null --autocorrect")

update-certs: install
bundle exec rake update_certs

# run sorbet to check type definitions
typecheck: install
{{ if semver_matches(`ruby -e "puts RUBY_VERSION"`, ">=2.7") == "true" { \
"bundle exec srb tc" \
} else { \
"echo \"Ruby version < 2.7, skipping srb tc\"" \
} }}

# called by tooling
[private]
update-version version:
echo "{{ version }}" > VERSION
perl -pi -e 's|VERSION = "[.\-\w\d]+"|VERSION = "{{ version }}"|' lib/stripe/version.rb
22 changes: 22 additions & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ module Stripe
DEFAULT_UPLOAD_BASE = "https://files.stripe.com"
DEFAULT_METER_EVENTS_BASE = "https://meter-events.stripe.com"

# Options that can be configured globally by users
USER_CONFIGURABLE_GLOBAL_OPTIONS = Set.new(%i[
api_key
api_version
stripe_account
api_base
uploads_base
connect_base
meter_events_base
open_timeout
read_timeout
write_timeout
proxy
verify_ssl_certs
ca_bundle_path
log_level
logger
max_network_retries
enable_telemetry
client_id
])

@app_info = nil

@config = Stripe::StripeConfiguration.setup
Expand Down
7 changes: 6 additions & 1 deletion lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class StripeClient

# attr_readers: The end of the section generated from our OpenAPI spec

# For internal use only. Does not provide a stable API and may be broken
# with future non-major changes.
CLIENT_OPTIONS = Set.new(%i[api_key stripe_account stripe_context api_version api_base uploads_base connect_base meter_events_base client_id])

# Initializes a new StripeClient
def initialize(api_key, # rubocop:todo Metrics/ParameterLists
stripe_account: nil,
Expand Down Expand Up @@ -40,7 +44,8 @@ def initialize(api_key, # rubocop:todo Metrics/ParameterLists
client_id: client_id,
}.reject { |_k, v| v.nil? }

@requestor = APIRequestor.new(config_opts)
config = StripeConfiguration.client_init(config_opts)
@requestor = APIRequestor.new(config)

# top-level services: The beginning of the section generated from our OpenAPI spec
@v1 = Stripe::V1Services.new(@requestor)
Expand Down
22 changes: 21 additions & 1 deletion lib/stripe/stripe_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ def self.setup
end
end

# Set options to the StripeClient configured options, if valid as a client option and provided
# Otherwise, for user configurable global options, set them to the global configuration
# For all other options, set them to the StripeConfiguration default value
def self.client_init(config_opts)
global_config = Stripe.config
imported_options = USER_CONFIGURABLE_GLOBAL_OPTIONS - StripeClient::CLIENT_OPTIONS
client_config = StripeConfiguration.setup do |instance|
imported_options.each do |key|
begin
instance.public_send("#{key}=", global_config.public_send(key)) if global_config.respond_to?(key)
rescue NotImplementedError => e
# In Ruby <= 2.5, we can't set write_timeout on Net::HTTP, log an error and continue
Util.log_error("Failed to set #{key} on client configuration: #{e}")
end
end
end
client_config.reverse_duplicate_merge(config_opts)
end

# Create a new config based off an existing one. This is useful when the
# caller wants to override the global configuration
def reverse_duplicate_merge(hash)
Expand Down Expand Up @@ -68,7 +87,8 @@ def initialize
@connect_base = DEFAULT_CONNECT_BASE
@uploads_base = DEFAULT_UPLOAD_BASE
@meter_events_base = DEFAULT_METER_EVENTS_BASE
@base_addresses = { api: @api_base, connect: @connect_base, files: @uploads_base, events: @meter_events_base }
@base_addresses = { api: @api_base, connect: @connect_base, files: @uploads_base,
meter_events: @meter_events_base, }
end

def log_level=(val)
Expand Down
24 changes: 22 additions & 2 deletions lib/stripe/thin_event.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
# frozen_string_literal: true

module Stripe
class EventReasonRequest
attr_reader :id, :idempotency_key

def initialize(event_reason_request_payload = {})
@id = event_reason_request_payload[:id]
@idempotency_key = event_reason_request_payload[:idempotency_key]
end
end

class EventReason
attr_reader :type, :request

def initialize(event_reason_payload = {})
@type = event_reason_payload[:type]
@request = EventReasonRequest.new(event_reason_payload[:request])
end
end

class ThinEvent
attr_reader :id, :type, :created, :context, :related_object
attr_reader :id, :type, :created, :context, :related_object, :livemode, :reason

def initialize(event_payload = {})
@id = event_payload[:id]
Expand All @@ -11,7 +29,9 @@ def initialize(event_payload = {})
@context = event_payload[:context]
@livemode = event_payload[:livemode]
@related_object = event_payload[:related_object]
@reason = event_payload[:reason]
return if event_payload[:reason].nil?

@reason = EventReason.new(event_payload[:reason])
end
end
end
2 changes: 1 addition & 1 deletion lib/stripe/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Stripe
VERSION = "13.3.0"
VERSION = "13.3.1"
end
27 changes: 26 additions & 1 deletion test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StripeClientTest < Test::Unit::TestCase
@orig_api_key = Stripe.api_key
@orig_stripe_account = Stripe.stripe_account
@orig_open_timeout = Stripe.open_timeout
@orig_api_version = Stripe.api_version

Stripe.api_key = "DONT_USE_THIS_KEY"
Stripe.stripe_account = "DONT_USE_THIS_ACCOUNT"
Expand All @@ -30,6 +31,7 @@ class StripeClientTest < Test::Unit::TestCase
Stripe.api_key = @orig_api_key
Stripe.stripe_account = @orig_stripe_account
Stripe.open_timeout = @orig_open_timeout
Stripe.api_version = @orig_api_version
end

should "use default config options" do
Expand All @@ -45,7 +47,6 @@ class StripeClientTest < Test::Unit::TestCase
client = StripeClient.new("test_123")
assert_equal "test_123", client.instance_variable_get(:@requestor).config.api_key
assert_nil client.instance_variable_get(:@requestor).config.stripe_account
assert_equal 30, client.instance_variable_get(:@requestor).config.open_timeout
end

should "use client config options" do
Expand All @@ -67,6 +68,30 @@ class StripeClientTest < Test::Unit::TestCase
assert_equal "2022-11-15", req.headers["Stripe-Version"]
end

should "use global config options for options unavailable in client" do
Stripe.api_key = "NOT_THIS_KEY"
Stripe.stripe_account = "NOT_THIS_ACCOUNT"
Stripe.api_version = "2022-11-15"
client = StripeClient.new("test_123", stripe_account: "acct_123")
# Imported from global options
assert_equal 30_000, client.instance_variable_get(:@requestor).config.open_timeout
# Not set in client options, not imported from global
assert_equal client.instance_variable_get(:@requestor).config.api_base, Stripe::DEFAULT_API_BASE
assert_equal client.instance_variable_get(:@requestor).config.api_version, Stripe::ApiVersion::CURRENT

req = nil
stub_request(:get, "#{Stripe::DEFAULT_API_BASE}/v1/customers/cus_123")
.with { |request| req = request }
.to_return(body: JSON.generate(object: "customer"))

client.v1.customers.retrieve("cus_123")

# Set in client options
assert_equal "Bearer test_123", req.headers["Authorization"]
assert_equal "acct_123", req.headers["Stripe-Account"]
assert_requested(:get, "#{Stripe::DEFAULT_API_BASE}/v1/customers/cus_123")
end

should "request options overrides client config options" do
client = StripeClient.new("test_123", stripe_version: "2022-11-15", stripe_account: "acct_123")

Expand Down
Loading

0 comments on commit b56aa56

Please sign in to comment.