Skip to content

Commit

Permalink
feat: branch protection setting with dft branch
Browse files Browse the repository at this point in the history
  • Loading branch information
seonglae committed Feb 13, 2022
1 parent cc389cb commit be8c522
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 38 deletions.
14 changes: 4 additions & 10 deletions .github/settings.yml
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
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"semi": false,
"tabWidth": 2,
"printWidth": 125,
"arrowParens": "avoid"
}
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 1.3.1
## 1.4.0

- Add branch protection feature & template


### 1.3.1

- config label added
- settings template added to repo
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ gh-setter
# create .github folder
gh-setter --token $GH_TOKEN --owner $GH_OWNER --folder .github

# change config.json
# change labels
gh-setter labels --token $GH_TOKEN --user $GH_USER

# change branch protection
gh-setter branch protection --token $GH_TOKEN --user $GH_USER
```

## module

```ts
import { GithubCommand } from "github-setter";
```


## Warn
maximum repo update is 100
this might be updated
8 changes: 8 additions & 0 deletions config/branch.json
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
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "github-setter",
"displayName": "Github sync `.github` folder & label for custom settings",
"version": "1.3.1",
"version": "1.4.0",
"main": "./build/github.js",
"bin": {
"gh-setter": "./build/main.js"
Expand Down
68 changes: 45 additions & 23 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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,
})
}
}
5 changes: 3 additions & 2 deletions src/main.ts
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)

0 comments on commit be8c522

Please sign in to comment.