From bd759a0be89606ae3ed844c91d9a7a56ed472e4a Mon Sep 17 00:00:00 2001 From: vinodbhorge Date: Wed, 4 Jun 2025 19:02:48 +0530 Subject: [PATCH 1/2] Added pr-check workflow. --- .github/workflows/content-pr-check.yml | 100 +++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 .github/workflows/content-pr-check.yml diff --git a/.github/workflows/content-pr-check.yml b/.github/workflows/content-pr-check.yml new file mode 100644 index 000000000..d80b48eb9 --- /dev/null +++ b/.github/workflows/content-pr-check.yml @@ -0,0 +1,100 @@ +name: Pull Request Checks + +on: + pull_request: + branches: + - '*' + +jobs: + test-and-quality: + runs-on: ubuntu-latest + env: + CLOUD_STORE_GROUP_ID: ${{ vars.CLOUD_STORE_GROUP_ID }} + CLOUD_STORE_ARTIFACT_ID: ${{ vars.CLOUD_STORE_ARTIFACT_ID }} + CLOUD_STORE_VERSION: ${{ vars.CLOUD_STORE_VERSION }} + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # Important for SonarQube to get full history + + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + cache: 'maven' + + - name: Cache Maven packages + uses: actions/cache@v3 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + + - name: Build and Run Tests + run: | + mvn clean install -DskipTests \ + -DCLOUD_STORE_GROUP_ID=${CLOUD_STORE_GROUP_ID} \ + -DCLOUD_STORE_ARTIFACT_ID=${CLOUD_STORE_ARTIFACT_ID} \ + -DCLOUD_STORE_VERSION=${CLOUD_STORE_VERSION} + cd content-api/content-service/ + mvn clean test org.jacoco:jacoco-maven-plugin:0.8.8:prepare-agent test org.jacoco:jacoco-maven-plugin:0.8.8:report \ + -DCLOUD_STORE_GROUP_ID=${CLOUD_STORE_GROUP_ID} \ + -DCLOUD_STORE_ARTIFACT_ID=${CLOUD_STORE_ARTIFACT_ID} \ + -DCLOUD_STORE_VERSION=${CLOUD_STORE_VERSION} + + - name: Upload Test Results + if: always() + uses: actions/upload-artifact@v4 + with: + name: test-results + path: 'content-api/content-service/target/surefire-reports/*.xml' + + - name: Publish Test Results + if: always() + uses: dorny/test-reporter@v1 + with: + name: Test Results + path: content-api/content-service/target/surefire-reports/*.xml + reporter: java-junit + fail-on-error: true + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'temurin' + + - name: SonarCloud Analysis + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + working-directory: content-api/content-service + run: | + mvn sonar:sonar \ + -DCLOUD_STORE_GROUP_ID=${CLOUD_STORE_GROUP_ID} \ + -DCLOUD_STORE_ARTIFACT_ID=${CLOUD_STORE_ARTIFACT_ID} \ + -DCLOUD_STORE_VERSION=${CLOUD_STORE_VERSION} \ + -Dsonar.projectKey=vinodbhorge_knowledge-platform \ + -Dsonar.organization=vinodbbhorge \ + -Dsonar.host.url=https://sonarcloud.io \ + -Dsonar.coverage.jacoco.xmlReportPaths=content-api/content-service/target/site/jacoco/jacoco.xml \ + -Dsonar.login=${SONAR_TOKEN} \ + -Dsonar.autoconfig.disabled=true + + + - name: Comment PR with SonarQube Results + uses: actions/github-script@v6 + if: github.event_name == 'pull_request' && always() + with: + script: | + const sonarUrl = `https://sonarcloud.io/dashboard?id=vinodbhorge_knowledge-platform`; + const message = `### Quality Gate Results + Check the detailed SonarQube analysis at: ${sonarUrl}`; + github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: message + }); \ No newline at end of file From e49815777ddb665c9aaa39233b3ebb0f21ca85c6 Mon Sep 17 00:00:00 2001 From: vinodbhorge Date: Wed, 4 Jun 2025 19:15:59 +0530 Subject: [PATCH 2/2] Added manual test --- .../content/service/impl/SampleService.java | 12 ++++++++ .../service/impl/SampleServiceTest.java | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 content-api/content-service/src/main/java/org/sunbird/content/service/impl/SampleService.java create mode 100644 content-api/content-service/src/test/java/org/sunbird/content/service/impl/SampleServiceTest.java diff --git a/content-api/content-service/src/main/java/org/sunbird/content/service/impl/SampleService.java b/content-api/content-service/src/main/java/org/sunbird/content/service/impl/SampleService.java new file mode 100644 index 000000000..e94e3ac0b --- /dev/null +++ b/content-api/content-service/src/main/java/org/sunbird/content/service/impl/SampleService.java @@ -0,0 +1,12 @@ +package org.sunbird.content.service.impl; + +public class SampleService { + + public int add(int a, int b) { + return a + b; + } + + public String getMessage() { + return "Hello World"; + } +} \ No newline at end of file diff --git a/content-api/content-service/src/test/java/org/sunbird/content/service/impl/SampleServiceTest.java b/content-api/content-service/src/test/java/org/sunbird/content/service/impl/SampleServiceTest.java new file mode 100644 index 000000000..6805ed591 --- /dev/null +++ b/content-api/content-service/src/test/java/org/sunbird/content/service/impl/SampleServiceTest.java @@ -0,0 +1,28 @@ +package org.sunbird.content.service.impl; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class SampleServiceTest { + + private final SampleService sampleService = new SampleService(); + + @Test + public void testAdd() { + assertEquals(4, sampleService.add(2, 2)); + } + + @Test + public void testGetMessage() { + String message = sampleService.getMessage(); + assertNotNull(message); + assertEquals("Hello World", message); + } + + @Test + public void testDeliberatelyFailing() { + // This test will fail to verify the workflow's behavior with failing tests + assertEquals("Expected message", sampleService.getMessage()); + // This assertion will fail because getMessage() returns "Hello World" + } +} \ No newline at end of file