@@ -25,6 +25,20 @@ 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
+ remote=${REMOTE:- origin}
29
+
30
+ # Ensure remote branch exists
31
+ echo " Check remote branch ${branch} exists..."
32
+
33
+ function create_branch() {
34
+ echo " Creating branch ${remote} /${branch} "
35
+ git stash
36
+ git push " ${remote} " " ${branch} "
37
+ git stash pop
38
+ git add .
39
+ }
40
+
41
+ git ls-remote --exit-code --heads origin " ${branch} " > /dev/null || create_branch
28
42
29
43
# Fetch the latest commit SHA
30
44
LATEST_COMMIT_SHA=$( curl -s -H " Authorization: token $github_token " \
@@ -47,12 +61,14 @@ for FILE_PATH in $MODIFIED_FILES; do
47
61
# Read file content encoded to base64
48
62
ENCODED_CONTENT=$( base64 -w0 < " ${FILE_PATH} " )
49
63
50
- # Create blob
64
+ # Create blob
65
+ echo " {\" content\" : \" $ENCODED_CONTENT \" , \" encoding\" : \" base64\" }" > content.blob
51
66
BLOB_JSON=$( curl -s -X POST -H " Authorization: token $github_token " \
52
67
-H " Accept: application/vnd.github.v3+json" \
53
- -d " { \" content\" : \" $ENCODED_CONTENT \" , \" encoding \" : \" base64 \" } " \
68
+ -d @ content.blob \
54
69
" https://api.github.com/repos/$repo_owner /$repo_name /git/blobs" )
55
70
BLOB_SHA=$( echo " ${BLOB_JSON} " | jq -r ' .sha' )
71
+ rm content.blob
56
72
57
73
# Append file info to tree JSON
58
74
NEW_TREE_ARRAY=" ${NEW_TREE_ARRAY} {\" path\" : \" $FILE_PATH \" , \" mode\" : \" 100644\" , \" type\" : \" blob\" , \" sha\" : \" $BLOB_SHA \" },"
61
77
# Remove trailing comma and close the array
62
78
NEW_TREE_ARRAY=" ${NEW_TREE_ARRAY% ,} ]"
63
79
64
- # Create new tree
80
+ # Create new tree
81
+ echo " {\" base_tree\" : \" $LATEST_TREE_SHA \" , \" tree\" : $NEW_TREE_ARRAY }" > new_tree_spec.json
65
82
NEW_TREE_SHA=$( curl -s -X POST -H " Authorization: token $github_token " \
66
83
-H " Accept: application/vnd.github.v3+json" \
67
- -d " { \" base_tree \" : \" $LATEST_TREE_SHA \" , \" tree \" : $NEW_TREE_ARRAY } " \
84
+ -d @new_tree_spec.json \
68
85
" https://api.github.com/repos/$repo_owner /$repo_name /git/trees" | jq -r ' .sha' )
86
+ rm new_tree_spec.json
69
87
70
88
echo " New tree SHA: $NEW_TREE_SHA "
71
89
@@ -76,9 +94,16 @@ NEW_COMMIT_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
76
94
" https://api.github.com/repos/$repo_owner /$repo_name /git/commits" | jq -r ' .sha' )
77
95
echo " New commit SHA: $NEW_COMMIT_SHA "
78
96
79
- # Update the reference of the branch to point to the new commit
97
+ # Update the reference of the branch to point to the new commit
80
98
curl -s -X PATCH -H " Authorization: token $github_token " \
81
99
-H " Accept: application/vnd.github.v3+json" -d " {\" sha\" : \" $NEW_COMMIT_SHA \" }" \
82
- " https://api.github.com/repos/$repo_owner /$repo_name /git/refs/heads/$branch "
100
+ " https://api.github.com/repos/$repo_owner /$repo_name /git/refs/heads/$branch "
83
101
84
102
echo " Branch ${branch} updated to new commit ${NEW_COMMIT_SHA} ."
103
+ git restore --staged .
104
+ git restore .
105
+ git clean -f
106
+ git fetch " ${remote} "
107
+ git checkout -b " ${branch} " " ${remote} /${branch} " || git checkout " ${branch} "
108
+
109
+ echo " Local branch set to ${remote} /${branch} "
0 commit comments