|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +WORKSPACE="$(pwd)" |
| 6 | + |
| 7 | +TMP_FOLDER_TEMPLATE_BASE="tmp.${GITHUB_PR_BASE_REPO}" |
| 8 | +TMP_FOLDER_TEMPLATE="${TMP_FOLDER_TEMPLATE_BASE}.XXXXXXXXX" |
| 9 | + |
| 10 | +cleanup() { |
| 11 | + echo "Deleting temporal files..." |
| 12 | + cd ${WORKSPACE} |
| 13 | + rm -rf "${TMP_FOLDER_TEMPLATE_BASE}.*" |
| 14 | + echo "Done." |
| 15 | +} |
| 16 | + |
| 17 | +trap cleanup EXIT |
| 18 | +source .buildkite/scripts/install_deps.sh |
| 19 | + |
| 20 | +add_bin_path |
| 21 | + |
| 22 | +echo "--- install gh cli" |
| 23 | +with_github_cli |
| 24 | + |
| 25 | +echo "--- install jq" |
| 26 | +with_jq |
| 27 | + |
| 28 | + |
| 29 | +INTEGRATIONS_SOURCE_BRANCH=main |
| 30 | +INTEGRATIONS_GITHUB_OWNER=elastic |
| 31 | +INTEGRATIONS_GITHUB_REPO_NAME=integrations |
| 32 | +INTEGRATIONS_PR_BRANCH="test-${GITHUB_PR_BASE_REPO}-pr-${BUILDKITE_PULL_REQUEST}" |
| 33 | +INTEGRATIONS_PR_TITLE="Test ${GITHUB_PR_BASE_REPO}#${BUILDKITE_PULL_REQUEST} - DO NOT MERGE" |
| 34 | +VERSION_DEP="" |
| 35 | + |
| 36 | +get_pr_number() { |
| 37 | + # requires GITHUB_TOKEN |
| 38 | + local branch="$1" |
| 39 | + gh pr list -H "${branch}" --json number | jq -r '.[]|.number' |
| 40 | +} |
| 41 | + |
| 42 | +get_integrations_pr_link() { |
| 43 | + local pr_number=$1 |
| 44 | + echo "https://github.com/elastic/integrations/pull/${pr_number}" |
| 45 | +} |
| 46 | + |
| 47 | +get_source_pr_link() { |
| 48 | + echo "https://github.com/${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}/pull/${BUILDKITE_PULL_REQUEST}" |
| 49 | +} |
| 50 | + |
| 51 | +get_source_commit_link() { |
| 52 | + echo "https://github.com/${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}/commit/${GITHUB_PR_HEAD_SHA}" |
| 53 | +} |
| 54 | + |
| 55 | +set_git_config() { |
| 56 | + git config user.name "${GITHUB_USERNAME_SECRET}" |
| 57 | + git config user.email "${GITHUB_EMAIL_SECRET}" |
| 58 | +} |
| 59 | + |
| 60 | +git_push_with_auth() { |
| 61 | + local owner="$1" |
| 62 | + local repository="$2" |
| 63 | + local branch="$3" |
| 64 | + |
| 65 | + retry 3 git push https://${GITHUB_USERNAME_SECRET}:${GITHUB_TOKEN}@github.com/${owner}/${repository}.git "${branch}" |
| 66 | +} |
| 67 | + |
| 68 | +clone_repository() { |
| 69 | + local target="$1" |
| 70 | + retry 5 git clone https://github.com/elastic/integrations ${target} |
| 71 | +} |
| 72 | + |
| 73 | +create_integrations_pull_request() { |
| 74 | + # requires GITHUB_TOKEN |
| 75 | + local temp_path=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE}) |
| 76 | + echo "Creating Pull Request" |
| 77 | + message="Update ${GITHUB_PR_BASE_REPO} reference to $(get_source_commit_link).\nAutomated by [Buildkite build](${BUILDKITE_BUILD_URL})\n\nRelates: $(get_source_pr_link)" |
| 78 | + echo -e $message > ${temp_path}/body-pr.txt |
| 79 | + retry 3 \ |
| 80 | + gh pr create \ |
| 81 | + --title "${INTEGRATIONS_PR_TITLE}" \ |
| 82 | + --body-file ${temp_path}/body-pr.txt \ |
| 83 | + --draft \ |
| 84 | + --base ${INTEGRATIONS_SOURCE_BRANCH} \ |
| 85 | + --head ${INTEGRATIONS_PR_BRANCH} \ |
| 86 | + --assignee ${GITHUB_PR_HEAD_USER} |
| 87 | +} |
| 88 | + |
| 89 | +update_dependency() { |
| 90 | + # it needs to set the Golang version from the integrations repository (.go-version file) |
| 91 | + echo "--- install go for integrations repository :go:" |
| 92 | + with_go |
| 93 | + |
| 94 | + echo "--- Updating go.mod and go.sum with ${GITHUB_PR_HEAD_SHA} :hammer_and_wrench:" |
| 95 | + local source_dep="github.com/${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}${VERSION_DEP}" |
| 96 | + local target_dep="github.com/${GITHUB_PR_OWNER}/${GITHUB_PR_REPO}${VERSION_DEP}@${GITHUB_PR_HEAD_SHA}" |
| 97 | + |
| 98 | + go mod edit -replace ${source_dep}=${target_dep} |
| 99 | + go mod tidy |
| 100 | + |
| 101 | + git add go.mod |
| 102 | + git add go.sum |
| 103 | + |
| 104 | + # allow not to commit if there are no changes |
| 105 | + # previous execution could fail and just pushed the branch but PR is not created |
| 106 | + if ! git diff-index --quiet HEAD ; then |
| 107 | + git commit -m "Test elastic-package from PR ${BUILDKITE_PULL_REQUEST} - ${GITHUB_PR_HEAD_SHA}" |
| 108 | + fi |
| 109 | + |
| 110 | + echo "" |
| 111 | + git --no-pager show --format=oneline HEAD |
| 112 | + echo "" |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +exists_branch() { |
| 117 | + local owner="$1" |
| 118 | + local repository="$2" |
| 119 | + local branch="$3" |
| 120 | + |
| 121 | + git ls-remote --exit-code --heads https://github.com/${owner}/${repository}.git ${branch} |
| 122 | +} |
| 123 | + |
| 124 | +create_or_update_pull_request() { |
| 125 | + local temp_path=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE}) |
| 126 | + local repo_path="${temp_path}/elastic-integrations" |
| 127 | + local checkout_options="" |
| 128 | + local integrations_pr_number="" |
| 129 | + |
| 130 | + echo "Cloning repository" |
| 131 | + clone_repository "${repo_path}" |
| 132 | + |
| 133 | + pushd "${repo_path}" > /dev/null |
| 134 | + |
| 135 | + set_git_config |
| 136 | + |
| 137 | + echo "Checking branch ${INTEGRATIONS_PR_BRANCH} in remote ${INTEGRATIONS_GITHUB_OWNER}/${INTEGRATIONS_GITHUB_REPO_NAME}" |
| 138 | + if ! exists_branch ${INTEGRATIONS_GITHUB_OWNER} ${INTEGRATIONS_GITHUB_REPO_NAME} ${INTEGRATIONS_PR_BRANCH} ; then |
| 139 | + checkout_options=" -b " |
| 140 | + echo "Creating a new branch..." |
| 141 | + else |
| 142 | + echo "Already existed" |
| 143 | + fi |
| 144 | + |
| 145 | + integrations_pr_number=$(get_pr_number "${INTEGRATIONS_PR_BRANCH}") |
| 146 | + if [ -z "${integrations_pr_number}" ]; then |
| 147 | + echo "Exists PR in integrations repository: ${integrations_pr_number}" |
| 148 | + fi |
| 149 | + |
| 150 | + git checkout ${checkout_options} ${INTEGRATIONS_PR_BRANCH} |
| 151 | + |
| 152 | + echo "--- Updating dependency :pushpin:" |
| 153 | + update_dependency |
| 154 | + |
| 155 | + echo "--- Pushing branch ${INTEGRATIONS_PR_BRANCH} to integrations repository..." |
| 156 | + git_push_with_auth ${INTEGRATIONS_GITHUB_OWNER} ${INTEGRATIONS_GITHUB_REPO_NAME} ${INTEGRATIONS_PR_BRANCH} |
| 157 | + |
| 158 | + if [ -z "${integrations_pr_number}" ]; then |
| 159 | + echo "--- Creating pull request :github:" |
| 160 | + create_integrations_pull_request |
| 161 | + |
| 162 | + sleep 10 |
| 163 | + |
| 164 | + integrations_pr_number=$(get_pr_number "${INTEGRATIONS_PR_BRANCH}") |
| 165 | + fi |
| 166 | + |
| 167 | + popd > /dev/null |
| 168 | + |
| 169 | + rm -rf "${temp_path}" |
| 170 | + |
| 171 | + echo "--- adding comment into ${GITHUB_PR_BASE_REPO} pull request :memo:" |
| 172 | + add_pr_comment "${BUILDKITE_PULL_REQUEST}" "$(get_integrations_pr_link ${integrations_pr_number})" |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +add_pr_comment() { |
| 177 | + local source_pr_number="$1" |
| 178 | + local integrations_pr_link="$2" |
| 179 | + |
| 180 | + retry 3 \ |
| 181 | + gh pr comment ${source_pr_number} \ |
| 182 | + --body "Created or updated PR in integrations repostiory to test this vesrion. Check ${integrations_pr_link}" \ |
| 183 | + --repo ${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO} |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +echo "--- creating or updating integrations pull request" |
| 188 | +create_or_update_pull_request |
0 commit comments