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

Limit issue creation to one per bad image #681

Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.idea/workspace.xml
.idea/checkstyle-idea.xml

site-validation/.idea/
site-validation/site-validation.iml

bad-image-check-results.json

# Logs
Expand Down
17 changes: 12 additions & 5 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { rewriteGuideUrl } = require("./src/components/util/guide-url-rewriter")
const ESLintPlugin = require("eslint-webpack-plugin")
const { validate } = require("./src/data/image-validation")
const fs = require("fs/promises")
let badImages = []
let badImages = { }

exports.sourceNodes = async ({
actions,
Expand Down Expand Up @@ -111,7 +111,13 @@ exports.sourceNodes = async ({
if (!isValid || isGitHubBlobPage) {
console.warn("Not a valid image in", node.artifact, ". Image link is:", iconUrl)
delete node.metadata.icon
badImages.push({ url: iconUrl, artifact: node.artifact })
if (badImages[iconUrl]) {
badImages[iconUrl].artifacts.push(node.artifact)
badImages[iconUrl].slugs.push(node.slug)
} else {
badImages[iconUrl] = { url: iconUrl, reason: "Invalid", slugs: [node.slug], artifacts: [node.artifact] }
}

} else {
try {
await createRemoteFileNode({
Expand Down Expand Up @@ -307,10 +313,11 @@ exports.onCreateWebpackConfig = ({ stage, actions }) => {
}

exports.onPostBootstrap = async () => {
const badImageDetails = Object.values(badImages);
// Write out to a file
if (badImages?.length > 0) {
console.warn(`Recording details of ${badImages.length} bad images.`)
const content = JSON.stringify(badImages)
if (badImageDetails?.length > 0) {
console.warn(`Recording details of ${badImageDetails.length} bad images.`)
const content = JSON.stringify(badImageDetails)
const resultsFile = "bad-image-check-results.json"
await fs.writeFile(resultsFile, content, { flag: "w+" }, err => {
console.warn("Error writing results:", err)
Expand Down
21 changes: 14 additions & 7 deletions site-validation/bad-image-issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.stream.Collectors;

@Command(name = "report", mixinStandardHelpOptions = true,
description = "Raises and closes issues depending on the results of bad image checking")
Expand Down Expand Up @@ -118,6 +119,9 @@ private void closeResolvedIssues(GitHub github, List<BadImage> links) throws IOE
}
}

private String getOwningPages(String[] slugs) {
return Arrays.stream(slugs).map(slug -> siteUrl + "/" + slug).collect(Collectors.joining("\n - "));
}

private void processBadImage(GitHub github, BadImage link) {
try {
Expand All @@ -135,18 +139,19 @@ private void processBadImage(GitHub github, BadImage link) {

// If there's no matching defect ...
if (answer.getTotalCount() == 0) {
String title = String.format("Invalid image: %s", link.url);
// Eventually, we would like to customise the message depending on the exact nature of the broken link.
// For now, just do something generic.
String title = String.format("%s image: %s", link.reason, link.url);
String body = String.format("""
Invalid image: %s
%s image: %s

The problem image was found on %s
The problem image was found on these artifacts: %s

Affected pages are
- %s

This issue was auto-created by the bad image helper.

--- %s --- Do not remove this line or the bad image helper will not be able to manage this issue
""", link.url, link.artifact, EYECATCHER);
""", link.reason, link.url, Arrays.toString(link.artifacts), getOwningPages(link.slugs), EYECATCHER);

if (!dryRun) {
GHIssue issue = repository.createIssue(title)
Expand Down Expand Up @@ -193,6 +198,8 @@ public static void main(String... args) {

record BadImage(
String url,
String artifact) {
String reason,
String[] artifacts,
String[] slugs) {
}
}
Loading