diff --git a/README.md b/README.md index 3396aad..ea996ec 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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")`. diff --git a/action.yml b/action.yml index f6fefcc..50f8c7f 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/entrypoint.sh b/entrypoint.sh index e1b8107..be87700 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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