All files / src/code_scanner sonarqube-commands.ts

55% Statements 11/20
0% Branches 0/9
0% Functions 0/9
53.33% Lines 8/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                    3x         3x                                 3x                                 3x                                   3x                                                           3x                                   3x                   3x                                  
/*********************************************************************************************************************
 Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 
 Licensed under the Apache License, Version 2.0 (the "License").
 You may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 ******************************************************************************************************************** */
 
import { SonarCodeScannerProps } from "./sonar-code-scanner";
 
const cube = (path: string, action: string = "GET", args?: string) =>
  `curl -X ${action} -u $SONARQUBE_TOKEN: "$SONARQUBE_ENDPOINT${path}" ${
    args || ""
  }`;
 
const setupSonarqubeQualityGates = (
  defaultProfileOrGateName: string,
  specificProfileOrGateName?: string
) => [
  `export DEFAULT_GATE=\`${cube(
    `/api/qualitygates/search?gateName=${defaultProfileOrGateName}`
  )}\``,
  `export SPECIFIC_GATE=\`${cube(
    `/api/qualitygates/search?gateName=${specificProfileOrGateName}`
  )}\``,
  `if [[ "$(echo $SPECIFIC_GATE | jq .errors)" == "null" && "$(echo $SPECIFIC_GATE | jq '.results | length')" -gt 0 ]]; then export GATE_NAME=${specificProfileOrGateName}; else export GATE_NAME=${defaultProfileOrGateName}; fi`,
  `${cube(
    "/api/qualitygates/select?projectKey=$PROJECT_NAME&gateName=$GATE_NAME",
    "POST"
  )}`,
];
 
const setupSonarqubeQualityProfiles = (
  defaultProfileOrGateName: string,
  specificProfileOrGateName?: string
) => [
  `export DEFAULT_PROFILE=\`${cube(
    `/api/qualityprofiles/search?qualityProfile=${defaultProfileOrGateName}`
  )} | jq .profiles\``,
  `export SPECIFIC_PROFILE=\`${cube(
    `/api/qualityprofiles/search?qualityProfile=${specificProfileOrGateName}`
  )} | jq .profiles\``,
  `export MERGED_PROFILES=\`jq --argjson arr1 "$DEFAULT_PROFILE" --argjson arr2 "$SPECIFIC_PROFILE" -n '$arr1 + $arr2 | group_by(.language) | map(.[-1])'\``,
  `echo $MERGED_PROFILES | jq -c '.[]' | while read i; do ${cube(
    "/api/qualityprofiles/add_project?project=$PROJECT_NAME&language=`echo $i | jq -r .language`&qualityProfile=`echo $i | jq -r .name`",
    "POST"
  )}; done`,
];
 
const setupSonarqubePermissions = (authorizedGroup?: string) =>
  !authorizedGroup
    ? []
    : [
        "admin",
        "codeviewer",
        "issueadmin",
        "securityhotspotadmin",
        "scan",
        "user",
      ].map(
        (p) =>
          `${cube(
            `/api/permissions/add_group?projectKey=$PROJECT_NAME&groupName=${authorizedGroup}&permission=${p}`,
            "POST"
          )}`
      );
 
const setupSonarqubeProject = ({
  sonarqubeTags,
  sonarqubeAuthorizedGroup,
  sonarqubeDefaultProfileOrGateName,
  sonarqubeSpecificProfileOrGateName,
}: SonarCodeScannerProps) => {
  return [
    ...setupSonarqubePermissions(sonarqubeAuthorizedGroup),
    `${cube(
      "/api/project_branches/rename?project=$PROJECT_NAME&name=mainline",
      "POST"
    )}`,
    `${cube(
      `/api/project_tags/set?project=$PROJECT_NAME&tags=${[
        sonarqubeAuthorizedGroup,
        ...(sonarqubeTags || []),
      ].join(",")}`,
      "POST"
    )}`,
    ...setupSonarqubeQualityProfiles(
      sonarqubeDefaultProfileOrGateName,
      sonarqubeSpecificProfileOrGateName
    ),
    ...setupSonarqubeQualityGates(
      sonarqubeDefaultProfileOrGateName,
      sonarqubeSpecificProfileOrGateName
    ),
  ].join(";");
};
 
export const generateSonarqubeReports = () => [
  cube(
    "/api/bitegarden/report/pdf_issues_breakdown?resource=$PROJECT_NAME&branch=mainline",
    "GET",
    "--output reports/prototype-issues-report.pdf"
  ),
  cube(
    "/api/bitegarden/report/pdf?resource=$PROJECT_NAME&branch=mainline",
    "GET",
    "--output reports/prototype-executive-report.pdf"
  ),
  cube(
    "/api/security_reports/download?project=$PROJECT_NAME",
    "GET",
    "--output reports/prototype-security-report.pdf"
  ),
];
 
export const createSonarqubeProject = (props: SonarCodeScannerProps) => [
  `CREATE_PROJECT_OUTPUT=\`${cube(
    "/api/projects/create?name=$PROJECT_NAME&project=$PROJECT_NAME&visibility=private",
    "POST"
  )}\``,
  `if [[ "$(echo $CREATE_PROJECT_OUTPUT | jq .errors)" == "null" ]]; then ${setupSonarqubeProject(
    props
  )}; fi;`,
];
 
export const sonarqubeScanner = (excludeGlobsForScan?: string[]) =>
  [
    "npx sonarqube-scanner -Dsonar.login=$SONARQUBE_TOKEN",
    "-Dsonar.projectKey=$PROJECT_NAME",
    "-Dsonar.projectName=$PROJECT_NAME",
    "-Dsonar.projectVersion=`echo $RESOLVED_SOURCE_VERSION | cut -c1-7`",
    "-Dsonar.branch.name=mainline",
    "-Dsonar.host.url=$SONARQUBE_ENDPOINT",
    "-Dsonar.cfn.nag.reportFiles=reports/cfn-nag-report.json",
    "-Dsonar.dependencyCheck.htmlReportPath=reports/dependency-check-report.html",
    "-Dsonar.javascript.lcov.reportPaths=**/coverage/lcov.info",
    "-Dsonar.clover.reportPath=**/coverage/clover.xml",
    `-Dsonar.exclusions="**/reports/**,**/coverage/**${
      excludeGlobsForScan ? `,${excludeGlobsForScan.join(",")}` : ""
    }"`,
    "-Dsonar.sources=.",
  ].join(" ");