|
18 | 18 | # https://github.com/peter-evans/create-pull-request/issues/1241#issuecomment-1232477512
|
19 | 19 |
|
20 | 20 | set -euxo 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 | 48 | ENCODED_CONTENT=$(base64 < "${FILE_PATH}")
|
48 |
| - |
| 49 | + |
49 | 50 | # Create blob
|
50 | 51 | BLOB_SHA=$(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 | 54 | "https://api.github.com/repos/$repo_owner/$repo_name/git/blobs" | jq -r '.sha')
|
54 |
| - |
| 55 | + |
55 | 56 | # Append file info to tree JSON
|
56 | 57 | NEW_TREE_ARRAY="${NEW_TREE_ARRAY}{\"path\": \"$FILE_PATH\", \"mode\": \"100644\", \"type\": \"blob\", \"sha\": \"$BLOB_SHA\"},"
|
57 |
| -done |
58 |
| - |
| 58 | +done |
| 59 | + |
59 | 60 | # Remove trailing comma and close the array
|
60 |
| -NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]" |
61 |
| - |
| 61 | +NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]" |
| 62 | + |
| 63 | +echo "NEW_TREE_ARRAY:" |
| 64 | +echo "${NEW_TREE_ARRAY}" | jq . |
| 65 | + |
62 | 66 | # Create new tree
|
63 | 67 | NEW_TREE_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
|
64 | 68 | -H "Accept: application/vnd.github.v3+json" \
|
65 | 69 | -d "{\"base_tree\": \"$LATEST_TREE_SHA\", \"tree\": $NEW_TREE_ARRAY}" \
|
66 | 70 | "https://api.github.com/repos/$repo_owner/$repo_name/git/trees" | jq -r '.sha')
|
67 |
| - |
| 71 | + |
68 | 72 | echo "New tree SHA: $NEW_TREE_SHA"
|
69 |
| - |
| 73 | + |
70 | 74 | # Create a new commit
|
71 | 75 | NEW_COMMIT_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
|
72 | 76 | -H "Accept: application/vnd.github.v3+json" \
|
73 | 77 | -d "{\"message\": \"$commit_message\", \"tree\": \"$NEW_TREE_SHA\", \"parents\": [\"$LATEST_COMMIT_SHA\"]}" \
|
74 | 78 | "https://api.github.com/repos/$repo_owner/$repo_name/git/commits" | jq -r '.sha')
|
75 | 79 | echo "New commit SHA: $NEW_COMMIT_SHA"
|
76 |
| - |
| 80 | + |
77 | 81 | # Update the reference of the branch to point to the new commit
|
78 | 82 | curl -s -X PATCH -H "Authorization: token $github_token" \
|
79 | 83 | -H "Accept: application/vnd.github.v3+json" -d "{\"sha\": \"$NEW_COMMIT_SHA\"}" \
|
80 | 84 | "https://api.github.com/repos/$repo_owner/$repo_name/git/refs/heads/$branch"
|
81 |
| - |
| 85 | + |
82 | 86 | echo "Branch ${branch} updated to new commit ${NEW_COMMIT_SHA}."
|
0 commit comments