@@ -44,67 +44,71 @@ 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- # Create a temporary file for JSON output
55- FILE_CHANGES_JSON_FILE=$(mktemp)
56-
57- # Initialize JSON structure in the file
58- echo '{ "deletions": [], "additions": [] }' > "$FILE_CHANGES_JSON_FILE"
54+ # Collect deletions
55+ DELETIONS_FILE=$(mktemp)
56+ echo '[]' > "$DELETIONS_FILE"
5957
60- # Add deletions
6158 git diff --diff-filter=D --name-only HEAD -- "$TARGET_DIR" | while read -r file; 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 "
59+ jq --arg path "$file" '. += [{"path": $path}]' "$DELETIONS_FILE " > "$DELETIONS_FILE .tmp"
60+ mv "$DELETIONS_FILE .tmp" "$DELETIONS_FILE "
6461 done
6562
66- # Add additions (new or modified files)
63+ # Collect additions
64+ ADDITIONS_FILE=$(mktemp)
65+ echo '[]' > "$ADDITIONS_FILE"
66+
6767 git diff --diff-filter=ACM --name-only HEAD -- "$TARGET_DIR" | while read -r file; do
6868 BASE64_CONTENT=$(base64 -w 0 <"$file")
69- jq --arg path "$file" --arg content "$BASE64_CONTENT" \
70- '.additions += [{"path": $path, "contents": $content}]' "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp"
71- mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_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"
7271 done
73-
72+
7473 # Create a temporary file for the final JSON payload
7574 JSON_PAYLOAD_FILE=$(mktemp)
76-
75+
7776 # Construct the final JSON using jq and store it in a 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"
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"
103107
104108 # Call GitHub API
105109 curl https://api.github.com/graphql -f \
106110 -sSf -H "Authorization: Bearer $GITHUB_TOKEN" \
107111 --data "@$JSON_PAYLOAD_FILE"
108-
112+
109113 # Clean up temporary files
110114 rm "$FILE_CHANGES_JSON_FILE" "$JSON_PAYLOAD_FILE"
0 commit comments