diff --git a/.github/workflows/update_compat_db.yml b/.github/workflows/update_compat_db.yml new file mode 100644 index 0000000..8b7d279 --- /dev/null +++ b/.github/workflows/update_compat_db.yml @@ -0,0 +1,84 @@ +name: Compatibility database update + +on: + push: + issues: + +jobs: + update_compat_db: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v3 + - name: Generate compatibility database xml file + run: | + $headers = @{ + Authorization = "Token ${{ secrets.GITHUB_TOKEN }}" + } + $compat_link = "https://api.github.com/repos/Vita3K/compatibility" + $issues_link = $compat_link + "/issues" + $app_updated = (Invoke-RestMethod -Uri $issues_link"?state=all&sort=updated" -Headers $headers)[0] + $db_updated_at = [DateTime]::Parse($app_updated.updated_at).ToUniversalTime().ToString("MM-dd-yyyy HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture) + $open_issues_count = (Invoke-RestMethod -Uri "$compat_link" -Headers $headers).open_issues_count + $page_count = @() + for ($i = 0; $i -lt $open_issues_count; $i += 100) { + $page = [Math]::Floor($i / 100) + 1 + $per_page = [Math]::Min($open_issues_count - $i, 100) + $page_count += New-Object PSObject -Property @{First=$page; Second=$per_page} + } + $xml = New-Object System.Xml.XmlDocument + $xml.AppendChild($xml.CreateXmlDeclaration("1.0", "UTF-8", $null)) | Out-Null + $xml.AppendChild($xml.CreateComment("This file is automatically generated by a GitHub action. Do not edit it manually.")) | Out-Null + $root = $xml.CreateElement("compatibility") + $root.SetAttribute("db_updated_at", $db_updated_at) + $root.SetAttribute("version", "1") + $xml.AppendChild($root) | Out-Null + $issue_count = 0 + foreach ($page in $page_count) { + $current_page = $page.First + $per_page = $page.Second + $issues = (Invoke-RestMethod -Uri $issues_link"?page=$current_page&per_page=100" -Headers $headers)[0..$per_page] + foreach ($issue in $issues) { + $issueNode = $xml.CreateElement("app") + $title = $issue.title.TrimEnd() + if ($title.Length -gt 9) { + $title = $title.Substring($title.Length-10,9) + } + $issueNode.SetAttribute("title_id", $title) + $numberNode = $xml.CreateElement("issue_id") + $numberNode.InnerText = $issue.number + $issueNode.AppendChild($numberNode) | Out-Null + + $labelsNode = $xml.CreateElement("labels") + foreach ($label in $issue.labels) { + $labelNode = $xml.CreateElement("label") + $labelNode.InnerText = $label.id + $labelsNode.AppendChild($labelNode) | Out-Null + } + $issueNode.AppendChild($labelsNode) | Out-Null + + $ts_updated_at = (([DateTime]::Parse($issue.updated_at).ToUniversalTime()) - ([DateTime]::Parse('01/01/1970 00:00:00'))).TotalSeconds + $updated_atNode = $xml.CreateElement("updated_at") + $updated_atNode.InnerText = $ts_updated_at + $issueNode.AppendChild($updated_atNode) | Out-Null + + $root.AppendChild($issueNode) | Out-Null + $issue_count++ + } + } + $xml.Save("app_compat_db.xml") + "$issue_count issues have been retrieved from the compatibility database" + + Add-Content -Path $env:GITHUB_ENV -Value "db_updated_at=$db_updated_at" + $app_updated_title = $app_updated.title + Add-Content -Path $env:GITHUB_ENV -Value "app_updated_title=$app_updated_title" + $issue_id_updated = $app_updated.number + Add-Content -Path $env:GITHUB_ENV -Value "issue_id_updated=$issue_id_updated" + + - name: Upload compatibility database + run: | + Invoke-WebRequest "https://github.com/tcnksm/ghr/releases/download/v0.16.0/ghr_v0.16.0_windows_amd64.zip" -OutFile ghr.zip + Expand-Archive -Force -Path ghr.zip -DestinationPath '.' + .\ghr_v0.16.0_windows_amd64\ghr -u Vita3K -r compatibility -recreate -n "Automatic CI generated database" -b "Corresponding issue: #${{ env.issue_id_updated }} for App: ${{ env.app_updated_title }}`r`nUpdated at: ${{ env.db_updated_at }}" -t "${{ secrets.GITHUB_TOKEN }}" compat_db app_compat_db.xml + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}