Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for remote target repos #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ Configure the action with a branch on your `target` repo - the one you want to u

| Name | Required? | Default | Example |
| -------------------------- | :----------------: | ------- | ---------------------------------------- |
| target_sync_repo | :white_check_mark: | | |
| target_sync_branch | :white_check_mark: | | 'master', 'main', 'my-branch' |
| target_repo_token | :white_check_mark: | | ${{ secrets.GITHUB_TOKEN }} |
| upstream_repo_access_token | | | ${{ secrets.NAME_OF_TOKEN }} |
| upstream_sync_repo | :white_check_mark: | | 'aormsby/Fork-Sync-With-Upstream-action' |
| upstream_sync_branch | :white_check_mark: | | 'master', 'main', 'my-branch' |
| test_mode | | false | true / false |

**Always** set `target_repo_token` to `${{ secrets.GITHUB_TOKEN }}` so the action can push to your target repo.
`target_sync_repo` can ***only*** be a repo in which you have owner access.
If it corresponds to the repo in which the workflow is being used set `target_repo_token` as `${{ secrets.GITHUB_TOKEN }}`, otherwise `target_repo_token` should be a **[Private Access Token](https://github.com/aormsby/Fork-Sync-With-Upstream-action/wiki/Setup-Access-Token)** (`persist-credentials: false` is also **[needed](https://github.com/aormsby/Fork-Sync-With-Upstream-action/wiki/Configuration#private)**)

> For more information on optional input variables, advanced configurations, and working with private repos, see [Wiki - Configuration](https://github.com/aormsby/Fork-Sync-With-Upstream-action/wiki/Configuration)

Expand Down Expand Up @@ -91,8 +93,9 @@ jobs:
id: sync
uses: aormsby/[email protected]
with:
target_sync_repo: my-repo
target_sync_branch: my-branch
# REQUIRED 'target_repo_token' exactly like this!
# can also be target_repo_token: ${{ secrets.MY_TOKEN }} (see Input Variables)
target_repo_token: ${{ secrets.GITHUB_TOKEN }}
upstream_sync_branch: main
upstream_sync_repo: aormsby/Fork-Sync-With-Upstream-action
Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ branding:
color: orange

inputs:
target_sync_repo:
description: 'Repo receiving updates from the upstream repo'
required: true

target_sync_branch:
description: 'Branch receiving updates from the upstream repo, e.g. => main, master, prod'
required: true
Expand Down
10 changes: 9 additions & 1 deletion entry/config_and_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if [ -z "${GITHUB_ACTIONS}" ] || [ "${GITHUB_ACTIONS}" = false ]; then
# shellcheck disable=SC2034
INPUT_TARGET_REPO_TOKEN=""
# shellcheck disable=SC2034
GITHUB_REPOSITORY=""
INPUT_TARGET_SYNC_REPO=""

# required vars (except token)
# shellcheck disable=SC2034
Expand Down Expand Up @@ -62,6 +62,14 @@ if [ -z "${GITHUB_ACTIONS}" ] || [ "${GITHUB_ACTIONS}" = false ]; then
INPUT_HOST_DOMAIN='github.com'
fi

if [ -z "${INPUT_TARGET_REPO_TOKEN}" ]; then
# shellcheck disable=SC2034
TARGET_REPO_URL="https://${INPUT_HOST_DOMAIN}/${INPUT_TARGET_SYNC_REPO}.git"
else
# shellcheck disable=SC2034
TARGET_REPO_URL="https://${GITHUB_ACTOR}:${INPUT_TARGET_REPO_TOKEN}@${INPUT_HOST_DOMAIN}/${INPUT_TARGET_SYNC_REPO}.git"
fi

if [ -z "${INPUT_UPSTREAM_REPO_ACCESS_TOKEN}" ]; then
# shellcheck disable=SC2034
UPSTREAM_REPO_URL="https://${INPUT_HOST_DOMAIN}/${INPUT_UPSTREAM_SYNC_REPO}.git"
Expand Down
4 changes: 4 additions & 0 deletions entry/run_action.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
. "${ACTION_PARENT_DIR}"/run/config_git.sh
config_for_action

# set target repo
. "${ACTION_PARENT_DIR}"/run/set_target_repo.sh
set_target

# checkout target branch in target repo
. "${ACTION_PARENT_DIR}"/run/checkout_branch.sh
checkout
Expand Down
5 changes: 5 additions & 0 deletions entry/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ test_upstream_branch_exists
test_has_upstream_repo_access
cleanup_test_dir

# shellcheck disable=SC1091
. "${ACTION_PARENT_DIR}"/test/verify_target_access.sh
test_has_target_repo_access
cleanup_test_dir

# shellcheck disable=SC1091
. "${ACTION_PARENT_DIR}"/test/verify_git_config.sh
test_config_git
2 changes: 1 addition & 1 deletion run/push_updates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ push_new_commits() {
# TODO: figure out how this would work in local mode...
# update remote url with token since it is not persisted during checkout step when syncing from a private repo
if [ -n "${INPUT_TARGET_REPO_TOKEN}" ]; then
git remote set-url origin "https://${GITHUB_ACTOR}:${INPUT_TARGET_REPO_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git remote set-url origin "https://${GITHUB_ACTOR}:${INPUT_TARGET_REPO_TOKEN}@github.com/${INPUT_TARGET_SYNC_REPO}.git"
fi

# shellcheck disable=SC2086
Expand Down
13 changes: 13 additions & 0 deletions run/set_target_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

set_target() {
write_out -1 "Setting target repo to '${INPUT_TARGET_SYNC_REPO}'."
git remote add origin "${TARGET_REPO_URL}"

# # exit if target can't be accessed
# if ! git ls-remote -h "${TARGET_REPO_URL}" --quiet; then
# write_out "$?" "Could not verify target repo."
# fi

write_out "g" "SUCCESS\n"
}
16 changes: 15 additions & 1 deletion test/verify_branches.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#!/bin/sh

test_target_repo_exists() {
write_out "y" "TEST"
write_out -1 "[Verify Target Sync Repo Exists] -> tests 'target_sync_repo' input"
VERIFY_TARGET_REPO=$(git ls-remote "${TARGET_REPO_URL}" --quiet)

# var is empty on success, so fail if return value has any data
if [ -n "${VERIFY_TARGET_REPO}" ]; then
write_out "r" "FAILED - Target repo '${INPUT_TARGET_SYNC_REPO}' not found OR you do not have permission to view it\n"
else
write_out "g" "PASSED\n"
fi

}

test_target_branch_exists() {
write_out "y" "TEST"
write_out -1 "[Verify Target Sync Branch] -> tests 'target_sync_branch' input"
VERIFY_TARGET_BRANCH=$(git rev-parse --verify "remotes/origin/${INPUT_TARGET_SYNC_BRANCH}")
VERIFY_TARGET_BRANCH=$(git ls-remote "${TARGET_REPO_URL}" "${INPUT_TARGET_SYNC_BRANCH}" --quiet)

if [ -z "${VERIFY_TARGET_BRANCH}" ]; then
write_out "r" "FAILED - no branch '${INPUT_TARGET_SYNC_BRANCH}' to run action on\nDid you set 'ref' correctly in the checkout step?\n"
Expand Down
43 changes: 43 additions & 0 deletions test/verify_target_access.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh

TEST_CLONE_DIR="fork-action-test-clone-dir"
SKIP_CLEANUP=false

# in case test dir already exits, delete it
pretest_remove_dir() {
rm -rf "${TEST_CLONE_DIR}"
}

test_has_target_repo_access() {
pretest_remove_dir

write_out "y" "TEST"
write_out -1 "[Target Repo Access] -> Try to clone private repo with access token / git clone should succeed"

git clone --quiet --depth 1 "${TARGET_REPO_URL}" "${TEST_CLONE_DIR}"
COMMAND_EXIT_CODE=$?

if [ "${COMMAND_EXIT_CODE}" -eq "0" ]; then
write_out "g" "PASSED" # no \n because of cleanup output
else
write_out "r" "FAILED - repo does not exist OR you do not have permission to clone from it\n"
SKIP_CLEANUP=true
fi
}

# remove test directory after clone is complete
cleanup_test_dir() {
if [ "${SKIP_CLEANUP}" = true ]; then
true # no-op skip
else
rm -rf "${TEST_CLONE_DIR}"
COMMAND_STATUS=$?

# warn if cloned test directory can't be removed
if [ "${COMMAND_STATUS}" != 0 ] ; then
write_out "r" "(Clone cleanup failed - please find and remove directory '${TEST_CLONE_DIR}')\n"
else
write_out -1 "(Clone directory cleanup successful.)\n"
fi
fi
}