Skip to content

Commit 57b7c70

Browse files
f-hollowDeveloper-Portal-BOT
authored andcommitted
ci: add stages to sync-merge feature branch from gitlab to github
1 parent a24848d commit 57b7c70

File tree

1 file changed

+166
-33
lines changed

1 file changed

+166
-33
lines changed

.gitlab-ci.yml

Lines changed: 166 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,62 @@
1+
default:
2+
interruptible: true
3+
4+
.comment_template:
5+
image: badouralix/curl-jq
6+
tags:
7+
- deploy_docs
8+
- shiny
9+
10+
.add_comment_script: &add_comment_script
11+
- |
12+
GITLAB_API="https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_ID}/notes"
13+
AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_BOT_API_TOKEN}"
14+
15+
# Get existing comments
16+
API_RESPONSE=$(curl --silent --header "$AUTH_HEADER" "$GITLAB_API")
17+
18+
# Check if the response contains the expected structure
19+
COMMENTS=$(echo "$API_RESPONSE" | jq -r ".[] | select(.body | contains(\"$COMMENT_IDENTIFIER\")) | .id")
20+
21+
# Delete previous preview comments
22+
if [ -n "$COMMENTS" ]; then
23+
for COMMENT_ID in $COMMENTS; do
24+
curl --silent --request DELETE \
25+
--header "$AUTH_HEADER" \
26+
"${GITLAB_API}/${COMMENT_ID}"
27+
done
28+
fi
29+
30+
# Post a new comment
31+
curl --silent --request POST \
32+
--header "$AUTH_HEADER" \
33+
--header "Content-Type: application/json" \
34+
--data "{\"body\": \"$COMMENT_BODY\"}" \
35+
"$GITLAB_API"
36+
37+
# Define the reusable rule sets
38+
.default-rules:
39+
rules:
40+
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
41+
42+
.label-based-rules:
43+
rules:
44+
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_LABELS =~ /GitHub-Sync-Merge/ && $CI_MERGE_REQUEST_LABELS !~ /GitHub-Edit/'
45+
146
stages:
247
- build
348
- deploy
4-
- comment
49+
- comment_preview
50+
- sync_merge
51+
- comment_github_pr
552

653
build_hugo:
754
stage: build
855
image: "${CI_TEMPLATE_REGISTRY_HOST}/pages/hugo/hugo_extended:0.135.0"
956
tags:
1057
- build_docs
1158
rules:
12-
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
59+
!reference [.default-rules, rules]
1360
variables:
1461
GIT_SUBMODULE_STRATEGY: recursive
1562
NAME: "${CI_COMMIT_REF_SLUG}"
@@ -30,7 +77,7 @@ deploy_preview_hugo:
3077
- deploy_docs
3178
- shiny
3279
rules:
33-
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
80+
!reference [.default-rules, rules]
3481
needs: ["build_hugo"]
3582
variables:
3683
SSH_KEY: "$DOCS_PREVIEW_PRIVATEKEY" # SSH_KEY used inside espressif/scp
@@ -50,46 +97,132 @@ deploy_preview_hugo:
5097
- echo "Preview ${SERVER_URL_BASE}/${NAME}"
5198

5299
post_preview_link:
53-
stage: comment
54-
image: badouralix/curl-jq
100+
extends: .comment_template
101+
stage: comment_preview
102+
rules:
103+
!reference [.default-rules, rules]
104+
needs: ["deploy_preview_hugo"]
105+
variables:
106+
SERVER_URL_BASE: "$DOCS_PREVIEW_URL_BASE"
107+
NAME: "${CI_COMMIT_REF_SLUG}"
108+
MR_ID: "$CI_MERGE_REQUEST_IID"
109+
PROJECT_ID: "$CI_MERGE_REQUEST_PROJECT_ID"
110+
script:
111+
- |
112+
# Create varialbes for adding a comment
113+
export COMMENT_IDENTIFIER="🎉 Preview for this MR"
114+
export PREVIEW_LINK="${SERVER_URL_BASE}/${NAME}"
115+
export COMMENT_BODY="🎉 Preview for this MR: ${PREVIEW_LINK}"
116+
117+
- *add_comment_script
118+
119+
sync_merge_to_github:
120+
stage: sync_merge
121+
image: alpine:latest
55122
tags:
56123
- deploy_docs
57124
- shiny
58125
rules:
59-
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
60-
needs: ["deploy_preview_hugo"]
126+
!reference [.label-based-rules, rules]
127+
variables:
128+
GITHUB_REPO: "espressif/developer-portal"
129+
script:
130+
- apk update # Update the package index
131+
- apk add --no-cache git bash github-cli # Install github-cli
132+
133+
- export SOURCE_BRANCH="${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
134+
135+
# Configure Git
136+
- git config --global user.email "[email protected]"
137+
- git config --global user.name "Developer-Portal-BOT"
138+
139+
# Clone the repository
140+
- git clone "$CI_REPOSITORY_URL" repo || (echo "Error cloning repository" && exit 1)
141+
- cd repo
142+
# Rebase the feature branch on main
143+
- git checkout "$SOURCE_BRANCH"
144+
- git fetch origin main
145+
- git rebase main
146+
# Add GitHub remote and push the rebased branch to GitHub
147+
- git remote add github "https://oauth2:${GITHUB_ACCESS_TOKEN}@github.com/${GITHUB_REPO}.git"
148+
# Consider using --force-with-lease here
149+
- git push -u -f github "$SOURCE_BRANCH"
150+
151+
# Wait for GitHub to register the branch
152+
- sleep 5
153+
154+
# Create a PR on GitHub
155+
- export GITHUB_TOKEN="$GITHUB_ACCESS_TOKEN"
156+
- |
157+
PR_URL=$(gh pr list \
158+
--repo "$GITHUB_REPO" \
159+
--head "$SOURCE_BRANCH" \
160+
--json url \
161+
--jq '.[].url')
162+
163+
if [[ -n "$PR_URL" ]]; then
164+
PR_NUMBER=$(gh pr view "$PR_URL" --json number -q '.number')
165+
echo "PR already exists: #$PR_NUMBER"
166+
echo "**GitHub PR:** $PR_URL"
167+
else
168+
echo "No PR found. Creating a new one..."
169+
gh pr create \
170+
--repo "$GITHUB_REPO" \
171+
--head "$SOURCE_BRANCH" \
172+
--base main \
173+
--title "Sync Merge: ${SOURCE_BRANCH}" \
174+
--body $'This PR syncs the GitLab branch `'"${SOURCE_BRANCH}"$'` to GitHub.\n\nThe changes have been reviewed internally.\n\n> [!NOTE]\n>If, for any reason, changes need be committed directly to the GitHub PR (bypassing GitLab), add in GitLab MR the label \`GitHub-Edit\`. This disables GitLab CI sync-merge to prevent overwriting changes on GitHub."
175+
176+
PR_URL=$(gh pr list \
177+
--repo "$GITHUB_REPO" \
178+
--head "$SOURCE_BRANCH" \
179+
--json url \
180+
--jq '.[].url')
181+
182+
if [[ -n "$PR_URL" ]]; then
183+
PR_NUMBER=$(gh pr view "$PR_URL" --json number -q '.number')
184+
echo "PR created successfully: #$PR_NUMBER"
185+
echo "**GitHub PR:** $PR_URL"
186+
else
187+
echo "Failed to create PR."
188+
exit 1
189+
fi
190+
fi
191+
192+
# Store PR_NUMBER and PR_URL as artifacts
193+
- cd $CI_PROJECT_DIR
194+
- echo "PR_NUMBER=$PR_NUMBER" > pr_info.txt
195+
- echo "PR_URL=$PR_URL" >> pr_info.txt
196+
197+
artifacts:
198+
paths:
199+
- pr_info.txt
200+
expire_in: 1 hour
201+
202+
sync_merge_comment:
203+
extends: .comment_template
204+
stage: comment_github_pr
205+
rules:
206+
!reference [.label-based-rules, rules]
207+
needs:
208+
- sync_merge_to_github
61209
variables:
62210
SERVER_URL_BASE: "$DOCS_PREVIEW_URL_BASE"
63211
NAME: "${CI_COMMIT_REF_SLUG}"
64212
MR_ID: "$CI_MERGE_REQUEST_IID"
65213
PROJECT_ID: "$CI_MERGE_REQUEST_PROJECT_ID"
66214
script:
67215
- |
68-
# Print MR_ID and PROJECT_ID for debugging
69-
echo "MR_ID: ${MR_ID}"
70-
echo "PROJECT_ID: ${PROJECT_ID}"
71-
echo "$CI_SERVER_HOST"
72-
echo "${CI_SERVER_PORT}"
73-
74-
GITLAB_API="https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_ID}/notes"
75-
AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_BOT_API_TOKEN}"
76-
77-
# Get existing comments
78-
API_RESPONSE=$(curl --silent --header "$AUTH_HEADER" "$GITLAB_API")
79-
echo "API Response: $API_RESPONSE" # Add this line for debugging
80-
81-
# Check if the response contains the expected structure
82-
COMMENTS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.body | contains("🎉 Preview for this MR")) | .id')
83-
84-
# Delete previous preview comments
85-
if [ -n "$COMMENTS" ]; then
86-
for COMMENT_ID in $COMMENTS; do
87-
curl --silent --request DELETE --header "$AUTH_HEADER" "${GITLAB_API}/${COMMENT_ID}"
88-
done
216+
# Load PR_NUMBER and PR_URL from artifact file
217+
if [ -f "pr_info.txt" ]; then
218+
source pr_info.txt
219+
else
220+
echo "pr_info.txt not found!"
221+
exit 1
89222
fi
90223
91-
# Post new preview link
92-
PREVIEW_LINK="${SERVER_URL_BASE}/${NAME}"
93-
COMMENT_BODY="🎉 Preview for this MR: ${PREVIEW_LINK}"
94-
curl --silent --request POST --header "$AUTH_HEADER" --header "Content-Type: application/json" \
95-
--data "{\"body\": \"${COMMENT_BODY}\"}" "$GITLAB_API"
224+
# Create varialbes for adding a comment
225+
COMMENT_IDENTIFIER="🚀 GitHub PR"
226+
COMMENT_BODY="🚀 GitHub PR for sync-merging: [#$PR_NUMBER]($PR_URL).\\n\\n> [!NOTE]\\n> \\n> If, for any reason, changes need be committed directly to the GitHub PR (bypassing GitLab), add the label \`GitHub-Edit\` here. This disables GitLab CI sync-merge to prevent overwriting changes on GitHub."
227+
228+
- *add_comment_script

0 commit comments

Comments
 (0)