Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add open and default placeholder #324

Merged
merged 2 commits into from
Dec 30, 2023
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gitarist",
"version": "1.2.9",
"version": "1.2.10-0",
"description": "A CLI tool to utilize Octokit",
"license": "MIT",
"publishConfig": {
Expand Down Expand Up @@ -73,6 +73,7 @@
"fast-glob": "^3.3.2",
"lodash": "^4.17.21",
"octokit": "^3.1.2",
"open": "^10.0.2",
"parse-git-config": "^3.0.0",
"zod": "^3.22.4"
},
Expand Down
48 changes: 45 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ program
DEFAULT.remote,
),
)
.action((options: SetupCommandOptions) => {
Gitarist.setup({ remote: options.remote });
.action(async (options: SetupCommandOptions) => {
await Gitarist.setup({ remote: options.remote });
});

program
Expand Down
18 changes: 15 additions & 3 deletions src/gitarist.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { execSync } from 'child_process';
import dotenv from 'dotenv';
import { mkdirSync, rmdirSync } from 'fs';
import { mkdir, writeFile } from 'fs/promises';
import { Octokit } from 'octokit';
import { dirname } from 'path';
import { dirname, join } from 'path';
import { chdir } from 'process';
import { Gitarist, IssueItem } from './gitarist';

jest.setTimeout(999999999);
Expand All @@ -24,9 +26,19 @@ describe('gitarist', () => {
});

it.skip('setup', async () => {
Gitarist.setup({});

// FIXME: `open` is not working on commonjs
const targetDir = join(
process.cwd(),
'.gitarist',
`directory_${Date.now()}`,
);
mkdirSync(targetDir, { recursive: true });
chdir(targetDir);
execSync('git init');
execSync('cd');
await Gitarist.setup({});
expect(true).toBeTruthy();
rmdirSync(targetDir);
});

it.skip('simulate active user', async () => {
Expand Down
62 changes: 44 additions & 18 deletions src/gitarist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const DEFAULT = {
cron: '0 */6 * * 0-6',
stale: 2, // days
language: 'GO',
ownerPlaceholder: '<OWNER>',
repoPlaceholder: '<REPOSITORY>',
} as const;

enum MODE {
Expand Down Expand Up @@ -254,20 +256,25 @@ GITARIST_TOKEN="${token}"
/**
* Setup a repository for gitarist which is creating a workflow file.
*/
static setup({ remote = DEFAULT.remote }: Partial<SetupCommandOptions>) {
static async setup({
remote = DEFAULT.remote,
}: Partial<SetupCommandOptions>) {
const gitConfigPath = path.join(process.cwd(), '.git', 'config');
if (!existsSync(gitConfigPath)) {
throw new Error(
`Could not find git config file from this directory, ${gitConfigPath}. This is not a git repository. Maybe you should run "git init" first.`,
);
}

const envPath = path.join(process.cwd(), '.env');
console.log(`Searching .env file at ${envPath}`);
const dotenvFile = '.env';
const envPath = path.join(process.cwd(), dotenvFile);
console.log(`Searching ${dotenvFile} file from ${envPath} ...`);
if (existsSync(envPath)) {
console.log(
'There is a .env file already so template will be appended to the file.',
`\tThere is a ${dotenvFile} file already so template will be appended to the file.`,
);
} else {
console.log(`\tCould not find a ${dotenvFile} file`);
}

const ymlPath = path.join(
Expand All @@ -276,21 +283,23 @@ GITARIST_TOKEN="${token}"
'workflows',
'gitarist.yml',
);
console.log(`Searching gitarist workflow file at ${envPath}.`);
console.log(`Searching gitarist workflow file from ${envPath} ...`);
if (existsSync(ymlPath)) {
console.log(`There is a gitarist workflow file already at ${ymlPath}.`);
console.log(`\tThere is a gitarist workflow file already at ${ymlPath}.`);
} else {
console.log(`\tCould not find a workflow file`);
}

const git = parseGitConfig.sync(
readFileSync(path.join(process.cwd(), '.git', 'config'), 'utf8'),
);
const owner = git?.user?.name ?? '';
const owner = git?.user?.name ?? DEFAULT.ownerPlaceholder;
const repo =
git[`remote "${remote}"`]?.url
?.split('/')
?.pop()
?.replace('.git', '')
.replace('/', '') ?? '';
.replace('/', '') ?? DEFAULT.repoPlaceholder;
const githubWorkflowDirectory = path.join(
process.cwd(),
'.github',
Expand All @@ -300,10 +309,7 @@ GITARIST_TOKEN="${token}"
writeFileSync(
path.join(githubWorkflowDirectory, 'gitarist.yml'),
Gitarist.getActionTemplate({ owner, repo }),
{
encoding: 'utf8',
flag: 'a+',
},
{ encoding: 'utf8', flag: 'a+' },
);
writeFileSync(
path.join(process.cwd(), '.env'),
Expand All @@ -312,13 +318,33 @@ GITARIST_TOKEN="${token}"
);

console.log(
[
'Generate a secret key settings:',
Gitarist.tokenIssueUrl,
'Register the secret key to action settings:',
Gitarist.tokenSettingUrl({ owner, repo }),
].join('\n'),
['Generate a secret key settings:', Gitarist.tokenIssueUrl].join('\n'),
);
const open = await import('open');
await open.default(Gitarist.tokenIssueUrl, { wait: false });

console.log(
`Register the secret key to action settings: ${Gitarist.tokenSettingUrl({
owner,
repo,
})}`,
);

console.log(
'Go to repository > settings > Secrets and variables > Actions > New repository secret',
);

if (
owner !== DEFAULT.ownerPlaceholder &&
repo !== DEFAULT.repoPlaceholder
) {
console.log(
[
'Register the secret key to action settings:',
Gitarist.tokenSettingUrl({ owner, repo }),
].join('\n'),
);
}
}

/**
Expand Down