Skip to content

CLOUDP-313350: Fix the commit sign base64 encoding #2287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions scripts/create-signed-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,67 @@
# https://github.com/peter-evans/create-pull-request/issues/1241#issuecomment-1232477512

set -euo pipefail

# Configuration defaults
github_token=${GITHUB_TOKEN:?}
repo_owner="${REPO_OWNER:-mongodb}"
repo_name="${REPO_NAME:-mongodb-atlas-kubernetes}"
branch="${BRANCH:?}"
commit_message="${COMMIT_MESSAGE:?}"

# Fetch the latest commit SHA
# Fetch the latest commit SHA
LATEST_COMMIT_SHA=$(curl -s -H "Authorization: token $github_token" \
"https://api.github.com/repos/$repo_owner/$repo_name/git/ref/heads/$branch" | jq -r '.object.sha')
"https://api.github.com/repos/$repo_owner/$repo_name/git/ref/heads/$branch" | jq -r '.object.sha')

LATEST_TREE_SHA=$(curl -s -H "Authorization: token $github_token" \
"https://api.github.com/repos/$repo_owner/$repo_name/git/commits/$LATEST_COMMIT_SHA" | jq -r '.tree.sha')

"https://api.github.com/repos/$repo_owner/$repo_name/git/commits/$LATEST_COMMIT_SHA" | jq -r '.tree.sha')

echo "Creating a signed commit in GitHub."
echo "Latest commit SHA: $LATEST_COMMIT_SHA"
echo "Latest tree SHA: $LATEST_TREE_SHA"
echo "Latest tree SHA: $LATEST_TREE_SHA"

# Collect all modified files
MODIFIED_FILES=$(git diff --name-only --cached)
echo "Modified files: $MODIFIED_FILES"

# Create blob and tree
NEW_TREE_ARRAY="["
for FILE_PATH in $MODIFIED_FILES; do
# Read file content encoded to base64
ENCODED_CONTENT=$(base64 < "${FILE_PATH}")
ENCODED_CONTENT=$(base64 -w0 < "${FILE_PATH}")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @roothorp this is the actual fix, all the rest are formatting fixes


# Create blob
BLOB_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
BLOB_JSON=$(curl -s -X POST -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"content\": \"$ENCODED_CONTENT\", \"encoding\": \"base64\"}" \
"https://api.github.com/repos/$repo_owner/$repo_name/git/blobs" | jq -r '.sha')

"https://api.github.com/repos/$repo_owner/$repo_name/git/blobs")
BLOB_SHA=$(echo "${BLOB_JSON}" | jq -r '.sha')

# Append file info to tree JSON
NEW_TREE_ARRAY="${NEW_TREE_ARRAY}{\"path\": \"$FILE_PATH\", \"mode\": \"100644\", \"type\": \"blob\", \"sha\": \"$BLOB_SHA\"},"
done
done

# Remove trailing comma and close the array
NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]"
NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]"

# Create new tree
NEW_TREE_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"base_tree\": \"$LATEST_TREE_SHA\", \"tree\": $NEW_TREE_ARRAY}" \
"https://api.github.com/repos/$repo_owner/$repo_name/git/trees" | jq -r '.sha')

echo "New tree SHA: $NEW_TREE_SHA"

# Create a new commit
set -x
NEW_COMMIT_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"message\": \"$commit_message\", \"tree\": \"$NEW_TREE_SHA\", \"parents\": [\"$LATEST_COMMIT_SHA\"]}" \
"https://api.github.com/repos/$repo_owner/$repo_name/git/commits" | jq -r '.sha')
set +x
"https://api.github.com/repos/$repo_owner/$repo_name/git/commits" | jq -r '.sha')
echo "New commit SHA: $NEW_COMMIT_SHA"

# Update the reference of the branch to point to the new commit
curl -s -X PATCH -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" -d "{\"sha\": \"$NEW_COMMIT_SHA\"}" \
"https://api.github.com/repos/$repo_owner/$repo_name/git/refs/heads/$branch"

echo "Branch ${branch} updated to new commit ${NEW_COMMIT_SHA}."
Loading