Skip to content

Commit

Permalink
Fixed jira client
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed May 3, 2024
1 parent c7f2222 commit 2bd74a0
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions Apps.Jira/JiraClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ public JiraClient(IEnumerable<AuthenticationCredentialsProvider> authenticationC
: base(new RestClientOptions
{ ThrowOnAnyError = false, BaseUrl = new Uri(authenticationCredentialsProviders.First(p => p.KeyName == "JiraUrl").Value) })
{
var username = authenticationCredentialsProviders.First(p => p.KeyName == "User").Value;
var password = authenticationCredentialsProviders.First(p => p.KeyName == "Authorization").Value;
var encodedCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));

this.AddDefaultHeader("Authorization", $"Basic {encodedCredentials}");
this.AddDefaultHeader("Authorization",
authenticationCredentialsProviders.First(p => p.KeyName == "Authorization").Value);
}

public async Task<T> ExecuteWithHandling<T>(RestRequest request)
Expand Down Expand Up @@ -46,11 +43,11 @@ private static string GetJiraCloudId(IEnumerable<AuthenticationCredentialsProvid
{
const string atlassianResourcesUrl = "https://api.atlassian.com/oauth/token/accessible-resources";
string jiraUrl = authenticationCredentialsProviders.First(p => p.KeyName == "JiraUrl").Value;
var authorizationHeader = GetAuthorizationHeader(authenticationCredentialsProviders);
string authorizationHeader = authenticationCredentialsProviders.First(p => p.KeyName == "Authorization").Value;
var restClient = new RestClient(new RestClientOptions
{ ThrowOnAnyError = true, BaseUrl = new Uri(atlassianResourcesUrl) });
var request = new RestRequest("");
request.AddHeader("Authorization", $"Basic {authorizationHeader}");
request.AddHeader("Authorization", authorizationHeader);
var atlassianCloudResources = restClient.Get<List<AtlassianCloudResourceDto>>(request);
var cloudId = atlassianCloudResources.First(jiraResource => jiraUrl.Contains(jiraResource.Url)).Id
?? throw new ArgumentException("The Jira URL is incorrect.");
Expand All @@ -63,12 +60,5 @@ private Exception ConfigureErrorException(RestResponse response)
var errorMessages = string.Join(" ", error.ErrorMessages);
return new(errorMessages);
}

private static string GetAuthorizationHeader(IEnumerable<AuthenticationCredentialsProvider> authenticationCredentialsProviders)
{
var username = authenticationCredentialsProviders.First(p => p.KeyName == "User").Value;
var password = authenticationCredentialsProviders.First(p => p.KeyName == "Authorization").Value;
return Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
}
}
}

0 comments on commit 2bd74a0

Please sign in to comment.