-
Notifications
You must be signed in to change notification settings - Fork 11
/
entrypoint.sh
executable file
Β·204 lines (168 loc) Β· 6.08 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
#!/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"
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"
get_access_token() {
curl -s --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() {
message=$1
curl --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\"
}"
}
add_link() {
url=$1
curl --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\": \"$url\"
}"
}
create_repository() {
resp=$(curl -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 -X POST -i -H "Authorization: token $github_token" -H "X-GitHub-Api-Version: 2022-11-28" \
-d "{ \
\"name\": \"$repository_name\", \"private\": true
}" \
$git_url/user/repos
elif [ $userType == "Organization" ]; then
curl -i -H "Authorization: token $github_token" \
-d "{ \
\"name\": \"$repository_name\", \"private\": true
}" \
$git_url/orgs/$org_name/repos
else
echo "Invalid user type"
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 "πͺ 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"
# Call cookiecutter with extra context arguments
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() {
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 main π’"
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 "main" '{
"title": $title,
"head": $head,
"base": $base
}')
echo "PR Payload: $PR_PAYLOAD"
pr_url=$(curl -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 main
git remote add origin https://oauth2:[email protected]/$org_name/$repository_name.git
git push -u origin main
fi
}
report_to_port() {
curl --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 "Created a new repository at https://github.com/$org_name/$repository_name π"
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
url="https://github.com/$org_name/$repository_name"
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 $url πβ
"
fi
}
main