Skip to content

Commit

Permalink
Add token var as job token doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Aug 2, 2021
1 parent 034bf04 commit 832e889
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public async Task<CommandResult> DeployStack(IStackConfiguration<T> configuratio
var gitLabClient = GitLabClient.Create();

if (gitLabClient != null) {
Information("GitLab API URL is defined");
Information("GitLab API URL and credentials are defined");
await gitLabClient.AddMergeRequestNote(previewResult.StandardOutput);
}
else {
Information("GitLab API URL is not defined");
Information("CI_API_V4_URL or GITLAB_API_TOKEN variable is not defined");
}

return new CommandResult(
Expand Down
18 changes: 8 additions & 10 deletions Ubiquitous.AutoDevOps.Automation/GitLab/GitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@

namespace Ubiquitous.AutoDevOps.GitLab {
class GitLabClient {
readonly HttpClient _httpClient;
readonly HttpClient _httpClient;

static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web);

public static GitLabClient? Create() {
var baseUrl = GetEnv("CI_API_V4_URL");
return baseUrl.IsEmpty() ? null : new GitLabClient(baseUrl!);
var token = GetEnv("GITLAB_API_TOKEN");
return baseUrl.IsEmpty() || token.IsEmpty() ? null : new GitLabClient(baseUrl!, token!);
}

GitLabClient(string baseUrl) {
GitLabClient(string baseUrl, string token) {
_httpClient = new HttpClient {
BaseAddress = new Uri(baseUrl),
DefaultRequestHeaders = {{"PRIVATE-TOKEN", GetEnv("CI_JOB_TOKEN")}}
DefaultRequestHeaders = {{"PRIVATE-TOKEN", token}}
};
}

public async Task AddMergeRequestNote(string content) {
var projectId = GetEnv("CI_PROJECT_ID");
var mrIid = GetEnv("CI_MERGE_REQUEST_IID");
var mrIid = GetEnv("CI_MERGE_REQUEST_IID");

if (projectId == null || mrIid == null) {
Log.Information("Project or merge request id not defined");
Expand All @@ -36,10 +37,7 @@ public async Task AddMergeRequestNote(string content) {
Log.Information("Adding a note to the merge request");

var resource = $"/projects/{projectId}/merge_requests/{mrIid}/notes";

Log.Information("API endpoint {Endpoint}", _httpClient.BaseAddress);
Log.Information("Calling {Resource}", resource);


var note = new NewNote(content);
var response = await _httpClient.PostAsJsonAsync(resource, note, SerializerOptions);
Log.Information("Result: {Code} {Reason}", response.StatusCode, response.ReasonPhrase);
Expand Down

0 comments on commit 832e889

Please sign in to comment.