Skip to content

Commit 39d509c

Browse files
author
Niharika
committed
OTWO-6954 api for create scan project
1 parent 79a16d4 commit 39d509c

12 files changed

+387
-1
lines changed

.env.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ JWT_SECRET_API_KEY='116016cca2a9f3eed660a65a78ba88091a73b330'
3838

3939
SUPPRESS_JASMINE_DEPRECATION = 1
4040

41+
COVERITY_SCAN_URL = 'http://vcrlocalhost.org:5008'
42+
4143
KB_API_AUTH_KEY = 'test'
4244
KB_AUTH_API = 'https://vcrlocalhost/auth'
4345
BDSA_VULNERABILITY_API = 'https://vcrlocalhost/bdsa/BDSA_ID'

app/controllers/api/v1/projects_controller.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Api::V1::ProjectsController < ApplicationController
66

77
skip_before_action :verify_authenticity_token
88
before_action :authenticate_jwt
9+
before_action :set_project_or_fail, only: [:create_scan_project]
910

1011
def create
1112
@project = build_project
@@ -18,6 +19,14 @@ def create
1819
end
1920
end
2021

22+
def create_scan_project
23+
response = get_scan_api_data(params[:url], 'api/projects')
24+
return unless response && response['scan_project_id']
25+
26+
CodeLocationScan.where(code_location_id: @project.enlistments.first.code_location_id,
27+
scan_project_id: response['scan_project_id']).first_or_create
28+
end
29+
2130
private
2231

2332
def project_params
@@ -63,4 +72,13 @@ def code_location_branch(url)
6372
out, _err, _status = Open3.capture3("git ls-remote --symref #{url} HEAD | head -1 | awk '{print $2}'")
6473
out.strip.sub(/refs\/heads\//, '')
6574
end
75+
76+
def get_scan_api_data(url, path)
77+
return unless @project
78+
79+
language = @project&.best_analysis&.main_language&.nice_name
80+
data = { name: @project&.name, repo_url: url, user_id: params[:user_id],
81+
language: scan_oh_language_mapping(language) }
82+
ScanCoverityApi.save(path, data)
83+
end
6684
end

app/helpers/projects_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,15 @@ def project_activity_level(project)
127127
def project_description_size_breached?(project)
128128
project.description && project.description.size > 800
129129
end
130+
131+
def scan_oh_language_mapping(language)
132+
case language
133+
when 'C++', 'C/C++', 'C' then 'CXX'
134+
when 'Java' then 'JAVA'
135+
when 'C#' then 'CSHARP'
136+
when 'JavaScript' then 'JAVASCRIPT'
137+
when 'Ruby', 'Python', 'PHP' then 'OTHER'
138+
end
139+
end
130140
end
131141
# rubocop: enable Metrics/ModuleLength
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
class ScanCoverityApi
4+
URL = ENV['COVERITY_SCAN_URL']
5+
6+
class << self
7+
def resource_uri(path = nil, _query = {})
8+
URI("#{URL}/#{path}.json")
9+
end
10+
11+
def get_response(path = nil, query = {})
12+
uri = resource_uri(path, query)
13+
response = Net::HTTP.get_response(uri)
14+
handle_errors(response) { JSON.parse(response.body) }
15+
end
16+
17+
def save(path = nil, query = {})
18+
uri = resource_uri(path, query)
19+
response = Net::HTTP.post_form(uri, query)
20+
handle_errors(response) do
21+
hsh = JSON.parse(response.body)
22+
set_attributes_or_errors(response, hsh)
23+
end
24+
rescue JSON::ParserError
25+
response.body
26+
end
27+
28+
private
29+
30+
def handle_errors(response)
31+
case response
32+
when Net::HTTPServerError
33+
raise ScanCoverityApiError, "#{response.message} => #{response.body}"
34+
else
35+
yield
36+
end
37+
end
38+
39+
def save_success?(response)
40+
response.is_a?(Net::HTTPSuccess)
41+
end
42+
43+
def set_errors(hsh)
44+
@errors = hsh.key?('error') ? hsh['error'].with_indifferent_access : hsh
45+
false
46+
end
47+
48+
def set_attributes(hsh)
49+
@attributes = hsh
50+
hsh.each do |key, value|
51+
instance_variable_set("@#{key}", value)
52+
end
53+
end
54+
55+
def set_attributes_or_errors(response, hsh)
56+
save_success?(response) ? set_attributes(hsh) : set_errors(hsh)
57+
end
58+
end
59+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class ScanCoverityApiError < StandardError
4+
end

config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,11 @@
493493
post 'enlist'
494494
end
495495
resources :jwt, only: [:create]
496-
resources :projects, only: [:create]
496+
resources :projects, only: [:create] do
497+
member do
498+
get :create_scan_project
499+
end
500+
end
497501
end
498502
end
499503

fixtures/vcr_cassettes/CreateProjectFromMatchURL_record_none.yml

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
http_interactions:
3+
- request:
4+
method: post
5+
uri: http://vcrlocalhost.org:5008/api/projects.json
6+
body:
7+
encoding: US-ASCII
8+
string: name=Dummytestdata&repo_url=https%3A%2F%2Fgithub.com%2Frails%2Frails&user_id=e1dc08285095f4ff99199c3436532768&language=JAVA
9+
headers:
10+
Accept-Encoding:
11+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12+
Accept:
13+
- "*/*"
14+
User-Agent:
15+
- Ruby
16+
Host:
17+
- vcrlocalhost.org:5008
18+
Content-Type:
19+
- application/x-www-form-urlencoded
20+
response:
21+
status:
22+
code: 201
23+
message: success
24+
headers:
25+
Date:
26+
- Tue, 14 Mar 2023 11:09:01 GMT
27+
Content-Type:
28+
- text/plain
29+
Transfer-Encoding:
30+
- chunked
31+
Connection:
32+
- keep-alive
33+
X-Request-Id:
34+
- 83ba289fe76f4ed9a882a2a823be6d87
35+
X-Runtime:
36+
- '0.006584'
37+
X-Powered-By:
38+
- Phusion Passenger 5.0.30
39+
Status:
40+
- 201
41+
Strict-Transport-Security:
42+
- max-age=15724800; includeSubDomains
43+
Set-Cookie:
44+
- incap_ses_1559_941207=QCvHB5yXehqCVRkBiK6iFc1VEGQAAAAA7G/+9TvEcxaAWzmLbgQZBg==;
45+
path=/; Domain=.coverity.com
46+
- nlbi_941207=bJ7MAhEsdhG1ODi7vBlnAwAAAAA3GPUJcWfVnLBF6Cb2d22Z; path=/; Domain=.coverity.com
47+
- visid_incap_941207=QJfhtfo9QgiltzVrch2GzcxVEGQAAAAAQUIPAAAAAAD7Vbp21tN9wMYyJIC789MH;
48+
expires=Tue, 12 Mar 2024 15:15:25 GMT; HttpOnly; path=/; Domain=.coverity.com
49+
X-Cdn:
50+
- Imperva
51+
X-Iinfo:
52+
- 12-49168636-49168647 NNYN CT(229 230 0) RT(1678792140667 100) q(0 0 5 5) r(7
53+
7) U24
54+
body:
55+
encoding: ASCII-8BIT
56+
string: '{"scan_project_id": 1 }'
57+
recorded_at: Tue, 14 Mar 2023 11:09:02 GMT
58+
---
59+
http_interactions:
60+
- request:
61+
method: post
62+
uri: http://vcrlocalhost.org:5008/api/projects.json
63+
body:
64+
encoding: US-ASCII
65+
string: name=&repo_url=https%3A%2F%2Fgithub.com%2Frails%2Frails&user_id=d1224324214
66+
headers:
67+
Accept-Encoding:
68+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
69+
Accept:
70+
- "*/*"
71+
User-Agent:
72+
- Ruby
73+
Host:
74+
- vcrlocalhost.org:5008
75+
Content-Type:
76+
- application/x-www-form-urlencoded
77+
response:
78+
status:
79+
code: 401
80+
message: unauthorized
81+
headers:
82+
Date:
83+
- Tue, 14 Mar 2023 11:09:01 GMT
84+
Content-Type:
85+
- text/plain
86+
Transfer-Encoding:
87+
- chunked
88+
Connection:
89+
- keep-alive
90+
X-Request-Id:
91+
- 83ba289fe76f4ed9a882a2a823be6d87
92+
X-Runtime:
93+
- '0.006584'
94+
X-Powered-By:
95+
- Phusion Passenger 5.0.30
96+
Status:
97+
- 401
98+
Strict-Transport-Security:
99+
- max-age=15724800; includeSubDomains
100+
Set-Cookie:
101+
- incap_ses_1559_941207=QCvHB5yXehqCVRkBiK6iFc1VEGQAAAAA7G/+9TvEcxaAWzmLbgQZBg==;
102+
path=/; Domain=.coverity.com
103+
- nlbi_941207=bJ7MAhEsdhG1ODi7vBlnAwAAAAA3GPUJcWfVnLBF6Cb2d22Z; path=/; Domain=.coverity.com
104+
- visid_incap_941207=QJfhtfo9QgiltzVrch2GzcxVEGQAAAAAQUIPAAAAAAD7Vbp21tN9wMYyJIC789MH;
105+
expires=Tue, 12 Mar 2024 15:15:25 GMT; HttpOnly; path=/; Domain=.coverity.com
106+
X-Cdn:
107+
- Imperva
108+
X-Iinfo:
109+
- 12-49168636-49168647 NNYN CT(229 230 0) RT(1678792140667 100) q(0 0 5 5) r(7
110+
7) U24
111+
body:
112+
encoding: ASCII-8BIT
113+
string: '{"message": "unauthorized"}'
114+
recorded_at: Tue, 14 Mar 2023 11:09:02 GMT
115+
recorded_with: VCR 6.0.0
116+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
http_interactions:
3+
- request:
4+
method: post
5+
uri: http://vcrlocalhost.org:5008/api/projects.json
6+
body:
7+
encoding: US-ASCII
8+
string: name=&repo_url=https%3A%2F%2Fgithub.com%2Frails%2Frails&user_id=d1224324214
9+
headers:
10+
Accept-Encoding:
11+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12+
Accept:
13+
- "*/*"
14+
User-Agent:
15+
- Ruby
16+
Host:
17+
- vcrlocalhost.org:5008
18+
Content-Type:
19+
- application/x-www-form-urlencoded
20+
response:
21+
status:
22+
code: 400
23+
message: bad_request
24+
headers:
25+
Date:
26+
- Tue, 14 Mar 2023 11:09:01 GMT
27+
Content-Type:
28+
- text/plain
29+
Transfer-Encoding:
30+
- chunked
31+
Connection:
32+
- keep-alive
33+
X-Request-Id:
34+
- 83ba289fe76f4ed9a882a2a823be6d87
35+
X-Runtime:
36+
- '0.006584'
37+
X-Powered-By:
38+
- Phusion Passenger 5.0.30
39+
Status:
40+
- 400
41+
Strict-Transport-Security:
42+
- max-age=15724800; includeSubDomains
43+
Set-Cookie:
44+
- incap_ses_1559_941207=QCvHB5yXehqCVRkBiK6iFc1VEGQAAAAA7G/+9TvEcxaAWzmLbgQZBg==;
45+
path=/; Domain=.coverity.com
46+
- nlbi_941207=bJ7MAhEsdhG1ODi7vBlnAwAAAAA3GPUJcWfVnLBF6Cb2d22Z; path=/; Domain=.coverity.com
47+
- visid_incap_941207=QJfhtfo9QgiltzVrch2GzcxVEGQAAAAAQUIPAAAAAAD7Vbp21tN9wMYyJIC789MH;
48+
expires=Tue, 12 Mar 2024 15:15:25 GMT; HttpOnly; path=/; Domain=.coverity.com
49+
X-Cdn:
50+
- Imperva
51+
X-Iinfo:
52+
- 12-49168636-49168647 NNYN CT(229 230 0) RT(1678792140667 100) q(0 0 5 5) r(7
53+
7) U24
54+
body:
55+
encoding: ASCII-8BIT
56+
string: '{"message": "Language cant be blank"}'
57+
recorded_at: Tue, 14 Mar 2023 11:09:02 GMT
58+
recorded_with: VCR 6.0.0
59+

test/controllers/api_v1_projects_controller_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,17 @@ class Api::V1::ProjectsControllerTest < ActionController::TestCase
8787
end
8888
end
8989
end
90+
91+
describe 'create_scan_project' do
92+
it 'it create a scan project if not found' do
93+
VCR.use_cassette('CreateProjectFromMatchURL, :record => :none') do
94+
url = 'https://github.com/rails/rails'
95+
project = create(:project, name: 'rails', description: 'Ruby on Rails', vanity_url: 'rails')
96+
create(:enlistment, project: project, code_location_id: 1)
97+
params = { id: project.vanity_url, JWT: @jwt, url: url, user_id: 'e1dc08285095f4ff99199c3436532768' }
98+
get :create_scan_project, params: params, format: :json
99+
assert_response 204
100+
end
101+
end
102+
end
90103
end

0 commit comments

Comments
 (0)