Code Monkey home page Code Monkey logo

Comments (13)

orta avatar orta commented on July 28, 2024 3

Build it into danger-kotlin

from kotlin.

davidbilik avatar davidbilik commented on July 28, 2024

I have created a little workaround by reading it through git shell command

data class GitStats(val additions: Int, val deletions: Int)

fun GitLab.gitStats(): GitStats {
    val diffRefs = mergeRequest.diffRefs
    
    val commandOutput = ProcessBuilder("bash", "-c", "git diff --numstat ${diffRefs.startSha} ${diffRefs.headSha}")
        .redirectOutput(ProcessBuilder.Redirect.PIPE)
        .redirectError(ProcessBuilder.Redirect.PIPE)
        .start().apply { waitFor(60, TimeUnit.MINUTES) }
        .inputStream.bufferedReader().readText()
    
    val pairs = commandOutput.lines()
        .filter { it.isNotEmpty() }
        .map { line ->
            val parts = line.split("\\s+".toRegex())
            parts[0].toInt() to parts[1].toInt()
        }
    val additions = pairs.fold(0) { acc, (addition, _) -> acc + addition }
    val deletions = pairs.fold(0) { acc, (_, deletion) -> acc + deletion }
    return GitStats(additions, deletions)
}

but it's not ideal of course :)

from kotlin.

gianluz avatar gianluz commented on July 28, 2024

Interesting workaround, let me to investigate!! if you are lucky i will find a solution shortly

from kotlin.

davidbilik avatar davidbilik commented on July 28, 2024

I have actually find out that danger js has it in their api - https://github.com/danger/danger-js/blob/b615ca41dffa0446c1651300c5d2731ff5988229/source/platforms/git/gitJSONToGitDSL.ts#L158 so maybe it could be easily read?

from kotlin.

gianluz avatar gianluz commented on July 28, 2024

good shout!! i'm on it!!

from kotlin.

f-meloni avatar f-meloni commented on July 28, 2024

I think with the current infrastructure that is the actual solution.
Danger-Kotlin doesn’t have a gitlab client, then if is the number of added/removed lines is not in the json we get from Danger-js, git looks the best way to get it.

But we added a danger utils exec to execute calls #98

Then we can release a new version and you can use that one instead, that could help to make the code more readable.

We could also have a function on git to get the added removed lines, but it would have an implementation really similar to the one you wrote.

from kotlin.

davidbilik avatar davidbilik commented on July 28, 2024

@f-meloni I'm probably a little bit lost now :\

Gitlab does not provide this info in their api. But danger-js does. And from my understanding of how danger-kotlin works, it's mainly a wrapper over danger-js. So if danger-js has this, it could be added to the Git class introduced in danger-kotlin, right?

Or am I missing something?

from kotlin.

gianluz avatar gianluz commented on July 28, 2024

yeah that's the problem, we do not have direct access to the api.
Basically what we do is:

we can keep in mind. but right now, your workaround is not a bad solution.. maybe can just use utils with something like:

data class GitStats(val additions: Int, val deletions: Int)

val GitLab.stats: GitStats
    get() {
        Utils().exec("git diff --numstat ${mergeRequest.diffRefs.startSha} ${mergeRequest.diffRefs.headSha}").let { output ->
            val pairs = output.lines().filter { it.isNotEmpty() }
                .map { line ->
                    val parts = line.split("\\s+".toRegex())
                    parts[0].toInt() to parts[1].toInt()
                }
            val additions = pairs.fold(0) { acc, (addition, _) -> acc + addition }
            val deletions = pairs.fold(0) { acc, (_, deletion) -> acc + deletion }
            return GitStats(additions, deletions)
        }
    }

from kotlin.

f-meloni avatar f-meloni commented on July 28, 2024

Danger Kotlin is a not exactly a wrapper over danger-js, they have only one “touch” point, that is the DSL danger-Kotlin receives from danger js (and then Danger Kotlin sends back another json).
Basically danger js handles getting and posting data on the PRs/MRs is not a continuous communication.
Everything else Danger-Kotlin has is built in

from kotlin.

davidbilik avatar davidbilik commented on July 28, 2024

aha! OK guys, now I understand it better :) I am planning to create a plugin with some common stuff that we will use for Merge requests at our company so I can put the extension there and it will be ok :) Thanks again! I love your work and I look forward when I move from danger in ruby to the danger in kotlin since kotlin is my day to day language. So see you soon with another question :D We can close this now I suppose :)

from kotlin.

gianluz avatar gianluz commented on July 28, 2024

and do not forget to think about us!!! and please feel free to fork our repo, and maybe contribute with interesting stuff!!

from kotlin.

orta avatar orta commented on July 28, 2024

Yeah, just to plus one this general thread as being the way to think about building Danger language implementations

Danger JS gets the same dumb JSON object as Danger Kotlin and then extends the JSON with it's own APIs - this API in particular is expensive in Danger JS and isn't done by default but it's up to languages to decide whether this should be built-in or not 👍

from kotlin.

davidbilik avatar davidbilik commented on July 28, 2024

Ok so I've tried to extract this to a plugin and I've hit a roadblock right from the beginning because I can't access GitLab class in the plugin, looks like it's not provided through systems.danger:danger-kotlin-sdk:1.1 artifact. I can of course make my method for computing GitStats more abstract without direct dependency to a GitLab class but I can imagine that in the future I would like to have access there. What do you suggest?

from kotlin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.