Skip to content

Instantly share code, notes, and snippets.

@alexandrebrg
Last active August 7, 2023 23:42
Show Gist options
  • Select an option

  • Save alexandrebrg/f19c526df9c0331a3101b8a27fde9dce to your computer and use it in GitHub Desktop.

Select an option

Save alexandrebrg/f19c526df9c0331a3101b8a27fde9dce to your computer and use it in GitHub Desktop.

Revisions

  1. alexandrebrg revised this gist Aug 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ci.yml
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ jobs:
    pull-requests: read
    steps:
    - uses: actions/checkout@v3
    name: Checkout Conduktor Charts
    name: Checkout Repository
    with:
    ref: ${{ github.event.pull_request.head.ref }}
    token: ${{ secrets.GITHUB_TOKEN }}
  2. alexandrebrg created this gist Aug 7, 2023.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # Determine files changed in Pull Request

    # Determine directories changed in Pull Request

    # Determine which Helm Charts have been changed in Pull Request
    44 changes: 44 additions & 0 deletions ci.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # Github Workflow
    name: 'Read diff'

    on:
    pull_request_target:
    branches:
    - main
    paths:
    - 'some_folder/**/*.yaml'

    # For security purposes, it preferred to lower permissions to the minimal, always, and then add job by job
    permissions: {}

    jobs:
    read-diff:
    runs-on: ubuntu-latest
    permissions:
    pull-requests: read
    steps:
    - uses: actions/checkout@v3
    name: Checkout Conduktor Charts
    with:
    ref: ${{ github.event.pull_request.head.ref }}
    token: ${{ secrets.GITHUB_TOKEN }}

    - name: Execute read-diff
    env:
    # Needed for Github CLI to work
    GH_TOKEN: ${{ github.token }}
    TEMP_FILE: "${{runner.temp}}/pr-${{github.event.number}}.diff"
    run: |
    export PR_ID=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
    # Gives a GIT Diff file
    # check: https://git-scm.com/docs/git-diff#_combined_diff_format
    gh pr diff ${PR_ID} > $TEMP_FILE
    # We filter only names of files
    files_changed="$(sed -nr 's/[\-\+]{3} [ab]\/(.*)/\1/p' $TEMP_FILE | sort | uniq)"
    # If you want to get all files changed, you can use variable $files_changed
    # but if you want to get upper directories, use customize and use $some_specific_dir_changed
    # Adding || true to avoid "Process exited with code 1" errors
    some_specific_dir_changed="$(echo "$files_changed" | xargs dirname | grep -o "templates/[^/]*" | sort | uniq || true)"
    for dir in ${some_specific_dir_changed}; do
    echo "Detected $dir changed"
    done