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 overloads to specify branch for ContentRequest #1093

Merged
merged 5 commits into from
Feb 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,54 @@ await fixture.DeleteFile(
new DeleteFileRequest("Deleted file", fileSha));

await Assert.ThrowsAsync<NotFoundException>(
async () => await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}

[IntegrationTest]
public async Task CrudTestWithNamedBranch()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Repository.Content;
var repoName = Helper.MakeNameWithTimestamp("source-repo");
var branchName = "other-branch";

using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true }))
{
var repository = context.Repository;

var master = await client.Git.Reference.Get(Helper.UserName, repository.Name, "heads/master");
await client.Git.Reference.Create(Helper.UserName, repository.Name, new NewReference("refs/heads/" + branchName, master.Object.Sha));
var file = await fixture.CreateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new CreateFileRequest("Test commit", "Some Content", branchName));
Assert.Equal("somefile.txt", file.Content.Name);

var contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
string fileSha = contents.First().Sha;
Assert.Equal("Some Content", contents.First().Content);

var update = await fixture.UpdateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new UpdateFileRequest("Updating file", "New Content", fileSha, branchName));
Assert.Equal("somefile.txt", update.Content.Name);

contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
Assert.Equal("New Content", contents.First().Content);
fileSha = contents.First().Sha;

await fixture.DeleteFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new DeleteFileRequest("Deleted file", fileSha, branchName));

await Assert.ThrowsAsync<NotFoundException>(
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}

Expand Down
130 changes: 130 additions & 0 deletions Octokit.Tests/Clients/RepositoryContentsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,135 @@ public async Task ReturnsContents()
Assert.Equal(1, contents.Count);
}
}

public class TheCreateFileMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repos/org/repo/contents/path/to/file";
client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));

connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public void PassesRequestObject()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));

connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<CreateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Branch == "mybranch"));
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile(null, "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", null, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", "repo", null, new CreateFileRequest("message", "myfilecontents", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", "repo", "path/to/file", null));
}
}

public class TheDeleteFileMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repos/org/repo/contents/path/to/file";
client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));

connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public void PassesRequestObject()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));

connection.Received().Delete(
Arg.Any<Uri>(),
Arg.Is<DeleteFileRequest>(a =>
a.Message == "message"
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile(null, "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", null, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", "repo", null, new DeleteFileRequest("message", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", "repo", "path/to/file", null));
}
}

public class TheUpdateFileMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repos/org/repo/contents/path/to/file";
client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));

connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public void PassesRequestObject()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));

connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<UpdateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile(null, "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", null, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", "repo", null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", "repo", "path/to/file", null));
}
}
}
}
62 changes: 60 additions & 2 deletions Octokit/Models/Request/CreateFileRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ protected ContentRequest(string message)
Message = message;
}

/// <summary>
/// Initializes a new instance of the <see cref="ContentRequest"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="branch">The branch the request is for.</param>
protected ContentRequest(string message, string branch): this(message)
{
Ensure.ArgumentNotNullOrEmptyString(branch, "branch");

Branch = branch;
}

/// <summary>
/// The commit message. This is required.
/// </summary>
Expand Down Expand Up @@ -54,6 +66,19 @@ public class DeleteFileRequest : ContentRequest
/// <param name="message">The message.</param>
/// <param name="sha">The sha.</param>
public DeleteFileRequest(string message, string sha) : base(message)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "content");

Sha = sha;
}

/// <summary>
/// Initializes a new instance of the <see cref="DeleteFileRequest"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="sha">The sha.</param>
/// <param name="branch">The branch the request is for.</param>
public DeleteFileRequest(string message, string sha, string branch) : base(message, branch)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "sha");

Expand Down Expand Up @@ -81,15 +106,27 @@ public class CreateFileRequest : ContentRequest
/// <summary>
/// Creates an instance of a <see cref="CreateFileRequest" />.
/// </summary>
/// <param name="message"></param>
/// <param name="content"></param>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
public CreateFileRequest(string message, string content) : base(message)
{
Ensure.ArgumentNotNull(content, "content");

Content = content;
}

/// <summary>
/// Initializes a new instance of the <see cref="CreateFileRequest"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
/// <param name="branch">The branch the request is for.</param>
public CreateFileRequest(string message, string content, string branch) : base(message, branch)
{
Ensure.ArgumentNotNullOrEmptyString(content, "content");

Content = content;
}
/// <summary>
/// The contents of the file to create. This is required.
/// </summary>
Expand All @@ -111,6 +148,12 @@ internal virtual string DebuggerDisplay
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class UpdateFileRequest : CreateFileRequest
{
/// <summary>
/// Creates an instance of a <see cref="UpdateFileRequest" />.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
/// <param name="sha">The sha.</param>
public UpdateFileRequest(string message, string content, string sha)
: base(message, content)
{
Expand All @@ -119,6 +162,21 @@ public UpdateFileRequest(string message, string content, string sha)
Sha = sha;
}

/// <summary>
/// Creates an instance of a <see cref="UpdateFileRequest" />.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
/// <param name="sha">The sha.</param>
/// <param name="branch">The branch the request is for.</param>
public UpdateFileRequest(string message, string content, string sha, string branch)
: base(message, content, branch)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "sha");

Sha = sha;
}

/// <summary>
/// The blob SHA of the file being replaced.
/// </summary>
Expand Down