Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Merge branch 'master' into clear-widget-of-deleted-project
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidel Coria committed Dec 3, 2019
2 parents e83c276 + 10cce08 commit 5a08404
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 141 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Hygieia/hygieia-codequality-sonar-collector
18 changes: 18 additions & 0 deletions .github/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 60
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- bug
- enhancement
- discussion
# Label to use when marking an issue as stale
staleLabel: wontfix
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
18 changes: 18 additions & 0 deletions .github/workflows/maven-pr-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Java Compile and Test

on:
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn test --file pom.xml
19 changes: 19 additions & 0 deletions .github/workflows/maven-snapshot-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release Snapshot

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Release to snapshots repository
run: mvn deploy -q --file pom.xml --settings src/devops/.travis.settings.xml -Denv.OSSRH_USERNAME=${{ secrets.OSSRH_USERNAME }} -Denv.OSSRH_PASSWORD=${{ secrets.OSSRH_PASSWORD }}
15 changes: 15 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Path to sources
#sonar.sources=.
#sonar.exclusions=
#sonar.inclusions=

# Path to tests
#sonar.tests=
#sonar.test.exclusions=
#sonar.test.inclusions=

# Source encoding
#sonar.sourceEncoding=UTF-8

# Exclusions for copy-paste detection
#sonar.cpd.exclusions=
16 changes: 11 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: java
jdk:
- openjdk8
services:
- docker

branches:
only:
- master
- gerrit-integration
- "/^v[0-9]+\\.[0-9]+\\.[0-9]+.*$/"

install: true
Expand All @@ -14,9 +14,15 @@ before_script:
- sudo chown -R $USER:$GROUP $TRAVIS_BUILD_DIR

script:
- if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then mvn clean install -q; fi
- if [ "$TRAVIS_PULL_REQUEST" = "false" ] && [ "$TRAVIS_BRANCH" = "master" ]; then cp ./src/devops/deploy-snapshot.sh . && ./deploy-snapshot.sh; fi
- if [ "$TRAVIS_PULL_REQUEST" = "false" ] && [[ "$TRAVIS_BRANCH" =~ ^v[0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then cp ./src/devops/release.sh . && ./release.sh; fi
- export TAG=$TRAVIS_BRANCH
- export IMAGE_NAME='hygieiadoc/sonarcodequalitycollector'
- echo $IMAGE_NAME
- echo $TAG
- docker build -t $IMAGE_NAME .
- docker tag $IMAGE_NAME $IMAGE_NAME:$TAG
- docker login -u $DOCKERHUB_USER -p $DOCKERHUB_PASS
- docker push $IMAGE_NAME

notifications:
webhooks:
Expand All @@ -30,4 +36,4 @@ notifications:
- [email protected]
- [email protected]
on_success: always
on_failure: always
on_failure: always
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ENV PROP_FILE /hygieia/config/application.properties

WORKDIR /hygieia

COPY target/*.jar /hygieia
COPY target/*.jar /hygieia/
COPY docker/properties-builder.sh /hygieia/

CMD ./properties-builder.sh &&\
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
## Hygieia Collector to collect static code analysis data from Sonar
[![Build Status](https://travis-ci.com/Hygieia/hygieia-codequality-sonar-collector.svg?branch=master)](https://travis-ci.com/Hygieia/hygieia-codequality-sonar-collector)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Hygieia_hygieia-codequality-sonar-collector&metric=alert_status)](https://sonarcloud.io/dashboard?id=Hygieia_hygieia-codequality-sonar-collector)
[![Total alerts](https://img.shields.io/lgtm/alerts/g/Hygieia/hygieia-codequality-sonar-collector.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Hygieia/hygieia-codequality-sonar-collector/alerts/)
[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/Hygieia/hygieia-codequality-sonar-collector.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Hygieia/hygieia-codequality-sonar-collector/context:java)
[![Maven Central](https://img.shields.io/maven-central/v/com.capitalone.dashboard/sonar-codequality-collector.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.capitalone.dashboard%22%20AND%20a:%22sonar-codequality-collector%22)
[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://www.apache.org/licenses/LICENSE-2.0)
<br/>
<br/>

The README is in the [gh-pages](https://github.com/capitalone/Hygieia/blob/gh-pages/pages/hygieia/collectors/build/sonar.md) branch. Please update it there.
19 changes: 0 additions & 19 deletions docker/Dockerfile

This file was deleted.

5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<description>CodeQuality Collector Microservices currently collects data from Sonar</description>
<url>https://hygieia.github.io/Hygieia/getting_started.html</url>
<packaging>jar</packaging>
<version>3.1.1-SNAPSHOT</version>
<version>3.1.3-SNAPSHOT</version>

<parent>
<groupId>com.capitalone.dashboard</groupId>
Expand Down Expand Up @@ -46,7 +46,7 @@
<properties>
<apache.rat.plugin.version>0.13</apache.rat.plugin.version>
<bc.version>3.0.1</bc.version>
<com.capitalone.dashboard.core.version>3.1.2</com.capitalone.dashboard.core.version>
<com.capitalone.dashboard.core.version>3.1.11</com.capitalone.dashboard.core.version>
<commons.io.version>2.4</commons.io.version>
<commons.lang.version>3.8.1</commons.lang.version>
<coveralls.maven.plugin.version>4.3.0</coveralls.maven.plugin.version>
Expand Down Expand Up @@ -199,6 +199,7 @@

<build>
<defaultGoal>clean verify apache-rat:check japicmp:cmp checkstyle:check spotbugs:check javadoc:javadoc</defaultGoal>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down
Binary file modified src/devops/keys.gpg.enc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/devops/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

cp src/devops/.travis.settings.xml $HOME/.m2/settings.xml

openssl aes-256-cbc -K $encrypted_8b6a4661ac23_key -iv $encrypted_8b6a4661ac23_iv -in src/devops/keys.gpg.enc -out keys.gpg -d
openssl aes-256-cbc -K $encrypted_64259960f58b_key -iv $encrypted_64259960f58b_iv -in src/devops/keys.gpg.enc -out keys.gpg -d

gpg --fast-import keys.gpg

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.capitalone.dashboard.collector;

import com.capitalone.dashboard.client.RestClient;
import com.capitalone.dashboard.model.SonarProject;
import com.capitalone.dashboard.util.Supplier;
import org.apache.commons.logging.Log;
Expand All @@ -21,12 +22,12 @@ public class DefaultSonar56Client extends DefaultSonarClient {
private static final String URL_PROJECTS = "/api/projects?format=json";

@Autowired
public DefaultSonar56Client(Supplier<RestOperations> restOperationsSupplier, SonarSettings settings) {
super(restOperationsSupplier,settings);
public DefaultSonar56Client(RestClient restClient, SonarSettings settings) {
super(restClient,settings);
}

@Override
public List<SonarProject> getProjects(String instanceUrl) {
public List<SonarProject> getProjects(String instanceUrl,String token) {
List<SonarProject> projects = new ArrayList<>();
String url = instanceUrl + URL_PROJECTS;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.capitalone.dashboard.collector;

import com.capitalone.dashboard.client.RestClient;
import com.capitalone.dashboard.client.RestUserInfo;
import com.capitalone.dashboard.model.CodeQuality;
import com.capitalone.dashboard.model.CodeQualityMetric;
import com.capitalone.dashboard.model.CodeQualityMetricStatus;
import com.capitalone.dashboard.model.CodeQualityType;
import com.capitalone.dashboard.model.SonarProject;
import com.capitalone.dashboard.model.UserInfo;
import com.capitalone.dashboard.util.SonarDashboardUrl;
import com.capitalone.dashboard.util.Supplier;
import org.apache.commons.codec.binary.Base64;
Expand All @@ -30,12 +33,13 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class DefaultSonar6Client implements SonarClient {
private static final Log LOG = LogFactory.getLog(DefaultSonar6Client.class);

private static final String URL_RESOURCES = "/api/components/search?qualifiers=TRK&ps=500";
private static final String URL_RESOURCES_AUTHENTICATED = "/api/projects/search?ps=500";
private static final String URL_RESOURCE_DETAILS = "/api/measures/component?format=json&componentId=%s&metricKeys=%s&includealerts=true";
private static final String URL_PROJECT_ANALYSES = "/api/project_analyses/search?project=%s";
private static final String URL_QUALITY_PROFILES = "/api/qualityprofiles/search";
Expand All @@ -56,8 +60,8 @@ public class DefaultSonar6Client implements SonarClient {
private static final String DATE = "date";
private static final String EVENTS = "events";

private final RestOperations rest;
private final HttpEntity<String> httpHeaders;
private final RestClient restClient;
private final RestUserInfo userInfo;

private static final String MINUTES_FORMAT = "%smin";
private static final String HOURS_FORMAT = "%sh";
Expand All @@ -66,21 +70,30 @@ public class DefaultSonar6Client implements SonarClient {
private static final int PAGE_SIZE=500;

@Autowired
public DefaultSonar6Client(Supplier<RestOperations> restOperationsSupplier, SonarSettings settings) {
this.httpHeaders = new HttpEntity<>(
this.createHeaders(settings.getUsername(), settings.getPassword())
);
this.rest = restOperationsSupplier.get();
public DefaultSonar6Client(RestClient restClient, SonarSettings settings) {
if(StringUtils.isEmpty(settings.getUsername())|| StringUtils.isEmpty(settings.getPassword())){
this.userInfo= new RestUserInfo("","");
}else{
this.userInfo = new RestUserInfo(settings.getUsername(),settings.getPassword());
}

this.restClient = restClient;
}

@Override
public List<SonarProject> getProjects(String instanceUrl) {
public List<SonarProject> getProjects(String instanceUrl,String token) {
List<SonarProject> projects = new ArrayList<>();
String url = instanceUrl + URL_RESOURCES;
String url = "";
// take authenticated route
if(Objects.nonNull(token)){
url = instanceUrl + URL_RESOURCES_AUTHENTICATED;
userInfo.setToken(token);
}else{
url = instanceUrl + URL_RESOURCES;
}

try {
String key = "components";
JSONArray jsonArray = getProjects(url, key);
JSONArray jsonArray = getProjects(url);
for (Object obj : jsonArray) {
JSONObject prjData = (JSONObject) obj;

Expand All @@ -100,7 +113,8 @@ public List<SonarProject> getProjects(String instanceUrl) {
return projects;
}

private JSONArray getProjects(String url, String key) throws ParseException {
private JSONArray getProjects(String url) throws ParseException {
String key = "components";
Long totalRecords = getTotalCount(parseJsonObject(url, "paging"));
int pages = (int) Math.ceil((double)totalRecords / PAGE_SIZE);
JSONArray jsonArray = new JSONArray();
Expand All @@ -114,14 +128,24 @@ private JSONArray getProjects(String url, String key, JSONArray jsonArray) throw
}

private JSONArray getProjects(String url, String key, int pages, JSONArray jsonArray) throws ParseException {
if(Objects.isNull(userInfo.getToken())){
pagingUnAuthenticated(url, key, pages, jsonArray);
}else{
for (int start=1;start<=pages;start++){
getProjects(url, key, jsonArray, start);
}
}
return jsonArray;
}

private void pagingUnAuthenticated(String url, String key, int pages, JSONArray jsonArray) throws ParseException {
int maxPages = 20;
if(pages <= maxPages) {
maxPages = pages;
}
for (int start=1;start<=maxPages;start++){
for (int start=1;start<=maxPages;start++){
getProjects(url, key, jsonArray, start);
}
return jsonArray;
}

private void getProjects(String url, String key, JSONArray jsonArray, int pageNumber) throws ParseException {
Expand All @@ -135,7 +159,7 @@ public CodeQuality currentCodeQuality(SonarProject project, String metrics) {
project.getInstanceUrl() + URL_RESOURCE_DETAILS, project.getProjectId(), metrics);

try {
ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET, this.httpHeaders, String.class);
ResponseEntity<String> response = restClient.makeRestCallGet(url,setHeaders(userInfo) );
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(response.getBody());
String key = "component";
Expand Down Expand Up @@ -258,7 +282,7 @@ private JSONObject parseJsonObject(String url, String key) throws ParseException
}

private JSONObject getResponse(String url) throws ParseException {
ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET, this.httpHeaders, String.class);
ResponseEntity<String> response = restClient.makeRestCallGet(url, setHeaders(userInfo));
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(response.getBody());
LOG.debug(url);
Expand Down Expand Up @@ -366,6 +390,10 @@ private CodeQualityMetricStatus metricStatus(String status) {
}
}

private Long getTotalCount(JSONObject pagingObject) {
return (Long) pagingObject.get("total");
}

private HttpHeaders createHeaders(String username, String password){
HttpHeaders headers = new HttpHeaders();
if (username != null && !username.isEmpty()) {
Expand All @@ -379,8 +407,27 @@ private HttpHeaders createHeaders(String username, String password){
return headers;
}

private Long getTotalCount(JSONObject pagingObject) {
return (Long) pagingObject.get("total");
private HttpHeaders createHeaders(String token) {
String auth = token.trim();
auth = auth+":";
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII"))
);
String authHeader = "Basic " + new String(encodedAuth);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", authHeader);
return headers;
}

private HttpHeaders setHeaders(RestUserInfo userInfo){
if(Objects.isNull(userInfo)) return null;
if(StringUtils.isNotBlank(userInfo.getUserId())&& StringUtils.isNotBlank(userInfo.getPassCode())){
return createHeaders(userInfo.getUserId(),userInfo.getPassCode());
}else if(StringUtils.isNotBlank(userInfo.getToken())){
return createHeaders(userInfo.getToken());
}
return null;
}


}
Loading

0 comments on commit 5a08404

Please sign in to comment.