Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
23 changes: 23 additions & 0 deletions .github/workflows/delete.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Testing Delete Action

on:
push:
branches: mk/tags

jobs:
delete:
name: Delete Testing
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
registry-url: https://registry.npmjs.org
- uses: ./synchronize-npm-tags
with:
PRESERVE: doo moo poo
env:
NODE_AUTH_TOKEN: ${{ secrets.UH_OH }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"license": "MIT",
"workspaces": {
"packages": [
"publish-pr-preview"
"utils",
"publish-pr-preview",
"synchronize-npm-tags"
]
},
"scripts": {
Expand All @@ -15,6 +17,10 @@
"problems": "yarn workspaces run problems",
"test": "yarn workspaces run test"
},
"devDependencies": {
"@frontside/eslint-config": "^2.1.0",
"@frontside/tsconfig": "^1.2.0"
},
"volta": {
"node": "12.22.7",
"yarn": "1.22.11"
Expand Down
2 changes: 0 additions & 2 deletions publish-pr-preview/.eslintignore

This file was deleted.

1 change: 1 addition & 0 deletions publish-pr-preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "fs";
import { main } from "effection";
import { run } from "./src";

// TODO we don't have token input so just do `process... || ""`
const token =
core.getInput("token") === ""
? process.env.GITHUB_TOKEN || ""
Expand Down
18 changes: 7 additions & 11 deletions publish-pr-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,32 @@
"license": "MIT",
"main": "dist/index.js",
"scripts": {
"lint": "eslint '**/*.ts'",
"lint": "eslint '{src,text}**/*.ts'",
"build": "ncc build index.ts -m",
"problems": "tsc --noEmit",
"test": "mocha -r node_modules/ts-node/register test/**.test.ts"
},
"volta": {
"node": "12.18.4",
"yarn": "1.22.11"
},
"dependencies": {
"@actions/core": "^1.6.0",
"@actions/github": "^5.0.0",
"@effection/process": "2.0.1",
"@frontside/actions-utils": "*",
"effection": "2.0.1",
"glob": "^7.2.0",
"markdown-table": "^3.0.1",
"semver": "^7.3.5"
},
"devDependencies": {
"@effection/mocha": "^2.0.1",
"@frontside/eslint-config": "^2.1.0",
"@frontside/tsconfig": "^1.2.0",
"@types/glob": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.2",
"@types/semver": "^7.3.9",
"@vercel/ncc": "^0.31.1",
"expect": "^27.3.1",
"mocha": "^9.1.3",
"mocha": "8.4.0",
"ts-node": "^10.4.0",
"typescript": "^4.4.3"
"typescript": "4.3.5"
},
"volta": {
"extends": "../package.json"
}
}
13 changes: 4 additions & 9 deletions publish-pr-preview/src/findPackages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Operation } from "effection";
import glob from "glob";
import colors from "./ansiColors";
import { colors, listAllPkgJsons } from "@frontside/actions-utils";

export function* findPackages(gitDiff: string[]): Operation<Iterable<string>> {
let directories = [...new Set(gitDiff.map(output => {
Expand All @@ -16,11 +15,6 @@ export function* findPackages(gitDiff: string[]): Operation<Iterable<string>> {
console.log(colors.blue(" "+dir));
});

let listAllPkgJsonsWithin = (directory: string) => glob.sync("**/package.json", {
cwd: directory,
ignore: ["node_modules/**"],
});

let depthOfPath = (directory: string): number => {
let matched = directory.match(/\//g);
return matched ? matched.length : 0;
Expand All @@ -32,20 +26,21 @@ export function* findPackages(gitDiff: string[]): Operation<Iterable<string>> {

let findRelativePkgJsonPaths = ({ acc, directory }: { acc: string[], directory: string }) => {
for (let i = directory; i != "."; i = superDirectory(i)) {
let pkgJsons = listAllPkgJsonsWithin(i);
let pkgJsons = listAllPkgJsons(i);
if (pkgJsons.length === 1 && pkgJsons[0] === "package.json") {
return [...acc, i];
}
}

let pkgJsonsAtRoot = listAllPkgJsonsWithin(".");
let pkgJsonsAtRoot = listAllPkgJsons();
if (pkgJsonsAtRoot.length === 1 && pkgJsonsAtRoot[0] === "package.json") {
return [...acc, "."];
}

return acc;
};

// TODO remove unnecessary space
let directoriesToPublish = [... new Set(directories.reduce((acc, directory) => findRelativePkgJsonPaths({ acc, directory }), [] as string[]))];

console.log("\n"+colors.yellow("Directories with package.json located:"));
Expand Down
1 change: 1 addition & 0 deletions publish-pr-preview/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function* run({ octokit, core, payload }: PreviewRun): Operation<void> {
if (!isValid) {
core.setFailed(reason);
} else {
// TODO: findPackages is synchronous - doesn't need to yield
let directoriesToPublish: string[] = yield findPackages(gitDiff);
let installScript = core.getInput("INSTALL_SCRIPT") || "";
let published: PublishResults = yield publish({ directoriesToPublish, installScript, branch });
Expand Down
2 changes: 1 addition & 1 deletion publish-pr-preview/src/postGithubComment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GitHub } from "@actions/github/lib/utils";
import { PullRequestPayload } from ".";
import { Operation } from "effection";
import colors from "./ansiColors";
import { colors } from "@frontside/actions-utils";

export function* postGithubComment({
comment,
Expand Down
3 changes: 2 additions & 1 deletion publish-pr-preview/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exec, Process, ProcessResult } from "@effection/process";
import { all, Operation } from "effection";
import fs from "fs";
import semver from "semver";
import colors from "./ansiColors";
import { colors } from "@frontside/actions-utils";

interface PublishRun {
directoriesToPublish: string[];
Expand All @@ -29,6 +29,7 @@ export function* publish({ directoriesToPublish, installScript, branch }: Publis
"\n"+
colors.yellow("Installing with command"),
colors.blue(installCommand),
// TODO should be +colors.yellow("...") to remove unnecessary space
colors.yellow("...")
);
yield exec(installCommand).join();
Expand Down
3 changes: 1 addition & 2 deletions publish-pr-preview/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"extends": "@frontside/tsconfig",
"exclude": ["node_modules"]
"extends": "../tsconfig.json"
}
7 changes: 0 additions & 7 deletions synchronize-npm-tags/Dockerfile

This file was deleted.

37 changes: 26 additions & 11 deletions synchronize-npm-tags/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
# Synchronize NPM Tags
`synchronize-npm-tags` is part of our Transparent Publishing workflow and is meant to be triggered `on: delete`. This action will cross-reference the NPM dist-tags with branch names and remove the tags that we no longer need (with the exception of `latest` and any other tag labels that are passed in as the `preserve` argument).

For instance, let's say we have a pull request that published a package with the tag of `foo-bar`. After we merge the pull request and delete the branch, this action will be triggered and see that "foo-bar" exists as a tag but there's no corresponding branch so it will remove that tag from the registry.
The `synchronize-npm-tags` action was created to be used in tandem with our [`publish-pr-preview`](../publish-pr-preview) action which publishes preview packages with NPM tags that are generated from branch names. Unless you remove stale tags manually, those NPM tags will keep accumulating. To address this inconvenience, the `synchronize-npm-tags` action will remove NPM tags that do not correspond to any of the git branches of your repository.

## Requirements
- Pass in `NPM_TOKEN`.
- Optional: You can specify any NPM tags you'd like to `preserve`.
By default the action will not remove common tags such as `dev`, `beta`, `alpha`, and `latest`. You can also specify your own list of tags that you wish to preserve. See below for details.

## Usage

```yaml
jobs:
job_name:
name: Job Name
synchronize-tags:
name: Synchronize NPM Tags
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: thefrontside/actions/synchronize-npm-tags@master
- uses: actions/checkout@v2
with:
fetch-depth: 0
## https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches
- uses: actions/setup-node@v2
with:
preserve: dev beta alpha
registry-url: https://registry.npmjs.org
## https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm
- uses: thefrontside/synchronize-npm-tags@main
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

### Specifying NPM Tags to Preserve

```yaml
- uses: thefrontside/synchronize-npm-tags@main
with:
PRESERVE: tag1 tag2 tag3
env:
NODE_AUTH_TOKEN: ...
GITHUB_TOKEN: ...
```
11 changes: 11 additions & 0 deletions synchronize-npm-tags/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Synchronize NPM Tags
description: Remove stale tags from NPM

inputs:
PRESERVE:
description: "Specify NPM tags to preserve"
required: false

runs:
using: "node12"
main: "dist/index.js"
1 change: 1 addition & 0 deletions synchronize-npm-tags/dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
7 changes: 7 additions & 0 deletions synchronize-npm-tags/dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions synchronize-npm-tags/dist/src/findPublicPackages.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function findPublicPackages(): string[];
3 changes: 3 additions & 0 deletions synchronize-npm-tags/dist/src/getGitBranches.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Operation } from "effection";
import { ActionPayload } from ".";
export declare function getGitBranches({ octokit, payload }: Omit<ActionPayload, "preserve">): Operation<void>;
5 changes: 5 additions & 0 deletions synchronize-npm-tags/dist/src/getTagsForEachPackage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Operation } from "effection";
import { PackageTags } from ".";
export declare function getTagsForEachPackage({ publicPackages }: {
publicPackages: string[];
}): Operation<PackageTags[]>;
22 changes: 22 additions & 0 deletions synchronize-npm-tags/dist/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { WebhookPayload as DefaultPayload } from "@actions/github/lib/interfaces";
import { GitHub } from "@actions/github/lib/utils";
import { Operation } from "effection";
interface WebhookPayload extends DefaultPayload {
repository: DefaultPayload["repository"] & {
name: string;
owner: {
login: string;
};
};
}
export interface ActionPayload {
octokit: InstanceType<typeof GitHub>;
payload: WebhookPayload;
preserve: string[];
}
export interface PackageTags {
name: string;
tags: string[];
}
export declare function run({ octokit, payload, preserve }: ActionPayload): Operation<void>;
export {};
1 change: 1 addition & 0 deletions synchronize-npm-tags/dist/src/logIterable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function logIterable(description: string, iterable: string[], emptyDescription?: string): void;
7 changes: 7 additions & 0 deletions synchronize-npm-tags/dist/src/removeTags.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Operation } from "effection";
import { PackageTags } from ".";
export declare function removeTags({ allPackageTags, gitBranches, preserve, }: {
allPackageTags: PackageTags[];
gitBranches: string[];
preserve: string[];
}): Operation<void>;
30 changes: 0 additions & 30 deletions synchronize-npm-tags/entrypoint.sh

This file was deleted.

19 changes: 19 additions & 0 deletions synchronize-npm-tags/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import fs from "fs";
import { main } from "effection";
import { run } from "./src";

const token = process.env.GITHUB_TOKEN || "";
const octokit = github.getOctokit(token);

const payload = JSON.parse(fs.readFileSync(`${process.env.GITHUB_EVENT_PATH}`, "utf-8"));

const defaultProtectedTags = ["alpha", "beta", "latest", "dev"];
const preserve = core.getInput("PRESERVE")
? core.getInput("PRESERVE").split(" ").concat(...defaultProtectedTags)
: defaultProtectedTags;

main(
run({ octokit, payload, preserve })
);
35 changes: 35 additions & 0 deletions synchronize-npm-tags/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "synchronize-npm-tags",
"version": "0.0.0",
"private": true,
"author": "Frontside Engineering <engineering@frontside.com>",
"license": "MIT",
"main": "dist/index.js",
"scripts": {
"lint": "eslint '{src,test}**/*.ts'",
"build": "ncc build index.ts -m",
"problems": "tsc --noEmit",
"test": "mocha -r node_modules/ts-node/register test/**.test.ts"
},
"dependencies": {
"@actions/core": "^1.6.0",
"@actions/github": "^5.0.0",
"@effection/process": "2.0.1",
"@frontside/actions-utils": "*",
"effection": "2.0.1"
},
"devDependencies": {
"@effection/mocha": "^2.0.1",
"@octokit/types": "^6.34.0",
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.2",
"@vercel/ncc": "^0.31.1",
"expect": "^27.3.1",
"mocha": "8.4.0",
"ts-node": "^10.4.0",
"typescript": "4.3.5"
},
"volta": {
"extends": "../package.json"
}
}
Loading