Skip to content

Commit

Permalink
chore: detect workflow cleanup (#15)
Browse files Browse the repository at this point in the history
* workflow cleanup

* backdate packages

* rename workflow job

* change detect job

* change detect workflow triggers
  • Loading branch information
willswire committed Sep 19, 2024
1 parent 50bf76d commit c93a994
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "Cosmic Dependency Submission Action"
name: "Cosmic Dependency Detection Action"
description: "Action using the dependency-submission-toolkit"
inputs:
token:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, test } from "vitest";
import { parseCosmicPackages, createPackageManifests } from "./cosmic-detector";
import { PackageURL } from "packageurl-js";

// Define a helper function to simulate a CosmicPackage
// Helper function to simulate a CosmicPackage
const createCosmicPackage = () => ({
name: "example-package",
url: "https://example.com",
Expand All @@ -19,10 +19,8 @@ describe("parseCosmicPackages", () => {
test("parses single package", () => {
const stdout = JSON.stringify(createCosmicPackage());
const result = parseCosmicPackages(stdout);
if (result[0]) {
expect(result).toHaveLength(1);
expect(result[0].name).toBe("example-package");
}
expect(result).toHaveLength(1);
expect(result[0]?.name).toBe("example-package");
});

test("parses multiple packages", () => {
Expand All @@ -39,6 +37,16 @@ describe("parseCosmicPackages", () => {
const result = parseCosmicPackages(stdout);
expect(result).toHaveLength(0);
});

test("handles mixed valid and invalid JSON", () => {
const stdout = [
JSON.stringify(createCosmicPackage()),
"invalid-json",
JSON.stringify(createCosmicPackage()),
].join("---");
const result = parseCosmicPackages(stdout);
expect(result).toHaveLength(2);
});
});

describe("createPackageManifests", () => {
Expand All @@ -51,19 +59,22 @@ describe("createPackageManifests", () => {
const manifest = manifests[0];

if (manifest) {
expect(manifest.name).toBe(cosmicPackage.name);
expect(manifest?.name).toBe(cosmicPackage.name);

const manifestDependencies = manifest.directDependencies();
expect(manifestDependencies).toHaveLength(1);

const purl = PackageURL.fromString(cosmicPackage.purl);
expect(
manifestDependencies
.map((p) => {
return p.packageURL;
})
.join(" "),
).toContain(purl);
manifestDependencies.map((p) => p.packageURL.toString()).join(" "),
).toContain(purl.toString());
}
});

test("handles empty package list", () => {
const cosmicPackages: any[] = [];
const manifests = createPackageManifests(cosmicPackages);

expect(manifests).toHaveLength(0);
});
});
115 changes: 115 additions & 0 deletions .github/workflows/cosmic-detector/cosmic-detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as glob from "@actions/glob";
import {
Manifest,
Package,
Snapshot,
submitSnapshot,
} from "@github/dependency-submission-toolkit";
import { PackageURL } from "packageurl-js";

// Type definition for the top-level structure from the output of 'pkl eval'
type CosmicPackage = {
name: string; // Name of the package
url: string; // URL of the package repository
version: string; // Version of the package
hash: string; // Hash of the package
executablePath: string; // Path to the executable
testArgs: string[]; // Arguments for testing the package
type: string; // Type of the package
purl: string; // Package URL in purl format
};

/**
* Parses the stdout from the 'pkl eval' command to extract package information.
* @param stdout - The stdout from the 'pkl eval' command.
* @returns An array of CosmicPackage objects.
*/
export function parseCosmicPackages(stdout: string): CosmicPackage[] {
const packages: CosmicPackage[] = [];

// Split the stdout by the '---' separator
const packageStrings = stdout
.split("---")
.map((pkgStr) => pkgStr.trim())
.filter((pkgStr) => pkgStr.length > 0);

// Parse each package string into an object
for (const pkgStr of packageStrings) {
try {
const cosmicPackage = JSON.parse(pkgStr) as CosmicPackage;
packages.push(cosmicPackage);
} catch (error) {
core.error(`Error parsing package: ${error}`);
}
}

return packages;
}

/**
* Creates package manifests from an array of CosmicPackage objects.
* @param cosmicPackages - An array of CosmicPackage objects.
* @returns An array of Manifest objects.
*/
export function createPackageManifests(
cosmicPackages: CosmicPackage[],
): Manifest[] {
return cosmicPackages.map((cosmicPackage) => {
const purl = PackageURL.fromString(cosmicPackage.purl);
const pkg = new Package(purl);
const manifest = new Manifest(
cosmicPackage.name,
`Packages/${cosmicPackage.name}.pkl`,
);
manifest.addDirectDependency(pkg);
return manifest;
});
}

/**
* Main function that orchestrates the process of retrieving, parsing, and submitting package information.
*/
export async function main() {
try {
const cosmicPackageDirectory = core.getInput("cosmic-package-directory");

const globber = await glob.create(`${cosmicPackageDirectory}/*.pkl`);
const files = await globber.glob();

// Ensure there are some .pkl files to process.
if (files.length === 0) {
core.setFailed(
`No .pkl files found in directory: ${cosmicPackageDirectory}`,
);
return;
}

const prodPackages = await exec.getExecOutput(
"pkl",
["eval", "-f", "json", ...files],
{ cwd: cosmicPackageDirectory },
);
if (prodPackages.exitCode !== 0) {
core.error(prodPackages.stderr);
core.setFailed("'pkl eval' failed!");
return;
}
const cosmicPackages = parseCosmicPackages(prodPackages.stdout);
const packageManifests = createPackageManifests(cosmicPackages);
const snapshot = new Snapshot({
name: "cosmic-detector",
url: "https://github.com/willswire/cosmic/tree/main/.github/workflows/cosmic-detector",
version: "0.0.1",
});

for (const pm of packageManifests) {
snapshot.addManifest(pm);
}

submitSnapshot(snapshot);
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dependency-submission",
"name": "cosmic-detector",
"private": true,
"description": "[UNPUBLISHED PACKAGE!] Example using the Dependency Submission Toolkit and npm list",
"version": "0.0.1",
"type": "module",
"scripts": {
"all": "npm run type-check && npm run build && npm run test",
Expand Down
96 changes: 0 additions & 96 deletions .github/workflows/dependency-submission/cosmic-detector.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: Submit Dependency Snapshot
name: Detect

on:
workflow_dispatch:
pull_request:
branches: [main]
push:
branches: [main]

env:
PKL_VERSION: 0.26.3
Expand All @@ -13,8 +15,10 @@ permissions:
contents: write

jobs:
submit-dependencies:
submit_dependencies:
name: Submit Dependencies
runs-on: macos-14
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4

Expand All @@ -30,10 +34,10 @@ jobs:
node-version: 20

- name: Install NPM dependencies
run: npm ci --prefix .github/workflows/dependency-submission/
run: npm ci --prefix .github/workflows/cosmic-detector/

- name: Run all NPM build/test actions
run: npm run all --prefix .github/workflows/dependency-submission/
run: npm run all --prefix .github/workflows/cosmic-detector/

- name: Run dependency submission
uses: ./.github/workflows/dependency-submission/
uses: ./.github/workflows/cosmic-detector/
4 changes: 2 additions & 2 deletions Packages/age.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ url = "https://github.com/FiloSottile/age/releases/download/v\(version)/age-v\(v

purl = "pkg:golang/filippo.io/age@\(version)"

version = "1.1.0"
version = "1.0.0"

hash = "f9dbc0726394f509e3d515a0bef5ffc02d8e59a818bfffc0f4acd826405af292"
hash = "42aaf089081d338ba5eba508d31fe81fb68ec692f404cea996fcbb34690ec2f1"

executablePath = "/age/age"

Expand Down
4 changes: 2 additions & 2 deletions Packages/k9s.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ url = "https://github.com/derailed/k9s/releases/download/v\(version)/k9s_Darwin_

purl = "pkg:golang/github.com/derailed/k9s@\(version)"

version = "0.32.5"
version = "0.26.0"

hash = "916a51cc8e0c48811a1b459cfad98201ba9e0b90e5741d8a889bed718169f17d"
hash = "43df569e527141dbfc53d859d7675b71c2cfc597ffa389a20f91297c6701f255"

executablePath = "/k9s"

Expand Down

0 comments on commit c93a994

Please sign in to comment.