-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: branch protection setting with dft branch
- Loading branch information
Showing
8 changed files
with
83 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
repository: | ||
has_wiki: false | ||
has_discussion: true | ||
default_branch: main | ||
|
||
branches: | ||
- name: main | ||
protection: | ||
required_pull_request_reviews: | ||
dismiss_stale_reviews: true | ||
require_code_owner_reviews: true | ||
enforce_admins: true | ||
has_projects: false | ||
delete_branch_on_merge: true | ||
enable_automated_security_fixes: true | ||
enable_vulnerability_alerts: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"singleQuote": true, | ||
"semi": false, | ||
"tabWidth": 2, | ||
"printWidth": 125, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"enforce_admins": true, | ||
"required_pull_request_reviews": { | ||
"dismiss_stale_reviews": true, | ||
"require_code_owner_reviews": true, | ||
"required_approving_review_count": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,42 +10,40 @@ const BOT_EMAIL = '[email protected]' | |
export abstract class GithubCommand extends Command { | ||
token = Option.String('-t,--token', { | ||
required: true, | ||
description: 'Github personal access token(PAT)' | ||
description: 'Github personal access token(PAT)', | ||
}) | ||
user = Option.String('-o,--owner', { | ||
required: true, | ||
description: 'user or organization' | ||
description: 'user or organization', | ||
}) | ||
repo = Option.String('-r,--repo', { | ||
description: 'If this option passed, only that repository will be affected' | ||
description: 'If this option passed, only that repository will be affected', | ||
}) | ||
async execute() { | ||
const octokit = new Octokit({ auth: this.token }) | ||
const repositories = await octokit.rest.repos.listForUser({ | ||
username: this.user | ||
username: this.user, | ||
per_page: 100, | ||
}) | ||
const promises = [] | ||
for (const repository of repositories.data) { | ||
const [owner, repo] = repository.full_name.split('/') | ||
const dftBranch = repository.default_branch | ||
if (this.repo && repo !== this.repo) continue | ||
this.context.stdout.write(repository.full_name + '\n') | ||
promises.push(this.repository(owner, repo, octokit)) | ||
promises.push(this.repository(owner, repo, octokit, dftBranch)) | ||
} | ||
await Promise.all(promises) | ||
} | ||
|
||
abstract repository( | ||
owner: string, | ||
repo: string, | ||
octokit: Octokit | ||
): Promise<unknown> | ||
abstract repository(owner: string, repo: string, octokit: Octokit, branch: string): Promise<unknown> | ||
} | ||
|
||
export class GithubFolderCommand extends GithubCommand { | ||
static paths = [Command.Default] | ||
async = Option.Boolean('-a,--async') | ||
folder = Option.String('-f,--folder', '.github', { | ||
description: 'Folder to apply all repo' | ||
description: 'Folder to apply all repo', | ||
}) | ||
list: string[] | ||
constructor() { | ||
|
@@ -64,12 +62,7 @@ export class GithubFolderCommand extends GithubCommand { | |
await Promise.all(promises) | ||
} | ||
|
||
async uploadFile( | ||
owner: string, | ||
repo: string, | ||
{ rest }: Octokit, | ||
path: string | ||
) { | ||
async uploadFile(owner: string, repo: string, { rest }: Octokit, path: string) { | ||
const option = { owner, repo, path, sha: undefined } | ||
const content = await rest.repos.getContent(option).catch(() => null) | ||
if (content) option.sha = content.data.sha | ||
|
@@ -78,24 +71,53 @@ export class GithubFolderCommand extends GithubCommand { | |
...option, | ||
message: `meta: add default \`${this.folder}\` folder`, | ||
content: file.toString('base64'), | ||
committer: { name: BOT_NAME, email: BOT_EMAIL } | ||
committer: { name: BOT_NAME, email: BOT_EMAIL }, | ||
}) | ||
} | ||
} | ||
export class GithubBranchProtectionCommand extends GithubCommand { | ||
static paths = [['branch', 'protection']] | ||
file = Option.String('-f,--file', { | ||
required: true, | ||
description: 'Config file for branch', | ||
}) | ||
branch = Option.String('-d,--default', 'main', { | ||
description: 'New Default branch name', | ||
}) | ||
list: string[] | ||
constructor() { | ||
super() | ||
} | ||
|
||
async repository(owner: string, repo: string, octokit: Octokit, branch: string) { | ||
await this.branchProtection(owner, repo, octokit, branch) | ||
} | ||
|
||
async branchProtection(owner: string, repo: string, { rest }: Octokit, branch: string) { | ||
if (this.branch !== branch) await rest.repos.renameBranch({ owner, repo, branch, new_name: this.branch }) | ||
const config = await readFile(this.file).then(json => JSON.parse(String(json))) | ||
const r = await rest.repos.updateBranchProtection({ | ||
owner, | ||
repo, | ||
branch: this.branch, | ||
required_status_checks: null, | ||
restrictions: null, | ||
...config, | ||
}) | ||
} | ||
} | ||
|
||
export class GithubLabelCommand extends GithubCommand { | ||
static paths = [['labels']] | ||
file = Option.String('-f,--file', 'config/repo.json', { | ||
description: 'label config json file' | ||
description: 'label config json file', | ||
}) | ||
async repository(owner: string, repo: string) { | ||
const { labels } = await readFile(this.file).then((json) => | ||
JSON.parse(String(json)) | ||
) | ||
const { labels } = await readFile(this.file).then(json => JSON.parse(String(json))) | ||
await githubLabelSync({ | ||
accessToken: this.token, | ||
repo: `${owner}/${repo}`, | ||
labels | ||
labels, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { Cli, Builtins } from 'clipanion' | ||
import { version, name, displayName } from '../package.json' | ||
import { GithubFolderCommand, GithubLabelCommand } from './github' | ||
import { GithubFolderCommand, GithubLabelCommand, GithubBranchProtectionCommand } from './github' | ||
|
||
const cli = new Cli({ | ||
binaryName: name, | ||
binaryLabel: displayName, | ||
binaryVersion: version | ||
binaryVersion: version, | ||
}) | ||
|
||
cli.register(GithubFolderCommand) | ||
cli.register(GithubLabelCommand) | ||
cli.register(GithubBranchProtectionCommand) | ||
cli.register(Builtins.HelpCommand) | ||
cli.register(Builtins.VersionCommand) | ||
cli.runExit(process.argv.slice(2), Cli.defaultContext) |