2
2
set -e
3
3
. functions.sh
4
4
5
- GITHUB_USERNAME=" nodejs-github-bot"
6
- gitpath=" ../docker-images"
5
+ if [ -z " $1 " ]; then
6
+ COMMIT_ID=" $TRAVIS_COMMIT "
7
+ COMMIT_MESSAGE=" $TRAVIS_COMMIT_MESSAGE "
8
+ BRANCH_NAME=" travis-$TRAVIS_BUILD_ID "
9
+ GITHUB_USERNAME=" nodejs-github-bot"
10
+ else
11
+ COMMIT_ID=" $1 "
12
+ COMMIT_MESSAGE=" $( git show -s --format=%B " $1 " ) "
13
+ BRANCH_NAME=" travis-$( date +%s) "
14
+ if [[ " $( git remote get-url origin) " =~ github.com/([^/]* )/docker-node.git ]]; then
15
+ GITHUB_USERNAME=" ${BASH_REMATCH[1]} "
16
+ fi
17
+ fi
18
+
7
19
IMAGES_FILE=" library/node"
8
20
REPO_NAME=" official-images"
9
- BRANCH_NAME=" travis-$TRAVIS_BUILD_ID "
10
21
ORIGIN_SLUG=" $GITHUB_USERNAME /$REPO_NAME "
11
22
UPSTREAM_SLUG=" docker-library/$REPO_NAME "
23
+ gitpath=" $REPO_NAME "
12
24
13
25
function updated() {
14
26
local versions
15
27
local images_changed
16
28
17
29
IFS=' ' read -ra versions <<< " $(IFS=','; get_versions)"
18
- images_changed=$( git show --name-only " $TRAVIS_COMMIT " " ${versions[@]} " )
30
+ images_changed=$( git show --name-only " $COMMIT_ID " " ${versions[@]} " )
19
31
20
32
if [ -z " $images_changed " ]; then
21
33
return 1
@@ -24,15 +36,24 @@ function updated() {
24
36
fi
25
37
}
26
38
39
+ function auth_header() {
40
+ echo " Authorization: token $GITHUB_API_TOKEN "
41
+ }
42
+
27
43
function permission_check() {
28
- auth=" $( curl -H " Authorization: token $GITHUB_API_TOKEN " \
44
+ if [ -z " $GITHUB_API_TOKEN " ]; then
45
+ fatal " Environment variable \$ GITHUB_API_TOKEN is missing or empty"
46
+ fi
47
+
48
+ auth=" $( curl -H " $( auth_header) " \
29
49
-s \
30
50
" https://api.github.com" ) "
51
+
31
52
if [ " $( echo " $auth " | jq .message) " = " \" Bad credentials\" " ]; then
32
53
fatal " Authentication Failed! Invalid \$ GITHUB_API_TOKEN"
33
54
fi
34
55
35
- auth=" $( curl -H " Authorization: token $GITHUB_API_TOKEN " \
56
+ auth=" $( curl -H " $( auth_header ) " \
36
57
-s \
37
58
" https://api.github.com/repos/$ORIGIN_SLUG /collaborators/$GITHUB_USERNAME /permission" ) "
38
59
if [ " $( echo " $auth " | jq .message) " != " null" ]; then
@@ -41,25 +62,25 @@ function permission_check() {
41
62
}
42
63
43
64
function setup_git_author() {
44
- GIT_AUTHOR_NAME=" $( git show -s --format=" %aN" " $TRAVIS_COMMIT " ) "
45
- GIT_AUTHOR_EMAIL=" $( git show -s --format=" %aE" " $TRAVIS_COMMIT " ) "
46
- GIT_COMMITTER_NAME=" $( git show -s --format=" %cN" " $TRAVIS_COMMIT " ) "
47
- GIT_COMMITTER_EMAIL=" $( git show -s --format=" %cN" " $TRAVIS_COMMIT " ) "
65
+ GIT_AUTHOR_NAME=" $( git show -s --format=" %aN" " $COMMIT_ID " ) "
66
+ GIT_AUTHOR_EMAIL=" $( git show -s --format=" %aE" " $COMMIT_ID " ) "
67
+ GIT_COMMITTER_NAME=" $( git show -s --format=" %cN" " $COMMIT_ID " ) "
68
+ GIT_COMMITTER_EMAIL=" $( git show -s --format=" %cN" " $COMMIT_ID " ) "
48
69
49
70
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
50
71
}
51
72
52
73
function message() {
53
- echo " Node: $TRAVIS_COMMIT_MESSAGE "
74
+ echo " Node: $COMMIT_MESSAGE "
54
75
}
55
76
56
77
function pr_payload() {
57
78
local escaped_message
58
- IFS=' ' read -ra escaped_message <<< " $TRAVIS_COMMIT_MESSAGE "
79
+ IFS=' ' read -ra escaped_message <<< " $COMMIT_MESSAGE "
59
80
escaped_message=" $( printf ' %q ' " ${escaped_message[@]} " ) "
60
81
echo ' {
61
82
"title": "Node: ' " $escaped_message " ' ",
62
- "body": "Commit: nodejs/docker-node@' " $TRAVIS_COMMIT " ' ",
83
+ "body": "Commit: nodejs/docker-node@' " $COMMIT_ID " ' ",
63
84
"head": "' " $GITHUB_USERNAME " ' :' " $BRANCH_NAME " ' ",
64
85
"base": "master"
65
86
}'
@@ -73,12 +94,13 @@ if updated; then
73
94
setup_git_author
74
95
75
96
info " Cloning..."
76
- git clone --depth 50 https://github.com/docker-library/official-images .git $gitpath 2> /dev/null
97
+ git clone --depth 50 " https://github.com/$UPSTREAM_SLUG .git" $gitpath 2> /dev/null
77
98
78
- ./generate-stackbrew-library.sh > " $gitpath / $IMAGES_FILE "
99
+ stackbrew= " $( ./generate-stackbrew-library.sh) "
79
100
80
101
cd $gitpath
81
102
103
+ echo " $stackbrew " > " $IMAGES_FILE "
82
104
git checkout -b " $BRANCH_NAME "
83
105
git add " $IMAGES_FILE "
84
106
git commit -m " $( message) "
@@ -87,17 +109,17 @@ if updated; then
87
109
git push
" https://$GITHUB_API_TOKEN :[email protected] /$ORIGIN_SLUG .git" -f
" $BRANCH_NAME " 2> /dev/null
|| fatal
" Error pushing the updated stackbrew"
88
110
89
111
info " Creating Pull request"
90
- response_payload =" $( curl -H " Authorization: token $GITHUB_API_TOKEN " \
112
+ pr_response_payload =" $( curl -H " $( auth_header ) " \
91
113
-s \
92
114
-X POST \
93
115
-d " $( pr_payload) " \
94
116
" https://api.github.com/repos/$UPSTREAM_SLUG /pulls" ) "
95
117
96
- url=" $( echo " $response_payload " | jq .html_url) "
118
+ url=" $( echo " $pr_response_payload " | jq .html_url) "
97
119
if [ " $url " != " null" ]; then
98
120
info " Pull request created at $url "
99
121
else
100
- error_message=$( echo " $response_payload " | jq .message)
122
+ error_message=$( echo " $pr_response_payload " | jq .message)
101
123
fatal " Error creating pull request ($error_message )"
102
124
fi
103
125
else
0 commit comments