2025-01-06 14:11:12 +01:00
|
|
|
merge-to-enterprise:
|
|
|
|
stage: ".pre"
|
|
|
|
rules:
|
|
|
|
# Only run for open-source protected branches.
|
|
|
|
- if: >-
|
|
|
|
$CI_PROJECT_NAME == "mender-server" &&
|
|
|
|
$CI_COMMIT_REF_PROTECTED == "true" &&
|
|
|
|
$CI_COMMIT_BRANCH != ""
|
|
|
|
when: always
|
|
|
|
allow_failure: true
|
|
|
|
- when: never
|
|
|
|
image: "${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/alpine:${ALPINE_VERSION}"
|
2025-01-08 13:29:22 +01:00
|
|
|
variables:
|
|
|
|
GITHUB_REPOSITORY_ENTERPRISE: "mendersoftware/mender-server-enterprise"
|
|
|
|
GITHUB_REPOSITORY_OPEN_SOURCE: "mendersoftware/mender-server"
|
2025-01-06 14:11:12 +01:00
|
|
|
before_script:
|
|
|
|
- apk add git jq
|
2025-01-08 13:29:22 +01:00
|
|
|
- GIT_REMOTE="https://mender-test-bot:${GITHUB_BOT_TOKEN_REPO_FULL}@github.com/$GITHUB_REPOSITORY_ENTERPRISE"
|
|
|
|
- GITHUB_ORG="${GITHUB_REPOSITORY_ENTERPRISE%%/*}"
|
|
|
|
- PR_BRANCH="${GITHUB_REPOSITORY_OPEN_SOURCE##*/}/$CI_COMMIT_BRANCH"
|
2025-01-06 14:11:12 +01:00
|
|
|
script:
|
|
|
|
# Get author Github username
|
2025-01-07 13:49:34 +01:00
|
|
|
- |
|
2025-01-08 13:29:22 +01:00
|
|
|
wget "https://api.github.com/repos/$GITHUB_REPOSITORY_OPEN_SOURCE/commits/$CI_COMMIT_SHA" \
|
2025-01-07 13:49:34 +01:00
|
|
|
--header "Authorization: Bearer $GITHUB_BOT_TOKEN_REPO_FULL" \
|
2025-01-06 14:11:12 +01:00
|
|
|
--header "Accept: application/vnd.github+json" \
|
|
|
|
--header "X-GitHub-Api-Version: 2022-11-28" \
|
2025-01-07 13:49:34 +01:00
|
|
|
-O commit.json
|
|
|
|
|
2025-01-06 14:11:12 +01:00
|
|
|
- GITHUB_USER=$(jq -r .author.login commit.json)
|
|
|
|
# Fetch existing PR (if any)
|
2025-01-07 13:49:34 +01:00
|
|
|
- |
|
2025-01-08 13:29:22 +01:00
|
|
|
wget "https://api.github.com/repos/$GITHUB_REPOSITORY_ENTERPRISE/pulls?head=$GITHUB_ORG:$PR_BRANCH&state=open" \
|
2025-01-06 14:11:12 +01:00
|
|
|
--header "Authorization: Bearer $GITHUB_BOT_TOKEN_REPO_FULL" \
|
|
|
|
--header "Accept: application/vnd.github+json" \
|
|
|
|
--header "X-GitHub-Api-Version: 2022-11-28" \
|
|
|
|
-O- | jq -r '.[]' > pull.json
|
2025-01-07 13:49:34 +01:00
|
|
|
|
|
|
|
- |
|
|
|
|
cat > pr_request_body.json << EOF
|
|
|
|
{
|
2025-01-08 13:29:22 +01:00
|
|
|
"title": "Merge branch $PR_BRANCH to enterprise",
|
2025-01-07 18:04:31 +01:00
|
|
|
"base": "$CI_COMMIT_BRANCH",
|
2025-01-08 13:29:22 +01:00
|
|
|
"head": "$GITHUB_ORG:$PR_BRANCH",
|
2025-01-07 13:49:34 +01:00
|
|
|
"maintainer_can_modify": true
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
- |
|
2025-01-07 17:51:22 +01:00
|
|
|
if test -z "$(jq -r .number pull.json)"; then
|
2025-01-06 14:11:12 +01:00
|
|
|
# Pull request does not exist: Forcefully fast-forward fork branch and create one
|
2025-01-08 13:29:22 +01:00
|
|
|
git push -f "$GIT_REMOTE" "$CI_COMMIT_SHA:refs/heads/$PR_BRANCH"
|
2025-01-07 13:49:34 +01:00
|
|
|
wget --post-file pr_request_body.json \
|
2025-01-06 14:11:12 +01:00
|
|
|
--header "Authorization: Bearer $GITHUB_BOT_TOKEN_REPO_FULL" \
|
|
|
|
--header "Accept: application/vnd.github+json" \
|
|
|
|
--header "Content-Type: application/json" \
|
|
|
|
--header "X-GitHub-Api-Version: 2022-11-28" \
|
|
|
|
-O pull.json \
|
2025-01-08 13:29:22 +01:00
|
|
|
"https://api.github.com/repos/$GITHUB_REPOSITORY_ENTERPRISE/pulls"
|
2025-01-07 17:51:22 +01:00
|
|
|
else
|
|
|
|
echo "Pull request exists: $(jq -r .url pull.json)"
|
|
|
|
echo "Attempting to fast-forward"
|
2025-01-06 14:11:12 +01:00
|
|
|
fi
|
2025-01-07 17:51:22 +01:00
|
|
|
- PULL_NUMBER=$(jq -r .number pull.json)
|
2025-01-07 13:49:34 +01:00
|
|
|
- |
|
2025-01-08 13:29:22 +01:00
|
|
|
if ! git push "$GIT_REMOTE" "$CI_COMMIT_SHA:refs/heads/$PR_BRANCH"; then
|
2025-01-06 14:11:12 +01:00
|
|
|
# Could not fast-forward PR, notify the author in the existing PR
|
2025-01-07 18:04:31 +01:00
|
|
|
wget --post-data "{\"body\": \"Tried to submit commits from @$GITHUB_USER, but this PR has commits ahead of $CI_COMMIT_BRANCH\"}" \
|
2025-01-06 14:11:12 +01:00
|
|
|
--header "Authorization: Bearer $GITHUB_BOT_TOKEN_REPO_FULL" \
|
|
|
|
--header "Accept: application/vnd.github+json" \
|
|
|
|
--header "Content-Type: application/json" \
|
|
|
|
--header "X-GitHub-Api-Version: 2022-11-28" \
|
2025-01-08 13:29:22 +01:00
|
|
|
"https://api.github.com/repos/$GITHUB_REPOSITORY_ENTERPRISE/issues/$PULL_NUMBER/comments"
|
2025-01-06 14:11:12 +01:00
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
# Add author as a reviewer
|
2025-01-07 13:49:34 +01:00
|
|
|
- |
|
2025-01-06 14:11:12 +01:00
|
|
|
wget --post-data "{\"reviewers\": [\"$GITHUB_USER\"]}" \
|
|
|
|
--header "Authorization: Bearer $GITHUB_BOT_TOKEN_REPO_FULL" \
|
|
|
|
--header "Accept: application/vnd.github+json" \
|
|
|
|
--header "Content-Type: application/json" \
|
|
|
|
--header "X-GitHub-Api-Version: 2022-11-28" \
|
2025-01-08 13:29:22 +01:00
|
|
|
"https://api.github.com/repos/$GITHUB_REPOSITORY_ENTERPRISE/pulls/$PULL_NUMBER/requested_reviewers"
|