Skip to content

Commit

Permalink
Enable relative links in Docker image tags tables (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussell authored Sep 25, 2024
1 parent 1204a2d commit 2d91b7a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,26 @@ public override async Task ExecuteAsync()

// Generate Product Family Readme
await GenerateArtifactsAsync(
new ManifestInfo[] { Manifest },
(manifest) => manifest.ReadmeTemplatePath,
(manifest) => manifest.ReadmePath,
(manifest, templatePath, indent) => GetTemplateState(manifest, templatePath, indent),
nameof(Readme.TemplatePath),
ArtifactName);
contexts: new ManifestInfo[] { Manifest },
getTemplatePath: (manifest) => manifest.ReadmeTemplatePath,
getArtifactPath: (manifest) => manifest.ReadmePath,
getState: (manifest, templatePath, indent) => GetTemplateState(manifest, templatePath, indent),
templatePropertyName: nameof(Readme.TemplatePath),
artifactName: ArtifactName);

// Generate Repo Readmes
await GenerateArtifactsAsync(
Manifest.FilteredRepos
contexts: Manifest.FilteredRepos
.Select(repo => repo.Readmes.Select(readme => (repo, readme)))
.SelectMany(repoReadme => repoReadme),
((RepoInfo repo, Readme readme) context) => context.readme.TemplatePath,
((RepoInfo repo, Readme readme) context) => context.readme.Path,
((RepoInfo repo, Readme readme) context, string templatePath, string indent) => GetTemplateState(context.repo, templatePath, indent),
nameof(Readme.TemplatePath),
ArtifactName,
(string readmeContent, (RepoInfo repo, Readme readme) context) => UpdateTagsListing(readmeContent, context.repo));
getTemplatePath: ((RepoInfo repo, Readme readme) context) => context.readme.TemplatePath,
getArtifactPath: ((RepoInfo repo, Readme readme) context) => context.readme.Path,
getState: ((RepoInfo repo, Readme readme) context, string templatePath, string indent) =>
GetTemplateState(context.repo, templatePath, indent),
templatePropertyName: nameof(Readme.TemplatePath),
artifactName: ArtifactName,
postProcess: (string readmeContent, (RepoInfo repo, Readme readme) context) =>
UpdateTagsListing(readmeContent, context.repo));

ValidateArtifacts();
}
Expand Down Expand Up @@ -115,8 +117,7 @@ private string UpdateTagsListing(string readme, RepoInfo repo)
return readme;
}

string tagsMetadata = McrTagsMetadataGenerator.Execute(
_gitService, Manifest, repo, Options.SourceRepoUrl, Options.SourceRepoBranch);
string tagsMetadata = McrTagsMetadataGenerator.Execute(Manifest, repo);
string tagsListing = GenerateTagsListing(repo.Name, tagsMetadata);
return ReadmeHelper.UpdateTagsListing(readme, tagsListing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private GitObject[] GetUpdatedReadmes(string productRepo)
foreach (string readmePath in readmePaths)
{
string fullPath = Path.Combine(Manifest.Directory, readmePath);

string updatedReadMe = File.ReadAllText(fullPath);
updatedReadMe = ReadmeHelper.UpdateTagsListing(updatedReadMe, McrTagsPlaceholder);
readmes.Add(GetGitObject(productRepo, fullPath, updatedReadMe));
Expand All @@ -182,7 +182,7 @@ private GitObject[] GetUpdatedTagsMetadata(string productRepo)

foreach (RepoInfo repo in Manifest.FilteredRepos)
{
string updatedMetadata = McrTagsMetadataGenerator.Execute(_gitService, Manifest, repo, Options.SourceRepoUrl);
string updatedMetadata = McrTagsMetadataGenerator.Execute(Manifest, repo, generateGitHubLinks: true, _gitService, Options.SourceRepoUrl);
string metadataFileName = Path.GetFileName(repo.Model.McrTagsMetadataTemplate);
metadata.Add(GetGitObject(productRepo, metadataFileName, updatedMetadata));
}
Expand Down
28 changes: 22 additions & 6 deletions src/Microsoft.DotNet.ImageBuilder/src/McrTagsMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ public class McrTagsMetadataGenerator
private string _sourceRepoUrl;
private string _sourceBranch;
private List<ImageDocumentationInfo> _imageDocInfos;

public static string Execute(IGitService gitService, ManifestInfo manifest, RepoInfo repo, string sourceRepoUrl, string sourceBranch = null)
private bool _generateGitHubLinks;

public static string Execute(
ManifestInfo manifest,
RepoInfo repo,
bool generateGitHubLinks = false,
IGitService gitService = null,
string sourceRepoUrl = null,
string sourceBranch = null)
{
McrTagsMetadataGenerator generator = new McrTagsMetadataGenerator()
// Generating GitHub permalinks requires gitService
if (generateGitHubLinks == true)
{
ArgumentNullException.ThrowIfNull(gitService);
}

McrTagsMetadataGenerator generator = new()
{
_gitService = gitService,
_manifest = manifest,
_repo = repo,
_generateGitHubLinks = generateGitHubLinks,
_gitService = gitService,
_sourceRepoUrl = sourceRepoUrl,
_sourceBranch = sourceBranch,
};
Expand Down Expand Up @@ -92,7 +106,9 @@ private string GetTagGroupYaml(IEnumerable<ImageDocumentationInfo> infos)
{
ImageDocumentationInfo firstInfo = infos.First();

string dockerfilePath = _gitService.GetDockerfileCommitUrl(firstInfo.Platform, _sourceRepoUrl, _sourceBranch);
string dockerfilePath = _generateGitHubLinks
? _gitService.GetDockerfileCommitUrl(firstInfo.Platform, _sourceRepoUrl, _sourceBranch)
: firstInfo.Platform.DockerfilePathRelativeToManifest;

// Generate a list of tags that have this sorting convention:
// <concrete tags>, <shared tags of platforms that have no concrete tags>, <shared tags of platforms that have concrete tags>
Expand Down Expand Up @@ -148,7 +164,7 @@ private string GetVariableValue(string variableType, string variableName)
{
_imageDocInfos.Remove(docInfo);
}

variableValue = GetTagGroupYaml(matchingDocInfos);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ namespace Microsoft.DotNet.ImageBuilder.Tests
public class McrTagsMetadataGeneratorTests
{
/// <summary>
/// Verfies the Dockerfile path is set correctly
/// Verfies the Dockerfile path is set correctly
/// </summary>
/// <remarks>
/// If the source branch isn't set, the commit SHA of the Dockerfile will be used in the URL
/// See https://github.com/dotnet/dotnet-docker/issues/1436
/// </remarks>
[Theory]
[InlineData("branch")]
[InlineData(null)]
public void DockerfileUrl(string sourceRepoBranch)
[InlineData(true, "branch")]
[InlineData(true, null)]
[InlineData(false, "branch")]
[InlineData(false, null)]
public static void DockerfileLink(bool generateGitHubLinks, string sourceRepoBranch)
{
using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

Expand All @@ -42,7 +44,7 @@ public void DockerfileUrl(string sourceRepoBranch)
// Create Dockerfile
string DockerfileDir = $"1.0/{RepoName}/os";
Directory.CreateDirectory(Path.Combine(tempFolderContext.Path, DockerfileDir));
string dockerfileRelativePath = Path.Combine(DockerfileDir, "Dockerfile");
string dockerfileRelativePath = DockerfileDir + '/' + "Dockerfile";
string dockerfileFullPath = PathHelper.NormalizePath(Path.Combine(tempFolderContext.Path, dockerfileRelativePath));
File.WriteAllText(dockerfileFullPath, "FROM base:tag");

Expand Down Expand Up @@ -89,16 +91,25 @@ public void DockerfileUrl(string sourceRepoBranch)

// Execute generator
string result = McrTagsMetadataGenerator.Execute(
gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, sourceRepoBranch);
manifestInfo,
repo,
generateGitHubLinks: generateGitHubLinks,
gitService: gitServiceMock.Object,
sourceRepoUrl: SourceRepoUrl,
sourceBranch: sourceRepoBranch);

TagsMetadata tagsMetadata = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()
.Deserialize<TagsMetadata>(result);

string branchOrSha = sourceRepoBranch ?? DockerfileSha;
Assert.Equal($"{SourceRepoUrl}/blob/{branchOrSha}/{DockerfileDir}/Dockerfile",
tagsMetadata.Repos[0].TagGroups[0].Dockerfile);

string expectedUrl = generateGitHubLinks
? $"{SourceRepoUrl}/blob/{branchOrSha}/{DockerfileDir}/Dockerfile"
: dockerfileRelativePath;

Assert.Equal(expectedUrl, tagsMetadata.Repos[0].TagGroups[0].Dockerfile);
}

/// <summary>
Expand Down Expand Up @@ -183,7 +194,12 @@ public void HandlesUndocumentedPlatform(bool hasSharedTag)

// Execute generator
string result = McrTagsMetadataGenerator.Execute(
gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, SourceBranch);
manifestInfo,
repo,
generateGitHubLinks: true,
gitService: gitServiceMock.Object,
sourceRepoUrl: SourceRepoUrl,
sourceBranch: SourceBranch);

TagsMetadata tagsMetadata = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
Expand Down Expand Up @@ -279,7 +295,13 @@ public void DuplicatedPlatform()

// Execute generator
string result = McrTagsMetadataGenerator.Execute(
gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, SourceBranch);
manifestInfo,
repo,
generateGitHubLinks: true,
gitService: gitServiceMock.Object,
sourceRepoUrl: SourceRepoUrl,
sourceBranch: SourceBranch);


TagsMetadata tagsMetadata = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
Expand Down

0 comments on commit 2d91b7a

Please sign in to comment.