Tuesday, 29 October 2013

Using Twitter as a build status board

Twitter is an ideal medium for creating a build board. Everyone has an account, team members can elect how and when they'd like to be informed, it works on a multitude of devices for the BYOD crowd.

You need a Twitter account, and a Twitter development account so that you can define an application and request the necessary Keys and Tokens.

But once you have this information, you're good to send your first tweets

Tweet Deck Illustration




TweetDeck employed as an 'activity board'



Function Set-TwitterStatus
{
    param (
        [string]$tweet)

    [Reflection.Assembly]::LoadWithPartialName("System.Security")  | Out-Null
    [Reflection.Assembly]::LoadWithPartialName("System.Net")  | Out-Null
  
    $status = [System.Uri]::EscapeDataString($tweet);  
    $oauth_consumer_key = "";  
    $oauth_consumer_secret = "";  
    $oauth_token = "";  
    $oauth_token_secret = "";  
    $oauth_nonce = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([System.DateTime]::Now.Ticks.ToString()));  

    $ts = [System.DateTime]::UtcNow - [System.DateTime]::ParseExact("01/01/1970", "dd/MM/yyyy", $null).ToUniversalTime();  
    $oauth_timestamp = [System.Convert]::ToInt64($ts.TotalSeconds).ToString();  
  
    $signature = "POST&";  
    $signature += [System.Uri]::EscapeDataString("https://api.twitter.com/1.1/statuses/update.json") + "&";  
    $signature += [System.Uri]::EscapeDataString("oauth_consumer_key=" + $oauth_consumer_key + "&");  
    $signature += [System.Uri]::EscapeDataString("oauth_nonce=" + $oauth_nonce + "&");   
    $signature += [System.Uri]::EscapeDataString("oauth_signature_method=HMAC-SHA1&");  
    $signature += [System.Uri]::EscapeDataString("oauth_timestamp=" + $oauth_timestamp + "&");  
    $signature += [System.Uri]::EscapeDataString("oauth_token=" + $oauth_token + "&");  
    $signature += [System.Uri]::EscapeDataString("oauth_version=1.0&");  
    $signature += [System.Uri]::EscapeDataString("status=" + $status);  
  
    $signature_key = [System.Uri]::EscapeDataString($oauth_consumer_secret) + "&" + [System.Uri]::EscapeDataString($oauth_token_secret);  
  
    $hmacsha1 = new-object System.Security.Cryptography.HMACSHA1;  
    $hmacsha1.Key = [System.Text.Encoding]::ASCII.GetBytes($signature_key);  
    $oauth_signature = [System.Convert]::ToBase64String($hmacsha1.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($signature)));  
  
    $oauth_authorization = 'OAuth ';  
    $oauth_authorization += 'oauth_consumer_key="' + [System.Uri]::EscapeDataString($oauth_consumer_key) + '",';  
    $oauth_authorization += 'oauth_nonce="' + [System.Uri]::EscapeDataString($oauth_nonce) + '",';  
    $oauth_authorization += 'oauth_signature="' + [System.Uri]::EscapeDataString($oauth_signature) + '",';  
    $oauth_authorization += 'oauth_signature_method="HMAC-SHA1",'  
    $oauth_authorization += 'oauth_timestamp="' + [System.Uri]::EscapeDataString($oauth_timestamp) + '",'  
    $oauth_authorization += 'oauth_token="' + [System.Uri]::EscapeDataString($oauth_token) + '",';  
    $oauth_authorization += 'oauth_version="1.0"';  



    $post_body = [System.Text.Encoding]::ASCII.GetBytes("status=" + $status);   
    [System.Net.HttpWebRequest] $request = [System.Net.WebRequest]::Create("https://api.twitter.com/1.1/statuses/update.json");  
    $request.Method = "POST";  
    $request.Headers.Add("Authorization", $oauth_authorization);  
    $request.ContentType = "application/x-www-form-urlencoded";  
    $body = $request.GetRequestStream();  
    $body.write($post_body, 0, $post_body.length);  
    $body.flush();  
    $body.close();  
    $response = $request.GetResponse();
}

Then, you're good to create tweets to suit your needs.

Set-TwitterStatus "Build 1000 has completed with 0 errors"
Set-TwitterStatus "Build 1000 was deploy to OAT in 1m:55s"