Skip to content

Initial commit

Initial commit #19

name: fmt
on:
# NOTE: Need to run on a PR so that the ${{ github.head_ref }} (branch) is non-null
pull_request:
types:
- opened
- synchronize
- reopened
jobs:
fmt-code:
runs-on: ubuntu-latest
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
steps:
- uses: actions/checkout@v4
# Include the pull request ref in the checkout action to prevent merge commit
# https://github.com/actions/checkout?tab=readme-ov-file#checkout-pull-request-head-commit-instead-of-merge-commit
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup git
run: echo ${{ github.head_ref || github.ref_name }}
- name: Create Commit on Branch using GraphQL API
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Define GraphQL mutation
mutation='mutation($input: CreateCommitOnBranchInput!) {
createCommitOnBranch(input: $input) {
commit {
url
}
}
}'
# Define the branch and repository
branch="main" # Target branch
repositoryNameWithOwner="ep-93/house-of-fun" # Combine owner and repository name
# Define the commit message as a key-value object (headline and body)
message_headline="Automated commit"
message_body="This commit was created via the GitHub GraphQL API."
# Convert file content to Base64 encoding
filePath="README.md" # The file you want to modify
fileContent="This is an automated update."
base64FileContent=$(echo -n "$fileContent" | base64)
# Get the latest commit SHA for the branch (expectedHeadOid)
expectedHeadOid=$(curl -s -X POST \
-H "Authorization: bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d '{"query": "query { repository(name: \"house-of-fun\", owner: \"ep-93\") { ref(qualifiedName: \"refs/heads/'$branch'\") { target { ... on Commit { oid } } } } }" }' \
https://api.github.com/graphql | jq -r '.data.repository.ref.target.oid')
# Create the payload for the mutation
payload=$(jq -n \
--arg mutation "$mutation" \
--arg branch "$branch" \
--arg message_headline "$message_headline" \
--arg message_body "$message_body" \
--arg filePath "$filePath" \
--arg base64FileContent "$base64FileContent" \
--arg repositoryNameWithOwner "$repositoryNameWithOwner" \
--arg expectedHeadOid "$expectedHeadOid" \
'{
"query": $mutation,
"variables": {
"input": {
"branch": {
"repositoryNameWithOwner": $repositoryNameWithOwner,
"branchName": $branch
},
"message": {
"headline": $message_headline,
"body": $message_body
},
"fileChanges": {
"additions": [{
"path": $filePath,
"contents": $base64FileContent
}]
},
"expectedHeadOid": $expectedHeadOid
}
}
}'
)
# Make the API call
curl -X POST \
-H "Authorization: bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "$payload" \
https://api.github.com/graphql