This commit is contained in:
kingChan 2024-06-20 10:12:48 +08:00
commit 36d17d37a4
13 changed files with 210 additions and 13 deletions

View File

@ -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

View File

@ -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.

View File

@ -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/

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
module Api::V1::SonarqubesHelper
end

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Api::V1::SonarqubesController, type: :controller do
end

View File

@ -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