This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
forked from nodejs/docker-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-stackbrew-pr.sh
executable file
·151 lines (122 loc) · 4.27 KB
/
generate-stackbrew-pr.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
#!/usr/bin/env bash
set -e
. functions.sh
if [ -z "${1}" ]; then
COMMIT_ID="${TRAVIS_COMMIT}"
COMMIT_MESSAGE="${TRAVIS_COMMIT_MESSAGE}"
BRANCH_NAME="travis-${TRAVIS_BUILD_ID}"
GITHUB_USERNAME="nodejs-github-bot"
else
COMMIT_ID="${1}"
COMMIT_MESSAGE="$(git show -s --format=%B "${COMMIT_ID}")"
BRANCH_NAME="travis-$(date +%s)"
if [[ "$(git remote get-url origin)" =~ github.com/([^/]*)/docker-node.git ]]; then
GITHUB_USERNAME="${BASH_REMATCH[1]}"
fi
fi
if [[ "${COMMIT_MESSAGE}" =~ Merge\ pull\ request\ \#([0-9]*) ]]; then
# This is a merge from a pull request
PR_NUMBER="${BASH_REMATCH[1]}"
COMMIT_MESSAGE="$(printf "%s" "${COMMIT_MESSAGE}" | tail -n 1)"
fi
IMAGES_FILE="library/node"
REPO_NAME="official-images"
ORIGIN_SLUG="${GITHUB_USERNAME}/${REPO_NAME}"
UPSTREAM_SLUG="docker-library/${REPO_NAME}"
DOCKER_SLUG="nodejs/docker-node"
gitpath="../${REPO_NAME}"
function auth_header() {
echo "Authorization: token ${GITHUB_API_TOKEN}"
}
function permission_check() {
if [ -z "${GITHUB_API_TOKEN}" ]; then
fatal "Environment variable \$GITHUB_API_TOKEN is missing or empty"
fi
auth="$(curl -H "$(auth_header)" \
-s \
"https://api.github.com")"
if [ "$(echo "${auth}" | jq -r .message)" = "Bad credentials" ]; then
fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN"
fi
auth="$(curl -H "$(auth_header)" \
-s \
"https://api.github.com/repos/${ORIGIN_SLUG}/collaborators/${GITHUB_USERNAME}/permission")"
if [ "$(echo "${auth}" | jq -r .message)" != "null" ]; then
fatal "\$GITHUB_API_TOKEN can't push to https://github.com/${ORIGIN_SLUG}.git"
fi
}
function setup_git_author() {
GIT_AUTHOR_NAME="$(git show -s --format="%aN" "${COMMIT_ID}")"
GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "${COMMIT_ID}")"
GIT_COMMITTER_NAME="$(git show -s --format="%cN" "${COMMIT_ID}")"
GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "${COMMIT_ID}")"
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
}
function message() {
echo "Node: ${COMMIT_MESSAGE}"
}
function pr_payload() {
local escaped_message
escaped_message="$(echo "${COMMIT_MESSAGE}" | sed -E -e "s/\"/\\\\\"/g")"
echo "{
'title': 'Node: ${escaped_message}',
'body': 'Commit: nodejs/docker-node@${COMMIT_ID}',
'head': '${GITHUB_USERNAME}:${BRANCH_NAME}',
'base': 'master'
}"
}
function comment_payload() {
local pr_url
pr_url="${1}"
echo "{
'body': 'Created PR to the ${REPO_NAME} repo (${pr_url})'
}"
}
if images_updated "${COMMIT_ID}"; then
permission_check
# Set Git User Info
setup_git_author
info "Cloning..."
git clone --depth 50 "https://github.com/${UPSTREAM_SLUG}.git" ${gitpath} 2> /dev/null
stackbrew="$(./generate-stackbrew-library.sh)"
cd ${gitpath}
echo "${stackbrew}" > "${IMAGES_FILE}"
git checkout -b "${BRANCH_NAME}"
git add "${IMAGES_FILE}"
git commit -m "$(message)"
info "Pushing..."
git push "https://${GITHUB_API_TOKEN}:[email protected]/${ORIGIN_SLUG}.git" -f "${BRANCH_NAME}" 2> /dev/null || fatal "Error pushing the updated stackbrew"
cd - && rm -rf ${gitpath}
info "Creating Pull request"
pr_response_payload="$(curl -H "$(auth_header)" \
-s \
-X POST \
-d "$(pr_payload)" \
"https://api.github.com/repos/${UPSTREAM_SLUG}/pulls")"
url="$(echo "${pr_response_payload}" | jq -r .html_url)"
if [ "${url}" != "null" ]; then
info "Pull request created at ${url}"
if [ ! -z "${PR_NUMBER}" ]; then
comment_endpoint="https://api.github.com/repos/${DOCKER_SLUG}/issues/${PR_NUMBER}/comments"
else
comment_endpoint="https://api.github.com/repos/${DOCKER_SLUG}/commits/${COMMIT_ID}/comments"
fi
info "Creating Commit Comment"
commit_response_payload="$(curl -H "$(auth_header)" \
-s \
-X POST \
-d "$(comment_payload "${url}")" \
"${comment_endpoint}")"
if [ "$(echo "${commit_response_payload}" | jq -r .message)" != "null" ]; then
fatal "Error linking the pull request (${error_message})"
else
comment_url="$(echo "${commit_response_payload}" | jq -r .html_url)"
info "Created comment at ${comment_url}"
fi
else
error_message=$(echo "${pr_response_payload}" | jq -r .message)
fatal "Error creating pull request (${error_message})"
fi
else
info "No change!"
fi