-
Notifications
You must be signed in to change notification settings - Fork 161
Assembla Deep Integration: Travis API Endpoint #1380
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
Merged
mshahzaib-travis
merged 15 commits into
prd-asm_integration_dev
from
TBT-381-assembla-jwt-login-endpoint
Aug 12, 2025
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
447f8e3
[TBT-381] Integrated assembla JWT login endpoint
1934be6
[TBT-381] Fixed assembla spec
mshahzaib-travis e461de2
[TBT-381] Fixed build failure Env issue
mshahzaib-travis 0526c13
[TBT-381] Create beta_plan for org
mshahzaib-travis 7b5948a
[TBT-381] - Sync and Create Subscription
mshahzaib-travis ab5b727
[TBT-381] Feedback incorporated
mshahzaib-travis a1bf5c3
[TBT-381] Feedback incorporated
mshahzaib-travis 1a2c602
[TBT-381] used default values in spec
mshahzaib-travis 5f79e3b
[TBT-381] changed config to use array
mshahzaib-travis fb82a6a
Sync only passed space and repository
daaafb7
[TBT-382] specs fixed
mshahzaib-travis db9cadb
Generated access token
mshahzaib-travis bf4eb60
Confirm user and fix spec
mshahzaib-travis 65a72ca
Fixed membership admin
mshahzaib-travis e4227ba
Merge pull request #1385 from travis-ci/TBT-382-sync-org-and-repo
mshahzaib-travis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'travis/api/app' | ||
require 'jwt' | ||
require 'travis/remote_vcs/user' | ||
require 'travis/remote_vcs/repository' | ||
require 'travis/api/v3/billing_client' | ||
require_relative '../jwt_utils' | ||
|
||
class Travis::Api::App | ||
class Endpoint | ||
class Assembla < Endpoint | ||
include Travis::Api::App::JWTUtils | ||
set prefix: '/assembla' | ||
set :check_auth, false | ||
|
||
before do | ||
halt 403, { error: 'Deep integration not enabled' } unless deep_integration_enabled? | ||
halt 403, { error: 'Invalid ASM cluster' } unless valid_asm_cluster? | ||
begin | ||
@jwt_payload = verify_jwt(request, Travis.config.assembla_jwt_secret) | ||
rescue JWTUtils::UnauthorizedError => e | ||
halt 401, { error: e.message }.to_json | ||
end | ||
end | ||
|
||
# POST /assembla/login | ||
# Accepts a JWT, finds or creates a user, and signs them in | ||
post '/login' do | ||
user = find_or_create_user(@jwt_payload) | ||
sync_user(user.id) | ||
create_org_subscription(user.id, @jwt_payload[:space_id]) | ||
|
||
{ | ||
user_id: user.id, | ||
login: user.login, | ||
token: user.token, | ||
status: 'signed_in' | ||
}.to_json | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
private | ||
|
||
def deep_integration_enabled? | ||
Travis.config.deep_integration_enabled | ||
end | ||
|
||
def valid_asm_cluster? | ||
allowed = Array(Travis.config.assembla_clusters.split(',')) | ||
cluster = request.env['HTTP_X_ASSEMBLA_CLUSTER'] | ||
allowed.include?(cluster) | ||
end | ||
|
||
# Finds or creates a user based on the payload | ||
def find_or_create_user(payload) | ||
required_fields = %w[name email login space_id] | ||
missing = required_fields.select { |f| payload[f].nil? || payload[f].to_s.strip.empty? } | ||
unless missing.empty? | ||
halt 400, { error: 'Missing required fields', missing: missing }.to_json | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
attrs = { | ||
name: payload['name'], | ||
email: payload['email'], | ||
login: payload['login'], | ||
org_id: payload['space_id'], | ||
vcs_type: 'AssemblaUser' | ||
} | ||
::User.find_or_create_by!(attrs) | ||
end | ||
|
||
def sync_user(user_id) | ||
Travis::RemoteVCS::User.new.sync(user_id: user_id) | ||
rescue => e | ||
halt 500, { error: 'User sync failed', details: e.message }.to_json | ||
end | ||
|
||
def create_org_subscription(user_id, space_id) | ||
plan = 'beta_plan' | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client = Travis::API::V3::BillingClient.new(user_id) | ||
client.create_v2_subscription({ | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'plan' => plan, | ||
'organization_id' => space_id, | ||
}) | ||
rescue => e | ||
halt 500, { error: 'Subscription creation failed', details: e.message }.to_json | ||
end | ||
end | ||
end | ||
end | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Travis::Api::App | ||
module JWTUtils | ||
def extract_jwt_token(request) | ||
request.env['HTTP_AUTHORIZATION']&.split(' ')&.last | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def verify_jwt(request, secret) | ||
token = extract_jwt_token(request) | ||
raise UnauthorizedError, 'Missing JWT' unless token | ||
begin | ||
decoded, = JWT.decode(token, secret, true, { algorithm: 'HS256' }) | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decoded | ||
rescue JWT::DecodeError => e | ||
raise UnauthorizedError, "Invalid JWT: #{e.message}" | ||
end | ||
end | ||
|
||
class UnauthorizedError < StandardError; end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
require 'spec_helper' | ||
require 'rack/test' | ||
require 'jwt' | ||
|
||
RSpec.describe Travis::Api::App::Endpoint::Assembla, set_app: true do | ||
include Rack::Test::Methods | ||
|
||
let(:jwt_secret) { 'testsecret' } | ||
let(:payload) do | ||
{ | ||
'name' => 'Test User', | ||
'email' => '[email protected]', | ||
'login' => 'testuser', | ||
'space_id' => 'space123' | ||
} | ||
end | ||
let(:token) { JWT.encode(payload, jwt_secret, 'HS256') } | ||
|
||
before do | ||
Travis.config[:deep_integration_enabled] = true | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Travis.config[:assembla_clusters] = 'cluster1' | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Travis.config[:assembla_jwt_secret] = jwt_secret | ||
|
||
header 'X_ASSEMBLA_CLUSTER', 'cluster1' | ||
end | ||
|
||
describe 'POST /assembla/login' do | ||
context 'with valid JWT' do | ||
before do | ||
allow_any_instance_of(Travis::RemoteVCS::User).to receive(:sync).and_return(true) | ||
allow_any_instance_of(Travis::API::V3::BillingClient).to receive(:create_v2_subscription).and_return(true) | ||
end | ||
|
||
it 'returns user info and token' do | ||
header 'Authorization', "Bearer #{token}" | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(200) | ||
body = JSON.parse(last_response.body) | ||
expect(body['login']).to eq('testuser') | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(body['token']).to be_present | ||
expect(body['status']).to eq('signed_in') | ||
end | ||
end | ||
|
||
context 'with missing JWT' do | ||
it 'returns 401' do | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(401) | ||
expect(last_response.body).to include('Missing JWT') | ||
end | ||
end | ||
|
||
context 'with invalid JWT' do | ||
it 'returns 401' do | ||
header 'Authorization', 'Bearer invalidtoken' | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(401) | ||
expect(last_response.body).to include('Invalid JWT') | ||
end | ||
end | ||
|
||
context 'when user sync fails' do | ||
before do | ||
allow(::User).to receive(:first_or_create!).and_return(double('User', id: 1, login: 'testuser', token: 'abc123')) | ||
allow_any_instance_of(Travis::RemoteVCS::User).to receive(:sync).and_raise(StandardError.new('sync error')) | ||
end | ||
|
||
it 'returns 500 with error message' do | ||
header 'Authorization', "Bearer #{token}" | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(500) | ||
expect(last_response.body).to include('User sync failed') | ||
expect(last_response.body).to include('sync error') | ||
end | ||
end | ||
|
||
context 'when integration is not enabled' do | ||
before { Travis.config[:deep_integration_enabled] = false } | ||
mshahzaib-travis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it 'returns 403' do | ||
header 'Authorization', "Bearer #{token}" | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(403) | ||
expect(last_response.body).to include('Deep integration not enabled') | ||
end | ||
end | ||
|
||
context 'when cluster is invalid' do | ||
before { header 'X_ASSEMBLA_CLUSTER', 'invalid-cluster' } | ||
it 'returns 403' do | ||
header 'Authorization', "Bearer #{token}" | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(403) | ||
expect(last_response.body).to include('Invalid ASM cluster') | ||
end | ||
end | ||
|
||
context 'with missing required fields in JWT payload' do | ||
let(:payload) do | ||
{ | ||
'name' => 'Test User', | ||
'login' => 'testuser', | ||
'space_id' => 'space123' # 'email' is missing | ||
} | ||
end | ||
let(:token) { JWT.encode(payload, jwt_secret, 'HS256') } | ||
|
||
it 'returns 400 with missing fields' do | ||
header 'Authorization', "Bearer #{token}" | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to include('Missing required fields') | ||
expect(last_response.body).to include('email') | ||
end | ||
end | ||
|
||
context 'with expired JWT token' do | ||
let(:payload) do | ||
{ | ||
'name' => 'Test User', | ||
'email' => '[email protected]', | ||
'login' => 'testuser', | ||
'space_id' => 'space123', | ||
'exp' => (Time.now.to_i - 60) | ||
} | ||
end | ||
let(:token) { JWT.encode(payload, jwt_secret, 'HS256') } | ||
|
||
it 'returns 401 with expired error' do | ||
header 'Authorization', "Bearer #{token}" | ||
post '/assembla/login' | ||
expect(last_response.status).to eq(401) | ||
expect(last_response.body).to match(/expired|exp/i) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.