Initial commit #14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="feature/signed-commits-testing" # Target branch | |
repositoryNameWithOwner="ep-93/house-of-fun" # Combine owner and repository name | |
# Define the commit message as a key-value object | |
message=$(jq -n --arg headline "Automated commit" --arg body "This commit was created via GitHub GraphQL API" '{"headline": $headline, "body": $body}') | |
# 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 "$message" \ | |
--arg filePath "$filePath" \ | |
--arg base64FileContent "$base64FileContent" \ | |
--arg repositoryNameWithOwner "$repositoryNameWithOwner" \ | |
--arg expectedHeadOid "$expectedHeadOid" \ | |
'{ | |
"query": $mutation, | |
"variables": { | |
"input": { | |
"branch": { | |
"repositoryNameWithOwner": $repositoryNameWithOwner, | |
"branchName": $branch | |
}, | |
"message": $message, | |
"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 | |