-
-
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
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
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