Skip to content

Commit

Permalink
Create (#1)
Browse files Browse the repository at this point in the history
* Add Create Option

* Remove Linter

* Fix Source

* Add curl

* Fix Order

* Update README.md

* Fix owner

* Set GIT_URL
  • Loading branch information
smashedr authored Sep 26, 2024
1 parent 8b3b19d commit 812d256
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 27 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ jobs:
host: https://codeberg.org
#owner: cssnr
#repo: mirror-repository-action
create: true
username: shaner
password: ${{ secrets.CODEBERG_TOKEN }}

lint:
name: "Lint"
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- name: "Checkout"
uses: actions/checkout@v4

- name: "Run ShellCheck"
uses: ludeeus/[email protected]
#
# lint:
# name: "Lint"
# runs-on: ubuntu-latest
# timeout-minutes: 5
#
# steps:
# - name: "Checkout"
# uses: actions/checkout@v4
#
# - name: "Run ShellCheck"
# uses: ludeeus/[email protected]
# with:
# check_together: "yes"
# SHELLCHECK_OPTS: "-e SC1091 -e SC2034"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:latest

RUN apk add --update --no-cache bash git
RUN apk add --update --no-cache bash curl git

COPY src/ /src

Expand Down
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ Mirror Git Repository to Remote Host.

## Inputs

| input | required | default | description |
| -------- | ------------ | ---------- | ---------------------------------------------------- |
| url | No if `host` | - | Full URL to Mirror, overrides: `host`/`owner`/`repo` |
| host | No if `url` | - | Full Host to Mirror, example: `https://codeberg.org` |
| owner | No | Repo Owner | Repository Owner of Mirror |
| repo | No | Repo Name | Repository Name of Mirror |
| username | No | Repo Owner | Username for Authentication to Mirror |
| password | Yes | - | Token or Password for Authentication to Mirror |
| input | required | default | description |
| -------- | ------------- | ---------- | -------------------------------------------------------- |
| url | Not w/ `host` | - | \* Full URL to Mirror, overrides: `host`/`owner`/`repo` |
| host | Not w/ `url` | - | \* Full Host to Mirror, example: `https://codeberg.org` |
| owner | No | Repo Owner | \* Repository Owner of Mirror (if different from source) |
| repo | No | Repo Name | \* Repository Name of Mirror (if different from source) |
| create | No | - | \* Set to `true` to attempt to Create the Mirror Repo |
| username | No | Repo Owner | Username for Authentication to Mirror |
| password | Yes | - | Token or Password for Authentication to Mirror |

Note: You must provide either a `url` or `host`.
**url/host** - You must provide either a full repository `url` or a `host` value.

If providing a `host` the `url` is created from `host`/`owner`/`repo` using either provided values or source repository values.
**owner/repo** - If different from source, you must specify these values.

1. Create Remote Repository to Mirror Too, for example on: https://codeberg.org
2. Create a Token to use as a Password for Pushing Commits on this Mirror.
**create** - Tested with codeberg but should also work with gitea/fojgeo. Do not set or leave empty to disable.

1. Create a Token for Mirror to use as a Password for Pushing Commits, or Creating Repositories.
2. Create Remote Repository to Mirror, or set `create` to `true`, for example: `https://codeberg.org`
3. Go to the settings for your source repository on GitHub and add the `CODEBERG_TOKEN` secret.
4. Add the following file to the following location: `.github/workflows/mirror.yaml`
4. Add the following file to source repository on GitHub: `.github/workflows/mirror.yaml`

```yaml
name: 'Mirror'
Expand Down Expand Up @@ -67,6 +70,7 @@ jobs:
#host: https://codeberg.org
#owner: cssnr
#repo: mirror-repository-action
#create: true
username: shaner
password: ${{ secrets.CODEBERG_TOKEN }}
```
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ inputs:
repo:
description: "Remote Repository Name"
required: false
create:
description: "Create Remote Repository"
required: false
username:
description: "Remote Repository Username"
required: false
Expand Down
34 changes: 34 additions & 0 deletions src/codeberg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set +e

read -r -d '' BODY << EOF
{
"auto_init": true,
"default_branch": "master",
"name": "${REPO:?err}",
"object_format_name": "sha1",
"private": false
}
EOF

#echo "${BODY}"

if [ "${USERNAME}" = "${OWNER}" ];then
echo "Personal Repository Detected."
URL="${GIT_URL}/api/v1/user/repos"
else
echo "Organization Repository Detected."
URL="${GIT_URL}/api/v1/orgs/${OWNER}/repos"
fi

echo "CREATE URL: ${URL}"

curl -X POST \
-H "Authorization: token ${PASSWORD}" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "${BODY}" \
"${URL}"

14 changes: 13 additions & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ echo "---------- INPUTS ----------"
if [ -z "${INPUT_URL}" ];then
HOST="${INPUT_HOST:?err}"
echo "HOST: ${HOST}"
OWNER="${INPUT_USER:-${GITHUB_REPOSITORY_OWNER}}"
OWNER="${INPUT_OWNER:-${GITHUB_REPOSITORY_OWNER}}"
echo "OWNER: ${OWNER}"
REPO="${INPUT_REPO:-$(echo "${GITHUB_REPOSITORY}" | awk -F'/' '{print $2}')}"
echo "REPO: ${REPO}"
Expand All @@ -42,6 +42,18 @@ PASSWORD="${INPUT_PASSWORD:?err}"
GIT_HOST=$(echo "${REMOTE_URL}" | awk -F'/' '{print $3}')
echo "GIT_HOST: ${GIT_HOST}"

GIT_URL="https://${GIT_HOST}"
echo "GIT_URL: ${GIT_URL}"

if [ -n "${INPUT_CREATE}" ];then
echo "Attempting Create Repository: ${INPUT_CREATE}"
set +e
# shellcheck source=/src/codeberg.sh
source /src/codeberg.sh
set -e
fi


git config --global --add safe.directory "$(pwd)"

git config --global credential.helper cache
Expand Down

0 comments on commit 812d256

Please sign in to comment.