Skip to content

Commit

Permalink
refactoring how pull requests are done, switching to use github actio…
Browse files Browse the repository at this point in the history
…ns bot username (#10)

* default branch and fixing pr

* trying again

* updating stuff

* logging

* cleaning up stuff

* fixing inputs

* trying again

* adding documentation

* use github action bot for actions

* logging help
  • Loading branch information
kbrashears5 committed Mar 15, 2021
1 parent d8cfd4a commit d358037
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"entrypoint"
]
}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
| REPOSITORIES | true | List of repositories to sync the files to. Optionally provide branch name |
| FILES | true | List of files to sync across repositories. See below for details |
| TOKEN | true | Personal Access Token with Repo scope |
| PULL_REQUEST | false | Whether or not you want to do a pull request. Only works when branch name is provided. Default false |
| PULL_REQUEST_BRANCH_NAME | false | Branch name of branch to do pull request into. Default is no pull request opened |

## Examples
### REPOSITORIES parameter
Expand Down Expand Up @@ -110,6 +110,12 @@ Root folder with new directory
FILES: |
sync/=newFolderName/
```
### PULL_REQUEST_BRANCH_NAME parameter
Specify branch name to create pull request against
```yaml
PULL_REQUEST_BRANCH_NAME: main
```

### TOKEN parameter
Use the repository secret named `ACTIONS`
```yaml
Expand Down
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ branding:
color: 'purple'
inputs:
REPOSITORIES:
description: 'Github repositories to sync files to. Optionally provide a branch with @branchName after the repo name. Default is master'
description: 'Github repositories to sync files to. Optionally provide a branch with @branchName after the repo name. Default is default branch of the repository'
required: true
FILES:
description: 'Files to sync to repositories'
required: true
TOKEN:
description: 'Personal access token with Repo privileges'
required: true
PULL_REQUEST:
description: 'Whether or not to submit a pull request. Only works if branch is not master'
PULL_REQUEST_BRANCH_NAME:
description: 'Branch name to submit pull request to'
required: false
default: 'false'
default: ''

runs:
using: 'docker'
Expand Down
28 changes: 17 additions & 11 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ REPOSITORIES=($RAW_REPOSITORIES)
echo "Repositories : $REPOSITORIES"
FILES=($RAW_FILES)
echo "Files : $FILES"
PULL_REQUEST="$INPUT_PULL_REQUEST"
echo "Pull request : $PULL_REQUEST"
PULL_REQUEST_BRANCH_NAME="$INPUT_PULL_REQUEST_BRANCH_NAME"
echo "Pull request : $PULL_REQUEST_BRANCH_NAME"

# set temp path
TEMP_PATH="/ghafs/"
Expand All @@ -35,10 +35,10 @@ echo "---------------------------------------------"
echo " "

# initalize git
echo "Intiializing git"
echo "Initializing git with github-actions[bot]"
git config --system core.longpaths true
git config --global core.longpaths true
git config --global user.email "action-bot@github.com" && git config --global user.name "Github Action"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" && git config --global user.name "github-actions[bot]"
echo "Git initialized"

echo " "
Expand All @@ -52,25 +52,31 @@ for repository in "${REPOSITORIES[@]}"; do
REPO_NAME=${REPO_INFO[0]}
echo "Repository name: [$REPO_NAME]"

echo "Determining default branch name"
DEFAULT_BRANCH_NAME=$(curl -X GET -H "Accept: application/vnd.github.v3+json" --silent "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}" | jq '.default_branch')
echo "Default branch name: [$DEFAULT_BRANCH_NAME]"

# determine branch name
BRANCH_NAME="master"
echo "Determining instructed branch name"
if [ ${REPO_INFO[1]+yes} ]; then
BRANCH_NAME="${REPO_INFO[1]}"
else
BRANCH_NAME="$DEFAULT_BRANCH_NAME"
fi
echo "Branch: [$BRANCH_NAME]"

# clone the repo
REPO_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${REPO_NAME}.git"
GIT_PATH="${TEMP_PATH}${REPO_NAME}"
echo "Cloning [$REPO_URL] to [$GIT_PATH]"
git clone --quiet --no-hardlinks --no-tags --depth 1 $REPO_URL $REPO_NAME
git clone --quiet --no-hardlinks --no-tags --depth 1 $REPO_URL $GIT_PATH

cd $GIT_PATH

# checkout the branch, if specified
if [ "$BRANCH_NAME" != "master" ]; then
if [ "$BRANCH_NAME" != "$DEFAULT_BRANCH_NAME" ]; then
# try to check out the origin, if fails, then create the local branch
git fetch && git checkout $BRANCH_NAME && git pull || git checkout -b $BRANCH_NAME
git fetch && git checkout -b "$BRANCH_NAME" origin/"$BRANCH_NAME" || git checkout -b "$BRANCH_NAME"
fi

echo " "
Expand Down Expand Up @@ -134,9 +140,9 @@ for repository in "${REPOSITORIES[@]}"; do
# push changes
echo "Push changes to [${REPO_URL}]"
git push $REPO_URL
if [ "$BRANCH_NAME" != "master" -a "$PULL_REQUEST" == "true" ]; then
echo "Creating pull request"
jq -n --arg title "File sync from ${GITHUB_REPOSITORY}" --arg head "$BRANCH_NAME" --arg base "master" '{title:$title,head:$head,base:$base}' | curl -d @- \
if [ ! -z "$PULL_REQUEST_BRANCH_NAME" -a "$BRANCH_NAME" != "$PULL_REQUEST_BRANCH_NAME" ]; then
echo "Creating pull request from [$BRANCH_NAME] into [$PULL_REQUEST_BRANCH_NAME]"
jq -n --arg title "File sync from ${GITHUB_REPOSITORY}" --arg head "$BRANCH_NAME" --arg base $PULL_REQUEST_BRANCH_NAME '{title:$title,head:$head,base:$base}' | curl -d @- \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-u ${USERNAME}:${GITHUB_TOKEN} \
Expand Down

0 comments on commit d358037

Please sign in to comment.