Skip to content

Commit

Permalink
Add two sources to support use as actions
Browse files Browse the repository at this point in the history
`pull-action` is used for pull request related events (create,
synchronize, comment).

`misc-action` is used for other scheduled events such as checking
mailing list emails.

Add *.tgz to .gitignore for untracked package.

Signed-off-by: Chris. Webster <[email protected]>
  • Loading branch information
webstech committed Jul 20, 2022
1 parent 357c94e commit f7c8c3f
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/obj/**
/*.sln*
/*.njsproj*
/*.tgz
82 changes: 82 additions & 0 deletions lib/misc-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { CIHelper } from "./ci-helper";
import { isDirectory } from "./fs-util";
import { getConfig } from "./gitgitgadget-config";
import { getVar } from "./gitgitgadget";
import { IConfig, loadConfig, setConfig } from "./project-config";
import path from "path";

export interface actionInterface {
action: string;
repositoryDir: string;
configRepositoryDir: string;
config: string;
repoOwner: string;
repoName: string;
skipUpdate?: string | undefined;
}

/**
* Handle various gitgitgadget requests.
*
* @param parms
*/

export async function handleAction(parms: actionInterface): Promise<void> {
const config: IConfig = parms.config ? setConfig(await getExternalConfig(parms.config)) : getConfig();

// Update with current values
config.repo.name = parms.repoName;
config.repo.owner = parms.repoOwner;
setConfig(config);

lintConfig(config);

if (!(await isDirectory(parms.repositoryDir))) {
throw new Error(`git WorkDir '${parms.repositoryDir}' not found.`);
}

const ci = new CIHelper(parms.repositoryDir, parms.skipUpdate ? true : false, parms.configRepositoryDir);

if (parms.action === "update-open-prs") {
const result = await ci.updateOpenPrs();
console.log(`Updated notes: ${result}`);
} else if (parms.action === "update-commit-mappings") {
const result = await ci.updateCommitMappings();
console.log(`Updated notes: ${result}`);
} else if (parms.action === "handle-open-prs") {
const options = await ci.getGitGitGadgetOptions();
if (!options.openPRs) {
throw new Error("No open PRs?");
}
const result = await ci.handleOpenPRs();
console.log(`Updated notes: ${result}`);
} else if (parms.action === "handle-new-mails") {
const mailArchiveGitDir = await getVar("loreGitDir", undefined);

if (!mailArchiveGitDir) {
throw new Error("Need a lore.kernel/git worktree.");
}

await ci.handleNewMails(mailArchiveGitDir);
} else {
throw new Error(`Unknown action '${parms.action}'.`);
}
}

async function getExternalConfig(file: string): Promise<IConfig> {
return await loadConfig(path.resolve(file));
}

function lintConfig(config: IConfig): void {
if (!config.hasOwnProperty("project")) {
throw new Error(`User configurations must have a 'project:'. Not found in ${path}`);
}

if (!config.repo.owner.match(/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i)) {
throw new Error(`Invalid 'owner' ${config.repo.owner} in ${path}`);
}

if (!config.repo.baseOwner.match(/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i)) {
throw new Error(`Invalid 'baseOwner' ${config.repo.baseOwner} in ${path}`);
}
}
74 changes: 74 additions & 0 deletions lib/pull-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { CIHelper } from "./ci-helper";
import { isDirectory } from "./fs-util";
import { getConfig } from "./gitgitgadget-config";
import { IConfig, loadConfig, setConfig } from "./project-config";
import path from "path";

export interface PRUpdateInterface {
action: string;
repositoryDir: string;
configRepositoryDir: string;
config: string;
repoOwner: string;
repoName: string;
repoBaseowner: string;
pullRequestNumber: string;
commentId?: string | undefined;
skipUpdate?: string | undefined;
}

/**
* Handle an update to a pull request. It may be a create or sync of changes or a comment.
*
* @param parms
*/

export async function handlePRUpdate(parms: PRUpdateInterface): Promise<void> {
const config: IConfig = parms.config ? setConfig(await getExternalConfig(parms.config)) : getConfig();

// Update with current values
config.repo.name = parms.repoName;
config.repo.owner = parms.repoOwner;
config.repo.baseOwner = parms.repoBaseowner;
setConfig(config);

lintConfig(config);

if (!(await isDirectory(parms.repositoryDir))) {
throw new Error(`git WorkDir '${parms.repositoryDir}' not found.`);
}

const ci = new CIHelper(parms.repositoryDir, parms.skipUpdate ? true : false, parms.configRepositoryDir);

if (parms.action === "comment") {
if (parms.commentId) {
const commentId = parseInt(parms.commentId, 10);
await ci.handleComment(parms.repoOwner, commentId);
} else {
throw new Error(`Action '${parms.action}' requires a comment-id.`);
}
} else if (parms.action === "push") {
const pullRequestNumber = parseInt(parms.pullRequestNumber, 10);
await ci.handlePush(parms.repoOwner, pullRequestNumber);
} else {
throw new Error(`Unknown action '${parms.action}'.`);
}
}

async function getExternalConfig(file: string): Promise<IConfig> {
return await loadConfig(path.resolve(file));
}

function lintConfig(config: IConfig): void {
if (!config.hasOwnProperty("project")) {
throw new Error(`User configurations must have a 'project:'. Not found in ${path}`);
}

if (!config.repo.owner.match(/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i)) {
throw new Error(`Invalid 'owner' ${config.repo.owner} in ${path}`);
}

if (!config.repo.baseOwner.match(/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i)) {
throw new Error(`Invalid 'baseOwner' ${config.repo.baseOwner} in ${path}`);
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"url": "https://github.com/gitgitgadget/gitgitgadget/issues"
},
"homepage": "https://gitgitgadget.github.io/",
"files": [
"lib/*.ts"
],
"jest": {
"moduleFileExtensions": [
"ts",
Expand Down

0 comments on commit f7c8c3f

Please sign in to comment.