Skip to content

Commit

Permalink
feat: add an organisation or owner check to run the action
Browse files Browse the repository at this point in the history
- [Forks problem: add an organisation or owner check to run the action · Issue #2 · Comfy-Org/publish-node-action]( Comfy-Org/publish-node-action#2 (comment) )
  • Loading branch information
snomiao committed Dec 31, 2024
1 parent 12867ff commit bdb4669
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
FORK_OWNER="ComfyNodePRs"
FORK_PREFIX="PR-"

DASHBOARD_ISSUE_URL="https://github.com/drip-art/Comfy-Registry-PR/issues/1"
DASHBOARD_ISSUE_URL="https://github.com/Comfy-Org/Comfy-PR/issues/1"
DASHBOARD_ISSUE_USER__IS='snomiao'

GIT_USERNAME=snomiao
Expand Down
5 changes: 0 additions & 5 deletions src/GIT_USEREMAIL.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/GIT_USERNAME.ts

This file was deleted.

11 changes: 3 additions & 8 deletions src/clone_modify_push_Branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import { makePublishcrBranch } from "./makePublishBranch";
import { makePyprojectBranch } from "./makeTomlBranch";

export async function clone_modify_push_Branches(upstreamUrl: string, forkUrl: string) {
return (
await Promise.all([
makePyprojectBranch(upstreamUrl, forkUrl),
makePublishcrBranch(upstreamUrl, forkUrl),
// makeLicenseUpdateBranch(upstreamUrl, forkUrl),
])
)
.flatMap((e) => (e ? [e] : []))
const pyprojectBranchInfo = makePyprojectBranch(upstreamUrl, forkUrl);
const publishcrBranchInfo = makePublishcrBranch(upstreamUrl, forkUrl);
return (await Promise.all([pyprojectBranchInfo, publishcrBranchInfo]))
.map(({ body, branch, title, type }) => ({
body,
branch,
Expand Down
23 changes: 7 additions & 16 deletions src/createComfyRegistryPullRequests.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import pMap from "p-map";
// import { chalk } from "zx";
import { clone_modify_push_Branches } from "./clone_modify_push_Branches";
import { createGithubForkForRepo } from "./createGithubForkForRepo";
import { createGithubPullRequest } from "./createGithubPullRequest";
import type { GithubPull } from "./gh/GithubPull";
import { makeUpdateTomlLicenseBranch } from "./makeUpdateTomlLicenseBranch";
import { parsePulls } from "./parsePullsState";

if (import.meta.main) {
// test repo
const test_repo = "https://github.com/snomiao/ComfyNode-Registry-test";
console.info(await createComfyRegistryPullRequests(test_repo));
}
export async function createComfyRegistryPullRequests(upstreamRepoUrl: string) {
console.log("forking " + upstreamRepoUrl);
const forkedRepo = await createGithubForkForRepo(upstreamRepoUrl);

console.log("modifing " + forkedRepo.html_url);
const PR_REQUESTS = await clone_modify_push_Branches(upstreamRepoUrl, forkedRepo.html_url);
const prs = await pMap(PR_REQUESTS, async ({ type, ...prInfo }) => await createGithubPullRequest({ ...prInfo }));

console.log("Registry PRs DONE");

return ([...prs] as GithubPull[]).map((e) => parsePulls([e])[0]);
}

export async function clone_modify_push_Branches_for_updateTomlLicense(upstreamUrl: string, forkUrl: string) {
return (await Promise.all([makeUpdateTomlLicenseBranch(upstreamUrl, forkUrl)]))
.flatMap((e) => (e ? [e] : []))
.map(({ body, branch, title, type }) => ({
body,
branch,
title,
type,
srcUrl: forkUrl,
dstUrl: upstreamUrl,
}));
}
19 changes: 17 additions & 2 deletions src/ghUser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import type { Task } from "@/packages/mongodb-pipeline-ts/Task";
import DIE from "@snomiao/die";
import { gh } from "./gh";
import type { AwaitedReturnType } from "./types/AwaitedReturnType";

export const ghUser = (await gh.users.getAuthenticated()).data;
export const ghUser = (
await gh.users.getAuthenticated().catch((error) => {
throw new Error(
`FAIL TO GET AUTHENTICATED USER INFO, CHECK ${!!process.env.GH_TOKEN ? "[?]" : "[ ]"}GH_TOKEN and ${!!process.env.GH_TOKEN_COMFY_PR ? "[?]" : "[ ]"}GH_TOKEN_COMFY_PR`,
{ cause: error },
);
})
).data;

console.log("Fetch Current Github User...");
console.log(`Current Github User: ${ghUser.login} <${ghUser.email}>`);
console.log(`GH_TOKEN User: ${ghUser.login} <${ghUser.email}>`);
export type GHUser = Task<AwaitedReturnType<typeof gh.users.getByUsername>["data"]>;
export const GIT_USEREMAIL =
process.env.GIT_USEREMAIL || (ghUser.email && ghUser.email) || DIE("Missing env.GIT_USEREMAIL");
export const GIT_USERNAME =
process.env.GIT_USERNAME || (ghUser.email && ghUser.name) || DIE("Missing env.GIT_USERNAME");

// read env/parameters
console.log(`GIT COMMIT USER: ${GIT_USERNAME} <${GIT_USEREMAIL}>`);
13 changes: 7 additions & 6 deletions src/makePublishBranch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { readFile } from "fs/promises";
import { dirname } from "path";
import { GIT_USEREMAIL } from "./GIT_USEREMAIL";
import { GIT_USERNAME } from "./GIT_USERNAME";
import { $ } from "./cli/echoBunShell";
import { getBranchWorkingDir } from "./getBranchWorkingDir";
import { gh } from "./gh";
import { GIT_USEREMAIL, GIT_USERNAME } from "./ghUser";
import { parseUrlRepoOwner, stringifyGithubOrigin, stringifyGithubRepoUrl } from "./parseOwnerRepo";
import { parseTitleBodyOfMarkdown } from "./parseTitleBodyOfMarkdown";

Expand All @@ -31,16 +30,18 @@ export async function makePublishcrBranch(upstreamUrl: string, forkUrl: Readonly
}

const cwd = await getBranchWorkingDir(upstreamUrl, forkUrl, branch);

const upsreamOwner = parseUrlRepoOwner(upstreamUrl).owner;
const file = `${cwd}/.github/workflows/publish.yml`;
const publishYmlPath = "./templates/publish.yaml";

const publishYmlTemplate = await readFile(publishYmlPath, "utf8");
const repalcedContent = publishYmlTemplate.replace("NODE_AUTHOR_OWNER", upsreamOwner);
if (publishYmlTemplate === repalcedContent) throw new Error("fail to replace NODE_AUTHOR_OWNER");
// commit & push changes
await $`
git clone ${upstreamUrl} ${cwd}
mkdir -p ${dirname(file)}
cat ${publishYmlPath} > ${file}
echo "${repalcedContent}" > ${file}
cd ${cwd}
Expand All @@ -50,7 +51,7 @@ git checkout -b ${branch} && \
git add . && \
git commit -am "chore(${branch}): ${title}" && \
git push "${origin}" ${branch}:${branch}
`;
`;

const branchUrl = `${stringifyGithubRepoUrl(repo)}/tree/${branch}`;
console.log(`Branch Push OK: ${branchUrl}`);
Expand Down
3 changes: 1 addition & 2 deletions src/makeTomlBranch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { readFile } from "fs/promises";
import { GIT_USEREMAIL } from "./GIT_USEREMAIL";
import { GIT_USERNAME } from "./GIT_USERNAME";
import { $ } from "./cli/echoBunShell";
import { getBranchWorkingDir } from "./getBranchWorkingDir";
import { gh } from "./gh";
import { GIT_USEREMAIL, GIT_USERNAME } from "./ghUser";
import { parseUrlRepoOwner, stringifyGithubOrigin } from "./parseOwnerRepo";
import { parseTitleBodyOfMarkdown } from "./parseTitleBodyOfMarkdown";
import { tomlFillDescription } from "./tomlFillDescription";
Expand Down
5 changes: 2 additions & 3 deletions src/makeUpdateTomlLicenseBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import { readFile } from "fs/promises";
import type { WithId } from "mongodb";
import { basename, dirname } from "path";
import sflow, { nil } from "sflow";
import { CNRepos } from "./CNRepos";
import { GIT_USEREMAIL } from "./GIT_USEREMAIL";
import { GIT_USERNAME } from "./GIT_USERNAME";
import { isRepoBypassed } from "./bypassRepos";
import { $ } from "./cli/echoBunShell";
import { CNRepos } from "./CNRepos";
import { createGithubForkForRepo } from "./createGithubForkForRepo";
import { createGithubPullRequest } from "./createGithubPullRequest";
import { $filaten, $stale, db } from "./db";
import { getBranchWorkingDir } from "./getBranchWorkingDir";
import { gh } from "./gh";
import type { GithubPull } from "./gh/GithubPull";
import { GIT_USEREMAIL, GIT_USERNAME } from "./ghUser";
import { parseUrlRepoOwner, stringifyGithubOrigin } from "./parseOwnerRepo";
import { parseTitleBodyOfMarkdown } from "./parseTitleBodyOfMarkdown";

Expand Down
26 changes: 26 additions & 0 deletions templates/outdated/publish-v2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Publish to Comfy registry
on:
workflow_dispatch:
push:
branches:
- main
- master
paths:
- "pyproject.toml"

jobs:
publish-node:
name: Publish Custom Node to registry
runs-on: ubuntu-latest
# if this is a forked repository. Skipping the workflow.
if: github.event.repository.fork == false
steps:
- name: Check out code
uses: actions/checkout@v4
with:
submodules: true
- name: Publish Custom Node
uses: Comfy-Org/publish-node-action@main
with:
## Add your own personal access token to your Github Repository secrets and reference it here.
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
3 changes: 1 addition & 2 deletions templates/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ jobs:
publish-node:
name: Publish Custom Node to registry
runs-on: ubuntu-latest
# if this is a forked repository. Skipping the workflow.
if: github.event.repository.fork == false
if: ${{ github.repository_owner == 'NODE_AUTHOR_OWNER' }}
steps:
- name: Check out code
uses: actions/checkout@v4
Expand Down

0 comments on commit bdb4669

Please sign in to comment.