Skip to content

Commit

Permalink
Fix issue 6 with private repos (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Lambert <[email protected]>
  • Loading branch information
lefrog and Pascal Lambert authored Jan 2, 2023
1 parent 22f5e45 commit f9e5f76
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
111 changes: 111 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Testing the Script

Here are some guidelines/steps which will allow you to test changes.

First it assumed you've created for yourself a repository on GitHub.
Note that in order to be able to finally test the github action in isolation, you'll need to grant "Read and Write permissions"
for actions on your repository.
ref: https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#configuring-the-default-github_token-permissions

## Test Cases

### Initial setup

Clone your test repository
```
git clone https://your_repo
```

Make some changes in the master branch

```
#git checkout master
cat << EOF >> file1.txt
function foo () {
console.log('hello foo')
}
function bar () {
console.log('hello bar')
}
EOF
git add file1.txt
git commit -m "first commit"
git push --set-upstream origin master
git tag init
git push origin init
```

#### Local Tests

T-01 Given a private repo, given a branch with no merge conflict,
when attempting to merge the branch it should succeed without errors.

```
export INPUT_DEVELOPMENT_BRANCH='feat-merge-no-conflict'
git checkout -b ${INPUT_DEVELOPMENT_BRANCH} init
sed -i 's/hello foo/hello my favorite foo/' file1.txt
git commit -a -m 'feature update 01'
git push --set-upstream origin ${INPUT_DEVELOPMENT_BRANCH}
git checkout master
sed -i 's/hello from foo/hello from foo again/' file1.txt
git commit -a -m 'update 02'
git push
GITHUB_TOKEN='...' GITHUB_REPOSITORY='lefrog/test-merging' GITHUB_WORKSPACE=$PWD INPUT_USER_NAME='your_github_user' INPUT_PUSH_TOKEN='GITHUB_TOKEN' INPUT_USER_EMAIL='[email protected]' INPUT_STABLE_BRANCH='master' ${PATH_TO_YOUR_CODE}/gh-action-nightly-merge/entrypoint.sh
```

T-02 Given a private repo, given a branch to merge which will have merge conflict,
when attempting to merge the branch it should fail and indicate merge conflicts
must be resolved.

```
export INPUT_DEVELOPMENT_BRANCH='feat-merge-with-conflict'
git checkout -b ${INPUT_DEVELOPMENT_BRANCH} init
echo "hello again" >> file1.txt
git add file1.txt
git commit -m "new content from ${INPUT_DEVELOPMENT_BRANCH}"
git push --set-upstream origin ${INPUT_DEVELOPMENT_BRANCH}
git checkout main
echo "hello gang" >> file1.txt
git add file1.txt
git commit -m "new content from main"
git push
GITHUB_TOKEN='...' GITHUB_REPOSITORY='lefrog/test-merging' GITHUB_WORKSPACE=$PWD INPUT_USER_NAME='your_github_user' INPUT_PUSH_TOKEN='GITHUB_TOKEN' INPUT_USER_EMAIL='[email protected]' INPUT_STABLE_BRANCH='main' ${PATH_TO_YOUR_CODE}/gh-action-nightly-merge/entrypoint.sh
```

#### End-to-end tests

In your test repository, add commit/push the following github action in your master branch

```
#file: .github/workflows/main.yml
name: 'Merge Stable branch'
on:
workflow_dispatch:
jobs:
nightly-merge:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Nightly Merge
uses: lefrog/gh-action-nightly-merge@fix-issue-6
with:
stable_branch: 'test-master'
development_branch: 'feat-merge-no-conflict'
allow_ff: false
user_name: 'github-actions[bot]'
user_email: '[email protected]'
push_token: 'GITHUB_TOKEN'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}
```

and repeat the same changes above but manually trigger the action.
3 changes: 2 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ echo
if [[ $INPUT_ALLOW_FORKS != "true" ]]; then
URI=https://api.github.com
API_HEADER="Accept: application/vnd.github.v3+json"
pr_resp=$(curl -X GET -s -H "${API_HEADER}" "${URI}/repos/$GITHUB_REPOSITORY")
AUTH_HEADER=$([ -z "${GITHUB_TOKEN}" ] && echo 'foo: bar' || echo "Authorization: bearer ${GITHUB_TOKEN}")
pr_resp=$(curl -X GET -s -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/$GITHUB_REPOSITORY")
if [[ "$(echo "$pr_resp" | jq -r .fork)" != "false" ]]; then
echo "Nightly merge action is disabled for forks (use the 'allow_forks' option to enable it)."
exit 0
Expand Down

0 comments on commit f9e5f76

Please sign in to comment.