Skip to content

Commit 16ec2e5

Browse files
Merge pull request #686 from LaurentGoderre/stackbrew-pr-report
Modified the stackbrew pr script to enable running it manually
2 parents 1e5219b + 364fba4 commit 16ec2e5

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

generate-stackbrew-pr.sh

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@
22
set -e
33
. functions.sh
44

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+
719
IMAGES_FILE="library/node"
820
REPO_NAME="official-images"
9-
BRANCH_NAME="travis-$TRAVIS_BUILD_ID"
1021
ORIGIN_SLUG="$GITHUB_USERNAME/$REPO_NAME"
1122
UPSTREAM_SLUG="docker-library/$REPO_NAME"
23+
gitpath="$REPO_NAME"
1224

1325
function updated() {
1426
local versions
1527
local images_changed
1628

1729
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[@]}")
1931

2032
if [ -z "$images_changed" ]; then
2133
return 1
@@ -24,15 +36,24 @@ function updated() {
2436
fi
2537
}
2638

39+
function auth_header() {
40+
echo "Authorization: token $GITHUB_API_TOKEN"
41+
}
42+
2743
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)" \
2949
-s \
3050
"https://api.github.com")"
51+
3152
if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then
3253
fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN"
3354
fi
3455

35-
auth="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \
56+
auth="$(curl -H "$(auth_header)" \
3657
-s \
3758
"https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")"
3859
if [ "$(echo "$auth" | jq .message)" != "null" ]; then
@@ -41,25 +62,25 @@ function permission_check() {
4162
}
4263

4364
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")"
4869

4970
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
5071
}
5172

5273
function message() {
53-
echo "Node: $TRAVIS_COMMIT_MESSAGE"
74+
echo "Node: $COMMIT_MESSAGE"
5475
}
5576

5677
function pr_payload() {
5778
local escaped_message
58-
IFS=' ' read -ra escaped_message <<< "$TRAVIS_COMMIT_MESSAGE"
79+
IFS=' ' read -ra escaped_message <<< "$COMMIT_MESSAGE"
5980
escaped_message="$(printf '%q ' "${escaped_message[@]}")"
6081
echo '{
6182
"title": "Node: '"$escaped_message"'",
62-
"body": "Commit: nodejs/docker-node@'"$TRAVIS_COMMIT"'",
83+
"body": "Commit: nodejs/docker-node@'"$COMMIT_ID"'",
6384
"head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'",
6485
"base": "master"
6586
}'
@@ -73,12 +94,13 @@ if updated; then
7394
setup_git_author
7495

7596
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
7798

78-
./generate-stackbrew-library.sh > "$gitpath/$IMAGES_FILE"
99+
stackbrew="$(./generate-stackbrew-library.sh)"
79100

80101
cd $gitpath
81102

103+
echo "$stackbrew" > "$IMAGES_FILE"
82104
git checkout -b "$BRANCH_NAME"
83105
git add "$IMAGES_FILE"
84106
git commit -m "$(message)"
@@ -87,17 +109,17 @@ if updated; then
87109
git push "https://$GITHUB_API_TOKEN:[email protected]/$ORIGIN_SLUG.git" -f "$BRANCH_NAME" 2> /dev/null || fatal "Error pushing the updated stackbrew"
88110

89111
info "Creating Pull request"
90-
response_payload="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \
112+
pr_response_payload="$(curl -H "$(auth_header)" \
91113
-s \
92114
-X POST \
93115
-d "$(pr_payload)" \
94116
"https://api.github.com/repos/$UPSTREAM_SLUG/pulls")"
95117

96-
url="$(echo "$response_payload" | jq .html_url)"
118+
url="$(echo "$pr_response_payload" | jq .html_url)"
97119
if [ "$url" != "null" ]; then
98120
info "Pull request created at $url"
99121
else
100-
error_message=$(echo "$response_payload" | jq .message)
122+
error_message=$(echo "$pr_response_payload" | jq .message)
101123
fatal "Error creating pull request ($error_message)"
102124
fi
103125
else

0 commit comments

Comments
 (0)