Skip to content
Snippets Groups Projects
Commit b5123d13 authored by L. Ellenbeck's avatar L. Ellenbeck
Browse files

Merge branch 'Topic/570-gitVersion' into 'master'

Topic/570 git version

See merge request coscine/cs/exampleproject!21
parents 87c0a106 fc593bb2
No related branches found
No related tags found
1 merge request!21Topic/570 git version
......@@ -2,6 +2,8 @@
#tool nuget:?package=vswhere&version=2.8.4
#tool nuget:?package=GitVersion.CommandLine&version=5.1.3
#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.Json&version=4.0.0
#addin nuget:https://api.nuget.org/v3/index.json?package=Newtonsoft.Json&version=11.0.2
#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.FileHelpers&version=3.2.1
using System.Net;
......@@ -32,11 +34,11 @@ var msBuildPathX64 = (vsLatest == null) ? null : vsLatest.CombineWithFilePath(".
Setup(context =>{
nupkgDir = $"{artifactsDir.ToString()}/nupkg";
Information($"Started at {DateTime.Now}");
Information("Started at {0}", DateTime.Now);
});
Teardown(context =>{
Information($"Finished at {DateTime.Now}");
Information("Finished at {0}", DateTime.Now);
});
Task("Clean")
......@@ -57,7 +59,7 @@ Task("Clean")
directoriesToClean.Add(artifactsDir);
foreach(var dir in directoriesToClean) {
Information($"Cleaning {dir.ToString()}");
Information("Cleaning {0}", dir.ToString());
if (DirectoryExists(dir)) {
DeleteDirectory(dir, settings);
CreateDirectory(dir);
......@@ -99,7 +101,7 @@ Task("GitVersion")
}
var index = version.IndexOf("-");
semanticVersion = index > 0 ? version.Substring(0, index) : version;
Information($"Version: {version}, SemanticVersion: {semanticVersion}");
Information("Version: {0}, SemanticVersion: {1}", version, semanticVersion);
});
Task("UpdateAssemblyInfo")
......@@ -124,71 +126,57 @@ Task("UpdateAssemblyInfo")
Task("GitlabRelease")
.IsDependentOn("GitVersion")
.Does(() => {
IEnumerable<string> redirectedStandardOutput;
var exitCodeWithArgument =
StartProcess(
"git",
new ProcessSettings {
Arguments = "describe --tags --abbrev=0",
RedirectStandardOutput = true
},
out redirectedStandardOutput
);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("PRIVATE-TOKEN", gitlabToken);
var lastTag = redirectedStandardOutput.LastOrDefault();
var logParam = "";
if(exitCodeWithArgument == 0) {
logParam = $"{lastTag}..Head";
} else {
lastTag = null;
// get the latest tag
var result = client.GetAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/repository/tags").Result;
if(!result.IsSuccessStatusCode) {
throw new Exception("Tag query failed.");
}
string url = null;
if(lastTag != null) {
if(String.IsNullOrWhiteSpace(gitlabProjectPath)) {
exitCodeWithArgument =
StartProcess(
"git",
new ProcessSettings {
Arguments = $"config --get remote.origin.url",
RedirectStandardOutput = true
},
out redirectedStandardOutput
);
var tagList = result.Content.ReadAsStringAsync().Result;
var jArray = JArray.Parse(tagList);
// null if not tags exists yet
var lastTag = jArray.Select(x => x["name"]).FirstOrDefault();
url = redirectedStandardOutput.LastOrDefault();
if(url.StartsWith("git@git.rwth-aachen.de:")){
url = url.Replace("git@git.rwth-aachen.de:", "https://git.rwth-aachen.de/");
}
} else {
url = $"https://git.rwth-aachen.de/{gitlabProjectPath}";
}
var url = $"https://git.rwth-aachen.de/{gitlabProjectPath}";
if(url.EndsWith(".git")) {
url = url.Substring(0, url.Length - ".git".Length);
}
if(url.EndsWith("/")) {
url = url.Substring(0, url.Length - 1);
}
url = $"{url}/compare/{lastTag}...v{semanticVersion}";
var description = "";
// First line of description
// Gitlab compare url, if something can be compared
if(lastTag == null) {
description = $"# {semanticVersion} ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n";
} else {
description = $"# [{semanticVersion}]({url}/compare/{lastTag}...v{semanticVersion}) ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n";
}
exitCodeWithArgument =
// From when will messages be parsed, null results in all messages
var logParam = "";
if(lastTag != null) {
logParam = $"{lastTag}..Head";
}
Information(lastTag);
IEnumerable<string> redirectedStandardOutput;
var exitCodeWithArgument =
StartProcess(
"git",
new ProcessSettings {
Arguments = $"log {logParam} --oneline",
Arguments = $"log {logParam} --pretty=format:HASH%h:%B",
RedirectStandardOutput = true
},
out redirectedStandardOutput
);
var description = "";
if(lastTag == null) {
description = $"# {semanticVersion} ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n";
} else {
description = $"# [{semanticVersion}]({url}) ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n";
}
var prefixList = new Dictionary<string, List<string>>{
{"Fix", new List<string>()},
......@@ -201,14 +189,22 @@ IEnumerable<string> redirectedStandardOutput;
{"Chore", new List<string>()},
};
var currentHash = "";
// Output last line of process output.
foreach(var line in redirectedStandardOutput) {
var index = line.IndexOf(" ");
var commitHash = line.Substring(0, index);
var commitMessage = line.Substring(index + 1);
var commitMessage = "";
if(line.StartsWith("HASH")) {
currentHash = line.Substring("HASH".Length);
currentHash = currentHash.Substring(0, currentHash.IndexOf(":"));
commitMessage = line.Substring(currentHash.Length + line.IndexOf(currentHash) + 1);
} else {
commitMessage = line;
}
foreach(var kv in prefixList) {
if(commitMessage.StartsWith($"{kv.Key}:")) {
kv.Value.Add($"* {commitMessage.Substring(kv.Key.Length + 1).Trim()} {commitHash}");
kv.Value.Add($"* {commitMessage.Substring(kv.Key.Length + 1).Trim()} {currentHash}");
break;
}
};
}
......@@ -222,19 +218,27 @@ IEnumerable<string> redirectedStandardOutput;
description += "\n";
}
}
// correctly escape the json newlines
description = description.Replace("\n", "\\n");
Information("Description: {0}", description);
throw new Exception();
// create tag
var client = new HttpClient();
client.DefaultRequestHeaders.Add("PRIVATE-TOKEN", gitlabToken);
var result = client.PostAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/repository/tags?tag_name=v{semanticVersion}&ref=master", null).Result;
result = client.PostAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/repository/tags?tag_name=v{semanticVersion}&ref=master", null).Result;
Information("Create tag: {0}", result.Content.ReadAsStringAsync().Result);
if(!result.IsSuccessStatusCode) {
throw new Exception("Tag creation failed.");
}
description = description.Replace("\n", "\\n");
// create release
var json = $"{{\"name\": \"v{semanticVersion}\", \"tag_name\": \"v{semanticVersion}\", \"description\": \"{description}\"}}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
result = client.PostAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/releases", content).Result;
Information("Create release: {0}", result.Content.ReadAsStringAsync().Result);
if(!result.IsSuccessStatusCode) {
throw new Exception("Release creation failed.");
}
});
Task("Build")
......@@ -256,7 +260,7 @@ Task("Build")
}
// Use MSBuild
Information($"Building {solutionFile}");
Information("Building {0}", solutionFile);
MSBuild(solutionFile, frameworkSettingsWindows);
});
......@@ -307,7 +311,7 @@ Task("CopyToArtifacts")
&& !FileExists($"{project.GetDirectory()}/{project.GetFilenameWithoutExtension()}.nuspec")
&& DirectoryExists(project.GetDirectory()))
{
Information($"Copying {project.GetDirectory()}/bin/{configuration}/* to {artifactsDir}");
Information("Copying {0}/* to {1}", $"{project.GetDirectory()}/bin/{configuration}", artifactsDir);
CopyDirectory($"{project.GetDirectory()}/bin/{configuration}/", artifactsDir);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment