-
Couldn't load subscription status.
- Fork 161
[TBT-392] Soft delete orgs and repo #1399
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
Open
mshahzaib-travis
wants to merge
8
commits into
master
Choose a base branch
from
TBT-392-assembla-notify-endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a16cf3f
[TBT-392] Soft delete orgs and repo
mshahzaib-travis 627fd20
removed pry
mshahzaib-travis 0cddfee
Added logging
mshahzaib-travis 5169134
Refactored code
mshahzaib-travis 115a4fe
Fixed specs
mshahzaib-travis ea14bbb
Fixed organization spec
mshahzaib-travis 4d1181d
Stub spec URL
mshahzaib-travis 3fae835
Stub request
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,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'travis/remote_vcs/client' | ||
|
|
||
| module Travis | ||
| class RemoteVCS | ||
| class Organization < Client | ||
| def destroy(org_id:) | ||
| request(:delete, __method__, false) do |req| | ||
| req.url "organizations/#{org_id}" | ||
| end | ||
| rescue ResponseError => e | ||
| Travis.logger.error("Failed to destroy organization: #{e.message}") | ||
| false | ||
| end | ||
| 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,53 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'travis/remote_vcs/repository' | ||
| require 'travis/remote_vcs/organization' | ||
|
|
||
| module Travis | ||
| module Services | ||
| class AssemblaNotifyService | ||
| VALID_ACTIONS = %w[destroy].freeze | ||
| VALID_OBJECTS = %w[space tool].freeze | ||
|
|
||
| def initialize(payload) | ||
| @action = payload[:action] | ||
| @object = payload[:object] | ||
| @object_id = payload[:id] | ||
| end | ||
|
|
||
| def run | ||
| validate | ||
| case @object | ||
| when 'tool' | ||
| handle_tool_destruction | ||
| when 'space' | ||
| handle_space_destruction | ||
| else | ||
| { status: 400, body: { error: 'Unsupported object type for destruction' } } | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate | ||
| unless VALID_ACTIONS.include?(@action) | ||
| return { status: 400, body: { error: 'Invalid action', allowed_actions: VALID_ACTIONS } } | ||
| end | ||
|
|
||
| unless VALID_OBJECTS.include?(@object) | ||
| return { status: 400, body: { error: 'Invalid object type', allowed_objects: VALID_OBJECTS } } | ||
| end | ||
| end | ||
|
|
||
| def handle_tool_destruction | ||
| vcs_repository = Travis::RemoteVCS::Repository.new | ||
| vcs_repository.destroy(repository_id: @object_id) | ||
| end | ||
|
|
||
| def handle_space_destruction | ||
| vcs_organization = Travis::RemoteVCS::Organization.new | ||
| vcs_organization.destroy(org_id: @object_id) | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| require 'spec_helper' | ||
| require 'travis/services/assembla_notify_service' | ||
|
|
||
| RSpec.describe Travis::Services::AssemblaNotifyService do | ||
| let(:payload) { { action: 'destroy', object: 'tool', id: '12345' } } | ||
| let(:service) { described_class.new(payload) } | ||
| let(:vcs_repository) { instance_double(Travis::RemoteVCS::Repository) } | ||
| let(:vcs_organization) { instance_double(Travis::RemoteVCS::Organization) } | ||
|
|
||
| before do | ||
| allow(Travis::RemoteVCS::Repository).to receive(:new).and_return(vcs_repository) | ||
| allow(vcs_repository).to receive(:destroy) | ||
| allow(Travis::RemoteVCS::Organization).to receive(:new).and_return(vcs_organization) | ||
| allow(vcs_organization).to receive(:destroy) | ||
| allow(Travis.logger).to receive(:error) | ||
| end | ||
|
|
||
| describe '#run' do | ||
| context 'with a valid payload for tool destruction' do | ||
| it 'calls handle_tool_destruction' do | ||
| expect(service).to receive(:handle_tool_destruction) | ||
| service.run | ||
| end | ||
| end | ||
|
|
||
| context 'with a valid payload for space destruction' do | ||
| let(:payload) { { action: 'destroy', object: 'space', id: '67890' } } | ||
|
|
||
| it 'calls handle_space_destruction' do | ||
| expect(service).to receive(:handle_space_destruction) | ||
| service.run | ||
| end | ||
| end | ||
|
|
||
| context 'with an invalid object type' do | ||
| let(:payload) { { action: 'destroy', object: 'repository', id: '12345' } } | ||
|
|
||
| it 'returns an error' do | ||
| result = service.run | ||
| expect(result[:status]).to eq(400) | ||
| end | ||
| end | ||
|
|
||
| context 'with an unsupported object type for destruction' do | ||
| before do | ||
| stub_const("Travis::Services::AssemblaNotifyService::VALID_OBJECTS", %w[space tool unsupported]) | ||
| end | ||
| let(:payload) { { action: 'destroy', object: 'unsupported', id: '12345' } } | ||
|
|
||
| it 'returns an error' do | ||
| result = service.run | ||
| expect(result[:status]).to eq(400) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#handle_tool_destruction' do | ||
| it 'destroys the repository using RemoteVCS' do | ||
| expect(vcs_repository).to receive(:destroy).with(repository_id: '12345') | ||
| service.send(:handle_tool_destruction) | ||
| end | ||
| end | ||
|
|
||
| describe '#handle_space_destruction' do | ||
| let(:payload) { { action: 'destroy', object: 'space', id: '67890' } } | ||
|
|
||
| it 'destroys the organization using RemoteVCS' do | ||
| expect(vcs_organization).to receive(:destroy).with(org_id: '67890') | ||
| service.send(:handle_space_destruction) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| require 'spec_helper' | ||
| require 'travis/remote_vcs/organization' | ||
|
|
||
| RSpec.describe Travis::RemoteVCS::Organization do | ||
| let(:client) { described_class.new } | ||
| let(:org_id) { '12345' } | ||
| let(:subject) { client.destroy(org_id: org_id) } | ||
|
|
||
| describe '#destroy' do | ||
| it 'sends a delete request to the correct URL' do | ||
| request = double('request') | ||
| expect(request).to receive(:url).with("organizations/#{org_id}") | ||
| expect(client).to receive(:request).with(:delete, :destroy, false).and_yield(request) | ||
| subject | ||
| end | ||
|
|
||
| context 'when request is successful' do | ||
| before { allow(client).to receive(:request).and_return(true) } | ||
| it { is_expected.to be true } | ||
| end | ||
|
|
||
| context 'when the request fails' do | ||
| before { allow(client).to receive(:request).and_return(false) } | ||
| it { is_expected.to be false } | ||
| 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
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.