Skip to content

Commit 9ced15c

Browse files
committed
Added sync PR to GitLab
1 parent 26d79a8 commit 9ced15c

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Sync PR to GitLab
2+
3+
on:
4+
# Draait als je een label toevoegt op een PR
5+
pull_request_target:
6+
types: [labeled]
7+
8+
jobs:
9+
sync-pr-to-gitlab:
10+
if: github.event.label.name == 'sync-to-gitlab'
11+
runs-on: ubuntu-latest
12+
13+
env:
14+
GITLAB_REPO_URL: ${{ secrets.GITLAB_REPO_URL }} # bv. https://gitlab.com/elgentos/magento2-playwright.git
15+
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }} # GitLab Personal Access Token (api scope)
16+
GITLAB_PROJECT_ID: ${{ secrets.GITLAB_PROJECT_ID }} # Numeriek project ID uit GitLab
17+
GITLAB_API_URL: ${{ secrets.GITLAB_API_URL }} # bv. https://gitlab.com/api/v4
18+
GITLAB_TARGET_BRANCH: ${{ secrets.GITLAB_TARGET_BRANCH }} # bv. main
19+
20+
steps:
21+
- name: Set defaults
22+
id: defaults
23+
run: |
24+
# Fallbacks als secrets niet gezet zijn
25+
if [ -z "$GITLAB_API_URL" ]; then
26+
echo "GITLAB_API_URL=https://gitlab.com/api/v4" >> $GITHUB_ENV
27+
fi
28+
29+
if [ -z "$GITLAB_TARGET_BRANCH" ]; then
30+
echo "GITLAB_TARGET_BRANCH=main" >> $GITHUB_ENV
31+
fi
32+
33+
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
34+
echo "PR_TITLE=${{ github.event.pull_request.title }}" >> $GITHUB_ENV
35+
echo "PR_URL=${{ github.event.pull_request.html_url }}" >> $GITHUB_ENV
36+
echo "PR_BRANCH=github-pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
37+
38+
- name: Checkout PR head (unmerged)
39+
uses: actions/checkout@v4
40+
with:
41+
# We zitten in pull_request_target, dus expliciet de PR-head referentie checken
42+
ref: refs/pull/${{ github.event.pull_request.number }}/head
43+
fetch-depth: 0
44+
45+
- name: Push branch to GitLab
46+
run: |
47+
if [ -z "$GITLAB_REPO_URL" ]; then
48+
echo "GITLAB_REPO_URL secret is not set"; exit 1;
49+
fi
50+
51+
git config user.name "github-actions[bot]"
52+
git config user.email "github-actions[bot]@users.noreply.github.com"
53+
54+
echo "Adding GitLab remote: $GITLAB_REPO_URL"
55+
git remote add gitlab "$GITLAB_REPO_URL"
56+
57+
echo "Pushing branch to GitLab as $PR_BRANCH"
58+
git push gitlab HEAD:"$PR_BRANCH" --force
59+
60+
- name: Create Merge Request on GitLab
61+
run: |
62+
if [ -z "$GITLAB_PROJECT_ID" ]; then
63+
echo "GITLAB_PROJECT_ID secret is not set"; exit 1;
64+
fi
65+
66+
TITLE="GitHub PR #$PR_NUMBER: $PR_TITLE"
67+
DESCRIPTION="Deze Merge Request is automatisch aangemaakt vanuit GitHub PR #$PR_NUMBER ($PR_URL)."
68+
69+
echo "Creating MR on GitLab:"
70+
echo " Project ID: $GITLAB_PROJECT_ID"
71+
echo " Source: $PR_BRANCH"
72+
echo " Target: $GITLAB_TARGET_BRANCH"
73+
74+
curl --fail -X POST "$GITLAB_API_URL/projects/$GITLAB_PROJECT_ID/merge_requests" \
75+
--header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
76+
--data-urlencode "source_branch=$PR_BRANCH" \
77+
--data-urlencode "target_branch=$GITLAB_TARGET_BRANCH" \
78+
--data-urlencode "title=$TITLE" \
79+
--data-urlencode "description=$DESCRIPTION" \
80+
--data "remove_source_branch=true" \
81+
--data "labels=github,external"

0 commit comments

Comments
 (0)