Skip to content

Commit 1050274

Browse files
authored
fix: don't assume submodule is on previous tag when strategy "tag" (#22)
1 parent 85914b7 commit 1050274

File tree

10 files changed

+146
-17
lines changed

10 files changed

+146
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ variables for each submodule that was updated:
7777
after it was updated.
7878
- `${prefix}--latestCommitSha`: The commit SHA of the submodule after it was
7979
updated.
80-
- `${prefix}--previousTag`: The tag of the submodule before it was updated. **May not exist if the submodule does not have any tags.**
80+
- `${prefix}--previousTag`: The latest tag of the submodule before it was updated. **May not exist if the submodule does not have any tags.**
8181
- `${prefix}--latestTag`: The tag that the submodule was updated to. **Only available when the strategy is set to 'tag'.**
8282
- `${prefix}--prBody`: A multi-line Markdown string intended for use in a pull request body.
8383

dist/index.js

Lines changed: 24 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/git.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test, vi } from "vitest";
22
import { getExecOutput } from "@actions/exec";
3-
import { getCommit, getPreviousTag, getTag } from "../git";
3+
import { getCommit, getPreviousTag, getTag, hasTag } from "../git";
44

55
vi.mock("@actions/exec", async () => {
66
return {
@@ -64,3 +64,33 @@ test("fail to get previous tag and continue", async () => {
6464
const previousTag = await getPreviousTag(input);
6565
expect(previousTag).toEqual(undefined);
6666
});
67+
68+
test("previous commit has a tag pointing to it", async () => {
69+
const input = "a19a19bd14f26c3bba311bbffc5a74710add5ac2";
70+
71+
vi.mocked(getExecOutput).mockReturnValueOnce(
72+
Promise.resolve({
73+
exitCode: 0,
74+
stdout: "v1.0.0\n",
75+
stderr: "",
76+
})
77+
);
78+
79+
const previousCommitHasTag = await hasTag("", input);
80+
expect(previousCommitHasTag).true;
81+
});
82+
83+
test("previous commit has no tag and continue", async () => {
84+
const input = "a19a19bd14f26c3bba311bbffc5a74710add5ac2";
85+
86+
vi.mocked(getExecOutput).mockReturnValueOnce(
87+
Promise.resolve({
88+
exitCode: 0,
89+
stdout: "\n\n",
90+
stderr: "",
91+
})
92+
);
93+
94+
const previousCommitHasTag = await hasTag("", input);
95+
expect(previousCommitHasTag).false;
96+
});

src/__tests__/main.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ test("extract single submodule from .gitmodules", async () => {
7474
stderr: "",
7575
})
7676
)
77+
.mockReturnValueOnce(
78+
Promise.resolve({
79+
exitCode: 0,
80+
stdout: `\n${expected[0].previousTag}`,
81+
stderr: "",
82+
})
83+
)
7784
.mockReturnValueOnce(
7885
Promise.resolve({
7986
exitCode: 0,
@@ -90,6 +97,7 @@ test("extract single submodule from .gitmodules that has no tags", async () => {
9097
const input = await readFile("src/__tests__/fixtures/single-gitmodules.ini");
9198
const expected = [mdBookSubmodule()];
9299
expected[0].previousTag = undefined;
100+
expected[0].previousCommitShaHasTag = false;
93101

94102
vi.mocked(getExecOutput)
95103
.mockReturnValueOnce(
@@ -99,6 +107,13 @@ test("extract single submodule from .gitmodules that has no tags", async () => {
99107
stderr: "",
100108
})
101109
)
110+
.mockReturnValueOnce(
111+
Promise.resolve({
112+
exitCode: 0,
113+
stdout: `\n\n`,
114+
stderr: "",
115+
})
116+
)
102117
.mockRejectedValueOnce(new Error());
103118

104119
const actual = await parseGitmodules(input);
@@ -152,6 +167,27 @@ test("extract multiple git submodules from .gitmodules", async () => {
152167
stderr: "",
153168
})
154169
)
170+
.mockReturnValueOnce(
171+
Promise.resolve({
172+
exitCode: 0,
173+
stdout: `\n${vscodeIcons.previousTag}`,
174+
stderr: "",
175+
})
176+
)
177+
.mockReturnValueOnce(
178+
Promise.resolve({
179+
exitCode: 0,
180+
stdout: `\n${nvim.previousTag}`,
181+
stderr: "",
182+
})
183+
)
184+
.mockReturnValueOnce(
185+
Promise.resolve({
186+
exitCode: 0,
187+
stdout: `\n${mdBook.previousTag}`,
188+
stderr: "",
189+
})
190+
)
155191
.mockReturnValueOnce(
156192
Promise.resolve({
157193
exitCode: 0,

src/__tests__/markdown.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,27 @@ test("single pr body for single submodule using tag strategy", async () => {
7575

7676
expect(actual).toContain(expected);
7777
});
78+
79+
test("markdown pr body for multiple submodules using tag strategy and previous commit has no tag", async () => {
80+
const [mdBook, nvim, vscodeIcons] = [
81+
mdBookSubmodule(),
82+
nvimSubmodule(),
83+
vscodeIconsSubmodule(),
84+
];
85+
mdBook.previousCommitShaHasTag = false;
86+
nvim.previousCommitShaHasTag = false;
87+
mdBook.latestTag = "v2.2.0";
88+
nvim.latestTag = "v1.9.0";
89+
vscodeIcons.latestTag = "v1.15.0";
90+
const submodules = [mdBook, nvim, vscodeIcons];
91+
const expected = `
92+
| --- | --- | --- |
93+
| [catppuccin/mdBook](https://github.com/catppuccin/mdBook.git) | ports/mdBook | [a19a19b...v2.2.0](https://github.com/catppuccin/mdBook/compare/a19a19b...v2.2.0) |
94+
| [catppuccin/nvim](https://github.com/catppuccin/nvim.git) | ports/nvim | [774a4ed...v1.9.0](https://github.com/catppuccin/nvim/compare/774a4ed...v1.9.0) |
95+
| [catppuccin/vscode-icons](https://github.com/catppuccin/vscode-icons.git) | ports/vscode-icons | [v1.14.0...v1.15.0](https://github.com/catppuccin/vscode-icons/compare/v1.14.0...v1.15.0) |
96+
`;
97+
98+
const actual = multiplePrBody(submodules);
99+
100+
expect(actual).toContain(expected);
101+
});

src/__tests__/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class SubmoduleBuilder {
1717
!this.submodule.remoteName ||
1818
!this.submodule.previousShortCommitSha ||
1919
!this.submodule.previousCommitSha ||
20+
!this.submodule.previousCommitShaHasTag ||
2021
!this.submodule.latestShortCommitSha ||
2122
!this.submodule.latestCommitSha
2223
) {
@@ -33,6 +34,7 @@ export const mdBookSubmodule = (
3334
remoteName: string = "catppuccin/mdBook",
3435
previousShortCommitSha: string = "a19a19b",
3536
previousCommitSha: string = "a19a19bd14f26c3bba311bbffc5a74710add5ac2",
37+
previousCommitShaHasTag: boolean = true,
3638
previousTag: string = "v0.1.2",
3739
latestShortCommitSha: string = "a19a19b",
3840
latestCommitSha: string = "a19a19bd14f26c3bba311bbffc5a74710add5ac2"
@@ -44,6 +46,7 @@ export const mdBookSubmodule = (
4446
remoteName,
4547
previousShortCommitSha,
4648
previousCommitSha,
49+
previousCommitShaHasTag,
4750
previousTag,
4851
latestShortCommitSha,
4952
latestCommitSha,
@@ -57,6 +60,7 @@ export const vscodeIconsSubmodule = (
5760
remoteName: string = "catppuccin/vscode-icons",
5861
previousShortCommitSha: string = "71d98b8",
5962
previousCommitSha: string = "71d98b81bfdb6b8d3527037c3017eb07e6ec0621",
63+
previousCommitShaHasTag: boolean = true,
6064
previousTag: string = "v1.14.0",
6165
latestShortCommitSha: string = "71d98b8",
6266
latestCommitSha: string = "71d98b81bfdb6b8d3527037c3017eb07e6ec0621"
@@ -68,6 +72,7 @@ export const vscodeIconsSubmodule = (
6872
remoteName,
6973
previousShortCommitSha,
7074
previousCommitSha,
75+
previousCommitShaHasTag,
7176
previousTag,
7277
latestShortCommitSha,
7378
latestCommitSha,
@@ -81,6 +86,7 @@ export const nvimSubmodule = (
8186
remoteName: string = "catppuccin/nvim",
8287
previousShortCommitSha: string = "774a4ed",
8388
previousCommitSha: string = "774a4ed9a69d0a2633da60f73aa63a8e23aacced",
89+
previousCommitShaHasTag: boolean = true,
8490
previousTag: string = "v1.8.0",
8591
latestShortCommitSha: string = "774a4ed",
8692
latestCommitSha: string = "774a4ed9a69d0a2633da60f73aa63a8e23aacced"
@@ -92,6 +98,7 @@ export const nvimSubmodule = (
9298
remoteName,
9399
previousShortCommitSha,
94100
previousCommitSha,
101+
previousCommitShaHasTag,
95102
previousTag,
96103
latestShortCommitSha,
97104
latestCommitSha,

src/git.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ export const getPreviousTag = async (
2727
return undefined;
2828
});
2929
};
30+
31+
export const hasTag = async (
32+
path: string,
33+
commitSha: string
34+
): Promise<boolean> => {
35+
core.info(`'${path}': Checking if tag exists at commit '${commitSha}'`);
36+
const options = { cwd: path };
37+
const tag = (
38+
await getExecOutput("git tag --points-at HEAD", [], options)
39+
).stdout.trim();
40+
return tag !== "";
41+
};

src/main.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fs from "node:fs/promises";
44
import { logInfoAndDebug, toJson, toJsonPretty } from "./logging";
55
import { multiplePrBody, singlePrBody } from "./markdown";
66
import { z } from "zod";
7-
import { getCommit, getPreviousTag, getTag } from "./git";
7+
import { getCommit, getPreviousTag, getTag, hasTag } from "./git";
88
import { parse } from "ini";
99

1010
const updateStrategy = z.enum(["commit", "tag"]);
@@ -31,6 +31,7 @@ export type Submodule = {
3131
remoteName: string;
3232
previousShortCommitSha: string;
3333
previousCommitSha: string;
34+
previousCommitShaHasTag: boolean;
3435
previousTag?: string;
3536
latestShortCommitSha: string;
3637
latestCommitSha: string;
@@ -96,6 +97,7 @@ export const parseGitmodules = async (
9697
const urlParts = url.replace(".git", "").split("/");
9798
const remoteName = `${urlParts[3]}/${urlParts[4]}`;
9899
const [previousCommitSha, previousShortCommitSha] = await getCommit(path);
100+
const previousCommitShaHasTag = await hasTag(path, previousCommitSha);
99101
const previousTag = await getPreviousTag(path);
100102

101103
return {
@@ -105,13 +107,13 @@ export const parseGitmodules = async (
105107
remoteName,
106108
previousShortCommitSha,
107109
previousCommitSha,
110+
previousCommitShaHasTag,
111+
previousTag,
108112

109113
// The latest commit should be updated after the submodule is updated
110114
// If you think about it, the "previous" commit is the latest commit too
111115
latestShortCommitSha: previousShortCommitSha,
112116
latestCommitSha: previousCommitSha,
113-
114-
previousTag,
115117
};
116118
})
117119
);

src/markdown.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ const tableRow = (submodule: Submodule) => {
1414
let changeDisplay = `${submodule.previousShortCommitSha}...${submodule.latestShortCommitSha}`;
1515
let changeUrl = `[${changeDisplay}](${cleanUrl}/compare/${submodule.previousCommitSha}...${submodule.latestCommitSha})`;
1616
if (submodule.latestTag) {
17-
changeDisplay = `${submodule.previousTag}...${submodule.latestTag}`;
17+
if (submodule.previousCommitShaHasTag) {
18+
changeDisplay = `${submodule.previousTag}...${submodule.latestTag}`;
19+
} else {
20+
changeDisplay = `${submodule.previousShortCommitSha}...${submodule.latestTag}`;
21+
}
1822
changeUrl = `[${changeDisplay}](${cleanUrl}/compare/${changeDisplay})`;
1923
}
2024
return `| ${name} | ${submodule.path} | ${changeUrl} |`;

0 commit comments

Comments
 (0)