Skip to content

Commit

Permalink
✨ (scripts) pullRequest export update
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeFitz committed May 13, 2021
1 parent 0e78ee8 commit 764998a
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ vercel-required.json
vercel-secrets.json
!vercel-secrets.example.json
size-snapshot.json
release
release
tsconfig.tsbuildinfo
8 changes: 3 additions & 5 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@
"registry": "https://registry.npmjs.org/"
},
"private": false,
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"build": "tsc --build src",
"lint-eslint": "eslint . --ext js,jsx,ts,tsx --max-warnings=0",
"lint-fix": "yarn prettier-fix && eslint . --ext js,jsx,ts,tsx --fix --max-warnings=0",
"lint-typescript": "yarn typescript",
"lint": "yarn lint-typescript && yarn prettier-check && yarn lint-eslint",
"tsc:watch": "tsc -w --preserveWatchOutput",
"prepublish": "yarn build",
"prettier-check": "prettier --check \"**/*.{js,jsx,ts,tsx,json,md,mdx,css,html,yml,yaml,scss}\"",
"prettier-fix": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md,mdx,css,html,yml,yaml,scss}\"",
"pretty-quick": "pretty-quick",
"typescript": "tsc --noEmit --declaration"
},
"devDependecies": {
"@jeromefitz/codestyle": "2.0.0-canary.3",
"typescript": "4.1.5"
},
"dependencies": {
"@octokit/rest": "18.5.3"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/scripts/src/data/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const labels = [
description: 'Do not merge until further notice',
color: '5e0b82',
},
{
name: 'Gitflow',
emoji: ':octocat:',
description: 'Gitflow Standards',
color: 'd4c5f9',
},
{
name: 'Help Wanted',
emoji: '🤝️',
Expand Down
8 changes: 4 additions & 4 deletions packages/scripts/src/github/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import getLabels from '../data/labels'

const octokit = new Octokit({ auth: process.env.GH_TOKEN })

const owner = process.env.REPO_OWNER
const repo = process.env.REPO_REPO
// const owner = process.env.REPO_OWNER
// const repo = process.env.REPO_REPO

const labels = getLabels.map((label) => ({
name: `${label.emoji} ${label.name}`,
Expand All @@ -21,7 +21,7 @@ const labels = getLabels.map((label) => ({
// @note
// - Potentialy may be to do a`listLabelsForRepo` then `deleteLabel` and just`createLabel` fresh
// - Or `listLabelsForRepo` then create a migration script instead.
async function createLabels() {
async function createLabels({ owner, repo }) {
try {
labels.map(async (label) => {
await octokit.rest.issues.createLabel({
Expand All @@ -41,4 +41,4 @@ async function createLabels() {
}
}

createLabels()
export default createLabels
8 changes: 4 additions & 4 deletions packages/scripts/src/github/milestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import getMilestones from '../data/milestones'

const octokit = new Octokit({ auth: process.env.GH_TOKEN })

const owner = process.env.REPO_OWNER
const repo = process.env.REPO_REPO
// const owner = process.env.REPO_OWNER
// const repo = process.env.REPO_REPO

const milestones = getMilestones.map((milestone) => ({
title: `${milestone.emoji} ${milestone.name}`,
description: milestone.description,
state: milestone.state,
}))

async function createMilestones() {
async function createMilestones({ owner, repo }) {
try {
milestones.map(async (milestone) => {
await octokit.rest.issues.createMilestone({
Expand All @@ -40,4 +40,4 @@ async function createMilestones() {
}
}

createMilestones()
export default createMilestones
132 changes: 83 additions & 49 deletions packages/scripts/src/github/pullRequest.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,100 @@
#!/usr/bin/env node
#!/usr/bin/env
const { execSync } = require('child_process')

import { Octokit } from '@octokit/rest'
import chalkPipe from 'chalk-pipe'
import isCI from 'is-ci'
const { Octokit } = require('@octokit/rest')
const chalkPipe = require('chalk-pipe')
const isCI = require('is-ci')

import { version } from '../package.json'
import PULL_REQUEST__RELEASE from '../templates/PULL_REQUEST__RELEASE'
!isCI && require('dotenv').config({ path: './.env' })
const PULL_REQUEST__GITFLOW = require('../templates/PULL_REQUEST__GITFLOW')

const octokit = new Octokit({ auth: process.env.GH_TOKEN })
const getVersion = (version) => {
const [major, minor, patch] = version.split('-')[0].split('.')
return [parseInt(major), parseInt(minor), parseInt(patch)].join('.')
}

const owner = process.env.REPO_OWNER
const repo = process.env.REPO_REPO
async function setPullRequest({ head, labels, repo_id, version, q }) {
try {
// @note(ci) assumes travis keeps us honest when this runs
const base = isMain ? 'develop' : 'main'
const bodyTemplate = PULL_REQUEST__GITFLOW()
const titleTemplate = `merge: 🔀️ v{version} => {base} [gitflow] [skip ci]`

const base = 'main'
const head = 'feature/github-templates'
const toStore = isMain ? 'Release' : 'Submit'

const bodyTemplate = PULL_REQUEST__RELEASE()
const titleTemplate = `merge: 🔀️ v{version} => {base} [gitflow] [skip ci]`
const body = bodyTemplate
.replace(/\{base\}/g, base)
.replace(/\{head\}/g, head)
.replace(/\{toStore\}/g, toStore)
.replace(/\{version\}/g, version)

const toStore = base === 'main' ? 'Release' : 'Submit'
const title = titleTemplate
.replace(/\{base\}/g, base)
.replace(/\{head\}/g, head)
.replace(/\{toStore\}/g, toStore)
.replace(/\{version\}/g, version)

const body = bodyTemplate
.replace(/\{base\}/g, base)
.replace(/\{head\}/g, head)
.replace(/\{toStore\}/g, toStore)
.replace(/\{version\}/g, version)
const response = await octokit.rest.search.issuesAndPullRequests({
q,
})
const doesGitflowPrExists = !!response.data && response.data.total_count === 1
if (doesGitflowPrExists) {
const pr = response.data.items[0]
const pull_number = pr.number
const issue_number = pull_number
console.log(
chalkPipe('orange.bold')(
`🤔️ ${pull_number} => Exists, should we update PR?`
)
)
console.log(chalkPipe('orange.bold')(`🤪️ Right now we are not`))
console.log(
chalkPipe('orange.bold')(
`😵️ https://github.com/${repo_id}/pull/${pull_number}`
)
)

const title = titleTemplate
.replace(/\{base\}/g, base)
.replace(/\{head\}/g, head)
.replace(/\{toStore\}/g, toStore)
.replace(/\{version\}/g, version)
// await octokit.rest.pulls.update({
// owner,
// repo,
// head,
// base,
// title,
// body,
// pull_number,
// });

async function pullRequest() {
try {
// const response = await octokit.rest.pulls.create({
// owner,
// repo,
// head,
// base,
// title,
// body,
// })
const response = await octokit.rest.pulls.update({
owner,
repo,
head,
base,
title,
body,
pull_number: 1,
})
if (!!response.data.body) {
console.log(chalkPipe('green.bold')(`✅️ success: ${owner}/${repo} => pr`))
// console.log(chalkPipe('white.italic')(response.body))
console.log(JSON.stringify(response, null, 2))
// await octokit.rest.issues.setLabels({
// owner,
// repo,
// issue_number,
// labels,
// });
} else {
console.log(chalkPipe('blue.bold')(`🤠️ Yee-haw, Create a Pee-Ahr`))
console.log(chalkPipe('blue.bold')(`🤓️ ${title}`))

const pull = await octokit.rest.pulls.create({
owner,
repo,
head,
base,
title,
body,
})

octokit.rest.issues.addLabels({
owner,
repo,
issue_number: pull.data.number,
labels,
})
}
} catch (error) {
console.log(chalkPipe('red.bold')(`❎️ error: ${owner}/${repo} => pr`))
// console.log(chalkPipe('white.italic')(error))
console.log(chalkPipe('red.bold')(`❎️ error: ${owner}/${repo} => pulls.list`))
console.log(chalkPipe('white.italic')(error))
}
}

pullRequest()
module.exports = setPullRequest
17 changes: 17 additions & 0 deletions packages/scripts/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import labels from './data/labels'
import milestones from './data/milestones'
import githubLabel from './github/label'
import githubMilestone from './github/milestone'
import githubPullRequest from './github/pullRequest'
import templatePullRequestRelease from './templates/PULL_REQUEST__RELEASE'
import { space } from './utils/getChars'

export {
labels,
milestones,
githubLabel,
githubMilestone,
githubPullRequest,
templatePullRequestRelease,
space,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { space } from '../utils/getChars'

const PULL_REQUEST__RELEASE = () => {
const PULL_REQUEST__GITFLOW = () => {
return `### 🔀️ ${space} **Gitflow Automation:** \`{version} => {base}\`
- [x] 📦️ ${space} **Version**: \`{version}\`
Expand All @@ -15,4 +15,4 @@ const PULL_REQUEST__RELEASE = () => {
`
}

export default PULL_REQUEST__RELEASE
export default PULL_REQUEST__GITFLOW
18 changes: 18 additions & 0 deletions packages/scripts/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"noEmit": false,
"outDir": "../dist",
"paths": {},
"noUnusedParameters": false, // @todo(lint)
"resolveJsonModule": true,
"rootDir": ".",
"strict": false,
"target": "ES6"
},
"exclude": ["node_modules"],
"extends": "../../codestyle/tsconfig.react.json",
"includes": ["**/*.js"],
"references": [{ "path": "../" }]
}
File renamed without changes.
3 changes: 3 additions & 0 deletions packages/scripts/src/utils/getPackageJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { version } from '../../package.json'

export { version }
17 changes: 5 additions & 12 deletions packages/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"noEmit": false,
"outDir": "./dist",
"paths": {},
"noUnusedParameters": false, // @todo(lint)
"rootDir": "./src",
"strict": false,
"target": "ES6"
"rootDir": ".",
"outDir": ".",
"resolveJsonModule": true,
"composite": true
},
"exclude": ["node_modules"],
"extends": "../codestyle/tsconfig.react.json",
"include": ["src/**/*.js", "src/**/*.ts"]
"files": ["package.json"]
}

0 comments on commit 764998a

Please sign in to comment.