diff --git a/.github/workflows/migrate-to-gitea.yml b/.github/workflows/migrate-to-gitea.yml new file mode 100644 index 0000000..362b537 --- /dev/null +++ b/.github/workflows/migrate-to-gitea.yml @@ -0,0 +1,106 @@ +name: Redirect contributions to Gitea + +on: + issues: + types: [opened, reopened] + pull_request_target: + types: [opened, reopened] + +permissions: + issues: write + pull-requests: write + +concurrency: + group: migrate-${{ github.event_name }}-${{ github.event.issue.number || github.event.pull_request.number }} + +env: + GITEA_API_URL: https://git.lechner-systems.at/api/v1 + GITEA_REPOSITORY: FrauJulian/Discord-Audio-Stream + +jobs: + migrate-issue: + if: github.event_name == 'issues' + runs-on: ubuntu-latest + steps: + - name: Migrate issue and close + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + run: | + set -euo pipefail + + number="$(jq -r '.issue.number' "$GITHUB_EVENT_PATH")" + title="$(jq -r '.issue.title' "$GITHUB_EVENT_PATH")" + body="$(jq -r '.issue.body // ""' "$GITHUB_EVENT_PATH")" + author="$(jq -r '.issue.user.login' "$GITHUB_EVENT_PATH")" + source_url="$(jq -r '.issue.html_url' "$GITHUB_EVENT_PATH")" + github_api="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}" + migrated_url="$( + curl --fail --silent --show-error \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "Accept: application/vnd.github+json" \ + "${github_api}/issues/${number}/comments?per_page=100" | + jq -r '[.[] | select(.user.login == "github-actions[bot]" and (.body | startswith("Migrated to Gitea: ")))] | first | .body // "" | sub("^Migrated to Gitea: "; "")' + )" + + if [ -z "$migrated_url" ]; then + migrated_body="${body} + + --- + Originally opened by @${author} on GitHub: ${source_url}" + + jq -n \ + --arg title "$title" \ + --arg body "$migrated_body" \ + '{title: $title, body: $body}' > gitea-request.json + + curl --fail-with-body \ + --request POST \ + --header "Authorization: token ${GITEA_TOKEN}" \ + --header "Content-Type: application/json" \ + --data @gitea-request.json \ + "${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/issues" > gitea-response.json + + migrated_url="$(jq -r '.html_url' gitea-response.json)" + jq -n --arg body "Migrated to Gitea: ${migrated_url}" '{body: $body}' > github-comment.json + curl --fail-with-body \ + --request POST \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "Content-Type: application/json" \ + --data @github-comment.json \ + "${github_api}/issues/${number}/comments" + fi + + curl --fail-with-body \ + --request PATCH \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "Content-Type: application/json" \ + --data '{"state":"closed","state_reason":"not_planned"}' \ + "${github_api}/issues/${number}" + + redirect-pull-request: + if: github.event_name == 'pull_request_target' + runs-on: ubuntu-latest + steps: + - name: Redirect pull request and close + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + + number="$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH")" + github_api="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}" + + curl --fail-with-body \ + --request POST \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "Content-Type: application/json" \ + --data '{"body":"Please open the Pull Request on Gitea"}' \ + "${github_api}/issues/${number}/comments" + + curl --fail-with-body \ + --request PATCH \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "Content-Type: application/json" \ + --data '{"state":"closed"}' \ + "${github_api}/pulls/${number}"