diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..7bc02b2 --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,33 @@ +name: ビルド + +on: + push: + branches: + - master + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: ソースコードのチェックアウト + uses: actions/checkout@v7 + + - name: JDK のセットアップ + uses: actions/setup-java@v6 + with: + distribution: temurin + java-version: '25' + cache: maven + + - name: プロジェクトのビルド + shell: pwsh + run: | + mvn -B package + + - name: 成果物の保存 + uses: actions/upload-artifact@v7 + with: + name: LunaChat + path: target/LunaChat.jar diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 0000000..12d4030 --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,316 @@ +name: リリース + +on: + push: + tags: + - 'v*' + +permissions: + contents: write + +jobs: + build-and-release: + runs-on: ubuntu-latest + + steps: + - name: ソースコードのチェックアウト + uses: actions/checkout@v7 + with: + fetch-depth: 0 + + - name: JDK のセットアップ + uses: actions/setup-java@v6 + with: + distribution: temurin + java-version: '25' + cache: maven + + - name: タグ名からバージョンを取得 + id: version + shell: pwsh + run: | + $tag = "${{ github.ref_name }}" + echo "TAG=$tag" >> $env:GITHUB_OUTPUT + echo "VERSION=$($tag.TrimStart('v'))" >> $env:GITHUB_OUTPUT + + - name: バージョン記載の照合 + shell: pwsh + run: | + $tag = "${{ steps.version.outputs.TAG }}" + $version = "${{ steps.version.outputs.VERSION }}" + $problems = @() + + $pomPath = "pom.xml" + [xml]$pom = Get-Content $pomPath -Raw + $current = $pom.project.version + if ($current -ne $version) { + if (-not $current) { $current = "記載なし" } + $problems += "$pomPath の が $current です。$version にしてください。" + } + + if ($problems.Count -eq 0) { + Write-Host "バージョン記載は $tag と一致しています" + exit 0 + } + + foreach ($problem in $problems) { + Write-Host "::error::$problem" + } + Write-Host "::error::リリースの対象は $tag が指すコミットです。タグを付けた後で master へ訂正を積んでも、その訂正はリリースに入りません。タグを削除し、記載を直したコミットを積んでから付け直してください。" + exit 1 + + - name: プロジェクトのビルド + shell: pwsh + run: | + mvn -B package + + - name: パッケージの作成 + id: package + shell: pwsh + run: | + $version = "${{ steps.version.outputs.TAG }}" + $jarPath = "target/LunaChat.jar" + $pkgName = "LunaChat_$version.jar" + + if (-not (Test-Path $jarPath)) { + Write-Error "ビルド結果に LunaChat.jar が見つかりませんでした: $jarPath" + exit 1 + } + + Copy-Item $jarPath $pkgName + echo "PKG_NAME=$pkgName" >> $env:GITHUB_OUTPUT + + - name: 変更履歴の作成 + id: changelog + shell: pwsh + env: + GITEA_TOKEN: ${{ github.token }} + TAG: ${{ steps.version.outputs.TAG }} + REPOSITORY: ${{ github.repository }} + SERVER_URL: ${{ github.server_url }} + PACKAGE: ${{ steps.package.outputs.PKG_NAME }} + run: | + $tag = $env:TAG + $repository = $env:REPOSITORY + $server = $env:SERVER_URL + $code = [char]0x60 + $fence = [string]::new($code, 3) + $unit = [char]0x1f + $headers = @{ Authorization = "token $env:GITEA_TOKEN" } + + $previous = "" + $candidates = @(git tag --list "v*" --merged $tag --sort=-creatordate | Where-Object { $_ -ne $tag }) + if ($candidates.Count -gt 0) { + $previous = $candidates[0] + } + Write-Host "直前のタグ: $previous" + + $range = if ($previous) { "$previous..$tag" } else { $tag } + $log = @(git log "--format=%H%x1f%s%x1f%an%x1f%ad" --date=short $range) + + $commits = @() + foreach ($line in $log) { + if (-not $line) { continue } + $fields = $line -split $unit + if ($fields.Count -lt 4) { continue } + $commits += [pscustomobject]@{ + Sha = $fields[0] + Subject = $fields[1] + Name = $fields[2] + Date = $fields[3] + Login = "" + Avatar = "" + } + } + Write-Host "コミット数: $($commits.Count)" + + $pending = [System.Collections.Generic.HashSet[string]]::new() + foreach ($commit in $commits) { + [void]$pending.Add($commit.Sha) + } + + $accounts = @{} + $page = 1 + while ($pending.Count -gt 0 -and $page -le 20) { + $rows = @() + try { + $rows = @(Invoke-RestMethod -Uri "$server/api/v1/repos/$repository/commits?sha=$tag&limit=50&page=$page&stat=false&verification=false&files=false" -Headers $headers) + } + catch { + Write-Host "コミットの作者を取得できませんでした: $($_.Exception.Message)" + break + } + if ($rows.Count -eq 0) { break } + + foreach ($row in $rows) { + if ($pending.Remove($row.sha)) { + $login = "" + $avatar = "" + if ($row.author) { + $login = [string]$row.author.login + $avatar = [string]$row.author.avatar_url + } + $accounts[$row.sha] = @{ Login = $login; Avatar = $avatar } + } + } + + if ($rows.Count -lt 50) { break } + $page++ + } + + foreach ($commit in $commits) { + if ($accounts.ContainsKey($commit.Sha)) { + $commit.Login = $accounts[$commit.Sha].Login + $commit.Avatar = $accounts[$commit.Sha].Avatar + } + } + + $emptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" + $base = if ($previous) { $previous } else { $emptyTree } + $shortstat = (@(git diff --shortstat $base $tag) -join " ") + $changedFiles = 0 + $insertions = 0 + $deletions = 0 + if ($shortstat -match "(\d+) file") { $changedFiles = [int]$Matches[1] } + if ($shortstat -match "(\d+) insertion") { $insertions = [int]$Matches[1] } + if ($shortstat -match "(\d+) deletion") { $deletions = [int]$Matches[1] } + $diffStat = @(git diff --stat $base $tag) + $tagDate = (git log -1 "--format=%ad" --date=short $tag) + + $contributors = @($commits | + Group-Object -Property { if ($_.Login) { $_.Login } else { $_.Name } } | + Sort-Object -Property Count -Descending) + + function Get-Icon($avatar, $login, $size) { + if (-not $avatar) { return "" } + $sized = if ($avatar.Contains("?")) { "$avatar&s=$($size * 2)" } else { "$avatar" + "?s=$($size * 2)" } + return " " + } + + $package = Get-Item $env:PACKAGE + $size = [math]::Round($package.Length / 1MB, 2) + + $body = [System.Collections.Generic.List[string]]::new() + $body.Add("## 概要") + $body.Add("") + $body.Add("| 項目 | 内容 |") + $body.Add("|---|---|") + $body.Add("| タグ | [$tag]($server/$repository/src/tag/$tag) |") + $body.Add("| 公開日 | $tagDate |") + if ($previous) { + $body.Add("| 直前のリリース | [$previous]($server/$repository/releases/tag/$previous) |") + } + $body.Add("| コミット数 | $($commits.Count) |") + $body.Add("| 貢献者 | $($contributors.Count) 名 |") + $body.Add("| 変更されたファイル | $changedFiles |") + $body.Add("| 追加された行 | $insertions |") + $body.Add("| 削除された行 | $deletions |") + + $body.Add("") + $body.Add("## ダウンロード") + $body.Add("") + $body.Add("| ファイル | サイズ |") + $body.Add("|---|---|") + $body.Add("| $($package.Name) | $size MB |") + + $body.Add("") + $body.Add("## 貢献者") + $body.Add("") + $body.Add("| 貢献者 | コミット数 |") + $body.Add("|---|---|") + foreach ($contributor in $contributors) { + $first = $contributor.Group[0] + if ($first.Login) { + $name = (Get-Icon $first.Avatar $first.Login 40) + "[@$($first.Login)]($server/$($first.Login))" + } + else { + $name = $first.Name + } + $body.Add("| $name | $($contributor.Count) |") + } + + $body.Add("") + $body.Add("## 変更履歴") + $body.Add("") + $body.Add("| コミット | 内容 | 貢献者 | 日付 |") + $body.Add("|---|---|---|---|") + $maximumCommits = 300 + $listed = 0 + foreach ($commit in $commits) { + if ($listed -ge $maximumCommits) { break } + $short = $commit.Sha.Substring(0, 7) + $subject = $commit.Subject -replace '\|', '\|' + if ($commit.Login) { + $author = (Get-Icon $commit.Avatar $commit.Login 20) + "[@$($commit.Login)]($server/$($commit.Login))" + } + else { + $author = $commit.Name + } + $body.Add("| [$code$short$code]($server/$repository/commit/$($commit.Sha)) | $subject | $author | $($commit.Date) |") + $listed++ + } + if ($commits.Count -gt $maximumCommits) { + $body.Add("") + $body.Add("残り $($commits.Count - $maximumCommits) 件のコミットは省略しました。") + } + + if ($diffStat.Count -gt 0) { + $body.Add("") + $body.Add("## 変更されたファイル") + $body.Add("") + $body.Add("
") + $body.Add("差分の統計") + $body.Add("") + $body.Add($fence) + $maximumStatLines = 200 + $shown = 0 + foreach ($line in $diffStat) { + if ($shown -ge $maximumStatLines) { break } + $body.Add($line) + $shown++ + } + if ($diffStat.Count -gt $maximumStatLines) { + $body.Add("... 残り $($diffStat.Count - $maximumStatLines) 行は省略しました。") + } + $body.Add($fence) + $body.Add("") + $body.Add("
") + } + + if ($previous) { + $body.Add("") + $body.Add("## 全ての変更") + $body.Add("") + $body.Add("$server/$repository/compare/$previous...$tag") + } + + $path = "release-body.md" + Set-Content -Path $path -Value ($body -join "`n") -Encoding utf8NoBOM + if (-not (Test-Path $path)) { + throw "変更履歴を作成できませんでした。" + } + Write-Host "変更履歴の行数: $($body.Count)" + $global:LASTEXITCODE = 0 + + - name: Gitea リリースの作成 + shell: pwsh + env: + GITEA_TOKEN: ${{ github.token }} + TAG: ${{ steps.version.outputs.TAG }} + REPOSITORY: ${{ github.repository }} + SERVER_URL: ${{ github.server_url }} + PACKAGE: ${{ steps.package.outputs.PKG_NAME }} + run: | + $headers = @{ Authorization = "token $env:GITEA_TOKEN" } + $api = "$env:SERVER_URL/api/v1/repos/$env:REPOSITORY" + $payload = @{ + tag_name = $env:TAG + name = $env:TAG + body = (Get-Content "release-body.md" -Raw -Encoding utf8) + draft = $false + prerelease = $false + } | ConvertTo-Json + $release = Invoke-RestMethod -Method Post -Uri "$api/releases" -Headers $headers -ContentType "application/json; charset=utf-8" -Body ([System.Text.Encoding]::UTF8.GetBytes($payload)) + $name = [System.Uri]::EscapeDataString($env:PACKAGE) + Invoke-RestMethod -Method Post -Uri "$api/releases/$($release.id)/assets?name=$name" -Headers $headers -Form @{ attachment = Get-Item $env:PACKAGE } | Out-Null + Write-Host "リリースを作成しました: $($release.html_url)"