forked from danskernesdigitalebibliotek/dpl-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
100 lines (84 loc) · 3.94 KB
/
create-pr.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# This workflow is triggered when the remote dependency repos request it.
# It will create a PR with the new dependency and the new branch.
# The PR will be created from the develop branch.
# Needed env variables:
# PULL_REQUEST_GIT_USER - The git user that will be used to create the branch and the PR.
# ACCEPTED_BUILD_URL_PREFIXES - The build URL need to match one of the prefixes in order to be accepted.
name: Create PR with external dependency
on:
repository_dispatch:
types: [create_pr]
jobs:
build:
name: Create PR with external dependency
runs-on: ubuntu-latest
steps:
- name: Verify Event and set env
env:
PR_BRANCH: ${{ github.event.client_payload.branch }}
PACKAGE: ${{ github.event.client_payload.dependency_package }}
PACKAGE_NAME: echo "${{ github.event.client_payload.dependency_package }}" | sed 's/[^\/]*\///'
run: |
echo "PR_BRANCH=$PR_BRANCH" >> $GITHUB_ENV
echo "PACKAGE=$PACKAGE" >> $GITHUB_ENV
echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_ENV
PACKAGE_NAME_IS_VALID=$(echo '${{ vars.ACCEPTED_PACKAGES }}' | jq --arg project "${{ env.PACKAGE }}" 'any(.[]; . as $name | $project | match($name))')
echo "PACKAGE_NAME_IS_VALID=$PACKAGE_NAME_IS_VALID" >> $GITHUB_ENV
echo $GITHUB_ENV
# if: ${{ github.event.action == 'create_pr' && github.event.client_payload.branch && github.event.client_payload.dependency_package }}
- name: Stop workflow if any variables are missing
run: |
echo "::error::Some variable is missing. Have to stop..."
exit 1
if: ${{ !env.PR_BRANCH || !env.PACKAGE || !env.PACKAGE_NAME || !vars.PACKAGE_NAME_IS_VALID }}
- name: Stop workflow if build url is not valid
run: |
echo "::error::The build URL is not valid. Should match one of the accepted prefixes."
exit 1
if: ${{ env.BUILD_URL_IS_ACCEPTED != 'true' }}
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: check_branch_exists
uses: GuillaumeFalourd/branch-exists@v1
with:
branch: ${{ env.PR_BRANCH }}
- name: Stop workflow if branch already exists
run: |
echo "::error::A branch with the same name as the requested branch name already exists."
exit 1
if: ${{ steps.check_branch_exists.outputs.exists == 'true' }}
- name: Insert dependency
id: manipulate_composer
run: |
set -e
composer config repositories.${{ env.PACKAGE }} '{"type":"package","package":{"name":"${{ env.PACKAGE }}","version":"dev-master","type":"drupal-library","dist":{"url":"${{ env.BUILD_URL }}","type":"zip"},"require":{"composer\/installers":"^1.2.0"}}}'
composer remove ${{ env.PACKAGE }} \
&& composer require ${{ env.PACKAGE }}
- name: Create branch
id: create_branch
uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: ${{ env.PR_BRANCH }}
commit_message: "Insert new reference to dependency ${{ env.PACKAGE }}: ${{ env.BUILD_URL }}"
create_branch: true
if: ${{ steps.manipulate_composer.outcome == 'success' }}
- name: Create PR
id: create_pr
run: |
set -e
gh pr create \
--base develop \
--head ${{ env.PR_BRANCH }} \
--title "${{ format('PR for {0}:{1}', env.PACKAGE, env.PR_BRANCH) }}" \
--body "${{ format('This is an automated PR for {0}:{1}', env.PACKAGE, env.PR_BRANCH) }}"
if: ${{ steps.create_branch.outcome == 'success' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Adding summary
run: |
URL=$(gh pr view --json=url ${{ env.PR_BRANCH }} | jq .url)
echo "Created automated PR at: ${URL}" >> $GITHUB_STEP_SUMMARY
if: ${{ steps.create_pr.outcome == 'success' }}
env:
GH_TOKEN: ${{ github.token }}