forked from port-labs/cookiecutter-gha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·214 lines (184 loc) · 6.76 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
set -e
port_client_id="$INPUT_PORTCLIENTID"
port_client_secret="$INPUT_PORTCLIENTSECRET"
port_run_id="$INPUT_PORTRUNID"
github_token="$INPUT_TOKEN"
blueprint_identifier="$INPUT_BLUEPRINTIDENTIFIER"
repository_name="$INPUT_REPOSITORYNAME"
repository_visibility="$INPUT_REPOSITORYVISIBILITY"
org_name="$INPUT_ORGANIZATIONNAME"
cookie_cutter_template="$INPUT_COOKIECUTTERTEMPLATE"
template_directory="$INPUT_TEMPLATEDIRECTORY"
port_user_inputs="$INPUT_PORTUSERINPUTS"
monorepo_url="$INPUT_MONOREPOURL"
scaffold_directory="$INPUT_SCAFFOLDDIRECTORY"
create_port_entity="$INPUT_CREATEPORTENTITY"
branch_name="port_$port_run_id"
git_url="$INPUT_GITHUBURL"
repo_url="https://github.com/$org_name/$repository_name"
add_link_to_port="$INPUT_ADDLINKTOPORT"
get_access_token() {
curl --silent --show-error --location --request POST 'https://api.getport.io/v1/auth/access_token' --header 'Content-Type: application/json' --data-raw "{
\"clientId\": \"$port_client_id\",
\"clientSecret\": \"$port_client_secret\"
}" | jq -r '.accessToken'
}
send_log() {
local message=$1
if [[ -n $port_run_id ]]; then
curl --silent --show-error --location "https://api.getport.io/v1/actions/runs/$port_run_id/logs" \
--header "Authorization: Bearer $access_token" \
--header "Content-Type: application/json" \
--data "{
\"message\": \"$message\"
}"
else
echo "$message"
fi
}
add_link() {
if [[ "$add_link_to_port" != "true" ]]; then
echo "Skipping adding link to Port"
return
fi
local link_url=$1
curl --silent --show-error --request PATCH --location "https://api.getport.io/v1/actions/runs/$port_run_id" \
--header "Authorization: Bearer $access_token" \
--header "Content-Type: application/json" \
--data "{
\"link\": \"$link_url\"
}"
}
create_repository() {
resp=$(curl --silent --show-error -H "Authorization: token $github_token" -H "Accept: application/json" -H "Content-Type: application/json" "$git_url/users/$org_name")
userType=$(jq -r '.type' <<<"$resp")
if [ "$userType" == "User" ]; then
curl --silent --show-error -X POST -i -H "Authorization: token $github_token" -H "X-GitHub-Api-Version: 2022-11-28" \
-d "{ \
\"name\": \"$repository_name\", \"visibility\": \"$repository_visibility\"
}" \
"$git_url/user/repos"
elif [ "$userType" == "Organization" ]; then
curl --silent --show-error -i -H "Authorization: token $github_token" \
-d "{ \
\"name\": \"$repository_name\", \"visibility\": \"$repository_visibility\"
}" \
"$git_url/orgs/$org_name/repos"
else
echo "Invalid user type: $userType"
echo "$resp"
exit 1
fi
}
clone_monorepo() {
git clone "$monorepo_url" monorepo
cd monorepo
git checkout -b "$branch_name"
}
prepare_cookiecutter_extra_context() {
echo "$port_user_inputs" | jq -r 'with_entries(select(.key | startswith("cookiecutter_")) | .key |= sub("cookiecutter_"; ""))'
}
cd_to_scaffold_directory() {
if [ -n "$monorepo_url" ] && [ -n "$scaffold_directory" ]; then
cd "$scaffold_directory"
fi
}
apply_cookiecutter_template() {
extra_context=$(prepare_cookiecutter_extra_context) || (
echo "Error parsing cookiecutter extra context: $port_user_inputs" >&2
exit 1
)
echo "🍪 Applying cookiecutter template $cookie_cutter_template with extra context $extra_context"
# Convert extra context from JSON to arguments
args=()
for key in $(echo "$extra_context" | jq -r 'keys[]'); do
args+=("$key=$(echo "$extra_context" | jq -r ".$key")")
done
# Call cookiecutter with extra context arguments
echo "cookiecutter --no-input $cookie_cutter_template ${args[*]}"
if [ -n "$template_directory" ]; then
cookiecutter --no-input "$cookie_cutter_template" --directory "$template_directory" "${args[@]}"
else
cookiecutter --no-input "$cookie_cutter_template" "${args[@]}"
fi
}
push_to_repository() {
local default_branch="master"
if [ -n "$monorepo_url" ] && [ -n "$scaffold_directory" ]; then
git config user.name "GitHub Actions Bot"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "Scaffolded project in $scaffold_directory"
git push -u origin "$branch_name"
send_log "Creating pull request to merge $branch_name into $default_branch 🚢"
owner=$(echo "$monorepo_url" | awk -F'/' '{print $4}')
repo=$(echo "$monorepo_url" | awk -F'/' '{print $5}')
echo "Owner: $owner"
echo "Repo: $repo"
PR_PAYLOAD=$(jq -n --arg title "Scaffolded project in $repo" --arg head "$branch_name" --arg base "$default_branch" '{
"title": $title,
"head": $head,
"base": $base
}')
echo "PR Payload: $PR_PAYLOAD"
pr_url=$(curl --silent --show-error -X POST \
-H "Authorization: token $github_token" \
-H "Content-Type: application/json" \
-d "$PR_PAYLOAD" \
"$git_url/repos/$owner/$repo/pulls" | jq -r '.html_url')
send_log "Opened a new PR in $pr_url 🚀"
add_link "$pr_url"
else
cd "$(ls -td -- */ | head -n 1)"
git init
git config user.name "GitHub Actions Bot"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "Initial commit after scaffolding"
git branch -M $default_branch
git remote add origin "https://oauth2:$github_token@github.com/$org_name/$repository_name.git"
git push -u origin $default_branch
add_link "$repo_url"
fi
}
report_to_port() {
curl --silent --show-error --location "https://api.getport.io/v1/blueprints/$blueprint_identifier/entities?run_id=$port_run_id" \
--header "Authorization: Bearer $access_token" \
--header "Content-Type: application/json" \
--data "{
\"identifier\": \"$repository_name\",
\"title\": \"$repository_name\",
\"properties\": {}
}"
}
main() {
access_token=$(get_access_token)
if [ -z "$monorepo_url" ] || [ -z "$scaffold_directory" ]; then
send_log "Creating a new repository: $repository_name 🏃"
create_repository
send_log "New repository created: $repo_url 🚀"
else
send_log "Using monorepo scaffolding 🏃"
clone_monorepo
cd_to_scaffold_directory
send_log "Cloned monorepo and created branch $branch_name 🚀"
fi
send_log "Starting templating with cookiecutter 🍪"
apply_cookiecutter_template
send_log "Pushing the template into the repository."
push_to_repository
if [[ "$create_port_entity" == "true" ]]; then
send_log "Reporting to Port the new entity created 🚢"
report_to_port
else
send_log "Skipping reporting to Port the new entity created 🚢"
fi
if [ -n "$monorepo_url" ] && [ -n "$scaffold_directory" ]; then
send_log "Finished! 🏁✅"
else
send_log "Finished! Visit $repo_url 🏁✅"
fi
echo "repoURL=$repo_url" >>"$GITHUB_OUTPUT"
}
main