-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentrypoint.sh
265 lines (217 loc) · 8.7 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env bash
set -e
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m'
function run_danger(){
echo -e "${BLUE}Installing Danger CI..${NC}"
yarn global add danger --dev
export PATH="$(yarn global bin):$PATH"
echo -e "${BLUE}Running Danger CI..${NC}"
remove_npmrc
danger ci
}
function yarn_setup(){
echo -e "${YELLOW}Updating yarn to v1.22.11...${NC}"
npm_config_unsafe_perm=true npm uninstall --global yarn
npm_config_unsafe_perm=true npm install --global --force [email protected]
}
function publish(){
function install_with_CLI(){
if [ -f "yarn.lock" ]; then
yarn_setup
echo -e "${YELLOW}Installing with yarn...${NC}"
npm_config_unsafe_perm=true yarn
else
echo -e "${YELLOW}Installing with npm...${NC}"
npm_config_unsafe_perm=true npm install
fi
}
function npm_version_SHA(){
npm version "`node -e \"console.log(require('./package.json').version)\"`-`git log --pretty=format:'%h' -n 1`" --no-git-tag-version
}
tag="$(echo $GITHUB_HEAD_REF | sed -E 's:_:__:g;s:\/:_:g')"
echo '{"tag":"","packages":[],"error":{"name":"","directory":""}}' > published.json
echo $(jq --arg BRANCH "$tag" '.tag = $BRANCH' published.json) > published.json
echo "@resideo:registry=https://npm.pkg.github.com" >> ~/.npmrc
echo "//npm.pkg.github.com/:_authToken=${NPM_TOKEN}" >> ~/.npmrc
echo "unsafe-perm=true" >> ~/.npmrc
remove_npmrc
install_with_CLI
for dir in ${confirmed_directories_array[@]}; do
cd $dir
json_within=($(find . -name 'package.json' -not -path './node_modules/*'));
json_count=${#json_within[@]};
is_private=$(echo $(jq '.private' package.json))
pkgname="`node -e \"console.log(require('./package.json').name)\"`"
if [ "$json_count" != "1" ]; then
echo -e "${RED}Skipping publishing process for: ${YELLOW}$dir${RED} because there is a sub-package.${NC}"
elif [ "$is_private" = "true" ]; then
echo -e "${RED}Skipping publishing process for: ${YELLOW}$dir${RED} because package is marked as private and therefore not intended to be published.${NC}"
else
echo -e "${GREEN}Running publishing process for: ${YELLOW}$dir${NC}"
npm_version_SHA
echo -e "${GREEN}Publishing...${NC}"
if [ "${#INPUT_NPM_PUBLISH}" -eq "0" ]; then
npm publish --access=public --tag $tag
else
$INPUT_NPM_PUBLISH --access=public --tag $tag
fi
pkgver="`node -e \"console.log(require('./package.json').version)\"`"
echo $(jq --arg PKG "$pkgname" --arg VER "$pkgver" '.packages[.packages | length] |= . + {"name": $PKG, "version": $VER}' $GITHUB_WORKSPACE/published.json) > $GITHUB_WORKSPACE/published.json
fi
cd $GITHUB_WORKSPACE
done;
if [ "$(jq '."packages"|length' ./published.json)" -eq "0" ]; then
cat << "EOT" > dangerfile.js
const { markdown } = require('danger');
const first_line = `:mega: NOTIFICATION`;
const second_line = `You are receiving this message because we did not publish any packages.`;
markdown(`${first_line}\n\n${second_line}`)
EOT
elif [ "$(jq '."packages"|length' ./published.json)" -eq "1" ]; then
cat << "EOT" > dangerfile.js
const { markdown } = require('danger');
const pjson = require('./published.json');
const first_line = `A preview package from this pull request has been published.`;
const second_line = `You can try it out by running the following command:`;
const install_tag = `$ npm install ${pjson.packages[0].name}@${pjson.packages[0].version}`;
const fourth_line = `or by updating your package.json to:`
const update_json = `\{\n \"${pjson.packages[0].name}\": \"${pjson.packages[0].version}\"\n\}`
markdown(`${first_line}\n${second_line}\n\`\`\`bash\n${install_tag}\n\`\`\`\n${fourth_line}\n\`\`\`bash\n${update_json}\n\`\`\``)
EOT
else
cat << "EOT" > dangerfile.js
const { markdown } = require('danger');
const pjson = require('./published.json');
let packages = pjson.packages.reduce((acc, item) => acc + package(item.name, item.version), '');
function package(name, version){
const first_line = `Install using the following command:`
const install_tag = `$ npm install ${name}@${version}`
const or_line = `Or update your package.json file:`
const json_line = `\"${name}\": \"${version}\"`
return `<details><summary>${name}</summary>
---
${first_line}
\`\`\`bash
${install_tag}
\`\`\`
${or_line}
\`\`\`bash
{
${json_line}
}
\`\`\`
---
</details>`
}
const first_line = `The preview packages of this pull request have been published.`;
const second_line = `Click on the following packages for instructions on how to install them:`;
markdown(`${first_line}\n${second_line}\n${packages}`)
EOT
fi;
run_danger
}
function remove_packages_to_skip(){
function unslash_end_of_args(){
for to_unslash in $*; do
echo $to_unslash | sed 's:\/$::g';
done;
}
defaults=("node_modules .github")
skip_directories=($(unslash_end_of_args $INPUT_IGNORE) ${defaults[@]})
confirmed_directories_array=(${confirmed_directories[@]})
for skip_directory in ${skip_directories[@]}; do
for i in ${!confirmed_directories_array[@]}; do
if [ $(echo "${confirmed_directories_array[$i]}" | sed -E "s:^$skip_directory.*::") ]; then
:
else
echo -e "${RED}Removing ${YELLOW}${confirmed_directories_array[$i]} ${RED}because of ${YELLOW}${skip_directory}${NC}"
unset confirmed_directories_array[$i]
fi
done
done
publish
}
function package_json_finder(){
function format_dit_giff(){
for to_format in $*; do
if [ "$(echo $to_format | grep -c "/")" = "0" ]; then
echo ".";
else
echo $(echo $to_format | sed 's:\(.*\)\/.*:\1:g');
fi;
done;
}
git checkout $GITHUB_BASE_REF
git checkout $GITHUB_HEAD_REF
diffs=$(git diff --name-only $GITHUB_BASE_REF..$GITHUB_HEAD_REF)
diff_directories=$(format_dit_giff $diffs)
diff_directories_array=(${diff_directories[@]})
echo -e "${GREEN}Directories of git-diff: ${BLUE}${diff_directories_array[@]}${NC}"
package_directories=()
function json_locater(){
if [ -d "${GITHUB_WORKSPACE}/${1}" ]; then
cd $GITHUB_WORKSPACE/$1
if [ ! -f "package.json" ]; then
if [ "$(echo $1 | grep -c "/")" = "1" ]; then
super_directory=$(echo $1 | sed 's:\(.*\)\/.*:\1:g');
json_locater $super_directory
else
package_directories+=(".")
fi
else
package_directories+=("$1")
fi
cd $GITHUB_WORKSPACE
else
echo -e "${RED}Skipping ${YELLOW}$1${RED} because the directory does not exist.${NC}"
fi
}
for i in ${!diff_directories_array[@]}; do
echo -e "${GREEN}Running json_locator for: ${YELLOW}${diff_directories_array[$i]}${NC}"
json_locater ${diff_directories_array[$i]}
done;
package_directories_array=(${package_directories[@]})
confirmed_directories=($(echo "${package_directories_array[@]}" | xargs -n1 | sort -u | xargs))
remove_packages_to_skip
}
function check_prerequisites(){
echo -e "${YELLOW}Checking prerequisites...${NC}"
PR="$(jq '."pull_request"' ../workflow/event.json)"
jsonpath="../workflow/event.json"
headurl="$(jq '."pull_request"|."head"|."repo"|."url"' $jsonpath)"
baseurl="$(jq '."pull_request"|."base"|."repo"|."url"' $jsonpath)"
if [[ "$PR" = "null" ]]; then
echo -e "${RED}Error: ${YELLOW}This action should only be called from a pull request. Please check your workflow configuration.${NC}"
else
if [[ "$headurl" != "$baseurl" ]]; then
echo -e "${RED}Error: ${YELLOW}Not generating a comment because this pull request was created from a forked repository.${NC}"
echo -e "${BLUE}Tip: ${YELLOW}Publishing preview will not work if the pull request was created from a forked repository unless you create the pull request against your own repository.${NC}"
else
if [[ "$GITHUB_HEAD_REF" = "latest" ]]; then
echo -e "${RED}ERROR: ${YELLOW}Unable to publish preview because your branch conflicts with NPM's protected 'latest' tag.${NC}"
echo -e "${BLUE}Tip: ${YELLOW}Please change the name of your branch and resubmit the pull request.${NC}"
cat << "EOT" > dangerfile.js
const { markdown } = require('danger');
const first_line = `:warning: WARNING :warning:`;
const second_line = `I can't publish a preview of this branch this because it would conflict with the \`latest\` tag which is [reserved by NPM](https://docs.npmjs.com/cli/dist-tag#purpose).`;
markdown(`${first_line}\n\n${second_line}`)
EOT
run_danger
else
echo -e "${GREEN}All checks passed! Proceeding...${NC}"
package_json_finder
fi
fi
fi
}
function remove_npmrc(){
if [ -f ".npmrc" ]; then
echo -e "${YELLOW}.npmrc detected: Removing npmrc file from repository...${NC}"
rm .npmrc
fi
}
check_prerequisites