codeql-action icon indicating copy to clipboard operation
codeql-action copied to clipboard

Runs a standalone action along with a configured one

Open akshatgarg12 opened this issue 4 years ago • 6 comments

I have a PR-workflow file in my .github/workflow Whenever a PR is created, a CodeQL action is being run from this config but an additional standalone CodeQL action is also created which fails with a warning

name: PR Workflow
on:
  pull_request:
    branches:
      - '**'

jobs:
  # few other actions here
  CodeQL:
    name: Analyse code with codeQL
    runs-on: ubuntu-latest
    needs: Continuous-Integration
    strategy:
      fail-fast: false
      matrix:
        language: [ 'javascript' ]
    steps:
       - name: Checkout repository
         uses: actions/checkout@v2

       - name: Initialize CodeQL
         uses: github/codeql-action/init@v1
         with:
          languages: ${{ matrix.language }}

       - name: Autobuild
         uses: github/codeql-action/autobuild@v1

       - name: Perform CodeQL Analysis
         uses: github/codeql-action/analyze@v1
       

Error Screenshot 2021-06-07 at 11 45 45 PM Workflow example Screenshot 2021-06-06 at 11 04 20 PM

What changes do i need to make , to stop the last action from running which remains unresolved with the above warning?

akshatgarg12 avatar Jun 07 '21 20:06 akshatgarg12

The check status labelled Code scanning results / CodeQL is not a second Actions run, but is created by the GitHub code scanning service whenever it processes an analysis for your PR. This is by design: it contains a summary of the code scanning alerts for the PR. See https://docs.github.com/en/code-security/secure-coding/automatically-scanning-your-code-for-vulnerabilities-and-errors/triaging-code-scanning-alerts-in-pull-requests for more information.

By default this check status is informational and non-blocking, unless there are error-severity alerts and you've created branch protection rules that require the check. (The severity threshold is customisable, according to the docs linked above).

adityasharad avatar Jun 07 '21 20:06 adityasharad

The warning about the base branch not being found is because your CodeQL analysis workflow has not run on the target branch of the PR (in this case, develop), so Code Scanning cannot perform a comparison to find out which alerts were fixed/introduced by the PR. Again this is not blocking, but if you set up the same workflow to run on your develop branch, then that should make the warning go away.

For example:

on:
  push:
    branches:
      - develop
  pull_request:
    branches:
      - develop

adityasharad avatar Jun 07 '21 20:06 adityasharad

@adityasharad Hey , thanks for the help man. Is there a way to run the CodeQL analysis on the target branch of the PR in all cases. Like here i have specified it to run on all branches at the top level of my yaml file

on:
  pull_request:
    branches:
      - '**'

akshatgarg12 avatar Jun 08 '21 05:06 akshatgarg12

Depends what you are trying to achieve. Do you have PRs against all possible branches of your project? And are your PRs usually from branches of the same repository, or from forks?

Could you narrow down the set of likely PR target branches? Then you could try something like:

on:
  push:
    branches:
      - main
      - other-possible-target-branch
      - ...
  pull_request:
    branches:
      - main
      - other-possible-target-branch
      - ...   

For completeness, I should point out that you can technically do the following to run on every single push and PR from/to any branch, but personally I wouldn't recommend it. Using pull_request events rather than push events for PR branches allows the PR merge commit to be analysed (rather than the base branch) and enables Code Scanning to compute an accurate comparison of the alerts fixed/introduced.

# Possible but not recommended
on:
  push:
    branches:
      - '**'
      - ...
  pull_request:
    branches:
      - '**'   

adityasharad avatar Jun 09 '21 16:06 adityasharad

You can also do:

on:
  push:
    branches:
      - main
      - other-possible-target-branch
      - ... # more branches here, but not all branches
  pull_request:  # no branches filter

This will analyse any PR. But you'll get the best results, without the "analysis not found" warning you asked about earlier, for the PRs whose base branch is analysed by the push events. Hope this helps!

adityasharad avatar Jun 09 '21 16:06 adityasharad

Thanks this was really helpful.

akshatgarg12 avatar Jun 09 '21 17:06 akshatgarg12