Skip to content

Commit 2c9ba2e

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

11 files changed

+300
-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
@@ -18,6 +18,14 @@ def create
1818
end
1919
end
2020

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

2331
def project_params
@@ -63,4 +71,14 @@ def code_location_branch(url)
6371
out, _err, _status = Open3.capture3("git ls-remote --symref #{url} HEAD | head -1 | awk '{print $2}'")
6472
out.strip.sub(/refs\/heads\//, '')
6573
end
74+
75+
def get_scan_api_data(url, path)
76+
project = populate_project_from_forge(url, true)
77+
project_data = Project.find_by(vanity_url: project.vanity_url)
78+
language = project_data&.best_analysis&.main_language&.nice_name
79+
data = { name: project&.name, repo_url: url, user_id: params[:user_id],
80+
language: scan_oh_language_mapping(language) }
81+
response = ScanCoverityApi.save(path, data)
82+
[response, project_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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 save(path = nil, query = {})
12+
uri = resource_uri(path, query)
13+
response = Net::HTTP.post_form(uri, query)
14+
handle_errors(response) do
15+
hsh = JSON.parse(response.body)
16+
set_attributes_or_errors(response, hsh)
17+
end
18+
rescue JSON::ParserError
19+
response.body
20+
end
21+
22+
private
23+
24+
def handle_errors(response)
25+
case response
26+
when Net::HTTPServerError
27+
raise ScanCoverityApiError, "#{response.message} => #{response.body}"
28+
else
29+
yield
30+
end
31+
end
32+
33+
def save_success?(response)
34+
response.is_a?(Net::HTTPSuccess)
35+
end
36+
37+
def set_errors(hsh)
38+
@errors = hsh.key?('error') ? hsh['error'].with_indifferent_access : hsh
39+
false
40+
end
41+
42+
def set_attributes(hsh)
43+
@attributes = hsh
44+
hsh.each do |key, value|
45+
instance_variable_set("@#{key}", value)
46+
end
47+
end
48+
49+
def set_attributes_or_errors(response, hsh)
50+
save_success?(response) ? set_attributes(hsh) : set_errors(hsh)
51+
end
52+
end
53+
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+
collection 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+

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 = { 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

test/helpers/projects_helper_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,14 @@ class ProjectsHelperTest < ActionView::TestCase
156156
_(project_description_size_breached?(@project)).must_equal false
157157
end
158158
end
159+
160+
describe 'scan_oh_language_mapping' do
161+
it 'should return matching value' do
162+
_(scan_oh_language_mapping('Java')).must_equal 'JAVA'
163+
_(scan_oh_language_mapping('C/C++')).must_equal 'CXX'
164+
_(scan_oh_language_mapping('C#')).must_equal 'CSHARP'
165+
_(scan_oh_language_mapping('JavaScript')).must_equal 'JAVASCRIPT'
166+
_(scan_oh_language_mapping('Ruby')).must_equal 'OTHER'
167+
end
168+
end
159169
end

test/lib/scan_coverity_api_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class ScanCoverityApiTest < ActiveSupport::TestCase
6+
it 'must return true when attributes are valid' do
7+
path = 'api/projects'
8+
data = { name: 'Dummytestdata', repo_url: 'https://github.com/rails/rails',
9+
user_id: 'e1dc08285095f4ff99199c3436532768', language: 'JAVA' }
10+
VCR.use_cassette('scan_projects', match_requests_on: [:path]) do
11+
_(ScanCoverityApi.save(path, data)).wont_be_empty
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)