diff --git a/.github/workflows/update-metadata.main.kts b/.github/workflows/validate-update-metadata.main.kts similarity index 78% rename from .github/workflows/update-metadata.main.kts rename to .github/workflows/validate-update-metadata.main.kts index 4a37bf3..47bd848 100755 --- a/.github/workflows/update-metadata.main.kts +++ b/.github/workflows/validate-update-metadata.main.kts @@ -23,12 +23,12 @@ import java.io.File import org.eclipse.jgit.api.Git workflow( - name = "Update metadata", + name = "Validate/Update metadata", consistencyCheckJobConfig = DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy( checkoutActionVersion = CheckoutActionVersionSource.InferFromClasspath(), ), sourceFile = __FILE__, - on = listOf(Push(branches = listOf("main"))), + on = listOf(Push()), ) { job( id = "generate", @@ -50,12 +50,19 @@ workflow( run(name = "Run generation logic") { removeMetadataFiles() generateMetadataFiles() - commitChanges(github.sha) + if (github.ref == "main") { + println("Main branch - just validating") + checkForChanges() + } else { + println("Non-main branch...") + if (commitChanges(github.sha)) { + println("Committing changes") + gitPush() + } else { + println("Nothing to commit") + } + } } - run( - name = "Push new commit", - command = "git push", - ) } } @@ -77,14 +84,28 @@ fun generateMetadataFiles() { } } -fun commitChanges(sha: String) { +fun checkForChanges() { + require(!Git.open(File(".")).status().call().hasUncommittedChanges()) { + "Metadata files changed after regeneration — they are not up to date!" + } +} + +fun commitChanges(sha: String): Boolean { Git.open(File(".")).apply { add().addFilepattern("typings/").call() if (status().call().hasUncommittedChanges()) { commit().setMessage("Update metadata for commit $sha").call() + return true } } + return false +} + +fun gitPush() { + val process = ProcessBuilder("git", "push") + .inheritIO().start() + check(process.waitFor() == 0) { "git push failed" } } fun writeToMetadataFile(actionRootDir: File, versionsWithTypings: List) { diff --git a/.github/workflows/update-metadata.yaml b/.github/workflows/validate-update-metadata.yaml similarity index 66% rename from .github/workflows/update-metadata.yaml rename to .github/workflows/validate-update-metadata.yaml index 5c55a83..2ce3da9 100644 --- a/.github/workflows/update-metadata.yaml +++ b/.github/workflows/validate-update-metadata.yaml @@ -1,12 +1,10 @@ -# This file was generated using Kotlin DSL (.github/workflows/update-metadata.main.kts). +# This file was generated using Kotlin DSL (.github/workflows/validate-update-metadata.main.kts). # If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file. # Generated with https://github.com/typesafegithub/github-workflows-kt -name: 'Update metadata' +name: 'Validate/Update metadata' on: - push: - branches: - - 'main' + push: {} jobs: check_yaml_consistency: name: 'Check YAML consistency' @@ -17,10 +15,10 @@ jobs: uses: 'actions/checkout@v6' - id: 'step-1' name: 'Execute script' - run: 'rm ''.github/workflows/update-metadata.yaml'' && ''.github/workflows/update-metadata.main.kts''' + run: 'rm ''.github/workflows/validate-update-metadata.yaml'' && ''.github/workflows/validate-update-metadata.main.kts''' - id: 'step-2' name: 'Consistency check' - run: 'git diff --exit-code ''.github/workflows/update-metadata.yaml''' + run: 'git diff --exit-code ''.github/workflows/validate-update-metadata.yaml''' generate: runs-on: 'ubuntu-latest' needs: @@ -42,7 +40,4 @@ jobs: name: 'Run generation logic' env: GHWKT_GITHUB_CONTEXT_JSON: '${{ toJSON(github) }}' - run: 'GHWKT_RUN_STEP=''update-metadata.yaml:generate:step-3'' ''.github/workflows/update-metadata.main.kts''' - - id: 'step-4' - name: 'Push new commit' - run: 'git push' + run: 'GHWKT_RUN_STEP=''validate-update-metadata.yaml:generate:step-3'' ''.github/workflows/validate-update-metadata.main.kts'''