Copilot commented on code in PR #429: URL: https://github.com/apache/grails-static-website/pull/429#discussion_r2873517910
########## buildSrc/src/main/groovy/website/gradle/tasks/BskyAtProtoDidTask.groovy: ########## @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package website.gradle.tasks + +import groovy.transform.CompileStatic +import org.gradle.api.GradleException +import org.gradle.api.Project +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity +import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.TaskProvider +import org.yaml.snakeyaml.Yaml +import website.gradle.GrailsWebsiteExtension + +@CompileStatic +@CacheableTask +abstract class BskyAtProtoDidTask extends GrailsWebsiteTask { + + @Internal + final String description = 'Generates the .well-known/atproto-did file for Bsky' + + public static final String NAME = 'genBskyAtProtoDid' + + @InputFile + @PathSensitive(PathSensitivity.RELATIVE) + abstract RegularFileProperty getSocial() + + @OutputFile + abstract RegularFileProperty getBskyAtProtoDid() + + static TaskProvider<BskyAtProtoDidTask> register( + Project project, + GrailsWebsiteExtension siteExt, + String name = NAME + ) { + project.tasks.register(name, BskyAtProtoDidTask) { + it.social.set(siteExt.social) + it.bskyAtProtoDid.set(siteExt.bskyAtProtoDid) + } + } + + @TaskAction + void generateAtProtoDid() { + bskyAtProtoDid.get().asFile.tap { + parentFile.mkdirs() + text = getBskyAtProtoDidText() + } + } + + private Map getSocialProperties() { + File socialFile = social.get().asFile + socialFile.withInputStream { is -> new Yaml().load(is) } as Map + } + + private String getBskyAtProtoDidText() { + try { + socialProperties['bsky']['did'] + } catch (NullPointerException ignore) { + throw new GradleException('Missing property [bsky.did]') + } Review Comment: `getBskyAtProtoDidText()` only throws a `GradleException` when the `bsky` map itself is missing (NPE). If `bsky` exists but `did` is missing/empty, this method returns `null` and the task will later fail when writing `File.text`, producing a less helpful NPE and potentially writing invalid output. Prefer explicit null/blank checks on `socialProperties.bsky.did` (and possibly trimming) and throw a `GradleException` with a clear message when it’s absent, rather than catching `NullPointerException`. ```suggestion Map socialProps = socialProperties def bskyProps = socialProps != null ? socialProps['bsky'] : null if (!(bskyProps instanceof Map)) { throw new GradleException('Missing property [bsky.did]') } def didValue = bskyProps['did'] if (!(didValue instanceof CharSequence)) { throw new GradleException('Missing property [bsky.did]') } String did = didValue.toString().trim() if (did.isEmpty()) { throw new GradleException('Missing property [bsky.did]') } return did ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
