SonarAnalyze #58
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from https://community.sonarsource.com/t/how-to-use-sonarcloud-with-a-forked-repository-on-github/7363/32 | |
# NOTE: In order for changes to this file to take effect, it must be changed on the master branch | |
name: SonarAnalyze | |
on: | |
workflow_run: | |
workflows: [build] | |
types: [completed] | |
jobs: | |
SonarAnalyze: | |
runs-on: ubuntu-latest | |
if: github.event.workflow_run.conclusion == 'success' | |
steps: | |
# Checkout the PR by the contents of PR_NUMBER.txt | |
- name: echo event | |
run: cat $GITHUB_EVENT_PATH | |
- name: Download PR number artifact | |
if: github.event.workflow_run.event == 'pull_request' | |
uses: dawidd6/action-download-artifact@v2 | |
with: | |
workflow: Java CI | |
run_id: ${{ github.event.workflow_run.id }} | |
name: PR_NUMBER | |
- name: Read PR_NUMBER.txt | |
if: github.event.workflow_run.event == 'pull_request' | |
id: pr_number | |
uses: juliangruber/read-file-action@v1 | |
with: | |
path: ./PR_NUMBER.txt | |
- name: Request GitHub API for PR data | |
if: github.event.workflow_run.event == 'pull_request' | |
uses: octokit/[email protected] | |
id: get_pr_data | |
with: | |
route: GET /repos/{full_name}/pulls/{number} | |
number: ${{ steps.pr_number.outputs.content }} | |
full_name: ${{ github.event.repository.full_name }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- uses: actions/checkout@v3 | |
with: | |
repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
ref: ${{ github.event.workflow_run.head_branch }} | |
fetch-depth: 0 | |
- name: Checkout base branch | |
if: github.event.workflow_run.event == 'pull_request' | |
run: | | |
git remote add upstream ${{ github.event.repository.clone_url }} | |
git fetch upstream | |
git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} | |
git checkout ${{ github.event.workflow_run.head_branch }} | |
git clean -ffdx && git reset --hard HEAD | |
# Set up | |
# Credit to https://www.ackama.com/what-we-think/values-github-actions/ for helping me figure these two steps out :P | |
- name: Set SonarCloud branch | |
run: | | |
branchName=${{ github.event.workflow_run.head_branch }} | |
echo "Branch is named $branchName" | |
echo "sonar.branch.name=$branchName" >> $GITHUB_ENV | |
- name: Set SonarCloud target | |
if: github.event.workflow_run.event == 'pull_request' | |
run: | | |
branchTarget=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} | |
echo "PR targets $branchTarget" | |
echo "sonar.branch.target=$branchTarget" >> $GITHUB_ENV | |
- name: Setup JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
distribution: temurin | |
java-version: 17 | |
cache: maven | |
- name: Cache SonarCloud packages | |
uses: actions/cache@v1 | |
with: | |
path: ~/.sonar/cache | |
key: ${{ runner.os }}-sonar | |
restore-keys: ${{ runner.os }}-sonar | |
- name: Cache Maven packages | |
uses: actions/cache@v1 | |
with: | |
path: ~/.m2 | |
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} | |
restore-keys: ${{ runner.os }}-m2 | |
# Apply SonarCloud | |
- name: SonarCloud Analyze | |
run: mvn -B test-compile org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=JorelAli_CommandAPI -P Platform.Bukkit,Platform.Velocity -Dmaven.javadoc.skip=true | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |