From aeba715f2876e0b29091e5fa75ad69e2ee968173 Mon Sep 17 00:00:00 2001 From: Mahesh Bandara Wijerathna Date: Tue, 24 Aug 2021 20:11:59 +0530 Subject: [PATCH 1/3] feat: add third-party (remote) repository support --- action.yml | 3 +++ entrypoint.sh | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 5372cbd..ae9fe58 100644 --- a/action.yml +++ b/action.yml @@ -5,6 +5,9 @@ branding: icon: 'git-pull-request' color: 'black' inputs: + destination_repository: + description: Repository (user/repo) to create the pull request in, falls back to checkout repository or triggered repository + required: false source_branch: description: Branch name to pull from, default is triggered branch required: false diff --git a/entrypoint.sh b/entrypoint.sh index ce2dd73..83a0a64 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -18,9 +18,10 @@ else fi DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH:-"master"}" +DESTINATION_REPOSITORY="${INPUT_DESTINATION_REPOSITORY:-$GITHUB_REPOSITORY}" # Github actions no longer auto set the username and GITHUB_TOKEN -git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" +git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$DESTINATION_REPOSITORY" # Pull all branches references down locally so subsequent commands can see them git fetch origin '+refs/heads/*:refs/heads/*' --update-head-ok From 0c1c08e7a50b2ce9dcc6b2d0ece3f7b3bfb29d20 Mon Sep 17 00:00:00 2001 From: Mahesh Bandara Wijerathna Date: Tue, 24 Aug 2021 20:22:48 +0530 Subject: [PATCH 2/3] feat: add support for `checkout` step remote repository --- entrypoint.sh | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 83a0a64..8573894 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -18,7 +18,28 @@ else fi DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH:-"master"}" -DESTINATION_REPOSITORY="${INPUT_DESTINATION_REPOSITORY:-$GITHUB_REPOSITORY}" + +# Determine repository url +if [[ -z "$INPUT_DESTINATION_REPOSITORY" ]]; then + # Try to query local repository's remote url if INPUT_DESTINATION_REPOSITORY is null + local_origin=$(git config --get remote.origin.url) + + if [[ "$local_origin" == *.git ]]; then + origin_no_suffix="${local_origin%.*}" + else + origin_no_suffix="$local_origin" + fi + + repo="$(basename "${origin_no_suffix}")" + owner="$(basename "${origin_no_suffix%/${repo}}")" + + if [[ ! -z "$repo" ]]; then + CHECKOUT_REPOSITORY="$owner/$repo" + fi +fi + +# Fallback to GITHUB_REPOSITORY if both INPUT_DESTINATION_REPOSITORY and CHECKOUT_REPOSITORY are unavailable +DESTINATION_REPOSITORY="${INPUT_DESTINATION_REPOSITORY:-${CHECKOUT_REPOSITORY:-${GITHUB_REPOSITORY}}}" # Github actions no longer auto set the username and GITHUB_TOKEN git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$DESTINATION_REPOSITORY" From ff6c35e8b32b5881ab4f8b2554b173112d364e17 Mon Sep 17 00:00:00 2001 From: Mahesh Bandara Wijerathna Date: Tue, 24 Aug 2021 20:54:16 +0530 Subject: [PATCH 3/3] docs: add third-party repository usage --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index e52fd55..f07a420 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ jobs: - name: pull-request uses: repo-sync/pull-request@v2 with: + destination_repository: "owner/repository" # If blank, default: checked out repository or triggered repository source_branch: "" # If blank, default: triggered branch destination_branch: "master" # If blank, default: master pr_title: "Pulling ${{ github.ref }} into master" # Title of pull request @@ -69,6 +70,33 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} ``` +### Third-party repositories + +Since it's possible to `checkout` third-party repositories, you can either define `destination_repository` manually or let +this action automatically pick up the checked out repository. + +```yaml +jobs: + pull-request: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + repository: "octocat/hello-world" + - name: pull-request + uses: repo-sync/pull-request@v2 + with: + destination_branch: "main" + github_token: ${{ secrets.GITHUB_TOKEN }} + # destination_repository: "octocat/hello-world" <- You can also do this but not necessary +``` + +**Priority will be set as follows:** + +1. `destination_repository` (Manually set) +2. Checked out repository +3. Repository that triggered the action (`GITHUB_REPOSITORY`) + ### Outputs The following outputs are available: `pr_url`, `pr_number`, `has_changed_files ("true"|"false")`.