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