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

Webhook on tagged build #173

Closed bnaji closed 1 year ago

bnaji commented 3 years ago

Feature Request Send out webhook when build tagged or untagged.

Similar to pin/unpinned issue #129 there is a buildPromotionTagsChanged method in the API.

I did notice something odd while trying to do this myself, it appears that if a build has tag A and you add tag B, internally Teamcity removes all tags, then adds tags A & B. To be clear, I never removed the tags in the logs below.

[2021-01-04 15:13:00,213]   INFO - s.buildServer.ACTIVITIES.AUDIT - build_promotion_tag_change: Build BUILD_PROMOTION{id=123}'s tags were changed by "'user' (Bob) {id=001}" with comment "Removed: test, test2"
[2021-01-04 15:13:00,225]   INFO - s.buildServer.ACTIVITIES.AUDIT - build_promotion_tag_change: Build BUILD_PROMOTION{id=123}'s tags were changed by "'user' (Bob) {id=001}" with comment "Added: user, test, test2"
[2021-01-04 15:13:00,312]   INFO -   jetbrains.buildServer.SERVER - AbstractWebHookExecutor ::  :: WebHook triggered : <WEBHOOK URL> using template TEMPLATE returned 202 Accepted
[2021-01-04 15:13:00,328]   INFO -   jetbrains.buildServer.SERVER - AbstractWebHookExecutor ::  :: WebHook triggered : <WEBHOOK URL> using template TEMPLATE returned 202 Accepted
netwolfuk commented 3 years ago

Hi bnaji.

I see you have a PR open here: https://github.com/lesserevil/tcWebHooks/pull/2/commits

Does it work ok? Is it worth trying to merge that in, it would you rather I worked on it.

What is that fork lesserevil? It looks like it has a few useful fixes in it.

From when I looked at tagged builds as part of #129, I saw that tags and pinning are asynchronous. Sometimes creating a pin with a tag will not have had the tag applied yet. Also, your discovery that tags are removed and re-added would appear to create quite a few events.

How does your pr handle that? I was thinking about delaying the event for a few seconds and trying to collect the events together. Then change the pinned event in the webhooks UI to be pinned or tagged with the option to select either or both, in a similar way to how the success/fixed events work.

bnaji commented 3 years ago

Ah the fork has a few features/fixes we use internally and webhook on tagged build was another feature that would be useful. I'm more than happy to push these changes upstream.

Does it work ok?

It works and I put a few test cases in, but I'm running into some errors when I get into the template editing pages. Something I'm missing.

How does your pr handle that?

Not in a good way. Currently I'm just ignoring events that don't have newTags, so I'm missing events where a build has a single tag that gets removed. I think your idea of catching events for a few seconds and sorting out what should happen is probably the right approach.

I'll open a PR here with tagging changes.

netwolfuk commented 3 years ago

Hi @bnaji I'm back from vacation now. Sorry for the delay.

Yes, if you'd like to raise a PR, that would be great. We can work through it together to get the delay logic going.

bnaji commented 3 years ago

Opened a PR. https://github.com/tcplugins/tcWebHooks/pull/174

netwolfuk commented 3 years ago

Hi @bnaji

I have a working build to test available for download.

I'll do some more testing over the weekend. I have Filters and Parameter editing in the UI left for Alpha8, and then I'll do a release. But can you test this in the mean time?

Lots of new stuff in here. Mainly ##teamcity[sendWebhook] service message support, and some nice statistics graphs on the admin tab.

netwolfuk commented 3 years ago

Oh. There are lots of changes to the config. I doubt rolling back will be successful. It'd be best to try it on a test server first.

netwolfuk commented 3 years ago

FYI. I'm seeing a race condition on my main TeamCity, but not under docker in my test environment or under docker on my laptop.

It looks like Apache http client is closing the http connection between the two events when both tagged and untagged are sent in quick succession.

I'll work on that over the weekend, assuming I get some time.

netwolfuk commented 3 years ago

So it turned out to be two issues.

  1. I am an idiot, and was trying to send the same webhook for both events. Fixed by using the same config, but creating a new webhook instance for each build event type (tagged or untagged).
  2. I needed to have a shared httpclient connection pool for sending multiple events quickly to the same URL. Otherwise, the client would close out the connections and empty the pool. This has now been implemented.

We should have a new build shortly on that link above.