diff --git a/.github/workflows/build_jar.yml b/.github/workflows/build_jar.yml index 7d44073..ff6740f 100644 --- a/.github/workflows/build_jar.yml +++ b/.github/workflows/build_jar.yml @@ -1,9 +1,15 @@ -name: Build JAR on Release +name: Build JAR on: release: types: [published] # Solo al publicar una release - workflow_dispatch: + pull_request: + branches: + - '**' + push: + branches: + - '**' + workflow_dispatch: jobs: build: @@ -52,12 +58,24 @@ jobs: - name: Rename artifact using release tag (or manual) run: | - TAG="${{ github.event.release.tag_name || 'manual' }}" - OUT="sonar-report-${TAG}.jar" + if [ "${{ github.event_name }}" == "release" ]; then + TAG="${{ github.event.release.tag_name }}" + OUT="sonar-report-${TAG}.jar" + elif [ "${{ github.event_name }}" == "pull_request" ]; then + PR_NUM="${{ github.event.pull_request.number }}" + SHORT_SHA="${GITHUB_SHA:0:7}" + OUT="sonar-report-pr${PR_NUM}-${SHORT_SHA}.jar" + else + # push or workflow_dispatch + BRANCH="${GITHUB_REF_NAME}" + SHORT_SHA="${GITHUB_SHA:0:7}" + SAFE_BRANCH=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9_-]/_/g') + OUT="sonar-report-${SAFE_BRANCH}-${SHORT_SHA}.jar" + fi cp "$ARTIFACT_PATH" "$OUT" echo "OUT=${OUT}" >> "$GITHUB_ENV" - - name: Upload workflow artifact (optional) + - name: Upload workflow artifact uses: actions/upload-artifact@v4 with: name: ${{ env.OUT }} diff --git a/pdf-generator/src/main/java/com/bi/GenerateSonarReport.java b/pdf-generator/src/main/java/com/bi/GenerateSonarReport.java index b7af4bb..7940e61 100644 --- a/pdf-generator/src/main/java/com/bi/GenerateSonarReport.java +++ b/pdf-generator/src/main/java/com/bi/GenerateSonarReport.java @@ -600,24 +600,33 @@ public static void main(String[] args) throws IOException { String currentLocations = existing.getString("location"); String file = originalObj.getString("component"); file = file.contains(":") ? file.split(":", 2)[1].trim() : file; - JSONObject textLines = originalObj.getJSONObject("textRange"); - String textLine = Integer.toString(textLines.getInt("startLine")); - existing.put("location", currentLocations + " | " + file + ": " + textLine); - + // Check if the issue is in a line of text or is the file itself + if (originalObj.has("textRange")) { + JSONObject textLines = originalObj.getJSONObject("textRange"); + String textLine = Integer.toString(textLines.getInt("startLine")); + existing.put("location", currentLocations + " | " + file + ": " + textLine); + } + else existing.put("location", currentLocations + " | " + file); } else { // Is new JSONObject newObj = new JSONObject(); - JSONObject textLines = originalObj.getJSONObject("textRange"); + + String file = originalObj.getString("component"); file = file.contains(":") ? file.split(":", 2)[1].trim() : file; - String textLine = Integer.toString(textLines.getInt("startLine")); + newObj.put("ruleKey", ruleKey); newObj.put("count", 1); newObj.put("severity", originalObj.getString("severity")); newObj.put("message", originalObj.getString("message")); newObj.put("type", originalObj.getString("type")); - newObj.put("location", file + ": " + textLine); - + // Check if the issue is in a line of text or is the file itself + if (originalObj.has("textRange")) { + JSONObject textLines = originalObj.getJSONObject("textRange"); + String textLine = Integer.toString(textLines.getInt("startLine")); + newObj.put("location", file + ": " + textLine); + } + else newObj.put("location", file); issuesMap.put(ruleKey, newObj); } } @@ -636,7 +645,7 @@ public static void main(String[] args) throws IOException { } pdf.insertIndexAtBeginning(); pdf.addCoverPage("SonarQube Report", "Generated for "+ project); - pdf.save("reportes.pdf"); + pdf.save("sonarqube-report.pdf"); } private static String minsToDaysHoursMins(int minutes) { @@ -652,10 +661,11 @@ private String getCountAsString(JSONArray jsonArray, int index) { try { return String.valueOf(jsonArray.getJSONObject(index).getString("count")); } catch (JSONException e) { - System.err.println("Error al obtener 'count' en el índice " + index + ": " + e.getMessage()); + System.err.println("Error at obtaning 'count' in the index " + index + ": " + e.getMessage()); return ""; } } return ""; } -} \ No newline at end of file + +}