Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Jan 7, 2023
1 parent 6e795bc commit 51e00eb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 57 deletions.
37 changes: 24 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,23 @@ var __webpack_exports__ = {};



const { COMMIT_EACH_FILE, COMMIT_PREFIX, PR_LABELS, ASSIGNEES, DRY_RUN, TMP_DIR, SKIP_CLEANUP, OVERWRITE_EXISTING_PR, SKIP_PR, ORIGINAL_MESSAGE, COMMIT_AS_PR_TITLE, FORK, REVIEWERS, TEAM_REVIEWERS } = _config__WEBPACK_IMPORTED_MODULE_4__

const {
COMMIT_EACH_FILE,
COMMIT_PREFIX,
PR_LABELS,
ASSIGNEES,
DRY_RUN,
TMP_DIR,
SKIP_CLEANUP,
OVERWRITE_EXISTING_PR,
SKIP_PR,
ORIGINAL_MESSAGE,
COMMIT_AS_PR_TITLE,
FORK,
REVIEWERS,
TEAM_REVIEWERS
} = _config__WEBPACK_IMPORTED_MODULE_4__

async function run() {
// Reuse octokit for each repo
Expand Down Expand Up @@ -2855,22 +2871,19 @@ async function run() {

// Loop through all selected files of the source repo
await (0,_helpers__WEBPACK_IMPORTED_MODULE_3__.forEach)(item.files, async (file) => {
const fileExists = (0,fs__WEBPACK_IMPORTED_MODULE_1__.existsSync)(file.source)
if (fileExists === false)
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Source ${ file.source } not found`)
const fileExists = fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(file.source)
if (fileExists === false) return _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Source ${ file.source } not found`)

const localDestination = `${ git.workingDir }/${ file.dest }`

const destExists = (0,fs__WEBPACK_IMPORTED_MODULE_1__.existsSync)(localDestination)
if (destExists === true && file.replace === false)
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`File(s) already exist(s) in destination and 'replace' option is set to false`)
const destExists = fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(localDestination)
if (destExists === true && file.replace === false) return _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`File(s) already exist(s) in destination and 'replace' option is set to false`)

const isDirectory = await (0,_helpers__WEBPACK_IMPORTED_MODULE_3__.pathIsDirectory)(file.source)
const source = isDirectory ? `${ (0,_helpers__WEBPACK_IMPORTED_MODULE_3__.addTrailingSlash)(file.source) }` : file.source
const dest = isDirectory ? `${ (0,_helpers__WEBPACK_IMPORTED_MODULE_3__.addTrailingSlash)(localDestination) }` : localDestination

if (isDirectory)
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Source is directory`)
if (isDirectory) _actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Source is directory`)

await (0,_helpers__WEBPACK_IMPORTED_MODULE_3__.copy)(source, dest, isDirectory, file)

Expand All @@ -2880,8 +2893,7 @@ async function run() {
if (COMMIT_EACH_FILE === true) {
const hasChanges = await git.hasChanges()

if (hasChanges === false)
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.debug('File(s) already up to date')
if (hasChanges === false) return _actions_core__WEBPACK_IMPORTED_MODULE_0__.debug('File(s) already up to date')

_actions_core__WEBPACK_IMPORTED_MODULE_0__.debug(`Creating commit for file(s) ${ file.dest }`)

Expand Down Expand Up @@ -2928,8 +2940,7 @@ async function run() {
if (hasChanges === false && modified.length < 1) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info('File(s) already up to date')

if (existingPr)
await git.removePrWarning()
if (existingPr) await git.removePrWarning()

return
}
Expand Down
39 changes: 26 additions & 13 deletions src/git.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { parse } from '@putout/git-status-porcelain'
import * as core from '@actions/core'
import { context } from '@actions/github'
import * as github from '@actions/github'
import { GitHub, getOctokitOptions } from '@actions/github/lib/utils'
import { throttling } from '@octokit/plugin-throttling'
import * as path from 'path'
import * as fs from 'fs/promises'

import { GITHUB_TOKEN, IS_INSTALLATION_TOKEN, IS_FINE_GRAINED, GIT_USERNAME, GIT_EMAIL, TMP_DIR, COMMIT_BODY, COMMIT_PREFIX, GITHUB_REPOSITORY, OVERWRITE_EXISTING_PR, SKIP_PR, PR_BODY, BRANCH_PREFIX, FORK } from './config'
import {
GITHUB_TOKEN,
IS_INSTALLATION_TOKEN,
IS_FINE_GRAINED,
GIT_USERNAME,
GIT_EMAIL,
TMP_DIR,
COMMIT_BODY,
COMMIT_PREFIX,
GITHUB_REPOSITORY,
OVERWRITE_EXISTING_PR,
SKIP_PR,
PR_BODY,
BRANCH_PREFIX,
FORK
} from './config'

import { dedent, execCmd } from './helpers'

class Git {
export default class Git {
constructor() {
const Octokit = GitHub.plugin(throttling)

Expand Down Expand Up @@ -134,11 +149,11 @@ class Git {
}

isOneCommitPush() {
return context.eventName === 'push' && context.payload.commits.length === 1
return github.context.eventName === 'push' && github.context.payload.commits.length === 1
}

originalCommitMessage() {
return context.payload.commits[0].message
return github.context.payload.commits[0].message
}

parseGitDiffOutput(string) { // parses git diff output and returns a dictionary mapping the file path to the diff output for this file
Expand All @@ -148,7 +163,7 @@ class Git {
const lastHeaderLineIndex = lines.findIndex((line) => line.startsWith('+++'))
if (lastHeaderLineIndex === -1) return resultDict // ignore binary files

const plainDiff = lines.slice(lastHeaderLineIndex + 1).path.join('\n').trim()
const plainDiff = lines.slice(lastHeaderLineIndex + 1).join('\n').trim()
let filePath = ''
if (lines[lastHeaderLineIndex].startsWith('+++ b/')) { // every file except removed files
filePath = lines[lastHeaderLineIndex].slice(6) // remove '+++ b/'
Expand All @@ -165,10 +180,10 @@ class Git {
mediaType: {
format: 'diff'
},
owner: context.payload.repository.owner.name,
repo: context.payload.repository.name,
base: context.payload.before,
head: context.payload.after
owner: github.context.payload.repository.owner.name,
repo: github.context.payload.repository.name,
base: github.context.payload.before,
head: github.context.payload.after
})
this.lastCommitChanges = this.parseGitDiffOutput(diff.data)
}
Expand Down Expand Up @@ -506,6 +521,4 @@ class Git {
})
this.lastCommitSha = request.data.sha
}
}

export default Git
}
32 changes: 15 additions & 17 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { lstat, outputFile, copy as _copy, remove as _remove } from 'fs-extra'
import * as fs from 'fs-extra'
import readfiles from 'node-readfiles'
import { exec } from 'child_process'
import * as core from '@actions/core'
import { join } from 'path'
import { configure, render } from 'nunjucks'
import * as path from 'path'
import * as nunjucks from 'nunjucks'

configure({ autoescape: true, trimBlocks: true, lstripBlocks: true })
nunjucks.configure({ autoescape: true, trimBlocks: true, lstripBlocks: true })

// From https://github.com/toniov/p-iteration/blob/master/lib/static-methods.js - MIT © Antonio V
export async function forEach(array, callback) {
Expand Down Expand Up @@ -65,16 +65,16 @@ export function addTrailingSlash(str) {
}

export async function pathIsDirectory(path) {
const stat = await lstat(path)
const stat = await fs.lstat(path)
return stat.isDirectory()
}

export async function write(src, dest, context) {
if (typeof context !== 'object') {
context = {}
}
const content = render(src, context)
await outputFile(dest, content)
const content = nunjucks.render(src, context)
await fs.outputFile(dest, content)
}

export async function copy(src, dest, isDirectory, file) {
Expand All @@ -97,8 +97,8 @@ export async function copy(src, dest, isDirectory, file) {
for (const srcFile of srcFileList) {
if (!filterFunc(srcFile)) { continue }

const srcPath = join(src, srcFile)
const destPath = join(dest, srcFile)
const srcPath = path.join(src, srcFile)
const destPath = path.join(dest, srcFile)
await write(srcPath, destPath, file.template)
}
} else {
Expand All @@ -108,7 +108,7 @@ export async function copy(src, dest, isDirectory, file) {
}
} else {
core.debug(`Copy ${ src } to ${ dest }`)
await _copy(src, dest, file.exclude !== undefined && { filter: filterFunc })
await fs.copy(src, dest, file.exclude !== undefined && { filter: filterFunc })
}


Expand All @@ -119,17 +119,15 @@ export async function copy(src, dest, isDirectory, file) {
const destFileList = await readfiles(dest, { readContents: false, hidden: true })

for (const destFile of destFileList) {
if (destFile.startsWith('.git'))
return
if (srcFileList.indexOf(destFile) === -1) {
const filePath = join(dest, destFile)
core.debug(`Found an orphaned file in the target repo - ${ filePath }`)
const filePath = path.join(dest, destFile)
core.debug(`Found a orphaned file in the target repo - ${ filePath }`)

if (file.exclude !== undefined && file.exclude.includes(join(src, destFile))) {
if (file.exclude !== undefined && file.exclude.includes(path.join(src, destFile))) {
core.debug(`Excluding file ${ destFile }`)
} else {
core.debug(`Removing file ${ destFile }`)
await _remove(filePath)
await fs.remove(filePath)
}
}
}
Expand All @@ -140,7 +138,7 @@ export async function remove(src) {

core.debug(`RM: ${ src }`)

return _remove(src)
return fs.remove(src)
}

export function arrayEquals(array1, array2) {
Expand Down
39 changes: 25 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import * as core from '@actions/core'
import { existsSync } from 'fs'
import * as fs from 'fs'

import Git from './git'
import { forEach, dedent, addTrailingSlash, pathIsDirectory, copy, remove, arrayEquals } from './helpers'

import { parseConfig, default as config } from './config'
const { COMMIT_EACH_FILE, COMMIT_PREFIX, PR_LABELS, ASSIGNEES, DRY_RUN, TMP_DIR, SKIP_CLEANUP, OVERWRITE_EXISTING_PR, SKIP_PR, ORIGINAL_MESSAGE, COMMIT_AS_PR_TITLE, FORK, REVIEWERS, TEAM_REVIEWERS } = config

const {
COMMIT_EACH_FILE,
COMMIT_PREFIX,
PR_LABELS,
ASSIGNEES,
DRY_RUN,
TMP_DIR,
SKIP_CLEANUP,
OVERWRITE_EXISTING_PR,
SKIP_PR,
ORIGINAL_MESSAGE,
COMMIT_AS_PR_TITLE,
FORK,
REVIEWERS,
TEAM_REVIEWERS
} = config

async function run() {
// Reuse octokit for each repo
Expand Down Expand Up @@ -44,22 +60,19 @@ async function run() {

// Loop through all selected files of the source repo
await forEach(item.files, async (file) => {
const fileExists = existsSync(file.source)
if (fileExists === false)
return core.warning(`Source ${ file.source } not found`)
const fileExists = fs.existsSync(file.source)
if (fileExists === false) return core.warning(`Source ${ file.source } not found`)

const localDestination = `${ git.workingDir }/${ file.dest }`

const destExists = existsSync(localDestination)
if (destExists === true && file.replace === false)
return core.warning(`File(s) already exist(s) in destination and 'replace' option is set to false`)
const destExists = fs.existsSync(localDestination)
if (destExists === true && file.replace === false) return core.warning(`File(s) already exist(s) in destination and 'replace' option is set to false`)

const isDirectory = await pathIsDirectory(file.source)
const source = isDirectory ? `${ addTrailingSlash(file.source) }` : file.source
const dest = isDirectory ? `${ addTrailingSlash(localDestination) }` : localDestination

if (isDirectory)
core.info(`Source is directory`)
if (isDirectory) core.info(`Source is directory`)

await copy(source, dest, isDirectory, file)

Expand All @@ -69,8 +82,7 @@ async function run() {
if (COMMIT_EACH_FILE === true) {
const hasChanges = await git.hasChanges()

if (hasChanges === false)
return core.debug('File(s) already up to date')
if (hasChanges === false) return core.debug('File(s) already up to date')

core.debug(`Creating commit for file(s) ${ file.dest }`)

Expand Down Expand Up @@ -117,8 +129,7 @@ async function run() {
if (hasChanges === false && modified.length < 1) {
core.info('File(s) already up to date')

if (existingPr)
await git.removePrWarning()
if (existingPr) await git.removePrWarning()

return
}
Expand Down

0 comments on commit 51e00eb

Please sign in to comment.