Skip to content

Commit

Permalink
refactor(github_scripts): improve code formatting and remove unnecess…
Browse files Browse the repository at this point in the history
…ary code\n \n ✨ Removed unused code and improved code formatting for better readability.\n
  • Loading branch information
jacopocarlini committed Jan 24, 2025
1 parent a8698c9 commit dee2980
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 199 deletions.
124 changes: 51 additions & 73 deletions .github/workflows/02_check_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,90 @@ name: Check PR

# Controls when the workflow will run
on:
merge_group:
pull_request:
branches:
- main
- next
types: [ opened, synchronize, labeled, unlabeled, reopened, edited ]


permissions:
contents: read
checks: write
issues: write
pull-requests: write


# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
auto_assign:
name: Auto Assign
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Assign Me
# You may pin to the exact commit or the version.
uses: kentaro-m/auto-assign-action@746a3a558fdd0e061f612ec9f8ff1b8a19c1a115 # v1.2.1
with:
configuration-path: '.github/auto_assign.yml'

check_labels:
name: Check Required Labels
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Checkout code
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4 # required to make the script available for next step
- name: Verify PR Labels
if: ${{ !contains(github.event.pull_request.labels.*.name, 'patch') && !contains(github.event.pull_request.labels.*.name, 'ignore-for-release') }}
if: ${{ !contains(github.event.pull_request.labels.*.name, 'major') && !contains(github.event.pull_request.labels.*.name, 'minor') && !contains(github.event.pull_request.labels.*.name, 'patch') && !contains(github.event.pull_request.labels.*.name, 'patch') && !contains(github.event.pull_request.labels.*.name, 'skip') }}
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0 # v6.3.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
var comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});
for (const comment of comments.data) {
if (comment.body.includes('This pull request does not contain a valid label')){
github.rest.issues.deleteComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id
})
}
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'This pull request does not contain a valid label. Please add one of the following labels: `[patch, ignore-for-release]`'
})
core.setFailed('Missing required labels')
const script = require('./.github/workflows/github_scripts/check_required_labels.js')
script({github, context, core})
check_format:
name: Check Format
formatter:
name: Formatter
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
- name: Formatting
id: format
continue-on-error: true
- name: Checkout code
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin' # See 'Supported distributions' for available options
java-version: '21'
- name: Find incorrectly formatted file
uses: axel-op/googlejavaformat-action@v3
with:
args: "--set-exit-if-changed"

- uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0 # v6.3.3
if: steps.format.outcome != 'success'
args: "--replace"
skip-commit: true
- name: Print diffs
run: git --no-pager diff
- name: suggester / google-java-format
uses: reviewdog/action-suggester@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
console.log(context);
var comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});
for (const comment of comments.data) {
console.log(comment);
if (comment.body.includes('Comment this PR with')){
github.rest.issues.deleteComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id
})
}
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Comment this PR with *update_code* to format the code. Consider to use pre-commit to format the code.'
})
core.setFailed('Format your code.')
tool_name: google-java-format
fail_on_error: true

check_size:
runs-on: ubuntu-latest
name: Check Size
if: ${{ github.event_name == 'pull_request' }}
steps:

- name: Dump GitHub context
run: echo $JSON
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
with:
fetch-depth: 0
- name: Check Size
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0 # v6.3.3
env:
JSON: ${{ toJSON(github) }}

- name: Check PR Size
uses: pagopa/github-actions-template/check-pr-size@main
IGNORED_FILES: openapi.json
BRANCH_NAME: ${{ github.head_ref}}
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
ignored_files: 'openapi.json'
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const script = require('./.github/workflows/github_scripts/check_size.js')
await script({github, context, core})
24 changes: 24 additions & 0 deletions .github/workflows/github_scripts/check_required_labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = async ({github, context, core}) => {
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});
for (const comment of comments.data) {
if (comment.body.includes('This pull request does not contain a valid label')) {
github.rest.issues.deleteComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id
})
}
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'This pull request does not contain a valid label. Please add one of the following labels: `[major, minor, patch, patch, skip]`'
})
core.setFailed('Missing required labels')
}
99 changes: 99 additions & 0 deletions .github/workflows/github_scripts/check_size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module.exports = async ({github, context, core}) => {
const additions = context.payload.pull_request.additions || 0
const deletions = context.payload.pull_request.deletions || 0
let changes = additions + deletions;
console.log('additions: ' + additions + ' + deletions: ' + deletions + ' = total changes: ' + changes);

const {IGNORED_FILES, BRANCH_NAME} = process.env
const ignored_files = IGNORED_FILES.trim().split(',').filter(word => word.length > 0);
if (ignored_files.length > 0) {
var ignored = 0
const execSync = require('child_process').execSync;
for (const file of IGNORED_FILES.trim().split(',')) {

const ignored_additions_str = execSync('git --no-pager diff --numstat origin/main..origin/' + BRANCH_NAME + ' | grep ' + file + ' | cut -f 1', {encoding: 'utf-8'})

Check warning

Code scanning / CodeQL

Indirect uncontrolled command line Medium

This command depends on an unsanitized
environment variable
.
const ignored_deletions_str = execSync('git --no-pager diff --numstat origin/main..origin/' + BRANCH_NAME + ' | grep ' + file + ' | cut -f 2', {encoding: 'utf-8'})

Check warning

Code scanning / CodeQL

Indirect uncontrolled command line Medium

This command depends on an unsanitized
environment variable
.

const ignored_additions = ignored_additions_str.split('\n').map(elem => parseInt(elem || 0)).reduce(
(accumulator, currentValue) => accumulator + currentValue,
0);
const ignored_deletions = ignored_deletions_str.split('\n').map(elem => parseInt(elem || 0)).reduce(
(accumulator, currentValue) => accumulator + currentValue,
0);

ignored += ignored_additions + ignored_deletions;
}
changes -= ignored
console.log('ignored lines: ' + ignored + ' , consider changes: ' + changes);
}

var labels = await github.rest.issues.listLabelsOnIssue({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});

if (changes <= 400) {
if (labels.data.find(label => label.name === 'size/large')) {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: 'size/large'
})
}
}

if (changes >= 200) {
if (labels.data.find(label => label.name === 'size/small')) {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: 'size/small'
})
}
}

var comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});
for (const comment of comments.data) {
if (comment.body.includes('This PR exceeds the recommended size')) {
github.rest.issues.deleteComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id
})
}
}

if (changes < 200) {
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['size/small']
})
}

if (changes > 400) {
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['size/large']
})

github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'This PR exceeds the recommended size of 400 lines. Please make sure you are NOT addressing multiple issues with one PR. _Note this PR might be rejected due to its size._'
})

}
}
10 changes: 3 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ repos:
language_version: python3
stages: [ commit ]

- repo: local
- repo: https://github.com/pagopa/google-style-precommit-hook
rev: be92ca3b151dda05fb027ee738509ebf68786bc1
hooks:
- id: google-style-java
name: Google Java Code Style for Java
description: Formats code in Google's Java codestyle.
entry: ./format-code.sh
language: script
files: \.java$ # We don't technically need this, as the script will filter for us, but this will allow the hook to be skipped if no Java is changed.
- id: google-style-java
16 changes: 0 additions & 16 deletions format-code.sh

This file was deleted.

Loading

0 comments on commit dee2980

Please sign in to comment.