Skip to content

Commit

Permalink
feat: store metadata file
Browse files Browse the repository at this point in the history
Signed-off-by: Ar Rakin <rakinar2@onesoftnet.eu.org>
  • Loading branch information
virtual-designer committed Aug 28, 2024
1 parent edb08bc commit b494f04
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 22 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ inputs:
git-push-branch:
description: "The branch to push the commit and tag to."
required: false
metadata-file:
description: "The path to the file that contains the metadata."
required: false
default: ".github/cra-metadata.json"
runs:
using: node20
main: "build/index.js"
66 changes: 59 additions & 7 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32997,6 +32997,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const exec_1 = __nccwpck_require__(1514);
const crypto = __nccwpck_require__(6113);
class GitClient {
constructor(gitPath) {
this.oldGitOptions = {};
Expand All @@ -33023,6 +33024,44 @@ class GitClient {
return stdout;
});
}
getCommits(start, end) {
return __awaiter(this, void 0, void 0, function* () {
const boundary = crypto.randomBytes(64).toString("hex");
const output = (yield this.execWithOutput({
args: [
"log",
"--no-decorate",
"--no-color",
`--pretty=format:%H %B\n${boundary}`,
start && `${start}${end ? `..${end}` : ""}`,
].filter(Boolean),
})).trim();
const commits = [];
for (let i = 0; i < output.length; i++) {
let sha = "";
while (output[i] !== " " && i < output.length) {
sha += output[i];
i++;
}
i++;
let message = "";
while (i < output.length) {
if (output[i] === "\n" &&
output.slice(i + 1, i + boundary.length + 1) === boundary) {
i += boundary.length;
break;
}
message += output[i];
i++;
}
commits.push({
id: sha.trim(),
message: message.trim(),
});
}
return commits;
});
}
add(...files) {
return __awaiter(this, void 0, void 0, function* () {
yield this.exec({ args: ["add", ...files] });
Expand Down Expand Up @@ -33319,6 +33358,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __nccwpck_require__(2186);
const github = __nccwpck_require__(5438);
const fs_1 = __nccwpck_require__(7147);
const promises_1 = __nccwpck_require__(3292);
const GitClient_1 = __nccwpck_require__(9367);
const VersionManager_1 = __nccwpck_require__(1526);
Expand Down Expand Up @@ -33347,11 +33387,23 @@ function run() {
const gitPush = core.getInput("git-push") === "true";
const gitPushRemote = core.getInput("git-push-remote") || "origin";
const gitPushBranch = core.getInput("git-push-branch") || undefined;
console.log(`Using git: ${gitPath}`);
const commits = github.context.payload.commits.map((commit) => ({
message: commit.message,
id: commit.id,
}));
const metadataFile = core.getInput("metadata-file");
core.info(`Metadata file: ${metadataFile}`);
let metadataFileJSON;
if (!(0, fs_1.existsSync)(metadataFile) || !createCommit) {
if (createCommit) {
core.info("Metadata file not found, will be created after the first run.");
}
metadataFileJSON = {
lastReadCommit: github.context.payload.before,
};
}
else {
metadataFileJSON = JSON.parse(yield (0, promises_1.readFile)(metadataFile, "utf-8"));
}
const gitClient = __addDisposableResource(env_1, new GitClient_1.default(gitPath), true);
const versionManager = new VersionManager_1.default();
const commits = yield gitClient.getCommits(metadataFileJSON.lastReadCommit, github.context.payload.after);
if (commits.length === 0) {
core.info("No new commits found.");
return;
Expand All @@ -33360,8 +33412,6 @@ function run() {
for (const commit of commits) {
core.info(`- ${commit.id}: ${commit.message}`);
}
const gitClient = __addDisposableResource(env_1, new GitClient_1.default("/usr/bin/git"), true);
const versionManager = new VersionManager_1.default();
if (allowedCommitTypes.length > 0) {
versionManager.setAllowedCommitTypes(allowedCommitTypes);
}
Expand Down Expand Up @@ -33398,6 +33448,7 @@ function run() {
gpgKey: gitGPPKey || undefined,
});
if (createCommit) {
yield gitClient.add(metadataFileJSON.lastReadCommit);
yield gitClient.add(versionJsonFile);
yield gitClient.commit(commitMessageFormat.replaceAll("%s", updatedVersion), gitSignOff);
}
Expand All @@ -33416,6 +33467,7 @@ function run() {
];
yield gitClient.push(...pushArgs.filter(Boolean));
}
yield (0, promises_1.writeFile)(metadataFile, JSON.stringify(metadataFileJSON, null, jsonTabWidth) + "\n");
}
catch (e_1) {
env_1.error = e_1;
Expand Down
55 changes: 55 additions & 0 deletions src/GitClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { exec, getExecOutput } from "@actions/exec";
import * as crypto from "crypto";

type SetupOptions = {
name: string;
Expand All @@ -11,6 +12,11 @@ type ExecOptions = {
exitCodeCheck?: boolean;
};

export type Commit = {
message: string;
id: string;
};

class GitClient implements AsyncDisposable {
private readonly gitPath: string;
private readonly oldGitOptions: {
Expand Down Expand Up @@ -45,6 +51,55 @@ class GitClient implements AsyncDisposable {
return stdout;
}

public async getCommits(start?: string, end?: string) {
const boundary = crypto.randomBytes(64).toString("hex");
const output = (
await this.execWithOutput({
args: [
"log",
"--no-decorate",
"--no-color",
`--pretty=format:%H %B\n${boundary}`,
start && `${start}${end ? `..${end}` : ""}`,
].filter(Boolean) as string[],
})
).trim();
const commits: Commit[] = [];

for (let i = 0; i < output.length; i++) {
let sha = "";

while (output[i] !== " " && i < output.length) {
sha += output[i];
i++;
}

i++;

let message = "";

while (i < output.length) {
if (
output[i] === "\n" &&
output.slice(i + 1, i + boundary.length + 1) === boundary
) {
i += boundary.length;
break;
}

message += output[i];
i++;
}

commits.push({
id: sha.trim(),
message: message.trim(),
});
}

return commits;
}

public async add(...files: string[]) {
await this.exec({ args: ["add", ...files] });
}
Expand Down
6 changes: 1 addition & 5 deletions src/VersionManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import * as semver from "semver";

export type Commit = {
message: string;
id: string;
};
import type { Commit } from "./GitClient";

class VersionManager {
private allowedCommitTypes = [
Expand Down
44 changes: 34 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { existsSync } from "fs";
import { readFile, writeFile } from "fs/promises";
import GitClient from "./GitClient";
import VersionManager, { Commit } from "./VersionManager";
import VersionManager from "./VersionManager";

type VersionManagerModule = {
resolver?: (versionManager: VersionManager) => string | Promise<string>;
Expand Down Expand Up @@ -35,14 +36,34 @@ async function run() {
const gitPush = core.getInput("git-push") === "true";
const gitPushRemote = core.getInput("git-push-remote") || "origin";
const gitPushBranch = core.getInput("git-push-branch") || undefined;
const metadataFile = core.getInput("metadata-file");

console.log(`Using git: ${gitPath}`);
core.info(`Metadata file: ${metadataFile}`);

const commits: Commit[] = github.context.payload.commits.map(
(commit: Commit) => ({
message: commit.message,
id: commit.id,
}),
let metadataFileJSON: {
lastReadCommit: string;
};

if (!existsSync(metadataFile) || !createCommit) {
if (createCommit) {
core.info(
"Metadata file not found, will be created after the first run.",
);
}

metadataFileJSON = {
lastReadCommit: github.context.payload.before,
};
} else {
metadataFileJSON = JSON.parse(await readFile(metadataFile, "utf-8"));
}

await using gitClient = new GitClient(gitPath);
const versionManager = new VersionManager();

const commits = await gitClient.getCommits(
metadataFileJSON.lastReadCommit,
github.context.payload.after,
);

if (commits.length === 0) {
Expand All @@ -56,9 +77,6 @@ async function run() {
core.info(`- ${commit.id}: ${commit.message}`);
}

await using gitClient = new GitClient("/usr/bin/git");
const versionManager = new VersionManager();

if (allowedCommitTypes.length > 0) {
versionManager.setAllowedCommitTypes(allowedCommitTypes);
}
Expand Down Expand Up @@ -122,6 +140,7 @@ async function run() {
});

if (createCommit) {
await gitClient.add(metadataFileJSON.lastReadCommit);
await gitClient.add(versionJsonFile);
await gitClient.commit(
commitMessageFormat.replaceAll("%s", updatedVersion),
Expand All @@ -147,6 +166,11 @@ async function run() {
...(pushArgs.filter(Boolean) as [string, string, string]),
);
}

await writeFile(
metadataFile,
JSON.stringify(metadataFileJSON, null, jsonTabWidth) + "\n",
);
}

run()
Expand Down

0 comments on commit b494f04

Please sign in to comment.