It's nearly impossible to make an alliance during a war where you are the aggressor unless you tried to call an ally in and they refused and then you re-ally them. The tooltip pretty much gives you the reasons why they wont ally. In a defensive war, you have about 6 months to a year to nail down an ally and bring them in. Unless you drop below -25% warscore, in which case you are pretty much screwed unless you can reverse it fast. The two big factors are how long the war has been going on and the warscore. Diplo Rep as always can be a huge factor as well. Basically, if you have a country that is a looming threat on you, butter up it's rivals and neighbors even if you don't necessarily want to be allied with them. That way if you do get attacked in a worst case scenario (your regular ally is distant and getting stomped in an aggressive war but didn't call you in), you might be able to secure a new alliance to help you out for the duration of the war, you can always drop them after you're safe again.
The AI doesn't do it nearly as often as it may appear since they usually secure as many strategic alliances as they can ahead of time. The times it will call in a new ally is if a regular ally is in such bad shape it didn't accept the CTA, in which case the AI falls on a backup since it's constantly increasing relations with those around it. For the most part, the way the AI picks it's alliances are to sandwich bordering nations it considers rivals or threats. That's why it's pretty common to see France and Hungary ally, because one or the other, or both have issues with Austria. Same thing with Venice + France vs Austria even though Venice is a historical friend to Austria. Another common one is the Ottoman+Timurid alliance because of QQ.