-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PART 3: Add UploadArchiveToGithubStorageWithMultiPart method #1271
Conversation
Unit Test Results815 tests 815 ✅ 21s ⏱️ Results for commit cd88309. ♻️ This comment has been updated with latest results. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped some comments mostly around simplifying the code and unit tests. Tests need to be added for GithubClient
too
using var content = new MultipartFormDataContent | ||
{ | ||
{ httpContent, "archive", archiveName } | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initializing a using variable with an object initializer might lead to problems if an exception is thrown during the initialization and the using variable may never get disposed so here it's better to add the content with the Add()
method:
using var content = new MultipartFormDataContent | |
{ | |
{ httpContent, "archive", archiveName } | |
}; | |
using var content = new MultipartFormDataContent(); | |
content.Add(streamContent, "archive", archiveName); |
{ | ||
var url = $"https://uploads.github.com/organizations/{orgDatabaseId.EscapeDataString()}/gei/archive?name={archiveName.EscapeDataString()}"; | ||
|
||
response = await _client.PostAsync(url, httpContent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renaming httpContent
to streamContent
:
response = await _client.PostAsync(url, httpContent); | |
response = await _client.PostAsync(url, streamContent); |
@@ -1071,6 +1072,33 @@ mutation abortRepositoryMigration( | |||
} | |||
} | |||
|
|||
public virtual async Task<string> UploadArchiveToGithubStorage(string orgDatabaseId, bool isMultipart, string archiveName, Stream archiveContent) | |||
{ | |||
using var httpContent = new StreamContent(archiveContent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would call this variable streamContent
for clarity as both stream and multipart from data are technically HttpContent
"
using var httpContent = new StreamContent(archiveContent); | |
using var streamContent = new StreamContent(archiveContent); |
_log.LogVerbose(body is MultipartFormDataContent or StreamContent ? "HTTP BODY: BLOB" : $"HTTP BODY: {body.ToJson()}"); | ||
|
||
request.Content = body.ToJson().ToStringContent(); | ||
request.Content = body switch | ||
{ | ||
MultipartFormDataContent multipartFormDataContent => multipartFormDataContent, | ||
StreamContent streamContent => streamContent, | ||
_ => body.ToJson().ToStringContent() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking this can be simplified as we now either support a HttpContent
or a JSON object (it cannot let me add a code suggestion):
if (body is HttpContent httpContent)
{
_log.LogVerbose("HTTP BODY: BLOB");
request.Content = httpContent;
}
else
{
var jsonBody = body.ToJson();
_log.LogVerbose($"HTTP BODY: {jsonBody}");
request.Content = jsonBody.ToStringContent();
}
@@ -3462,6 +3463,62 @@ await _githubApi.Invoking(api => api.AbortMigration(migrationId)) | |||
.WithMessage(expectedErrorMessage); | |||
} | |||
|
|||
[Fact] | |||
public async Task UploadArchiveToGithubStorage() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public async Task UploadArchiveToGithubStorage() | |
public async Task UploadArchiveToGithubStorage_Should_Upload_Stream_Content() |
var jsonResponse = $"{{ \"archiveId\": \"{expectedArchiveId}\" }}"; | ||
|
||
_githubClientMock | ||
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), null)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make sure that PostAsync
receives a StreamContent
:
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), null)) | |
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<StreamContent>(), null)) |
|
||
|
||
[Fact] | ||
public async Task UploadArchiveToGithubStorageWithMultiPart() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use snaked cased test names throughout the project:
public async Task UploadArchiveToGithubStorageWithMultiPart() | |
public async Task UploadArchiveToGithubStorage_Should_Upload_Multipart_Content() |
var jsonResponse = $"{{ \"archiveId\": \"{expectedArchiveId}\" }}"; | ||
|
||
_githubClientMock | ||
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), null)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), null)) | |
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<MultipartFormDataContent>(), null)) |
This pull request introduces a new feature for uploading archives to GitHub storage and includes corresponding unit tests.
We are also updating the content types of Stream and Multipart uploads in the SendAsync method.
ThirdPartyNotices.txt
(if applicable)