|
| 1 | +# This workflow performs a static analysis of your Kotlin source code using |
| 2 | +# Detekt. |
| 3 | +# |
| 4 | +# Scans are triggered: |
| 5 | +# 1. On every push to default and protected branches |
| 6 | +# 2. On every Pull Request targeting the default branch |
| 7 | +# 3. On a weekly schedule |
| 8 | +# 4. Manually, on demand, via the "workflow_dispatch" event |
| 9 | +# |
| 10 | +# The workflow should work with no modifications, but you might like to use a |
| 11 | +# later version of the Detekt CLI by modifing the $DETEKT_RELEASE_TAG |
| 12 | +# environment variable. |
| 13 | +name: Scan with Detekt |
| 14 | + |
| 15 | +on: |
| 16 | + # Triggers the workflow on push or pull request events but only for default and protected branches |
| 17 | + push: |
| 18 | + branches: [ dev, beta, stable ] |
| 19 | + pull_request: |
| 20 | + branches: [ dev ] |
| 21 | + schedule: |
| 22 | + - cron: '19 11 * * 4' |
| 23 | + |
| 24 | + # Allows you to run this workflow manually from the Actions tab |
| 25 | + workflow_dispatch: |
| 26 | + |
| 27 | +env: |
| 28 | + # Release tag associated with version of Detekt to be installed |
| 29 | + # SARIF support (required for this workflow) was introduced in Detekt v1.15.0 |
| 30 | + DETEKT_RELEASE_TAG: v1.15.0 |
| 31 | + |
| 32 | +# A workflow run is made up of one or more jobs that can run sequentially or in parallel |
| 33 | +jobs: |
| 34 | + # This workflow contains a single job called "scan" |
| 35 | + scan: |
| 36 | + name: Scan |
| 37 | + # The type of runner that the job will run on |
| 38 | + runs-on: ubuntu-latest |
| 39 | + |
| 40 | + # Steps represent a sequence of tasks that will be executed as part of the job |
| 41 | + steps: |
| 42 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 43 | + - uses: actions/checkout@v2 |
| 44 | + |
| 45 | + # Gets the download URL associated with the $DETEKT_RELEASE_TAG |
| 46 | + - name: Get Detekt download URL |
| 47 | + id: detekt_info |
| 48 | + env: |
| 49 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 50 | + run: | |
| 51 | + DETEKT_DOWNLOAD_URL=$( gh api graphql --field tagName=$DETEKT_RELEASE_TAG --raw-field query=' |
| 52 | + query getReleaseAssetDownloadUrl($tagName: String!) { |
| 53 | + repository(name: "detekt", owner: "detekt") { |
| 54 | + release(tagName: $tagName) { |
| 55 | + releaseAssets(name: "detekt", first: 1) { |
| 56 | + nodes { |
| 57 | + downloadUrl |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + ' | \ |
| 64 | + jq --raw-output '.data.repository.release.releaseAssets.nodes[0].downloadUrl' ) |
| 65 | + echo "::set-output name=download_url::$DETEKT_DOWNLOAD_URL" |
| 66 | +
|
| 67 | + # Sets up the detekt cli |
| 68 | + - name: Setup Detekt |
| 69 | + run: | |
| 70 | + dest=$( mktemp -d ) |
| 71 | + curl --request GET \ |
| 72 | + --url ${{ steps.detekt_info.outputs.download_url }} \ |
| 73 | + --silent \ |
| 74 | + --location \ |
| 75 | + --output $dest/detekt |
| 76 | + chmod a+x $dest/detekt |
| 77 | + echo $dest >> $GITHUB_PATH |
| 78 | +
|
| 79 | + # Performs static analysis using Detekt |
| 80 | + - name: Run Detekt |
| 81 | + continue-on-error: true |
| 82 | + run: | |
| 83 | + detekt --input ${{ github.workspace }} --report sarif:${{ github.workspace }}/detekt.sarif.json |
| 84 | +
|
| 85 | + # Modifies the SARIF output produced by Detekt so that absolute URIs are relative |
| 86 | + # This is so we can easily map results onto their source files |
| 87 | + # This can be removed once relative URI support lands in Detekt: https://git.io/JLBbA |
| 88 | + - name: Make artifact location URIs relative |
| 89 | + continue-on-error: true |
| 90 | + run: | |
| 91 | + echo "$( |
| 92 | + jq \ |
| 93 | + --arg github_workspace ${{ github.workspace }} \ |
| 94 | + '. | ( .runs[].results[].locations[].physicalLocation.artifactLocation.uri |= if test($github_workspace) then .[($github_workspace | length | . + 1):] else . end )' \ |
| 95 | + ${{ github.workspace }}/detekt.sarif.json |
| 96 | + )" > ${{ github.workspace }}/detekt.sarif.json |
| 97 | +
|
| 98 | + # Uploads results to GitHub repository using the upload-sarif action |
| 99 | + - uses: github/codeql-action/upload-sarif@v1 |
| 100 | + with: |
| 101 | + # Path to SARIF file relative to the root of the repository |
| 102 | + sarif_file: ${{ github.workspace }}/detekt.sarif.json |
| 103 | + checkout_path: ${{ github.workspace }} |
0 commit comments