Using StackQL to find changed files in a commmit using GitHub Actions
· 2 min read
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 ofadded,removed,modified,renamed,copied,changedorunchangedfilename- filename which has changedprevious_filename- previous filename if the filename had changed in the commitadditions- the number of additions in each filechanges- the number of changes to each filedeletions- the number of deletions in each filepatch-git diffoutput for each fileblob_url- the blob url for the fileraw_url- the raw url for the filecontents_url- the contents url for the filesha- The sha for each individual file