Skip to content
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

Add alias for creating GitHub Releases #7

Merged
merged 16 commits into from
Feb 4, 2022

Conversation

ap0llo
Copy link
Contributor

@ap0llo ap0llo commented Nov 1, 2021

Description

This PR adds the alias GitHubCreateReleaseAsync() which allows creating a GitHub Release from cake,
as discussed in ap0llo/Cake.GitHubReleases#1.

Since the code I brought over from ap0llo/Cake.GitHubReleases was already using nullable reference types, I enabled nullable reference types (and C# 8) for the entire project and added nullable annotations to the existing code.

Related Issue

Motivation and Context

With this change, it will be possible to create a GitHub Release from a Cake script.
The alias simplifies creation of the release without the need to use Octokit directly.

Integrating this functionality into Cake.GitHub rather than maintaining a separate addin should make it easier for users to integrate GitHub functionality into their Cake script and also avoids potential dependency conflicts that might arise from two addins depending on different versions of Octokit.

How Has This Been Tested?

Almost all of the functionality for creating releases is implemented in the GitHubReleaseCreator class.
I added unit tests to the existing Cake.GitHub.Tests project that cover all scenarios (at least all of the scenarios I could think of). The tests use Moq to mock the access to GitHub.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project:
    I tried to make the signature of GitHubCreateReleaseAsync() as similar to the existing GitHubStatus() alias to make both of them as consistent as possible. If you have additional notes w.r.t. code style, please let me know
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@ap0llo ap0llo changed the title Add alias to create a GitHub Release Add alias for creating GitHub Releases Nov 1, 2021
When building on Visual Studio 2017, the xunit analyzer seems to run into an error when checking the siganture of a test case provider.
Disabling nullable fpr that method should fix the problem
@GeertvanHorrik
Copy link
Contributor

Not sure why I missed this one. Will review asap.

Copy link
Contributor

@GeertvanHorrik GeertvanHorrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, sorry for my late review, the notification got buried in my inbox. This code looks good, I've reviewed most of it and added some comments. Looking forward to hearing your opinion about it.

Once I know what direction we are going with the current comments, we can finalize the review and merge this.

internal static class MoqExtensions
{
public static IReturnsResult<TMock> ThrowsNotFoundAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock) where TMock : class =>
mock.ThrowsAsync(new NotFoundException("", HttpStatusCode.NotFound));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not wrong, but I prefer string.Empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've updated it to use string.Empty

{
public class RepositoriesClientMock
{
private readonly Mock<IRepositoriesClient> m_Mock = new Mock<IRepositoriesClient>(MockBehavior.Strict);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't use the m_ prefix anywhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That convention is used in the code bases I usually work on, so I just used it without thinking about it, I've updated all fields to use just a _ prefix

/// </summary>
internal class XunitCakeLog : ICakeLog
{
private readonly ITestOutputHelper m_TestOutputHelper;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_testOutputHelper instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

[CakeMethodAlias]
public static async Task<GitHubRelease> GitHubCreateReleaseAsync(
this ICakeContext context,
string userName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be nullable, similar to what is mentioned in the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right

/// </summary>
public sealed class GitHubRelease
{
private readonly List<GitHubReleaseAsset> m_Assets = new List<GitHubReleaseAsset>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use _assets instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


internal void Add(GitHubReleaseAsset asset)
{
if (asset is null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be null when we have nullable enabled on the project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, we can omit the null checks for internal methods, I've updated the code accordingly

/// </summary>
internal static GitHubReleaseAsset FromReleaseAsset(ReleaseAsset asset)
{
if (asset is null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@ap0llo
Copy link
Contributor Author

ap0llo commented Feb 1, 2022

First of all, sorry for my late review, the notification got buried in my inbox.

No worries :)

This code looks good, I've reviewed most of it and added some comments. Looking forward to hearing your opinion about it.
Once I know what direction we are going with the current comments, we can finalize the review and merge this.

All your comments are reasonable, I've updated the code accordingly.

Would you like me to squash the commits together a bit or should I just leave the cleanup commits in place?

@GeertvanHorrik
Copy link
Contributor

Would you like me to squash the commits together a bit or should I just leave the cleanup commits in place?

My preference is to keep it, even for PRs the history of commits can be very useful to understand the "thought process" afterwards. So if you agree, let's keep it.

Will do a re-review as soon as I can.

Copy link
Contributor

@GeertvanHorrik GeertvanHorrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed everything except the unit tests. Let's discuss the current comments and see if we both agree on them.

Then we can hopefully finalize this PR because it looks great so far 👍

/// </summary>
internal static GitHubRelease FromRelease(Release release)
{
if (release is null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot be null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


public GitHubReleaseCreator(ICakeLog cakeLog, IFileSystem fileSystem, IGitHubClient githubClient)
{
_cakeLog = cakeLog ?? throw new ArgumentNullException(nameof(cakeLog));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the throws can be removed as well, nothing will ever be null here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if (String.IsNullOrWhiteSpace(tagName))
throw new ArgumentException("Value must not be null or whitespace", nameof(tagName));

if (settings is null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

var existingRelease = await TryGetReleaseAsync(owner: owner, repository: repository, tagName: tagName);
if (existingRelease != null)
{
if (settings.Overwrite)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for readability: if we invert this, it saves us reading / understanding the code:

if (!settings.Overwrite)
{
     throw new Release ....
}

await DeleteReleaseAsync...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@GeertvanHorrik
Copy link
Contributor

@pascalberger we might need your help here.

@ap0llo did a great job adding this PR, and I've reviewed (and approved). Do we need to synchronize any other files to make this work (e.g. cake scripts, recipes, etc)?

@gep13
Copy link
Member

gep13 commented Feb 4, 2022

@GeertvanHorrik said...
Do we need to synchronize any other files to make this work (e.g. cake scripts, recipes, etc)?

What changes are you thinking need to happen here?

@GeertvanHorrik
Copy link
Contributor

I saw the check failing, and it's been a while we synced the recipe files, so I made the assumption it might be time for a synchronization. Can we just merge this PR?

@gep13
Copy link
Member

gep13 commented Feb 4, 2022

Oh, I see what you mean...

There is a known issue that I have seen with running GitVersion on AppVeyor when it is a Pull Request. I am not entirely sure what is going on there, as I haven't dug into it. It could be that this is solved with a newer version of GitVersion, but as mentioned, I haven't dug into it.

If the build is working locally for you, then I would recommend merging the PR, and in all likelihood it will succeed currently when building off the develop branch.

@GeertvanHorrik
Copy link
Contributor

Thanks for the explanation, going to merge now.

@GeertvanHorrik GeertvanHorrik merged commit 14ee8ae into cake-contrib:develop Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants