tcplugins / tcWebHooks

WebHooks plugin for Teamcity. Supports many build states and payload formats.
https://netwolfuk.wordpress.com/category/teamcity/tcplugins/tcwebhooks/
155 stars 30 forks source link

Added to queue event: null buildId #159

Closed nrukavkov closed 1 year ago

nrukavkov commented 4 years ago

Hello @netwolfuk 😋

Current Behavior

Event 'added to queue' generates webhook without buildid. We were expecting to be able to change a tag on a build. But it is not possible, because webhook's payload has a null value in buildId. Is it possible to have some trick and pass a build value?

Your Environment

netwolfuk commented 4 years ago

I don't believe an ID has been assigned at this point. My understanding is that the ID is assigned at the started event.

nrukavkov commented 4 years ago

I don't believe an ID has been assigned at this point. My understanding is that the ID is assigned at the started event.

🤔 If you use API to add a new build to queue, teamcity returns build ID and other additional information. And it doesn’t matter has it been started right now or just was added to a queue.

Here is my simple part of PowerShell code which I use to start builds and get their IDs:

function Add-ToTeamcityQueue($baseUrl, $buildTypeId, $teamcityEnvVariables , 
$teamcityComment, $username, $password, $tags) {
    $baseUrl = $baseUrl + '/httpAuth/app/rest/buildQueue'
    $payload = @{ buildType = @{ id = "$buildTypeId" }; comment = @{ text = "$teamcityComment" } ; tags = @{ tag = $tags} ; properties = @{property = $teamcityEnvVariables} } 

    Send-HttpRequest "$baseUrl" $username $password 'POST' ($payload | ConvertTo-Json -Depth 3)
}
$response = Add-ToTeamcityQueue $($project.BaseUrl) $($project.ProjectId) $preparedParameters $teamcityComment $TeamcityUsername $TeamcityPassword @(@{ name = $DestinationBranch})
Write-Host "$type build id: " $response.id

$response.id has an ID of every build

netwolfuk commented 4 years ago

There is an item id. http://javadoc.jetbrains.net/teamcity/openapi/current/jetbrains/buildServer/QueuedBuild.html#getItemId--

Is that id the same as the id of the build when it's running/finished?

netwolfuk commented 4 years ago

Is the buildTypeid something like bt11 ? If so that is the internal id of the build configuration, not the build instance

Sorry. Ignore that. That is part of your payload, and I misread it on my phone.

netwolfuk commented 4 years ago

ok, I found what they are doing. They're getting the promotion and mapping it. I'll need to have a better look when I get to a computer.

https://github.com/JetBrains/teamcity-rest/blob/ad895583981a304d405d0b6ca98f4108782ab5c5/src/jetbrains/buildServer/server/rest/model/build/Build.java#L175-L187

Also, https://github.com/JetBrains/teamcity-rest/blob/ad895583981a304d405d0b6ca98f4108782ab5c5/src/jetbrains/buildServer/server/rest/model/build/Build.java#L201-L205

Thanks for raising it. It seems like we can make this better!

netwolfuk commented 4 years ago

This ticket implies that promotionId and buildId are the same for TeamCity 9.0 and above.

I'll do some testing to confirm.

netwolfuk commented 4 years ago

I've committed a change that copies the promotionId into the buildId field in the Queued webhook events (queue and de-queue). After it finishes building it will be available to download from here. Just login as a guest if you don't have a Jetbrains account.

nrukavkov commented 4 years ago

I will take a look on it on Monday. Thank you.

netwolfuk commented 4 years ago

Cool. If I get a chance I'll do some more testing too.

netwolfuk commented 4 years ago

I have a webservice that receives a LegacyJson webhook request for every build event in my development environment. I can confirm that for every buildAddedToQueue event, there is now a buildId set. Previous to yesterday, it was null.

I didn't have any previous buildRemovedFromQueue events, but testing that now they are also showing a buildId. From the 17 builds that ran in the last 24 hours, all the buildIds appear to match so I am assuming that the promotionId and the buildId are the same value.

netwolfuk commented 4 years ago

I'd appreciate it if you could keep an eye on the buildId, and let me know if they ever don't match for the other events in the build. However from my little bit of testing, and that comment in the youtrack ticket, I think it should be fine.

nrukavkov commented 4 years ago

No problem! I guess everything will be fine. I've tested your fix on a newly installed teamcity and it works.

Just one case I want to show you. The payload looks like this when there are no agents to start. But I suppose that is fine. You can't definitely know buildId.

{
        "embeds": [
            {
                "title" : "Been Added To The Build Queue : test build build #${buildNumber}",
                "url" : "http://localhost:8111/viewLog.html?buildTypeId=bt1&buildId=lastFinished",
                "author" : {
                    "name" : "TeamCity",
                    "url" : "http://localhost:8111/",
                    "icon_url" : "https://raw.githubusercontent.com/tcplugins/tcWebHooks/master/docs/icons/teamcity-logo-48x48.png"
                },
                "fields": [

                    { "name" : "Status", "value" : "${buildStatus}", "inline": false },
                    { "name" : "Project Name", "value" : "[test](http://localhost:8111//project.html?projectId=Test)", "inline": true },
                    { "name" : "Build Name", "value" : "[test build](http://localhost:8111//viewType.html?buildTypeId=Test_TestBuild)", "inline": true },
                    { "name" : "Commit", "value" : "[UNRESOLVED](http://localhost:8111/viewLog.html?buildTypeId=bt1&buildId=lastFinished&tab=buildChangesDiv)", "inline": true },
                    { "name" : "Triggered By", "value" : "admin", "inline" : true },
                    { "name" : "Agent", "value" : "${agentName}", "inline" : true }
                ]
            }
        ]
    }

None the less I'll get you to know If something goes wrong.

netwolfuk commented 4 years ago

Thanks for the update. I didn't really think about it, but I guess it makes sense that the id is allocated when it starts. Otherwise there would be buildIds that are never used when multiple queued events are grouped into a single build.

I'll merge this branch into master and release a new alpha in the next few weeks.

nrukavkov commented 4 years ago

@netwolfuk do you have plans to release v1.2?

netwolfuk commented 4 years ago

Hi @nrukavkov. Yes. A new alpha will be released this weekend.

Alpha 7 adds project parameter editing in the UI. Alpha 8 is filter editing in the UI.

I suspect the final release is still a few months away. But the alphas are very stable.

netwolfuk commented 4 years ago

https://github.com/tcplugins/tcWebHooks/projects/2

netwolfuk commented 4 years ago

Sorry for the delay. We found bug #167 just before I wanted to release, and I thought it was going to affect all users.

Alpha 7 will be released this weekend.