Skip to content

Commit

Permalink
fix: published_at being set even if published: true already set
Browse files Browse the repository at this point in the history
Fixes #310

Prevent setting `published_at` when `published: true` is already set.

* **src/functions.ts**
  - Update `updateZennMetadata` to exclude `published_at` when `published: true`.
  - Add a check to skip updating `published_at` if `published: true` is already set.
* **src/__tests__/main.test.ts**
  - Add a test to verify `published_at` is not set when `published: true`.
  - Update existing test to include `published: true` and verify `published_at` is updated only when `published: false`.
* **.devcontainer.json**
  - Add a devcontainer configuration file with a test task.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/korosuke613/zenn-metadata-updater-action/issues/310?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
korosuke613 committed Oct 30, 2024
1 parent 72d7956 commit 441368f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"test": "npm run test"
}
}
31 changes: 29 additions & 2 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ published_at: "2020-01-01 00:00"
expect(actual).toEqual(expected);
});

test("published_at 未設定の場合は published_at を更新する", async () => {
test("published: true かつ published_at 未設定の場合は published_at を更新する", async () => {
const updateParam: Partial<ZennMetadata> = {
published: true,
published_at: "2021-01-01 00:00",
};

Expand All @@ -104,7 +105,7 @@ title: "Productivity Weekly (20xx-xx-xx号)"
emoji: ""
type: "idea" # tech: 技術記事 / idea: アイデア
topics: ["ProductivityWeekly", "生産性向上"]
published: true
published: false
---`);

const actual = await updateZennMetadata(updater, updateParam);
Expand All @@ -119,6 +120,32 @@ published: true

expect(actual).toEqual(expected);
});

test("published: true が設定されている場合は published_at を設定しない", async () => {
const updateParam: Partial<ZennMetadata> = {
published_at: "2023-12-27 10:00",
};

const updater = new Updater();
updater.load(`---
title: "Productivity Weekly (20xx-xx-xxtrue号)"
emoji: ""
type: "idea" # tech: 技術記事 / idea: アイデア
topics: ["ProductivityWeekly", "生産性向上"]
published: true
---`);

const actual = await updateZennMetadata(updater, updateParam);
const expected: Partial<ZennMetadata> = {
title: "Productivity Weekly (20xx-xx-xxtrue号)",
emoji: "",
type: "idea",
topics: ["ProductivityWeekly", "生産性向上"],
published: true,
};

expect(actual).toEqual(expected);
});
});

test("saveUpdatedMarkdown", async () => {
Expand Down
17 changes: 11 additions & 6 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,27 @@ export async function updateZennMetadata(
updater: Updater,
updateParams: Partial<ZennMetadata>,
) {
const metadata = updater.get();
debug(`now metadata: ${JSON.stringify(metadata, null, 2)}`);
const oldMetadata = structuredClone(updater.get());
const newMetadata = structuredClone(updater.get());
debug(`now metadata: ${JSON.stringify(oldMetadata, null, 2)}`);
debug(`input metadata: ${JSON.stringify(updateParams, null, 2)}`);
for (const [key, value] of Object.entries(updateParams)) {
if (value === "" || value === undefined) continue;

const publishedAtKey: keyof ZennMetadata = "published_at";

// もしすでに `published_at` が設定されていたら更新しない
if (key === publishedAtKey && metadata.published_at !== undefined) continue;
if (key === publishedAtKey && oldMetadata.published_at !== undefined)
continue;

metadata[key] = value;
// もし `published: true` が設定されていたら `published_at` を更新しない
if (key === publishedAtKey && oldMetadata.published === true) continue;

newMetadata[key] = value;
}
debug(`updated metadata: ${JSON.stringify(metadata, null, 2)}`);
debug(`updated metadata: ${JSON.stringify(newMetadata, null, 2)}`);

return metadata;
return newMetadata;
}

export async function validateMetadata(markdown: Buffer) {
Expand Down

0 comments on commit 441368f

Please sign in to comment.