Skip to content

Commit ead719d

Browse files
committed
Copy chkn/[email protected] files
1 parent 4bd9ba6 commit ead719d

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed

.github/composite/docs/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM alpine/git
2+
3+
RUN apk --no-cache --update add bash
4+
5+
RUN wget -q https://api.github.com/repos/cli/cli/releases/latest \
6+
&& wget -q $(cat latest | grep linux_amd64.tar.gz | grep browser_download_url | grep -v .asc | cut -d '"' -f 4) \
7+
&& tar -xvzf gh*.tar.gz \
8+
&& mv gh*/bin/gh /usr/local/bin/ \
9+
&& rm -fr *
10+
11+
ADD entrypoint.sh /entrypoint.sh
12+
RUN chmod +x /entrypoint.sh
13+
ENTRYPOINT [ "/entrypoint.sh" ]

.github/composite/docs/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Action pull request another repository
2+
This GitHub Action copies a folder from the current repository to a location in another repository and create a pull request.
3+
4+
This Action is a modified version of [paygoc6/action-pull-request-another-repo](https://github.com/paygoc6/action-pull-request-another-repo).
5+
6+
## Example Workflow
7+
name: Push File
8+
9+
on: push
10+
11+
jobs:
12+
pull-request:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
18+
- name: Create pull request
19+
uses: MDRCNY/[email protected]
20+
env:
21+
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
22+
with:
23+
source_folder: 'source-folder'
24+
destination_repo: 'user-name/repository-name'
25+
destination_folder: 'folder-name'
26+
destination_base_branch: 'branch-name'
27+
destination_head_branch: 'branch-name'
28+
user_email: '[email protected]'
29+
user_name: 'user-name'
30+
pull_request_reviewers: 'reviewers'
31+
title: 'title'
32+
body: 'body'
33+
34+
## Variables
35+
* source_folder: The folder to be moved. Uses the same syntax as the `cp` command. Incude the path for any files not in the repositories root directory.
36+
* destination_repo: The repository to place the file or directory in.
37+
* destination_folder: [optional] The folder in the destination repository to place the file in, if not the root directory.
38+
* user_email: The GitHub user email associated with the API token secret.
39+
* user_name: The GitHub username associated with the API token secret.
40+
* destination_base_branch: [optional] The branch into which you want your code merged. Default is `main`.
41+
* destination_head_branch: The branch to create to push the changes. Cannot be `master` or `main`.
42+
* pull_request_reviewers: [optional] The pull request reviewers. It can be only one (just like 'reviewer') or many (just like 'reviewer1,reviewer2,...')
43+
* title: [optional] The pull request title.
44+
* body: [optional] The pull request body.
45+
46+
## ENV
47+
* API_TOKEN_GITHUB: You must create a personal access token in you account. Follow the link:
48+
- [Personal access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token)
49+
50+
> You must select the scopes: 'repo = Full control of private repositories', 'admin:org = read:org' and 'write:discussion = Read:discussion';
51+
52+
53+
## Behavior Notes
54+
The action will create any destination paths if they don't exist. It will also overwrite existing files if they already exist in the locations being copied to. It will not delete the entire destination repository.

.github/composite/docs/action.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: 'MDRCNY-Create Pull Request for Another Repository'
2+
description: 'Used for pull request a copy of files or folder to another repository'
3+
inputs:
4+
source_path:
5+
description: 'Source path from origin'
6+
required: true
7+
destination_repo:
8+
description: 'Destination repository'
9+
required: true
10+
destination_path:
11+
description: 'Destination path in the destination repository'
12+
required: false
13+
user_email:
14+
description: 'Email for the git commit'
15+
required: true
16+
user_name:
17+
description: 'GitHub username for the commit'
18+
required: true
19+
destination_head_branch:
20+
description: 'The branch to create to push the changes. Cannot be master or main'
21+
required: true
22+
destination_base_branch:
23+
description: 'The branch into which you want your code merged.'
24+
required: false
25+
default: "main"
26+
pull_request_reviewers:
27+
description: 'Pull request reviewers users'
28+
required: false
29+
title:
30+
description: 'The title of the pull request'
31+
required: false
32+
default: "Pull Request for Repo: ${{ github.repository }}"
33+
body:
34+
description: 'The body of the pull request'
35+
required: false
36+
default: " "
37+
label:
38+
description: 'The label of the pull request'
39+
required: true
40+
outputs:
41+
pr_url:
42+
description: 'The URL of the pull request'
43+
created:
44+
description: 'Whether the pull request was created'
45+
runs:
46+
using: 'docker'
47+
image: 'Dockerfile'
48+
args:
49+
- ${{ inputs.source-path }}
50+
- ${{ inputs.destination-repo }}
51+
- ${{ inputs.destination-path }}
52+
- ${{ inputs.user-email }}
53+
- ${{ inputs.user-name }}
54+
- ${{ inputs.destination-head-branch }}
55+
- ${{ inputs.destination-base-branch }}
56+
- ${{ inputs.pull-request-reviewers }}
57+
- ${{ inputs.title }}
58+
- ${{ inputs.body }}
59+
- ${{ inputs.label }}
60+
branding:
61+
icon: 'git-commit'
62+
color: 'green'
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -x
5+
6+
if [ -z "$INPUT_SOURCE_PATH" ]
7+
then
8+
echo "Source path must be defined"
9+
exit -1
10+
fi
11+
12+
if [ $INPUT_DESTINATION_HEAD_BRANCH == "main" ] || [ $INPUT_DESTINATION_HEAD_BRANCH == "master"]
13+
then
14+
echo "Destination head branch cannot be 'main' nor 'master'"
15+
exit -1
16+
fi
17+
18+
if [ -z "$INPUT_PULL_REQUEST_REVIEWERS" ]
19+
then
20+
PULL_REQUEST_REVIEWERS=$INPUT_PULL_REQUEST_REVIEWERS
21+
else
22+
PULL_REQUEST_REVIEWERS='-r '$INPUT_PULL_REQUEST_REVIEWERS
23+
fi
24+
25+
CLONE_DIR=$(mktemp -d)
26+
27+
echo "Setting git variables"
28+
export GITHUB_TOKEN=$API_TOKEN_GITHUB
29+
git config --global user.email "$INPUT_USER_EMAIL"
30+
git config --global user.name "$INPUT_USER_NAME"
31+
32+
echo "Cloning destination git repository"
33+
git clone "https://$API_TOKEN_GITHUB@github.com/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR"
34+
35+
echo "Checking if branch already exists"
36+
set +e
37+
pushd "$CLONE_DIR"
38+
git checkout "$INPUT_DESTINATION_HEAD_BRANCH"
39+
BRANCH_ALREADY_EXISTS=$?
40+
popd
41+
set -e
42+
43+
echo "Copying contents to git repo"
44+
cp -r $INPUT_SOURCE_PATH "$CLONE_DIR/$INPUT_DESTINATION_PATH"
45+
cd "$CLONE_DIR"
46+
if [ $BRANCH_ALREADY_EXISTS -ne 0 ]
47+
then
48+
git checkout -b "$INPUT_DESTINATION_HEAD_BRANCH"
49+
fi
50+
51+
echo "Adding git commit"
52+
git add .
53+
if git status | grep -q "Changes to be committed"
54+
then
55+
git commit --message "Update from https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
56+
echo "Pushing git commit"
57+
git push -u origin HEAD:$INPUT_DESTINATION_HEAD_BRANCH
58+
echo "Creating a pull request"
59+
echo -n "pr_url=" >> $GITHUB_OUTPUT
60+
61+
set +e
62+
gh pr create -t "$INPUT_TITLE" \
63+
-b "$INPUT_BODY" \
64+
-B $INPUT_DESTINATION_BASE_BRANCH \
65+
-H $INPUT_DESTINATION_HEAD_BRANCH \
66+
-l $INPUT_LABEL \
67+
$PULL_REQUEST_REVIEWERS >> $GITHUB_OUTPUT
68+
69+
if [ $? -ne 0 ]
70+
then
71+
echo "created=false" >> $GITHUB_OUTPUT
72+
echo "Failed to create pull request"
73+
# if the branch already existed then it's ok that we couldn't create the PR because it was probably already created
74+
exit $BRANCH_ALREADY_EXISTS
75+
else
76+
echo "created=true" >> $GITHUB_OUTPUT
77+
echo "Pull request created"
78+
fi
79+
else
80+
echo "No changes detected"
81+
fi

0 commit comments

Comments
 (0)