Git.SemVersioning.Gradle icon indicating copy to clipboard operation
Git.SemVersioning.Gradle copied to clipboard

Provide an easier way to format the individual changes

Open dalewking opened this issue 1 year ago • 4 comments

I would like to format the individual change entries which are hardcoded in the formatters with something like:

            formatChanges {
                append("- ").append(hash()).appendLine(fullHeader())
            }

I want to shorten the size of the hash and make it a link to the commit in the repo and add the user who made the change.

The only way I can see how to do that is completely customize the entire formatting, copying code from the predefined formatters.

dalewking avatar Oct 21 '24 19:10 dalewking

Hi, If you publish the change log to GitHub the shortening and linking of the hashes to the commit is made automatically by GitHub. Example from this repository: https://github.com/jmongard/Git.SemVersioning.Gradle/releases

Do you need this for some other git service?

jmongard avatar Oct 22 '24 09:10 jmongard

About user in the commit. The git repo contains the users email adress and that is possible to show in the output.

However if you want to lookup the github username from the email to show that I think that might be out of scope for this plugin.

But perhaps this is easily fixed in a post generate change log build action or maybe github does this automatically as well. I have not tested it.

jmongard avatar Oct 23 '24 13:10 jmongard

No, this is private Bit Bucket server and in particular the change log will be stored in the wiki associated with the repo.

Here is how I am working around it:

    changeLogFormat {
        fun ChangeLogTextFormatter.hashLink(): String {
            val hash = commitInfo.commits.joinToString(" ") { it.sha.take(6) }
            return "[$hash ${authorName("_by %s_")}](https://bitbucket.org/XXXXXX/XXXXXXX/commits/$hash) "
        }

        appendLine(constants.header).appendLine()

        withType("release") {
            skip()
        }

        // Breaking changes
        withBreakingChanges {
            appendLine(constants.breakingChange)
            formatChanges {
                append("- ").append(hashLink()).appendLine(fullHeader())
            }
            appendLine()
        }

        // Fixes and then Features from typesOrder
        withType(types = constants.typesOrder.toTypedArray()) {
            filterEmptyHeader(constants.headerTexts[groupKey]) {
                appendLine(groupKey)
                formatChanges {
                    append("- ").append(hashLink()).append(scope()).appendLine(header())
                }
                appendLine()
            }
        }

        // Other defined types and scopes in headerTexts
        groupBySorted({ constants.headerTexts[it.scope] ?: constants.headerTexts[it.type] }) {
            appendLine(groupKey)
            with({ constants.headerTexts.containsKey(it.scope) }) {
                formatChanges {
                    append("- ").append(hashLink()).append(type()).appendLine(header())
                }
            }
            formatChanges {
                append("- ").append(hashLink()).append(scope()).appendLine(header())
            }
            appendLine()
        }

        // Other changes
        otherwise {
            filterEmptyHeader(constants.otherChange) {
                appendLine(groupKey)
                formatChanges {
                    append("- ").append(hashLink()).appendLine(fullHeader())
                }
                appendLine()
            }
        }

        appendLine(constants.footer)
    }

Which is a copy of the defaultChangeLog code with the only difference being that all calls to hash() are replaced with my hashLink() function.

It occurs to me what I really want is a way to override the behavior of the ChangeLogTextFormatter class to change its hash() function but you do not allow overriding that and have it hardcoded.

dalewking avatar Oct 23 '24 17:10 dalewking

by opening up the ChangeLogTextFormatter I can make this work in gradle 7.6.4

changeLogTexts = object : ChangeLogTexts(DefaultChangeLogTexts.headerTexts) {
    override fun createChangeLogTextFormatter(commitInfo: ChangeLogFormatter.CommitInfo) =
              object : ChangeLogTextFormatter(commitInfo) {
                              override fun hash(format: String, len: Int) = 
                                  commitInfo.commits.joinToString(" ", "<<<", ">>>") { format.format(it.sha.take(len)) }
              }
}

But in gradle 8 I get Unexpected build execution failure.

I'm not sure why and I don't know how to get around it. Maybe overriding is interfering with the gradle internal logic.

jmongard avatar Oct 24 '24 22:10 jmongard

Closing this as the new changes satisfy my requirements

dalewking avatar Mar 25 '25 21:03 dalewking