Skip to content

Commit 3e4e1f8

Browse files
authored
Merge pull request #486 from ap0llo/prepare-release-json-output
Add JSON output option to 'prepare-release' command
2 parents c7a3395 + 12c81cc commit 3e4e1f8

File tree

4 files changed

+351
-13
lines changed

4 files changed

+351
-13
lines changed

doc/nbgv-cli.md

+43
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,49 @@ The behaviour of the `prepare-release` command can be customized in
121121
| versionIncrement | `minor` | Specifies which part of the version on the current branch is incremented when preparing a release. Allowed values are `major`, `minor` and `build`. |
122122
| firstUnstableTag | `alpha` | Specified the unstable tag to use for the main branch. |
123123

124+
### Customizing the `prepare-release` output format
125+
126+
By default, the `prepare-release` command writes information about created and updated branches to the console as text.
127+
Alternatively the information can be written to the output as `json`.
128+
The output format to use can be set using the `--format` command line parameter.
129+
130+
For example, running the follwoing command on `master`
131+
132+
```
133+
nbgv prepare-release --format json
134+
```
135+
136+
will generate output similar to this:
137+
138+
```json
139+
{
140+
"CurrentBranch": {
141+
"Name": "master",
142+
"Commit": "5a7487098ac1be1ceb4dbf72d862539cf0b0c27a",
143+
"Version": "1.7-alpha"
144+
},
145+
"NewBranch": {
146+
"Name": "v1.7",
147+
"Commit": "b2f164675ffe891b66b601c00efc4343581fc8a5",
148+
"Version": "1.7"
149+
}
150+
}
151+
```
152+
153+
The JSON object has to properties:
154+
155+
- `CurrentBranch` provides information about the branch `prepare-release` was started on (typically `master`)
156+
- `NewBranch` provides information about the new branch created by the command.
157+
158+
For each branch, the following proprties are provided:
159+
160+
- `Name`: The name of the branch
161+
- `Commit`: The id of the latest commit on that branch
162+
- `Version`: The version configured in that branch's `version.json`
163+
164+
**Note:** When the current branch is already the release branch for the current version, no new branch will be created.
165+
In that case, the `NewBranch` property will be `null`.
166+
124167
## Learn more
125168

126169
There are several more sub-commands and switches to each to help you build and maintain your projects, find a commit that built a particular version later on, create tags, etc.

src/NerdBank.GitVersioning.Tests/ReleaseManagerTests.cs

+152
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using LibGit2Sharp;
55
using Nerdbank.GitVersioning;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
68
using Xunit;
79
using Xunit.Abstractions;
810

@@ -401,6 +403,156 @@ public void PrepareRelease_InvalidVersionIncrement()
401403
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.InvalidVersionIncrementSetting);
402404
}
403405

406+
[Fact]
407+
public void PrepareRelease_TextOutput()
408+
{
409+
// create and configure repository
410+
this.InitializeSourceControl();
411+
412+
// create version.json
413+
var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse("1.0") };
414+
this.WriteVersionFile(versionOptions);
415+
416+
var stdout = new StringWriter();
417+
var releaseManager = new ReleaseManager(stdout);
418+
releaseManager.PrepareRelease(this.RepoPath);
419+
420+
// by default, text output mode should be used => trying to parse it as JSON should fail
421+
Assert.ThrowsAny<JsonException>(() => JsonConvert.DeserializeObject(stdout.ToString()));
422+
}
423+
424+
[Fact]
425+
public void PrepareRelease_JsonOutput()
426+
{
427+
// create and configure repository
428+
this.InitializeSourceControl();
429+
430+
// create version.json
431+
var versionOptions = new VersionOptions()
432+
{
433+
Version = SemanticVersion.Parse("1.0"),
434+
Release = new ReleaseOptions()
435+
{
436+
BranchName = "v{version}",
437+
VersionIncrement = ReleaseVersionIncrement.Minor
438+
}
439+
};
440+
this.WriteVersionFile(versionOptions);
441+
442+
var currentBranchName = this.Repo.Head.FriendlyName;
443+
var releaseBranchName = "v1.0";
444+
445+
// run release preparation
446+
var stdout = new StringWriter();
447+
var releaseManager = new ReleaseManager(stdout);
448+
releaseManager.PrepareRelease(this.RepoPath, outputMode: ReleaseManager.ReleaseManagerOutputMode.Json);
449+
450+
451+
// Expected output:
452+
// {
453+
// "CurrentBranch" : {
454+
// "Name" : "<NAME-OF-CURRENT-BRANCH>",
455+
// "Commit" : "<HEAD-COMMIT-OF-CURRENT-BRANCH>",
456+
// "Version" : "<UPDATED-VERSION-ON-CURRENT-BRANCH>",
457+
// },
458+
// "NewBranch" : {
459+
// "Name" : "<NAME-OF-CREATED-BRANCH>",
460+
// "Commit" : "<HEAD-COMMIT-OF-CREATED-BRANCH>",
461+
// "Version" : "<VERSION-ON-CREATED-BRANCH>",
462+
// }
463+
// }
464+
465+
var jsonOutput = JObject.Parse(stdout.ToString());
466+
467+
// check "CurrentBranch" output
468+
{
469+
var expectedCommitId = this.Repo.Branches[currentBranchName].Tip.Sha;
470+
var expectedVersion = VersionFile.GetVersion(this.Repo.Branches[currentBranchName].Tip).Version.ToString();
471+
472+
var currentBranchOutput = jsonOutput.Property("CurrentBranch")?.Value as JObject;
473+
Assert.NotNull(currentBranchOutput);
474+
475+
Assert.Equal(currentBranchName, currentBranchOutput.GetValue("Name")?.ToString());
476+
Assert.Equal(expectedCommitId, currentBranchOutput.GetValue("Commit")?.ToString());
477+
Assert.Equal(expectedVersion, currentBranchOutput.GetValue("Version")?.ToString());
478+
479+
}
480+
481+
// Check "NewBranch" output
482+
{
483+
var expectedCommitId = this.Repo.Branches[releaseBranchName].Tip.Sha;
484+
var expectedVersion = VersionFile.GetVersion(this.Repo.Branches[releaseBranchName].Tip).Version.ToString();
485+
486+
var newBranchOutput = jsonOutput.Property("NewBranch")?.Value as JObject;
487+
Assert.NotNull(newBranchOutput);
488+
489+
Assert.Equal(releaseBranchName, newBranchOutput.GetValue("Name")?.ToString());
490+
Assert.Equal(expectedCommitId, newBranchOutput.GetValue("Commit")?.ToString());
491+
Assert.Equal(expectedVersion, newBranchOutput.GetValue("Version")?.ToString());
492+
}
493+
}
494+
495+
[Fact]
496+
public void PrepareRelease_JsonOutputWhenUpdatingReleaseBranch()
497+
{
498+
// create and configure repository
499+
this.InitializeSourceControl();
500+
501+
// create version.json
502+
var versionOptions = new VersionOptions()
503+
{
504+
Version = SemanticVersion.Parse("1.0"),
505+
Release = new ReleaseOptions()
506+
{
507+
BranchName = "v{version}",
508+
VersionIncrement = ReleaseVersionIncrement.Minor
509+
}
510+
};
511+
this.WriteVersionFile(versionOptions);
512+
var branchName = "v1.0";
513+
514+
// switch to release branch
515+
Commands.Checkout(this.Repo, this.Repo.CreateBranch(branchName));
516+
517+
// run release preparation
518+
var stdout = new StringWriter();
519+
var releaseManager = new ReleaseManager(stdout);
520+
releaseManager.PrepareRelease(this.RepoPath, outputMode: ReleaseManager.ReleaseManagerOutputMode.Json);
521+
522+
523+
// Expected output:
524+
// {
525+
// "CurrentBranch" : {
526+
// "Name" : "<NAME>",
527+
// "Commit" : "<COMMIT>",
528+
// "Version" : "<VERSION>",
529+
// },
530+
// "NewBranch" : null
531+
// }
532+
533+
var jsonOutput = JObject.Parse(stdout.ToString());
534+
535+
// check "CurrentBranch" output
536+
{
537+
var expectedCommitId = this.Repo.Branches[branchName].Tip.Sha;
538+
var expectedVersion = VersionFile.GetVersion(this.Repo.Branches[branchName].Tip).Version.ToString();
539+
540+
var currentBranchOutput = jsonOutput.Property("CurrentBranch")?.Value as JObject;
541+
Assert.NotNull(currentBranchOutput);
542+
543+
Assert.Equal(branchName, currentBranchOutput.GetValue("Name")?.ToString());
544+
Assert.Equal(expectedCommitId, currentBranchOutput.GetValue("Commit")?.ToString());
545+
Assert.Equal(expectedVersion, currentBranchOutput.GetValue("Version")?.ToString());
546+
547+
}
548+
// Check "NewBranch" output
549+
{
550+
// no new branch was created, so "NewBranch" should be null
551+
var newBranchOutput = jsonOutput.Property("NewBranch")?.Value as JObject;
552+
Assert.Null(newBranchOutput);
553+
}
554+
}
555+
404556
private void AssertError(Action testCode, ReleasePreparationError expectedError)
405557
{
406558
var ex = Assert.Throws<ReleasePreparationException>(testCode);

0 commit comments

Comments
 (0)