From 2fac2e9c881182db3ffa7a16ccc9c55e1a5948e0 Mon Sep 17 00:00:00 2001 From: yan Date: Mon, 29 Apr 2024 22:32:43 -0700 Subject: [PATCH 1/2] externalize action.yml script using https://github.com/actions/github-script?tab=readme-ov-file#run-a-separate-file-with-an-async-function --- action.cjs | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++ action.yml | 131 ++--------------------------------------------------- 2 files changed, 132 insertions(+), 127 deletions(-) create mode 100644 action.cjs diff --git a/action.cjs b/action.cjs new file mode 100644 index 0000000..a9712d1 --- /dev/null +++ b/action.cjs @@ -0,0 +1,128 @@ +module.exports = async ({github, context, inputs, actionPath}) => { + const { default: filterdiff } = await import(`${actionPath}/src/filterdiff.js`) + const { default: getConfig } = await import(`${actionPath}/src/getConfig.js`) + const { default: getProperties } = await import(`${actionPath}/src/getProperties.js`) + + // delete if empty string in inputs value + Object.keys(inputs).forEach(key => inputs[key] === '' && delete inputs[key]) + + let debug = process.env.DEBUG == 'true' + if (debug) console.log('Initializing puLL-Merge') + + const config = await getConfig({ owner: context.repo.owner, repo: context.repo.repo, path: '.github/pull-merge.json', debug, github }) + const properties = await getProperties({ owner: context.repo.owner, repo: context.repo.repo, debug, github }) + + const options = Object.assign({ + debounce_time: '6', + amplification: '4', + filterdiff_args: '--exclude=**/package-lock.json --exclude=**/yarn.lock --exclude=**/*.js.map --exclude=**/*.svg --exclude=**/test/data/**/* --exclude=**/docs/**/* --exclude=**/deploy/**/* --exclude=**/.htpasswd', + openai_models: 'gpt-4-turbo-2024-04-09 gpt-3.5-turbo-0125', + anthropic_models: 'claude-3-opus-20240229', + bedrock_models: 'anthropic.claude-3-opus-20240229-v1:0', + owner: context.repo.owner, + repo: context.repo.repo, + prnum: context.issue.number, + max_tokens: '2048', + subtle_mode: 'false' + }, config, properties, inputs) + + // convert to numbers some options + options.debounce_time = parseFloat(options.debounce_time, 10) + options.amplification = parseFloat(options.amplification, 10) + options.prnum = parseFloat(options.prnum, 10) + options.max_tokens = parseFloat(options.max_tokens, 10) + options.subtle_mode = options.subtle_mode === 'true' + + const { default: explainPatch } = + options.bedrock_aws_iam_role_arn + ? await import(`${actionPath}/src/bedrockExplainPatch.js`) + : options.anthropic_api_key + ? await import(`${actionPath}/src/anthropicExplainPatch.js`) + : await import(`${actionPath}/src/openaiExplainPatch.js`) + + const { default: submitReview } = options.subtle_mode + ? await import(`${actionPath}/src/subtleSubmitReview.js`) + : await import(`${actionPath}/src/submitReview.js`) + + const { default: getPatch } = (context.payload.pull_request && context.payload.pull_request.user.login === 'renovate[bot]') || context.actor === 'renovate[bot]' + ? await import(`${actionPath}/src/getRenovatePatch.js`) + : (context.payload.pull_request && context.payload.pull_request.user.login === 'dependabot[bot]') || context.actor === 'dependabot[bot]' + ? await import(`${actionPath}/src/getDependabotPatch.js`) + : await import(`${actionPath}/src/getPatch.js`) + + options.key = options.anthropic_api_key || options.openai_api_key + options.models = options.bedrock_aws_iam_role_arn + ? options.bedrock_models + : options.anthropic_api_key + ? options.anthropic_models + : options.openai_models + + debug = options.debug ? (options.debug == 'true') : debug + + if (debug) { + console.log(`Using options: ${JSON.stringify(options)}`) + console.log(`Using config: ${JSON.stringify(config)}`) + console.log(`Using properties: ${JSON.stringify(properties)}`) + console.log(`Using inputs: ${JSON.stringify(inputs)}`) + } + + try { + const patch = await getPatch({ + owner: options.owner, + repo: options.repo, + prnum: options.prnum, + debug, + runIfPrivate: options.run_if_private, + github + }) + + const filteredPatch = await filterdiff({ + content: patch.body, + args: options.filterdiff_args, + debug + }) + + const explainPatchCb = async () => await explainPatch({ + apiKey: options.key, + patchBody: filteredPatch, + owner: patch.owner, + repo: patch.repo, + debug, + models: options.models.split(' '), + amplification: options.amplification, + max_tokens: options.max_tokens, + region: options.region + }) + + let watermark = patch.watermark + + if (debug) { + watermark = options.bedrock_aws_iam_role_arn + ? `bedrock debug - ${watermark}` + : options.anthropic_api_key + ? `anthropic debug - ${watermark}` + : `openai debug - ${watermark}` + } + + await submitReview({ + owner: options.owner, + repo: options.repo, + prnum: options.prnum, + watermark, + explainPatch: explainPatchCb, + debounceTime: options.debounce_time, + debug, + github + }) + + await github.rest.issues.addLabels({ + owner: options.owner, + repo: options.repo, + issue_number: options.prnum, + labels: ['puLL-Merge'] + }) + } catch (error) { + console.log(error) + if (debug) throw error + } +} diff --git a/action.yml b/action.yml index 1048989..5a97556 100644 --- a/action.yml +++ b/action.yml @@ -87,130 +87,7 @@ runs: DEBUG: ${{ (inputs.debug == 'true' || runner.debug) && 'true' || 'false'}} with: script: | - const { default: filterdiff } = await import('${{ github.action_path }}/src/filterdiff.js') - const { default: getConfig } = await import('${{ github.action_path }}/src/getConfig.js') - const { default: getProperties } = await import('${{ github.action_path }}/src/getProperties.js') - - const inputs = ${{ toJson(inputs) }}; - // delete if empty string in inputs value - Object.keys(inputs).forEach(key => inputs[key] === '' && delete inputs[key]) - - let debug = process.env.DEBUG == 'true' - if (debug) console.log('Initializing puLL-Merge') - - const config = await getConfig({ owner: context.repo.owner, repo: context.repo.repo, path: '.github/pull-merge.json', debug, github }) - const properties = await getProperties({ owner: context.repo.owner, repo: context.repo.repo, debug, github }) - - const options = Object.assign({ - debounce_time: '6', - amplification: '4', - filterdiff_args: '--exclude=**/package-lock.json --exclude=**/yarn.lock --exclude=**/*.js.map --exclude=**/*.svg --exclude=**/test/data/**/* --exclude=**/docs/**/* --exclude=**/deploy/**/* --exclude=**/.htpasswd', - openai_models: 'gpt-4-turbo-2024-04-09 gpt-3.5-turbo-0125', - anthropic_models: 'claude-3-opus-20240229', - bedrock_models: 'anthropic.claude-3-opus-20240229-v1:0', - owner: context.repo.owner, - repo: context.repo.repo, - prnum: context.issue.number, - max_tokens: '2048', - subtle_mode: 'false' - }, config, properties, inputs) - - // convert to numbers some options - options.debounce_time = parseFloat(options.debounce_time, 10) - options.amplification = parseFloat(options.amplification, 10) - options.prnum = parseFloat(options.prnum, 10) - options.max_tokens = parseFloat(options.max_tokens, 10) - options.subtle_mode = options.subtle_mode === 'true' - - const { default: explainPatch } = - options.bedrock_aws_iam_role_arn - ? await import('${{ github.action_path }}/src/bedrockExplainPatch.js') - : options.anthropic_api_key - ? await import('${{ github.action_path }}/src/anthropicExplainPatch.js') - : await import('${{ github.action_path }}/src/openaiExplainPatch.js') - - const { default: submitReview } = options.subtle_mode - ? await import('${{ github.action_path }}/src/subtleSubmitReview.js') - : await import('${{ github.action_path }}/src/submitReview.js') - - const { default: getPatch } = (context.payload.pull_request && context.payload.pull_request.user.login === 'renovate[bot]') || context.actor === 'renovate[bot]' - ? await import('${{ github.action_path }}/src/getRenovatePatch.js') - : (context.payload.pull_request && context.payload.pull_request.user.login === 'dependabot[bot]') || context.actor === 'dependabot[bot]' - ? await import('${{ github.action_path }}/src/getDependabotPatch.js') - : await import('${{ github.action_path }}/src/getPatch.js') - - options.key = options.anthropic_api_key || options.openai_api_key - options.models = options.bedrock_aws_iam_role_arn - ? options.bedrock_models - : options.anthropic_api_key - ? options.anthropic_models - : options.openai_models - - debug = options.debug ? (options.debug == 'true') : debug - - if (debug) { - console.log(`Using options: ${JSON.stringify(options)}`) - console.log(`Using config: ${JSON.stringify(config)}`) - console.log(`Using properties: ${JSON.stringify(properties)}`) - console.log(`Using inputs: ${JSON.stringify(inputs)}`) - } - - try { - const patch = await getPatch({ - owner: options.owner, - repo: options.repo, - prnum: options.prnum, - debug, - runIfPrivate: options.run_if_private, - github - }) - - const filteredPatch = await filterdiff({ - content: patch.body, - args: options.filterdiff_args, - debug - }) - - const explainPatchCb = async () => await explainPatch({ - apiKey: options.key, - patchBody: filteredPatch, - owner: patch.owner, - repo: patch.repo, - debug, - models: options.models.split(' '), - amplification: options.amplification, - max_tokens: options.max_tokens, - region: options.region - }) - - let watermark = patch.watermark - - if (debug) { - watermark = options.bedrock_aws_iam_role_arn - ? `bedrock debug - ${watermark}` - : options.anthropic_api_key - ? `anthropic debug - ${watermark}` - : `openai debug - ${watermark}` - } - - await submitReview({ - owner: options.owner, - repo: options.repo, - prnum: options.prnum, - watermark, - explainPatch: explainPatchCb, - debounceTime: options.debounce_time, - debug, - github - }) - - await github.rest.issues.addLabels({ - owner: options.owner, - repo: options.repo, - issue_number: options.prnum, - labels: ['puLL-Merge'] - }) - } catch (error) { - console.log(error) - if (debug) throw error - } + const actionPath = '${{ github.action_path }}' + const script = require(`${actionPath}/action.cjs`) + const inputs = ${{ toJson(inputs) }} + await script({github, context, inputs, actionPath}) From 75f8b282e5ae0323863434449ab006877b27b0fb Mon Sep 17 00:00:00 2001 From: yan Date: Tue, 30 Apr 2024 14:45:55 -0700 Subject: [PATCH 2/2] run the linter --- action.cjs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/action.cjs b/action.cjs index a9712d1..7abe13f 100644 --- a/action.cjs +++ b/action.cjs @@ -1,4 +1,4 @@ -module.exports = async ({github, context, inputs, actionPath}) => { +module.exports = async ({ github, context, inputs, actionPath }) => { const { default: filterdiff } = await import(`${actionPath}/src/filterdiff.js`) const { default: getConfig } = await import(`${actionPath}/src/getConfig.js`) const { default: getProperties } = await import(`${actionPath}/src/getProperties.js`) @@ -6,7 +6,7 @@ module.exports = async ({github, context, inputs, actionPath}) => { // delete if empty string in inputs value Object.keys(inputs).forEach(key => inputs[key] === '' && delete inputs[key]) - let debug = process.env.DEBUG == 'true' + let debug = process.env.DEBUG === 'true' if (debug) console.log('Initializing puLL-Merge') const config = await getConfig({ owner: context.repo.owner, repo: context.repo.repo, path: '.github/pull-merge.json', debug, github }) @@ -33,7 +33,7 @@ module.exports = async ({github, context, inputs, actionPath}) => { options.max_tokens = parseFloat(options.max_tokens, 10) options.subtle_mode = options.subtle_mode === 'true' - const { default: explainPatch } = + const { default: explainPatch } = options.bedrock_aws_iam_role_arn ? await import(`${actionPath}/src/bedrockExplainPatch.js`) : options.anthropic_api_key @@ -47,17 +47,17 @@ module.exports = async ({github, context, inputs, actionPath}) => { const { default: getPatch } = (context.payload.pull_request && context.payload.pull_request.user.login === 'renovate[bot]') || context.actor === 'renovate[bot]' ? await import(`${actionPath}/src/getRenovatePatch.js`) : (context.payload.pull_request && context.payload.pull_request.user.login === 'dependabot[bot]') || context.actor === 'dependabot[bot]' - ? await import(`${actionPath}/src/getDependabotPatch.js`) - : await import(`${actionPath}/src/getPatch.js`) + ? await import(`${actionPath}/src/getDependabotPatch.js`) + : await import(`${actionPath}/src/getPatch.js`) options.key = options.anthropic_api_key || options.openai_api_key options.models = options.bedrock_aws_iam_role_arn - ? options.bedrock_models - : options.anthropic_api_key - ? options.anthropic_models - : options.openai_models + ? options.bedrock_models + : options.anthropic_api_key + ? options.anthropic_models + : options.openai_models - debug = options.debug ? (options.debug == 'true') : debug + debug = options.debug ? (options.debug === 'true') : debug if (debug) { console.log(`Using options: ${JSON.stringify(options)}`) @@ -98,10 +98,10 @@ module.exports = async ({github, context, inputs, actionPath}) => { if (debug) { watermark = options.bedrock_aws_iam_role_arn - ? `bedrock debug - ${watermark}` - : options.anthropic_api_key - ? `anthropic debug - ${watermark}` - : `openai debug - ${watermark}` + ? `bedrock debug - ${watermark}` + : options.anthropic_api_key + ? `anthropic debug - ${watermark}` + : `openai debug - ${watermark}` } await submitReview({