Skip to content

Commit e0686bd

Browse files
changed to push
1 parent 9421230 commit e0686bd

4 files changed

Lines changed: 37 additions & 30 deletions

File tree

.github/workflows/updateCodeJSON.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name: Update Code.json
22
on:
33
workflow_dispatch:
4-
pull_request:
5-
types: [opened, synchronize]
4+
push:
5+
branches:
6+
- "code-json-*"
67
paths:
7-
- 'code.json'
8+
- "code.json"
89

910
permissions:
1011
contents: write
@@ -25,7 +26,7 @@ jobs:
2526
uses: DSACMS/automated-codejson-generator@sachin/jsonValidationImplementation
2627
with:
2728
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28-
ADMIN_TOKEN: ${{ secrets.ADMIN_PAT }}
29+
ADMIN_TOKEN: ${{ secrets.ADMIN_PAT }}
2930
BRANCH: "main"
3031
SKIP_PR: "false"
3132

@@ -37,4 +38,3 @@ jobs:
3738
elif [ "${{ steps.generator.outputs.method_used }}" = "pull_request" ]; then
3839
echo "Created pull request: ${{ steps.generator.outputs.pr_url }}"
3940
fi
40-

src/helper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,22 @@ export async function validateOnly(): Promise<void> {
148148
const codeJSON = await readJSON("/github/workspace/code.json");
149149

150150
if (!codeJSON) {
151-
core.setFailed("code.json file not found, is empty, or contains invalid JSON syntax...");
151+
core.setFailed(
152+
"code.json file not found, is empty, or contains invalid JSON syntax...",
153+
);
152154
return;
153155
}
154156

155157
const validationErrors = validateCodeJSON(codeJSON);
156158

157159
if (validationErrors.length > 0) {
158-
const errorMessage = `code.json validation failed with ${validationErrors.length} error(s):\n\n${validationErrors.map((err, idx) => `${idx + 1}. ${err}`).join('\n')}`;
160+
const errorMessage = `code.json validation failed with ${validationErrors.length} error(s):\n\n${validationErrors.map((err, idx) => `${idx + 1}. ${err}`).join("\n")}`;
159161
core.setFailed(errorMessage);
160162
return;
161163
}
162164

163165
core.info("code.json is valid!");
164166
core.setOutput("validated", true);
165-
166167
} catch (error) {
167168
core.setFailed(`validation error: ${error}`);
168169
}

src/main.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,18 @@ async function getMetaData(
121121
export async function run(): Promise<void> {
122122
try {
123123
const eventName = process.env.GITHUB_EVENT_NAME;
124+
const refName = process.env.GITHUB_REF;
124125
core.info(`Event name: ${eventName}`);
125126

126-
if (eventName === "pull_request") {
127-
core.info("Detected pull_request event - validating only!");
127+
if (eventName === "push" && refName?.startsWith("refs/heads/code-json-")) {
128+
core.info("Detected push to PR branch - validating only!");
128129
await helpers.validateOnly();
129130
return;
130131
}
131132

132-
const currentCodeJSON = await helpers.readJSON("/github/workspace/code.json");
133+
const currentCodeJSON = await helpers.readJSON(
134+
"/github/workspace/code.json",
135+
);
133136
const metaData = await getMetaData(currentCodeJSON);
134137
let finalCodeJSON = {} as CodeJSON;
135138

@@ -150,7 +153,7 @@ export async function run(): Promise<void> {
150153
const validationErrors = helpers.validateCodeJSON(finalCodeJSON);
151154

152155
if (validationErrors.length > 0) {
153-
const errorMessage = `Generated code.json is invalid:\n\n${validationErrors.map((err, idx) => `${idx + 1}. ${err}`).join('\n')}`;
156+
const errorMessage = `Generated code.json is invalid:\n\n${validationErrors.map((err, idx) => `${idx + 1}. ${err}`).join("\n")}`;
154157
core.setFailed(errorMessage);
155158
return;
156159
}
@@ -181,4 +184,4 @@ export async function run(): Promise<void> {
181184
} catch (error) {
182185
core.setFailed(`Action failed: ${error}`);
183186
}
184-
}
187+
}

src/validation.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { z } from "zod";
33
const DateSchema = z.object({
44
created: z.string().min(1, "created date is required"),
55
lastModified: z.string().min(1, "lastModified date is required"),
6-
metaDataLastUpdated: z.string().min(1, "metaDataLastUpdated date is required"),
6+
metaDataLastUpdated: z
7+
.string()
8+
.min(1, "metaDataLastUpdated date is required"),
79
});
810

911
const ContactSchema = z.object({
@@ -16,18 +18,17 @@ const LicenseSchema = z.object({
1618
URL: z.url("license URL must be valid").min(1, "license URL is required"),
1719
});
1820

19-
const PermissionsSchema = z.object({
20-
license: z.array(LicenseSchema).optional(),
21-
licenses: z.array(LicenseSchema).optional(),
22-
usageType: z.union([z.array(z.string()), z.string()]),
23-
exemptionText: z.string(),
24-
}).refine(
25-
(data) => data.license || data.licenses,
26-
{
21+
const PermissionsSchema = z
22+
.object({
23+
license: z.array(LicenseSchema).optional(),
24+
licenses: z.array(LicenseSchema).optional(),
25+
usageType: z.union([z.array(z.string()), z.string()]),
26+
exemptionText: z.string(),
27+
})
28+
.refine((data) => data.license || data.licenses, {
2729
message: "Either 'license' or 'licenses' array is required",
2830
path: ["license"],
29-
}
30-
);
31+
});
3132

3233
const ReuseFrequencySchema = z.object({
3334
forks: z.number(),
@@ -58,7 +59,9 @@ export const CodeJSONSchema = z.object({
5859
status: z.string().min(1, "status is required"),
5960
permissions: PermissionsSchema,
6061
organization: z.string().min(1, "organization is required"),
61-
repositoryURL: z.url("must be a valid URL").min(1, "repositoryURL is required"),
62+
repositoryURL: z
63+
.url("must be a valid URL")
64+
.min(1, "repositoryURL is required"),
6265
repositoryHost: z.string(),
6366
repositoryVisibility: z.string().min(1, "repositoryVisibility is required"),
6467
homepageURL: z.string().optional(),
@@ -97,14 +100,14 @@ export const CodeJSONSchema = z.object({
97100

98101
export function validateCodeJSON(codeJSON: any): string[] {
99102
const result = CodeJSONSchema.safeParse(codeJSON);
100-
103+
101104
if (result.success) {
102105
return [];
103106
}
104-
107+
105108
return result.error.issues.map((err: z.ZodIssue) => {
106-
const path = err.path.join('.');
107-
const field = path || 'root';
109+
const path = err.path.join(".");
110+
const field = path || "root";
108111
return `${field}: ${err.message}`;
109112
});
110-
}
113+
}

0 commit comments

Comments
 (0)