@@ -44,66 +44,62 @@ jobs:
4444 run : |
4545 # Define the target directory
4646 TARGET_DIR="."
47-
47+
4848 # Fetch deleted files in the target directory
4949 DELETED_FILES=$(git diff --diff-filter=D --name-only HEAD -- "$TARGET_DIR")
50-
50+
5151 # Fetch added/modified files in the target directory
5252 MODIFIED_FILES=$(git diff --diff-filter=ACM --name-only HEAD -- "$TARGET_DIR")
5353
54- # Collect deletions
55- DELETIONS_FILE=$(mktemp)
56- echo '[]' > "$DELETIONS_FILE"
54+ # Create a temporary file for JSON output
55+ FILE_CHANGES_JSON_FILE=$(mktemp)
5756
58- git diff --diff-filter=D --name-only HEAD -- "$TARGET_DIR" | while read -r file; do
59- jq --arg path "$file" '. += [{"path": $path}]' "$DELETIONS_FILE" > "$DELETIONS_FILE.tmp"
60- mv "$DELETIONS_FILE.tmp" "$DELETIONS_FILE"
61- done
57+ # Initialize JSON structure
58+ echo '{ "deletions": [], "additions": [] }' > "$FILE_CHANGES_JSON_FILE"
6259
63- # Collect additions
64- ADDITIONS_FILE=$(mktemp)
65- echo '[]' > "$ADDITIONS_FILE"
60+ # Add deletions
61+ for file in $DELETED_FILES; do
62+ jq --arg path "$file" '.deletions += [{"path": $path}]' "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp"
63+ mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_FILE"
64+ done
6665
67- git diff --diff-filter=ACM --name-only HEAD -- "$TARGET_DIR" | while read -r file; do
68- BASE64_CONTENT=$(base64 -w 0 <"$file")
69- jq --arg path "$file" --arg content "$BASE64_CONTENT" '. += [{"path": $path, "contents": $content}]' "$ADDITIONS_FILE" > "$ADDITIONS_FILE.tmp"
70- mv "$ADDITIONS_FILE.tmp" "$ADDITIONS_FILE"
66+ # Prepare additions (new or modified files)
67+ for file in $MODIFIED_FILES; do
68+ jq --rawfile content "$file" --arg path "$file" \
69+ '.additions += [{"path": $path, "contents": ($content | @base64)}]' \
70+ "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp"
71+ mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_FILE"
7172 done
72-
73+
7374 # Create a temporary file for the final JSON payload
7475 JSON_PAYLOAD_FILE=$(mktemp)
75-
76+
7677 # Construct the final JSON using jq and store it in a file
77- jq -n \
78- --slurpfile deletions "$DELETIONS_FILE" \
79- --slurpfile additions "$ADDITIONS_FILE" \
80- --arg repo "$GITHUB_REPOSITORY" \
81- --arg branch "$GITHUB_HEAD_REF" \
82- --arg message "post upgrade changes from renovate" \
83- --arg expectedOid "$GITHUB_SHA" \
84- '{
85- query: "mutation ($input: CreateCommitOnBranchInput!) {
86- createCommitOnBranch(input: $input) {
87- commit {
88- url
89- }
90- }
91- }",
92- variables: {
93- input: {
94- branch: {
95- repositoryNameWithOwner: $repo,
96- branchName: $branch
97- },
98- message: { headline: $message },
99- fileChanges: {
100- deletions: $deletions[0],
101- additions: $additions[0]
102- },
103- expectedHeadOid: $expectedOid
104- }
105- }
106- }' > "$JSON_PAYLOAD_FILE"
78+ jq -n --arg repo "$GITHUB_REPOSITORY" \
79+ --arg branch "$GITHUB_HEAD_REF" \
80+ --arg message "post upgrade changes from renovate" \
81+ --arg expectedOid "$GITHUB_SHA" \
82+ --slurpfile fileChanges "$FILE_CHANGES_JSON_FILE" \
83+ '{
84+ query: "mutation ($input: CreateCommitOnBranchInput!) {
85+ createCommitOnBranch(input: $input) {
86+ commit {
87+ url
88+ }
89+ }
90+ }",
91+ variables: {
92+ input: {
93+ branch: {
94+ repositoryNameWithOwner: $repo,
95+ branchName: $branch
96+ },
97+ message: { headline: $message },
98+ fileChanges: $fileChanges[0],
99+ expectedHeadOid: $expectedOid
100+ }
101+ }
102+ }' > "$JSON_PAYLOAD_FILE"
107103
108104 # Call GitHub API
109105 curl https://api.github.com/graphql -f \
0 commit comments