-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit to Master
- Loading branch information
Showing
12 changed files
with
886 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.1.0 (Jun 30, 2017) | ||
|
||
- Initial release |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#Required only for Powershell 2 | ||
if ($PSVersionTable.PSVersion.Major -lt 3 ){ | ||
|
||
function ConvertTo-Json([object] $item){ | ||
add-type -assembly system.web.extensions | ||
$ps_js=new-object system.web.script.serialization.javascriptSerializer | ||
return $ps_js.Serialize($item) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
function Send-HipChat { | ||
<# | ||
.SYNOPSIS | ||
Sends messages to a Hipchat room. | ||
.DESCRIPTION | ||
Send-HipChat can be used within a script or at the console to send a message to a HipChat room. | ||
.EXAMPLE | ||
Send-Hipchat -Message 'Hello' -Color 'Green' -Notify -ApiToken myapitoken -Room MyRoom -Retry 5 -RetrySec 10 | ||
This sends a message of 'Hello' highlighted green in to a room named MyRoom. Users in the room will be notified | ||
in their clients that a new message has been recevied. If it cannot successfully send the message it will retry | ||
5 times, at 10 second intervals. | ||
#> | ||
[CmdletBinding()] | ||
[OutputType([Boolean])] | ||
Param( | ||
#Required. The message body. 10,000 characters max. | ||
[Parameter(Mandatory = $True)] | ||
[string]$message, | ||
|
||
#The background colour of the HipChat message. One of "yellow", "green", "red", "purple", "gray", or "random". (default: gray) | ||
[ValidateSet('yellow', 'green', 'red', 'purple', 'gray','random')] | ||
[string]$color = 'gray', | ||
|
||
#Set whether or not this message should trigger a notification for people in the room. (default: false) | ||
[switch]$notify, | ||
|
||
#Required. This must be a HipChat API token created by a Room Admin for the room you are sending notifications to. | ||
[Parameter(Mandatory = $True)] | ||
[string]$apitoken, | ||
|
||
#Required. The id or URL encoded name of the HipChat room you want to send the message to. | ||
[Parameter(Mandatory = $True)] | ||
[string]$room, | ||
|
||
#The number of times to retry sending the message (default: 0) | ||
[int]$retry = 0, | ||
|
||
#The number of seconds to wait between tries (default: 30) | ||
[int]$retrysecs = 30 | ||
) | ||
|
||
$messageObj = @{ | ||
"message" = $message | ||
"color" = $color | ||
"notify" = [string]$notify | ||
} | ||
|
||
$uri = "https://api.hipchat.com/v2/room/$room/notification?auth_token=$apitoken" | ||
$Body = ConvertTo-Json $messageObj | ||
$Post = [System.Text.Encoding]::UTF8.GetBytes($Body) | ||
|
||
$Retrycount = 0 | ||
|
||
While($RetryCount -le $retry){ | ||
try { | ||
if ($PSVersionTable.PSVersion.Major -gt 2 ){ | ||
$Response = Invoke-WebRequest -Method Post -Uri $uri -Body $Body -ContentType "application/json" -ErrorAction SilentlyContinue | ||
}else{ | ||
$Request = [System.Net.WebRequest]::Create($uri) | ||
$Request.ContentType = "application/json" | ||
$Request.ContentLength = $Post.Length | ||
$Request.Method = "POST" | ||
|
||
$requestStream = $Request.GetRequestStream() | ||
$requestStream.Write($Post, 0,$Post.length) | ||
$requestStream.Close() | ||
|
||
$Response = $Request.GetResponse() | ||
|
||
$stream = New-Object IO.StreamReader($Response.GetResponseStream(), $Response.ContentEncoding) | ||
$stream.ReadToEnd() | Out-Null | ||
$stream.Close() | ||
$Response.Close() | ||
} | ||
Write-Verbose "'$message' sent!" | ||
Return $true | ||
|
||
} catch { | ||
Write-Error "Could not send message: `r`n $_.Exception.ToString()" | ||
|
||
If ($retrycount -lt $retry){ | ||
Write-Verbose "retrying in $retrysecs seconds..." | ||
Start-Sleep -Seconds $retrysecs | ||
} | ||
} | ||
$Retrycount++ | ||
} | ||
|
||
Write-Verbose "Could not send after $Retrycount tries. I quit." | ||
Return $false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# HipChatPS | ||
|
||
[![Build status](https://ci.appveyor.com/api/projects/status/qfeei0wai40h8f3u?svg=true)](https://ci.appveyor.com/project/markwragg/hipchatps) | ||
|
||
A module for PowerShell with functions for interacting with the team chat tool "Hipchat" by Atlassian. The module utilises Hipchat API v2: https://www.hipchat.com/docs/apiv2. | ||
|
||
## Commands | ||
|
||
The following functions are available: | ||
|
||
#### 1. Send-HipChat | ||
|
||
For sending notifications into a room. Before you can use this you need to create an API v2 token for the room that you want to send notifications to. | ||
|
||
To do this: | ||
|
||
1. Go to https://yourdomain.hipchat.com/admin/rooms/ and select the room you wish to notify. | ||
2. Go to Tokens. | ||
3. Create a Send Notification token. Note the "Label" you define will be included with the notification. | ||
|
||
> **Beware, tokens here: https://yourdomain.hipchat.com/admin/api will not work, these are for API v1.** | ||
##### Example | ||
|
||
Attempt to send a message to a room named "My Room" coloured green. Will retry 5 additional times if it fails, waiting 30 seconds between each attempt. Will write verbose output to console. | ||
|
||
Send-HipChat -message "my message" -room "My%20Room" -apitoken a1b2c3d4e5f6a1b2c3d4e5f6 -color green -verbose -retry 5 -retrysec 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
$projectRoot = Split-Path -Parent $here | ||
$moduleRoot = "$projectRoot\HipChatPS" | ||
|
||
. "$moduleRoot\Public\Send-HipChat.ps1" | ||
|
||
Describe 'Send-HipChat' { | ||
|
||
Mock Invoke-WebRequest { Import-Clixml "$here\Send-HipChat.invoke-webrequest.xml" } | ||
|
||
It "should return true" { | ||
|
||
$params = @{ | ||
message = "Pester test message" | ||
room = "Test" | ||
apitoken = "c6cS2qXSv1zRyUUXpPsu3bebVF43wx8bvPQK5vg6" | ||
} | ||
|
||
Send-HipChat @params | Should Be $true | ||
} | ||
|
||
It "should reject the colour blue" { | ||
|
||
$params = @{ | ||
message = "Pester test message" | ||
room = "Test" | ||
apitoken = "fakefalsetoken" | ||
color = "blue" | ||
} | ||
|
||
{send-hipchat @params} | Should Throw | ||
} | ||
} | ||
|
||
|
||
Describe "send-hipchat timeouts" { | ||
|
||
Mock Invoke-WebRequest {Throw} | ||
|
||
It "should retry 3 additional times" { | ||
|
||
$params = @{ | ||
message = "Pester test message" | ||
room = "Test" | ||
apitoken = "fakefalsetoken" | ||
retry = 3 | ||
retrysecs = 1 | ||
ErrorAction = "SilentlyContinue" | ||
} | ||
|
||
send-hipchat @params | Should be $false | ||
Assert-MockCalled Invoke-WebRequest -Exactly 4 -ModuleName $moduleName -Scope It | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.