From 812d2567f5403343bade776964e3bb69b34ca8a0 Mon Sep 17 00:00:00 2001 From: SmAsHeD <6071159+smashedr@users.noreply.github.com> Date: Thu, 26 Sep 2024 00:50:17 -0700 Subject: [PATCH] Create (#1) * Add Create Option * Remove Linter * Fix Source * Add curl * Fix Order * Update README.md * Fix owner * Set GIT_URL --- .github/workflows/test.yaml | 28 ++++++++++++++++------------ Dockerfile | 2 +- README.md | 30 +++++++++++++++++------------- action.yml | 3 +++ src/codeberg.sh | 34 ++++++++++++++++++++++++++++++++++ src/main.sh | 14 +++++++++++++- 6 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 src/codeberg.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5e9428d..45ac5f0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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/action-shellcheck@2.0.0 +# +# lint: +# name: "Lint" +# runs-on: ubuntu-latest +# timeout-minutes: 5 +# +# steps: +# - name: "Checkout" +# uses: actions/checkout@v4 +# +# - name: "Run ShellCheck" +# uses: ludeeus/action-shellcheck@2.0.0 +# with: +# check_together: "yes" +# SHELLCHECK_OPTS: "-e SC1091 -e SC2034" diff --git a/Dockerfile b/Dockerfile index bb56209..fe89f4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index e9ba7cd..f5e9df8 100644 --- a/README.md +++ b/README.md @@ -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' @@ -67,6 +70,7 @@ jobs: #host: https://codeberg.org #owner: cssnr #repo: mirror-repository-action + #create: true username: shaner password: ${{ secrets.CODEBERG_TOKEN }} ``` diff --git a/action.yml b/action.yml index 60af1e8..01397f7 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/codeberg.sh b/src/codeberg.sh new file mode 100644 index 0000000..9b87929 --- /dev/null +++ b/src/codeberg.sh @@ -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}" + diff --git a/src/main.sh b/src/main.sh index 285d242..e747415 100644 --- a/src/main.sh +++ b/src/main.sh @@ -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}" @@ -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