DEV Community

Viacheslav Poturaev
Viacheslav Poturaev

Posted on • Updated on

Changed lines of code by language in GitHub pull request

Keeping pull requests small is generally a good idea, and often checking the number of changed lines is the first step in code review.

Unfortunately, keeping the number of changed lines small is not always feasible. For a few lines of an actual program changes you may need to supply a large body of mocked data, generated code, documentation changes, tests and other resources.

For example you have received a PR for review.
alt text
Looks pretty big at glance.

A breakdown of changes into several groups may help to get a better understanding of the scope of the changes.

alt text

And then you can see that most of the changes in this PR are generated Go code, which may be reviewed with less caution.

Such a comment can be added to a PR using GitHub Action and a small tool built on top of an awesome scc.

name: cloc
on:
  pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  cloc:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: pr
      - name: Checkout base code
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.base.sha }}
          path: base
      - name: Count Lines Of Code
        id: loc
        run: |
          curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
          sccdiff_hash=$(git hash-object ./sccdiff)
          [ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1)
          OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
          echo "${OUTPUT}"
          OUTPUT="${OUTPUT//$'\n'/%0A}"
          echo "::set-output name=diff::$OUTPUT"

      - name: Comment Code Lines
        continue-on-error: true
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          header: LOC
          message: |
            ### Lines Of Code

            ${{ steps.loc.outputs.diff }}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)