Skip to content

Commit

Permalink
Allow commits to open PRs from gitlab (#5108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyc-splunk authored Jul 16, 2024
1 parent 83bb86c commit 0458318
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
34 changes: 23 additions & 11 deletions .gitlab/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,35 @@ validate_version() {
fi
}

get_pr_count() {
local branch="$1"
local repo_url="$2"

pr_count="$( gh pr list --repo "$repo_url" --head "$branch" --state open --json id --jq length )"
if [[ ! "$pr_count" =~ ^[0-9]+$ ]]; then
echo "ERROR: Failed to get PRs for the $branch branch!" >&2
echo "$pr_count" >&2
exit 1
fi

echo "$pr_count"
}

setup_branch() {
local branch="$1"
local repo_url="$2"
local check_for_pr="${3:-true}"

# check if the branch exists
if git ls-remote --exit-code --heads origin "$branch"; then
# get number of open PRs for the branch
pr_count="$( gh pr list --repo "$repo_url" --head "$branch" --state open --json id --jq length )"
if [[ ! "$pr_count" =~ ^[0-9]+$ ]]; then
echo "ERROR: Failed to get PRs for the $branch branch!" >&2
echo "$pr_count" >&2
exit 1
fi
if [[ "$pr_count" != "0" ]]; then
echo ">>> The $branch branch exists and has $pr_count open PR(s)."
echo ">>> Nothing to do."
exit 0
if [ "$check_for_pr" = "true" ]; then
# get number of open PRs for the branch
pr_count="$( get_pr_count "$branch" "$repo_url" )"
if [[ "$pr_count" != "0" ]]; then
echo ">>> The $branch branch exists and has $pr_count open PR(s)."
echo ">>> Nothing to do."
exit 0
fi
fi
echo ">>> Resetting the $branch branch to main ..."
git checkout "$branch"
Expand Down
25 changes: 16 additions & 9 deletions .gitlab/update-otel-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ create_collector_pr() {
local repo="signalfx/splunk-otel-collector"
local repo_url="https://srv-gh-o11y-gdi:${GITHUB_TOKEN}@github.com/${repo}.git"
local message="Update OpenTelemetry Dependencies to $OTEL_VERSION"
local check_for_pr="true"
if [ "$OTEL_VERSION" = "main" ]; then
check_for_pr="false"
fi

echo ">>> Cloning the $repo repository ..."
git clone "$repo_url" collector-mirror
cd collector-mirror

setup_branch "$BRANCH" "$repo_url"
setup_branch "$BRANCH" "$repo_url" "$check_for_pr"

echo ">>> Updating otel deps to $OTEL_VERSION ..."
OTEL_VERSION="$OTEL_VERSION" ./internal/buildscripts/update-deps
Expand All @@ -32,14 +36,17 @@ create_collector_pr() {
if ! git diff --exit-code >/dev/null 2>&1; then
git commit -S -am "$message"
git push -f "$repo_url" "$BRANCH"
echo ">>> Creating the PR ..."
gh pr create \
--draft \
--repo "$repo" \
--title "$message" \
--body "$message" \
--base main \
--head "$BRANCH"
pr_count="$( get_pr_count "$BRANCH" "$repo_url" )"
if [ "$pr_count" = "0" ]; then
echo ">>> Creating the PR ..."
gh pr create \
--draft \
--repo "$repo" \
--title "$message" \
--body "$message" \
--base main \
--head "$BRANCH"
fi
fi
}

Expand Down

0 comments on commit 0458318

Please sign in to comment.