Skip to content

Commit af85d25

Browse files
Bug fix cycle 16 and 17 (#1280)
* 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 ---------
1 parent bcbf31a commit af85d25

File tree

8 files changed

+24
-6
lines changed

8 files changed

+24
-6
lines changed

lib/travis/api/app/endpoint/authorization.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ def user_for_github_token(token, drop_token = false)
381381
data = GH.with(token: token.to_s, client_id: nil) { GH['user'] }
382382
scopes = parse_scopes data.headers['x-oauth-scopes']
383383
manager = UserManager.new(data, token, drop_token)
384+
385+
# The new GitHub fine-grained tokens do not include scopes header yet
386+
# or any way of retrieving scopes so we just have to assume that
387+
# user gave all necessary permissions
388+
# TODO: Remove this when GitHub implements x-oauth-scopes header for
389+
# fine-grained tokens https://github.com/orgs/community/discussions/36441#discussioncomment-5183431
390+
scopes = Travis::Github::Oauth.wanted_scopes if github_fine_grained_pat?(token.to_s)
384391

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

407+
if github_fine_grained_pat?(token.to_s)
408+
user.github_scopes = Travis::Github::Oauth.wanted_scopes
409+
user.save!
410+
end
411+
400412
Travis.run_service(:sync_user, user)
401413

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

420+
def github_fine_grained_pat?(token)
421+
token.start_with?('github_pat_')
422+
end
423+
408424
def get_token(endpoint, values)
409425
# Get base URL for when we setup Faraday since otherwise it'll ignore no_proxy
410426
url = URI.parse(endpoint)

lib/travis/api/app/endpoint/builds.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Builds < Endpoint
4242
status 422
4343
respond_with json
4444
else
45-
payload = { id: params[:id], user_id: current_user.id, source: 'api' }
45+
payload = { id: params[:id], user_id: current_user.id, source: 'api', reason: "Build Cancelled manually by User: #{current_user.login}" }
4646

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

lib/travis/api/app/endpoint/jobs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Jobs < Endpoint
5050
status 422
5151
respond_with json
5252
else
53-
payload = { id: params[:id], user_id: current_user.id, source: 'api' }
53+
payload = { id: params[:id], user_id: current_user.id, source: 'api', reason: "Job Cancelled manually by User with id: #{current_user.login}" }
5454
service.push("job:cancel", payload)
5555

5656
Metriks.meter("api.v2.request.cancel_job.success").mark

lib/travis/api/v3/queries/build.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def find
1515
def cancel(user, build_id)
1616
raise BuildNotCancelable if %w(passed failed canceled errored).include? find.state
1717

18-
payload = { id: build_id, user_id: user.id, source: 'api' }
18+
payload = { id: build_id, user_id: user.id, source: 'api', reason: "Build Cancelled manually by User: #{user.login}" }
1919
service = Travis::Enqueue::Services::CancelModel.new(user, { build_id: build_id })
2020
service.push("build:cancel", payload)
2121
payload

lib/travis/api/v3/queries/job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def find
1313
def cancel(user)
1414
raise JobNotCancelable if %w(passed failed canceled errored).include? find.state
1515

16-
payload = { id: id, user_id: user.id, source: 'api' }
16+
payload = { id: id, user_id: user.id, source: 'api', reason: "Job Cancelled manually by User with id: #{user.login}" }
1717
service = Travis::Enqueue::Services::CancelModel.new(user, { job_id: id })
1818
service.push("job:cancel", payload)
1919
payload

lib/travis/api/v3/services/job/debug.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def run
1414
job.debug_options = debug_data
1515
job.save!
1616

17+
Travis::API::V3::Models::Audit.create!(owner: access_control.user, change_source: 'travis-api', source: job.repository, source_changes: { debug: 'Debug build triggered' })
18+
1719
query.restart(access_control.user)
1820
accepted(job: job, state_change: :created)
1921
end

lib/travis/services/update_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def raise_unknown_event
4747
end
4848

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

5353
def publisher

spec/lib/services/update_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
publisher = double('publisher')
2323
allow(service).to receive(:publisher).and_return(publisher)
2424

25-
expect(publisher).to receive(:publish).with(type: 'cancel_job', job_id: job.id, source: 'update_job_service')
25+
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!')
2626

2727
service.cancel_job_in_worker
2828
end

0 commit comments

Comments
 (0)