Skip to content

Commit d502786

Browse files
authored
Merge pull request #203 from elgentos/sync-pr-to-gitlab
Added sync PR to GitLab
2 parents 26d79a8 + f38d496 commit d502786

File tree

1 file changed

+79
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)