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

New version patch #7

Merged
merged 3 commits into from
Apr 22, 2024
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
14 changes: 0 additions & 14 deletions .TODO

This file was deleted.

6 changes: 6 additions & 0 deletions .changeset/thin-snakes-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"quirgo": patch
---

- Program auto exit on invalid github access token option
- Now using user's default username/login as repository owner
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI
on:
push:
branches:
- "**"
- dev

jobs:
build:
Expand Down
25 changes: 25 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Todo

- Replace OctokitResponse<any> by proper typing!
- Consider writing tests (unit tests and whatnot)
- Add warning about gitbub's rate limit
- Changeset to major version when basic cli is active
- Octokit
- Check if user is authenticated
- Add a mini logger lib file
- Handle verbose properly

## From feedback

- [x] use more branches (e.g. `main`, `develop`, `feature/...`, `bugfix/...`), moost of them will be temporary till reviewing process
- [] see if owner can be retrieved from GitHub access token, use default as username
- [] better README
- [] donner plus d'infos pour l'access token, permettre d'obtenir un lien pour le générer avec peut-etre en query params de quoi remplir le formulaire avec le minimum requis pour l'access token
- [] preciser les permissions accordees a quirgo sont les permissions minimales requises
- [] donner la possibilite de stocker le token, voire meme plusieurs
- [] set verbose to false at default in configuration
- [] mutual exclusion of --json && --env
- [] simplify the usage of --verbose (specially when default is false)

-- CLI --
Other: [gulp, jest]
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quirgo",
"version": "0.0.0",
"version": "0.0.1",
"private": false,
"description": "A CLI tool to automate repositories secrets and variables setting via the Gihub API",
"type": "module",
Expand All @@ -12,7 +12,7 @@
},
"scripts": {
"init-cli": "pnpm --global unlink quirgo && pnpm link --global",
"build": "tsup src/**/*.ts --format esm,cjs --dts",
"build": "tsup src/**/*.ts --format esm --dts",
"ci": "pnpm run build && pnpm run lint",
"release": "pnpm run build && pnpm run lint && changeset publish",
"dev": "tsc -w",
Expand Down
41 changes: 35 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { spinner } from "./lib/spinner.js";

console.log(BANNER);

/**
* TODO: handle the case when --env/--json is provided
* TODO: create todolist in README.md
*/

// Declare the program
const program = new Command();

Expand Down Expand Up @@ -251,8 +256,27 @@ program.on("option:verbose", () => {
config.verbose = true;
});

program.on("option:token", (token) => {
if (!repoManager) repoManager = new RepoManager(token);
program.on("option:token", async (token) => {
try {
if (!repoManager) repoManager = new RepoManager(token);

// DOC: note that this will be default if no --owner option is detected
config.repositoryOwner = (await repoManager.getUserLogin()) || "";

if (config.verbose) {
console.log(
chalk.cyan("-> Using"),
chalk.bgBlueBright(config.repositoryOwner),
chalk.cyan("as default repository owner")
);
}
} catch (err: any) {
console.error(
chalk.red("Impossible to authenticate using the GitHub Access Token")
);
console.error(chalk.red(err));
process.exit(-1);
}
});

program.on("option:repo", (repo) => {
Expand All @@ -271,6 +295,11 @@ program.on("option:json", (json) => {
parsedKeyValues = jsonParser(json, { verbose: config.verbose || false });
});

program.action(() => {
program.outputHelp();
process.exit(0);
});

program.parse(process.argv);

/**
Expand Down Expand Up @@ -312,7 +341,7 @@ async function fn() {
}
}

// If no command is provided, show help
if (!process.argv.slice(2).length) {
program.outputHelp();
}
// // If no command is provided, show help
// if (!process.argv.slice(2).length) {
// program.outputHelp();
// }
10 changes: 8 additions & 2 deletions src/lib/repository-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { spinner } from "./spinner.js";
* Faster & shorter
* Tag commit as "refactor"
* TODO: check returnTypes
* TODO: different request methods for variables
*/

export class RepoManager {
Expand All @@ -18,6 +19,11 @@ export class RepoManager {
this.app = new Octokit({ auth: githubAccessToken });
}

public async getUserLogin(): Promise<string | null> {
const user = await this.app.rest.users.getAuthenticated();
return user.data.login;
}

/**
* List variables in the repository for GitHub Actions.
* @param {Configuration} config The configuration object containing repository details.
Expand Down Expand Up @@ -125,13 +131,13 @@ export class RepoManager {
* @param {Configuration} config The configuration object containing repository details.
* @param {string} secretName The name of the variable to create.
* @param {string} secretValue The value of the variable to create.
* @returns {Promise<void | OctokitResponse<any>>} A Promise containing the Octokit response.
* @returns {Promise<OctokitResponse<any>>} A Promise containing the Octokit response.
*/
public async setRepoSecret(
config: Configuration,
secretName: string,
secretValue: string
): Promise<void | OctokitResponse<any>> {
): Promise<OctokitResponse<any>> {
spinner.start(`Setting secret <${secretName}>...\n`);
// get repo public key
const encryptedValue = await this.app.rest.actions.getRepoPublicKey({
Expand Down