Skip to content

Commit

Permalink
Bug fix cycle 16 and 17 (#1280)
Browse files Browse the repository at this point in the history
* Add reason for manual cancel in build cancellation

* Add login of user who cancelled the build

* Use user instead of current user

* Use RSS token for builds atom feed

re #BSFY-206

* Change reason for cancellation

* Revert "Add reason for manual cancel in build cancellation"

* Save card fingerprint from Stripe

* Spec

* added debug build in audit

* Allow login with GitHub fine grained token

* Add cancel reason in one more place

* debug audit updates

* Added logs for job cancellation

* Fixed spec

* Added back reason for cancellation in api builds

---------
  • Loading branch information
murtaza-swati authored May 29, 2023
1 parent bcbf31a commit af85d25
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 6 deletions.
16 changes: 16 additions & 0 deletions lib/travis/api/app/endpoint/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ def user_for_github_token(token, drop_token = false)
data = GH.with(token: token.to_s, client_id: nil) { GH['user'] }
scopes = parse_scopes data.headers['x-oauth-scopes']
manager = UserManager.new(data, token, drop_token)

# The new GitHub fine-grained tokens do not include scopes header yet
# or any way of retrieving scopes so we just have to assume that
# user gave all necessary permissions
# TODO: Remove this when GitHub implements x-oauth-scopes header for
# fine-grained tokens https://github.com/orgs/community/discussions/36441#discussioncomment-5183431
scopes = Travis::Github::Oauth.wanted_scopes if github_fine_grained_pat?(token.to_s)

unless acceptable?(scopes, drop_token)
# TODO: we should probably only redirect if this is a web
Expand All @@ -397,6 +404,11 @@ def user_for_github_token(token, drop_token = false)
halt 403, 'not a Travis user'
end

if github_fine_grained_pat?(token.to_s)
user.github_scopes = Travis::Github::Oauth.wanted_scopes
user.save!
end

Travis.run_service(:sync_user, user)

user
Expand All @@ -405,6 +417,10 @@ def user_for_github_token(token, drop_token = false)
halt 403, 'not a Travis user'
end

def github_fine_grained_pat?(token)
token.start_with?('github_pat_')
end

def get_token(endpoint, values)
# Get base URL for when we setup Faraday since otherwise it'll ignore no_proxy
url = URI.parse(endpoint)
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/app/endpoint/builds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Builds < Endpoint
status 422
respond_with json
else
payload = { id: params[:id], user_id: current_user.id, source: 'api' }
payload = { id: params[:id], user_id: current_user.id, source: 'api', reason: "Build Cancelled manually by User: #{current_user.login}" }

service.push("build:cancel", payload)

Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/app/endpoint/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Jobs < Endpoint
status 422
respond_with json
else
payload = { id: params[:id], user_id: current_user.id, source: 'api' }
payload = { id: params[:id], user_id: current_user.id, source: 'api', reason: "Job Cancelled manually by User with id: #{current_user.login}" }
service.push("job:cancel", payload)

Metriks.meter("api.v2.request.cancel_job.success").mark
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/v3/queries/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def find
def cancel(user, build_id)
raise BuildNotCancelable if %w(passed failed canceled errored).include? find.state

payload = { id: build_id, user_id: user.id, source: 'api' }
payload = { id: build_id, user_id: user.id, source: 'api', reason: "Build Cancelled manually by User: #{user.login}" }
service = Travis::Enqueue::Services::CancelModel.new(user, { build_id: build_id })
service.push("build:cancel", payload)
payload
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/v3/queries/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def find
def cancel(user)
raise JobNotCancelable if %w(passed failed canceled errored).include? find.state

payload = { id: id, user_id: user.id, source: 'api' }
payload = { id: id, user_id: user.id, source: 'api', reason: "Job Cancelled manually by User with id: #{user.login}" }
service = Travis::Enqueue::Services::CancelModel.new(user, { job_id: id })
service.push("job:cancel", payload)
payload
Expand Down
2 changes: 2 additions & 0 deletions lib/travis/api/v3/services/job/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def run
job.debug_options = debug_data
job.save!

Travis::API::V3::Models::Audit.create!(owner: access_control.user, change_source: 'travis-api', source: job.repository, source_changes: { debug: 'Debug build triggered' })

query.restart(access_control.user)
accepted(job: job, state_change: :created)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/services/update_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def raise_unknown_event
end

def cancel_job_in_worker
publisher.publish(type: 'cancel_job', job_id: job.id, source: 'update_job_service')
publisher.publish(type: 'cancel_job', job_id: job.id, source: 'update_job_service', reason: 'Some event other than reset was called on the job!')
end

def publisher
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/services/update_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
publisher = double('publisher')
allow(service).to receive(:publisher).and_return(publisher)

expect(publisher).to receive(:publish).with(type: 'cancel_job', job_id: job.id, source: 'update_job_service')
expect(publisher).to receive(:publish).with(type: 'cancel_job', job_id: job.id, source: 'update_job_service', reason: 'Some event other than reset was called on the job!')

service.cancel_job_in_worker
end
Expand Down

0 comments on commit af85d25

Please sign in to comment.