diff --git a/Gemfile b/Gemfile index 9776f4914..fa62a395a 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,7 @@ group :development do gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' gem 'spring' + gem 'pry-rails' gem 'spring-watcher-listen', '~> 2.0.0' gem "annotate", "~> 2.6.0" end diff --git a/app/assets/javascripts/api/v1/sonarqubes.js b/app/assets/javascripts/api/v1/sonarqubes.js new file mode 100644 index 000000000..dee720fac --- /dev/null +++ b/app/assets/javascripts/api/v1/sonarqubes.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/api/v1/sonarqubes.scss b/app/assets/stylesheets/api/v1/sonarqubes.scss new file mode 100644 index 000000000..8b651fe3a --- /dev/null +++ b/app/assets/stylesheets/api/v1/sonarqubes.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api/v1/sonarqubes controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index 810c40171..d6a90e14a 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -1,5 +1,5 @@ class Api::V1::ProjectsController < Api::V1::BaseController - before_action :require_public_and_member_above, only: [:show, :compare, :blame] + before_action :require_public_and_member_above, only: [:show, :compare, :blame, :sonar_search] def index render_ok @@ -9,6 +9,7 @@ class Api::V1::ProjectsController < Api::V1::BaseController @result_object = Api::V1::Projects::GetService.call(@project, current_user.gitea_token) end + def compare @result_object = Api::V1::Projects::CompareService.call(@project, params[:from], params[:to], current_user&.gitea_token) end diff --git a/app/controllers/api/v1/sonarqubes_controller.rb b/app/controllers/api/v1/sonarqubes_controller.rb new file mode 100644 index 000000000..5fe88ab91 --- /dev/null +++ b/app/controllers/api/v1/sonarqubes_controller.rb @@ -0,0 +1,123 @@ +class Api::V1::SonarqubesController < Api::V1::BaseController + before_action :load_repository + def sonar_initialize + gitea_params = { has_actions: params[:has_actions] == 'true' ? true :false } + gitea_setting = Gitea::Repository::UpdateService.call(@owner, @project.identifier, gitea_params) + if gitea_setting['has_actions'] == true + Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_HOST_URL', Rails.application.config_for(:configuration)['sonarqube']['url'] ).call + Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_TOKEN', Rails.application.config_for(:configuration)['sonarqube']['secret'] ).call + else + Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_HOST_URL', Rails.application.config_for(:configuration)['sonarqube']['url'] ).destroy + Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_TOKEN', Rails.application.config_for(:configuration)['sonarqube']['secret'] ).destroy + end + render_ok + end + + def insert_file + sonar_scanner_content = { + filepath: '.gitea/workflows/SonarScanner.yaml', + branch: params[:branch], + new_branch: nil, + content: 'b246CiAgIyBUcmlnZ2VyIGFuYWx5c2lzIHdoZW4gcHVzaGluZyB0byB5b3VyIG1haW4gYnJhbmNoZXMsIGFuZCB3aGVuIGNyZWF0aW5nIGEgcHVsbCByZXF1ZXN0LgogIHB1c2g6CiAgICBicmFuY2hlczoKICAgICAgLSBtYWluCiAgICAgIC0gbWFzdGVyCiAgICAgIC0gZGV2ZWxvcAogICAgICAtICdyZWxlYXNlcy8qKicKICBwdWxsX3JlcXVlc3Q6CiAgICAgIHR5cGVzOiBbb3BlbmVkLCBzeW5jaHJvbml6ZSwgcmVvcGVuZWRdCgpuYW1lOiBNYWluIFdvcmtmbG93CmpvYnM6CiAgc29uYXJxdWJlOgogICAgcnVucy1vbjogdWJ1bnR1LWxhdGVzdAogICAgc3RlcHM6CiAgICAtIHVzZXM6IGh0dHBzOi8vZ2l0bGluay5vcmcuY24vS2luZ0NoYW4vY2hlY2tvdXRAdjQKICAgICAgd2l0aDoKICAgICAgICAjIERpc2FibGluZyBzaGFsbG93IGNsb25lcyBpcyByZWNvbW1lbmRlZCBmb3IgaW1wcm92aW5nIHRoZSByZWxldmFuY3kgb2YgcmVwb3J0aW5nCiAgICAgICAgZmV0Y2gtZGVwdGg6IDAKICAgIC0gbmFtZTogU29uYXJRdWJlIFNjYW4KICAgICAgdXNlczogaHR0cHM6Ly9naXRsaW5rLm9yZy5jbi9LaW5nQ2hhbi9zb25hcnF1YmUtc2Nhbi1hY3Rpb25AbWFzdGVyCiAgICAgIGVudjoKICAgICAgICBTT05BUl9UT0tFTjogJHt7IHNlY3JldHMuU09OQVJfVE9LRU4gfX0KICAgICAgICBTT05BUl9IT1NUX1VSTDogICR7eyBzZWNyZXRzLlNPTkFSX0hPU1RfVVJMIH19', + message: 'Add .gitea/workflows/SonarScanner.yaml', + committer: { + email: @owner.mail, + name: @owner.login + }, + identifier: @project.identifier + } + @path = GiteaService.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{params[:branch]}/" + sonar_scanner_exit = Repositories::EntriesInteractor.call(@owner, @project.identifier, '.gitea/workflows/SonarScanner.yaml', ref: params[:branch]) + if sonar_scanner_exit.success? + sonar_scanner_content[:content] = Base64.decode64(sonar_scanner_content[:content]) + Gitea::UpdateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_scanner_content.merge(sha:sonar_scanner_exit.result['sha'])) + else + Gitea::CreateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_scanner_content) + end + + sonar_project_content = { + filepath: 'sonar-project.properties', + branch: params[:branch], + new_branch: nil, + "content": "sonar.projectKey=#{params[:owner]}-#{params[:repo]}\nsonar.sources=.", + "message": 'Add sonar-project.properties', + committer: { + email: @owner.mail, + name: @owner.login + }, + identifier: @project.identifier + } + sonar_project_exit = Repositories::EntriesInteractor.call(@owner, @project.identifier, 'sonar-project.properties', ref: params[:branch]) + if sonar_project_exit.success? + Gitea::UpdateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_project_content.merge(sha:sonar_project_exit.result['sha'])) + else + sonar_project_content[:content] = Base64.strict_encode64(sonar_project_content[:content]) + Gitea::CreateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_project_content) + end + render_ok + end + + def issues_search + params_data = { + components: "#{params[:owner]}-#{params[:repo]}", + s: params[:s], + impactSoftwareQualities: params[:impactSoftwareQualities], + issueStatuses: params[:issueStatuses], + ps: params[:ps], + p: params[:p], + facets: params[:facets], + additionalFields: params[:additionalFields], + timeZone: params[:timeZone], + types: params[:types], + impactSeverities: params[:impactSeverities], + tags: params[:tags] + } + data = Sonarqube.client.get('/api/issues/search', query: params_data) + render_ok data + end + + def ce_component + params_data = { + components: "#{params[:owner]}-#{params[:repo]}", + } + data = Sonarqube.client.get('/api/ce/component', query: params_data) + render_ok data + end + + def sources_issue_snippet + params_data = { + issueKey: params[:issueKey] + } + data = Sonarqube.client.get('/api/sources/issue_snippets', query: params_data) + render_ok data + end + + def rules_show + params_data = { + key: params[:key] + } + data = Sonarqube.client.get('/api/rules/show', query: params_data) + render_ok data + end + + def measures_search_history + params_data = { + from: params[:form], + component: "#{params[:owner]}-#{params[:repo]}", + metrics: params[:metrics], + ps: params[:ps] + } + data = Sonarqube.client.get('/api/measures/search_history', query: params_data) + render_ok data + end + + def measures_component + params_data = { + component: "#{params[:owner]}-#{params[:repo]}", + additionalFields: params[:additionalFields], + metricKeys: params[:metricKeys] + } + data = Sonarqube.client.get('/api/measures/component', query: params_data) + render_ok data + end +end diff --git a/app/helpers/api/v1/sonarqubes_helper.rb b/app/helpers/api/v1/sonarqubes_helper.rb new file mode 100644 index 000000000..94205dc10 --- /dev/null +++ b/app/helpers/api/v1/sonarqubes_helper.rb @@ -0,0 +1,2 @@ +module Api::V1::SonarqubesHelper +end diff --git a/app/services/gitea/repository/action_secrets_service.rb b/app/services/gitea/repository/action_secrets_service.rb new file mode 100644 index 000000000..d5e782073 --- /dev/null +++ b/app/services/gitea/repository/action_secrets_service.rb @@ -0,0 +1,33 @@ +class Gitea::Repository::ActionSecretsService < Gitea::ClientService + attr_reader :owner, :repo, :secret_name, :secret + + def initialize(owner, repo, secret_name, secret) + @owner = owner + @repo = repo + @secret_name = secret_name + @secret = secret + end + + def call + response = put(url, request_params) + render_201_response(response) + end + + def destroy + response = delete(url, request_params) + render_201_response(response) + end + + + private + + def request_params + Hash.new.merge(token: owner.gitea_token, data: { data: secret } ) + end + + + + def url + "/repos/#{owner.login}/#{repo}/actions/secrets/#{secret_name}".freeze + end +end diff --git a/app/views/repositories/detail.json.jbuilder b/app/views/repositories/detail.json.jbuilder index 0e5d850a5..4f04e9701 100644 --- a/app/views/repositories/detail.json.jbuilder +++ b/app/views/repositories/detail.json.jbuilder @@ -20,6 +20,7 @@ json.version_releasesed_count @project.releases_size(@user.try(:id), "released") json.permission render_permission(@user, @project) json.mirror_url @project&.repository.remote_mirror_url json.mirror @project&.repository.mirror_url.present? +json.has_actions @project.try(:has_actions) json.web_site @project.page.try(:identifier) json.type @project.numerical_for_project_type json.open_devops @project.open_devops? diff --git a/config/environments/development.rb b/config/environments/development.rb index 1adbdde7c..199481259 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -62,8 +62,8 @@ Rails.application.configure do # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. - config.file_watcher = ActiveSupport::EventedFileUpdateChecker - + config.file_watcher = ActiveSupport::FileUpdateChecker + config.reload_classes_only_on_change = false config.action_controller.perform_caching = true config.action_mailer.delivery_method = :smtp diff --git a/config/initializers/sonarqube.rb b/config/initializers/sonarqube.rb index 99c88c82a..30754885c 100644 --- a/config/initializers/sonarqube.rb +++ b/config/initializers/sonarqube.rb @@ -1,6 +1,8 @@ +sonarqube_config = Rails.application.config_for(:configuration)['sonarqube'] + Sonarqube.configure do |config| - config.endpoint = 'http://172.20.32.202:9999' # API endpoint URL, default: ENV['SONARQUBE_API_ENDPOINT'] - config.private_token = 'squ_fb81f52a7b2c2db00c71c29f71c9595f48c2ff3f' # user's private token, default: ENV['SONARQUBE_API_PRIVATE_TOKEN'] + config.endpoint = sonarqube_config["url"] # API endpoint URL, default: ENV['SONARQUBE_API_ENDPOINT'] + config.private_token = sonarqube_config["secret"] # user's private token, default: ENV['SONARQUBE_API_PRIVATE_TOKEN'] # Optional # config.user_agent = 'Custom User Agent' # user agent, default: 'Sonarqube Ruby Gem [version]' end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 18d47591f..febb11a59 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -48,14 +48,8 @@ defaults format: :json do end end - resources :sonarqubes ,only: [:index] do - collection do - resource :issues - end - end - - scope ':owner' do - resource :users, path: '/', only: [:update, :edit, :destroy] do + scope ':owner' do + resource :users, path: '/', only: [:update, :edit, :destroy] do collection do get :send_email_vefify_code post :check_password @@ -82,6 +76,21 @@ defaults format: :json do collection do get :compare get :blame + + end + end + resource :sonarqubes, only: [:index] do + collection do + get :issues_search + get :ce_component + get :sources_issue_snippet + get :rules_show + get :measures_search_history + get :measures_component + + post :sonar_initialize + post :insert_file + end end diff --git a/spec/controllers/api/v1/sonarqubes_controller_spec.rb b/spec/controllers/api/v1/sonarqubes_controller_spec.rb new file mode 100644 index 000000000..ce71590c4 --- /dev/null +++ b/spec/controllers/api/v1/sonarqubes_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Api::V1::SonarqubesController, type: :controller do + +end diff --git a/spec/helpers/api/v1/sonarqubes_helper_spec.rb b/spec/helpers/api/v1/sonarqubes_helper_spec.rb new file mode 100644 index 000000000..9021add1e --- /dev/null +++ b/spec/helpers/api/v1/sonarqubes_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Api::V1::SonarqubesHelper. For example: +# +# describe Api::V1::SonarqubesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Api::V1::SonarqubesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end