Skip to content

Instantly share code, notes, and snippets.

@frankfuu
Created June 15, 2019 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankfuu/e1350ec8d18de284e4c76507ff0c2d6c to your computer and use it in GitHub Desktop.
Save frankfuu/e1350ec8d18de284e4c76507ff0c2d6c to your computer and use it in GitHub Desktop.
Show difference in commit messages in two different git branches using Bitbucket REST API
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Foo
{
public interface IBitbucketService
{
string GetDiffMessages(string sourceBranch, string destinationBranch, string fields, string pagelen);
}
public class BitbucketService : IBitbucketService
{
public string GetDiffMessages(string sourceBranch, string destinationBranch, string fields, string pagelen)
{
// reference - https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commits
var url = "https://api.bitbucket.org/2.0/repositories/mycompany/myproject/commits/";
var client = new RestClient(url);
var req = new RestRequest(Method.GET);
if(fields == null || fields.Length == 0)
fields = "values.message,next";
if (string.IsNullOrEmpty(pagelen))
pagelen = "100";
req.AddQueryParameter("access_token", "<my_access_token>");
req.AddQueryParameter("fields", fields);
req.AddQueryParameter("include", sourceBranch);
req.AddQueryParameter("exclude", destinationBranch);
req.AddQueryParameter("pagelen", pagelen);
var access_token = GetAccessToken();
req.AddHeader("Authorization", $"Bearer {access_token}");
return client.Execute(req).Content;
}
private string GetAccessToken()
{
var url = "https://bitbucket.org/site/oauth2/access_token";
var client = new RestClient(url);
var req = new RestRequest(Method.POST);
req.AddHeader("Content-Type", "application/x-www-form-urlencoded");
req.AddHeader("Authorization", "Basic <my_token>");
req.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials", ParameterType.RequestBody);
dynamic res = JsonConvert.DeserializeObject(client.Execute(req).Content);
return res.access_token;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment