diff --git a/Gemfile b/Gemfile index 811fabc41..07fb6b68b 100644 --- a/Gemfile +++ b/Gemfile @@ -26,7 +26,7 @@ gem 'roo-xls' gem 'simple_xlsx_reader', '~>1.0.4' gem 'rubyzip' - +gem 'sonarqube', :git => 'https://gitlink.org.cn/KingChan/sonarqube.git' gem 'spreadsheet' gem 'ruby-ole' # 导出为xlsx @@ -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/Gemfile.lock b/Gemfile.lock index b7f060b2a..fd4d95fa4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,11 @@ +GIT + remote: https://gitlink.org.cn/KingChan/sonarqube.git + revision: 80f07d427322ef02c0714c77a382e87aed0bef81 + specs: + sonarqube (1.3.0) + httparty (~> 0.14, >= 0.14.0) + terminal-table (~> 1.5, >= 1.5.1) + GEM remote: https://mirrors.cloud.tencent.com/rubygems/ specs: @@ -99,6 +107,7 @@ GEM archive-zip (~> 0.10) nokogiri (~> 1.8) chunky_png (1.3.11) + coderay (1.1.3) concurrent-ruby (1.1.6) connection_pool (2.2.2) crass (1.0.6) @@ -135,7 +144,7 @@ GEM fugit (1.4.1) et-orbi (~> 1.1, >= 1.1.8) raabro (~> 1.4) - gitea-client (1.4.2) + gitea-client (1.5.7) rest-client (~> 2.1.0) globalid (0.4.2) activesupport (>= 4.2.0) @@ -150,6 +159,9 @@ GEM http-accept (1.7.0) http-cookie (1.0.5) domain_name (~> 0.5) + httparty (0.21.0) + mini_mime (>= 1.0.0) + multi_xml (>= 0.5.2) i18n (1.8.2) concurrent-ruby (~> 1.0) io-like (0.3.1) @@ -187,9 +199,9 @@ GEM mimemagic (~> 0.3.2) maruku (0.7.3) method_source (0.9.2) - mime-types (3.4.1) + mime-types (3.5.2) mime-types-data (~> 3.2015) - mime-types-data (3.2023.0218.1) + mime-types-data (3.2024.0507) mimemagic (0.3.10) nokogiri (~> 1) rake @@ -244,14 +256,18 @@ GEM popper_js (1.16.0) powerpack (0.1.2) prettier (0.18.2) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.9) + pry (>= 0.10.4) public_suffix (4.0.3) - puma (3.12.2) + puma (5.6.8) + nio4r (~> 2.0) raabro (1.4.0) rack (2.0.9) rack-cors (1.1.1) rack (>= 2.0.0) - rack-mini-profiler (2.0.1) - rack (>= 1.2.0) rack-protection (2.0.8.1) rack rack-test (1.1.0) @@ -438,6 +454,8 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + terminal-table (1.8.0) + unicode-display_width (~> 1.1, >= 1.1.1) thor (1.0.1) thread_safe (0.3.6) tilt (2.0.10) @@ -450,7 +468,7 @@ GEM execjs (>= 0.3.0, < 3) unf (0.1.4) unf_ext - unf_ext (0.0.8.2) + unf_ext (0.0.9.1) unicode-display_width (1.6.1) web-console (3.7.0) actionview (>= 5.0) @@ -492,7 +510,7 @@ DEPENDENCIES enumerize faraday (~> 0.15.4) font-awesome-sass (= 4.7.0) - gitea-client (~> 1.4.2) + gitea-client (~> 1.5.7) grape-entity (~> 0.7.1) groupdate (~> 4.1.0) harmonious_dictionary (~> 0.0.1) @@ -514,9 +532,9 @@ DEPENDENCIES parallel (~> 1.19, >= 1.19.1) pdfkit prettier - puma (~> 3.11) + pry-rails + puma (~> 5.6.5) rack-cors - rack-mini-profiler rails (~> 5.2.0) rails-i18n (~> 5.1) ransack @@ -538,9 +556,10 @@ DEPENDENCIES sidekiq-cron (= 1.2.0) sidekiq-failures simple_form - simple_xlsx_reader + simple_xlsx_reader (~> 1.0.4) sinatra solargraph (~> 0.38.0) + sonarqube! spreadsheet spring spring-watcher-listen (~> 2.0.0) 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 new file mode 100644 index 000000000..30754885c --- /dev/null +++ b/config/initializers/sonarqube.rb @@ -0,0 +1,8 @@ +sonarqube_config = Rails.application.config_for(:configuration)['sonarqube'] + +Sonarqube.configure do |config| + 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 c74b38836..998d699bd 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -48,7 +48,7 @@ defaults format: :json do end end - scope ':owner' do + scope ':owner' do resource :users, path: '/', only: [:update, :edit, :destroy] do collection do get :send_email_vefify_code @@ -76,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