Skip to main content

Using StackQL to find changed files in a commmit using GitHub Actions

· 2 min read
SRE at Sitemate and Cloud Enthusiast
info

stackql is a dev tool that allows you to query and manage cloud and SaaS resources using SQL, which developers and analysts can use for CSPM, assurance, user access management reporting, IaC, XOps and more.

For more background on using StackQL with GitHub Actions see StackQL GitHub Actions Tutorial

- name: setup StackQL
uses: stackql/setup-stackql@v1.1.0
with:
use_wrapper: true

- name: get changed files
env:
STACKQL_GITHUB_USERNAME: ${{ secrets.STACKQL_GITHUB_USERNAME }}
STACKQL_GITHUB_PASSWORD: ${{ secrets.STACKQL_GITHUB_PASSWORD }}
shell: bash
run: |
ORG=$(echo "$GITHUB_REPOSITORY" | cut -d '/' -f1)
REPO=$(echo "$GITHUB_REPOSITORY" | cut -d '/' -f2)
QUERY="WITH changed_files AS (SELECT json_each.value AS file FROM github.repos.commits, JSON_EACH(files) WHERE owner = '${ORG}' AND repo = '${REPO}' AND ref = '${GITHUB_SHA}') SELECT JSON_EXTRACT(file, '$.filename') AS filename FROM changed_files"
echo "pulling github provider"
stackql exec "REGISTRY PULL github"
echo "running query: ${QUERY}"
stackql --output json -f changed_files.txt exec "${QUERY}"

changed_files.txt looks like this...

[{"filename":"src/app.ts"},{"filename":"src/mod.ts"},...]

You could then do something with the changed files in a further step like:

- name: Do something with changed files
run: |
while IFS="" read -r filename || [ -n "$filename" ]
do
echo "processing ${filename}..."
#
# do something interesting here...
#
done < <(jq -r '.[] | .filename' changed_files.txt)

Each element of the files array returned by github.repos.commits has other interesting fields which could be projected (via JSON_EXTRACT) and used for actioning or reporting. You can inspect the files structure using:

DESCRIBE EXTENDED github.repos.commits;

Fields available within each files array element include:

  • status - one of added, removed, modified, renamed, copied, changed or unchanged
  • filename - filename which has changed
  • previous_filename - previous filename if the filename had changed in the commit
  • additions - the number of additions in each file
  • changes - the number of changes to each file
  • deletions - the number of deletions in each file
  • patch - git diff output for each file
  • blob_url - the blob url for the file
  • raw_url - the raw url for the file
  • contents_url - the contents url for the file
  • sha - The sha for each individual file

Read More

Check out a full demo on using StackQL with GitHub Actions

Check the GitHub Repos