Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
feat: Add third-party repository support (#69)
Browse files Browse the repository at this point in the history
* feat: add third-party (remote) repository support

* feat: add support for `checkout` step remote repository

* docs: add third-party repository usage
  • Loading branch information
m4heshd authored Dec 24, 2022
1 parent 1b6c626 commit d5b8353
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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
Expand All @@ -71,6 +72,33 @@ jobs:
github_token: ${{ secrets.CUSTOM_GH_TOKEN }} # If blank, default: 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")`.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ branding:
icon: 'git-pull-request'
color: 'gray-dark'
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
Expand Down
24 changes: 23 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,33 @@ fi

DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH:-"master"}"

# 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}}}"

# Fix for the unsafe repo error: https://github.com/repo-sync/pull-request/issues/84
git config --global --add safe.directory /github/workspace

# Github actions no longer auto set the username and GITHUB_TOKEN
git remote set-url origin "https://x-access-token:$GITHUB_TOKEN@${GITHUB_SERVER_URL#https://}/$GITHUB_REPOSITORY"
git remote set-url origin "https://x-access-token:$GITHUB_TOKEN@${GITHUB_SERVER_URL#https://}/$DESTINATION_REPOSITORY"

# Pull all branches references down locally so subsequent commands can see them
git fetch origin '+refs/heads/*:refs/heads/*' --update-head-ok
Expand Down

0 comments on commit d5b8353

Please sign in to comment.