|
18 | 18 | # https://github.com/peter-evans/create-pull-request/issues/1241#issuecomment-1232477512 |
19 | 19 |
|
20 | 20 | set -euo pipefail |
21 | | - |
| 21 | + |
22 | 22 | # Configuration defaults |
23 | 23 | github_token=${GITHUB_TOKEN:?} |
24 | 24 | repo_owner="${REPO_OWNER:-mongodb}" |
25 | 25 | repo_name="${REPO_NAME:-mongodb-atlas-kubernetes}" |
26 | 26 | branch="${BRANCH:?}" |
27 | 27 | commit_message="${COMMIT_MESSAGE:?}" |
28 | 28 |
|
29 | | -# Fetch the latest commit SHA |
| 29 | +# Fetch the latest commit SHA |
30 | 30 | LATEST_COMMIT_SHA=$(curl -s -H "Authorization: token $github_token" \ |
31 | | - "https://api.github.com/repos/$repo_owner/$repo_name/git/ref/heads/$branch" | jq -r '.object.sha') |
32 | | - |
| 31 | + "https://api.github.com/repos/$repo_owner/$repo_name/git/ref/heads/$branch" | jq -r '.object.sha') |
| 32 | + |
33 | 33 | LATEST_TREE_SHA=$(curl -s -H "Authorization: token $github_token" \ |
34 | | - "https://api.github.com/repos/$repo_owner/$repo_name/git/commits/$LATEST_COMMIT_SHA" | jq -r '.tree.sha') |
35 | | - |
| 34 | + "https://api.github.com/repos/$repo_owner/$repo_name/git/commits/$LATEST_COMMIT_SHA" | jq -r '.tree.sha') |
| 35 | + |
| 36 | +echo "Creating a signed commit in GitHub." |
36 | 37 | echo "Latest commit SHA: $LATEST_COMMIT_SHA" |
37 | | -echo "Latest tree SHA: $LATEST_TREE_SHA" |
38 | | - |
| 38 | +echo "Latest tree SHA: $LATEST_TREE_SHA" |
| 39 | + |
39 | 40 | # Collect all modified files |
40 | 41 | MODIFIED_FILES=$(git diff --name-only --cached) |
41 | 42 | echo "Modified files: $MODIFIED_FILES" |
42 | | - |
| 43 | + |
43 | 44 | # Create blob and tree |
44 | 45 | NEW_TREE_ARRAY="[" |
45 | 46 | for FILE_PATH in $MODIFIED_FILES; do |
46 | 47 | # Read file content encoded to base64 |
47 | | - ENCODED_CONTENT=$(base64 < "${FILE_PATH}") |
48 | | - |
| 48 | + ENCODED_CONTENT=$(base64 -w0 < "${FILE_PATH}") |
| 49 | + |
49 | 50 | # Create blob |
50 | | - BLOB_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \ |
| 51 | + BLOB_JSON=$(curl -s -X POST -H "Authorization: token $github_token" \ |
51 | 52 | -H "Accept: application/vnd.github.v3+json" \ |
52 | 53 | -d "{\"content\": \"$ENCODED_CONTENT\", \"encoding\": \"base64\"}" \ |
53 | | - "https://api.github.com/repos/$repo_owner/$repo_name/git/blobs" | jq -r '.sha') |
54 | | - |
| 54 | + "https://api.github.com/repos/$repo_owner/$repo_name/git/blobs") |
| 55 | + BLOB_SHA=$(echo "${BLOB_JSON}" | jq -r '.sha') |
| 56 | + |
55 | 57 | # Append file info to tree JSON |
56 | 58 | NEW_TREE_ARRAY="${NEW_TREE_ARRAY}{\"path\": \"$FILE_PATH\", \"mode\": \"100644\", \"type\": \"blob\", \"sha\": \"$BLOB_SHA\"}," |
57 | | -done |
58 | | - |
| 59 | +done |
| 60 | + |
59 | 61 | # Remove trailing comma and close the array |
60 | | -NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]" |
61 | | - |
| 62 | +NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]" |
| 63 | + |
62 | 64 | # Create new tree |
63 | 65 | NEW_TREE_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \ |
64 | 66 | -H "Accept: application/vnd.github.v3+json" \ |
65 | 67 | -d "{\"base_tree\": \"$LATEST_TREE_SHA\", \"tree\": $NEW_TREE_ARRAY}" \ |
66 | 68 | "https://api.github.com/repos/$repo_owner/$repo_name/git/trees" | jq -r '.sha') |
67 | | - |
| 69 | + |
68 | 70 | echo "New tree SHA: $NEW_TREE_SHA" |
69 | | - |
| 71 | + |
70 | 72 | # Create a new commit |
71 | | -set -x |
72 | 73 | NEW_COMMIT_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \ |
73 | 74 | -H "Accept: application/vnd.github.v3+json" \ |
74 | 75 | -d "{\"message\": \"$commit_message\", \"tree\": \"$NEW_TREE_SHA\", \"parents\": [\"$LATEST_COMMIT_SHA\"]}" \ |
75 | | - "https://api.github.com/repos/$repo_owner/$repo_name/git/commits" | jq -r '.sha') |
76 | | -set +x |
| 76 | + "https://api.github.com/repos/$repo_owner/$repo_name/git/commits" | jq -r '.sha') |
77 | 77 | echo "New commit SHA: $NEW_COMMIT_SHA" |
78 | | - |
| 78 | + |
79 | 79 | # Update the reference of the branch to point to the new commit |
80 | 80 | curl -s -X PATCH -H "Authorization: token $github_token" \ |
81 | 81 | -H "Accept: application/vnd.github.v3+json" -d "{\"sha\": \"$NEW_COMMIT_SHA\"}" \ |
82 | 82 | "https://api.github.com/repos/$repo_owner/$repo_name/git/refs/heads/$branch" |
83 | | - |
| 83 | + |
84 | 84 | echo "Branch ${branch} updated to new commit ${NEW_COMMIT_SHA}." |
0 commit comments