-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentrypoint.sh
executable file
·127 lines (105 loc) · 4 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
#!/bin/sh
# Backwards compatibility mapping
if [ -z $FACTORIO_USER ]; then :; else
INPUT_FACTORIO_USER=$FACTORIO_USER
fi
if [ -z $FACTORIO_PASSWORD ]; then :; else
INPUT_FACTORIO_PASSWORD=$FACTORIO_PASSWORD
fi
if [ "${GITHUB_REF}" == "${GITHUB_REF#refs/tags/}" ]; then
echo "This is not a tagged push." 1>&2
exit 78
fi
TAG="${GITHUB_REF#refs/tags/}"
if ! echo "${TAG}" | grep -qE '^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$'; then
echo "Bad version in tag, needs to be %u.%u.%u" 1>&2
exit 1
fi
export PACKAGE_NAME=$(jq -r .name info.json)
export PACKAGE_VERSION=$(jq -r .version info.json)
export PACKAGE_FULL_NAME=$PACKAGE_NAME\_$PACKAGE_VERSION
export PACKAGE_FILE="$PACKAGE_FULL_NAME.zip"
if ! [[ ${PACKAGE_VERSION} == "${TAG}" ]]; then
echo "ERROR: Tag version (${TAG}) doesn't match info.json version (${PACKAGE_VERSION}) (or info.json is invalid)." 1>&2
exit 1
fi
if ! grep -q "$PACKAGE_VERSION" changelog.txt; then
echo "ERROR: Current version (${PACKAGE_VERSION}) not found in 'changelog.txt'. Please add ingame changelog for current version." 1>&2
echo "See https://forums.factorio.com/viewtopic.php?f=25&t=67140 for mor information." 1>&2
exit 1
fi
export DIST_DIR=dist
export FILE_PATH=$DIST_DIR/$PACKAGE_FILE
export FILESIZE=$(stat -c "%s" "${FILE_PATH}")
echo ${FILE_PATH} ${FILESIZE}
CSRF=$(curl -sSL \
-b cookiejar.txt \
-c cookiejar.txt \
https://mods.factorio.com/login |
grep csrf_token |
sed -r -e 's/.*value="(.*)".*/\1/')
# Authenticate with the credential secrets and the CSRF token, getting a session cookie for the authorized user
curl -sSL \
-b cookiejar.txt \
-c cookiejar.txt \
-F "csrf_token=${CSRF}" \
-F "username=${INPUT_FACTORIO_USER}" \
-F "password=${INPUT_FACTORIO_PASSWORD}" \
-o /dev/null \
https://mods.factorio.com/login
# Query the mod info, verify the version number we're trying to push doesn't already exist
curl -sSL \
-b cookiejar.txt \
-c cookiejar.txt \
-s "https://mods.factorio.com/api/mods/${PACKAGE_NAME}/full" |
jq --arg new_version ${PACKAGE_VERSION} -e '.releases[] | select(.version == $new_version)'
STATUS_CODE=$?
if [[ $STATUS_CODE -ne 4 ]]; then
echo "Release already exists, skipping"
exit 78
fi
echo "Release doesn't exist for ${PACKAGE_VERSION}, uploading"
# Load the upload form, getting an upload token
UPLOAD_TOKEN=$(curl -sSL \
-b cookiejar.txt \
-c cookiejar.txt \
"https://mods.factorio.com/mod/${PACKAGE_NAME}/downloads/edit" |
grep token |
sed -r -e "s/.*token: '(.*)'.*/\1/")
if [[ -z ${UPLOAD_TOKEN} ]]; then
echo "ERROR: Couldn't get an upload token, failed" 1>&2
exit 1
fi
# Upload the file, getting back a response with details to send in the final form submission to complete the upload
UPLOAD_RESULT=$(curl -sSL \
-b cookiejar.txt \
-c cookiejar.txt \
-F "file=@${FILE_PATH};type=application/x-zip-compressed" \
"https://direct.mods-data.factorio.com/upload/mod/${UPLOAD_TOKEN}")
# Parse 'em and stat the file for the form fields
CHANGELOG=$(echo "${UPLOAD_RESULT}" | jq -r '@uri "\(.changelog)"')
INFO=$(echo "${UPLOAD_RESULT}" | jq -r '@uri "\(.info)"')
FILENAME=$(echo "${UPLOAD_RESULT}" | jq -r '.filename')
THUMBNAIL=$(echo "${UPLOAD_RESULT}" | jq -r '.thumbnail // empty')
if [[ ${FILENAME} == "null" ]] || [[ -z ${FILENAME} ]]; then
echo "ERROR: Upload failed" 1>&2
exit 1
fi
echo "Uploaded ${PACKAGE_NAME}_${PACKAGE_VERSION}.zip to ${FILENAME}, submitting as new version"
# Post the form, completing the release
HTTP_RESPONSE=$(curl --write-out "HTTPSTATUS:%{http_code}" \
-sSL \
-b cookiejar.txt \
-c cookiejar.txt \
-XPOST \
-d "file=&info_json=${INFO}&changelog=${CHANGELOG}&filename=${FILENAME}&file_size=${FILESIZE}&thumbnail=${THUMBNAIL}" \
-H "Content-Type: application/x-www-form-urlencoded" \
-o /dev/null \
"https://mods.factorio.com/mod/${PACKAGE_NAME}/downloads/edit")
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
if [ $HTTP_STATUS -eq 200 ]; then
echo "Release upload completed"
else
echo "ERROR: Failed to upload release" 1>&2
exit 1
fi