diff --git a/.github/workflows/check_pr.yml b/.github/workflows/check_pr.yml index b87471f..ef49616 100644 --- a/.github/workflows/check_pr.yml +++ b/.github/workflows/check_pr.yml @@ -10,8 +10,7 @@ on: permissions: pull-requests: write - contents: write - id-token: write + # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: @@ -35,8 +34,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - with: - token: ${{ secrets.BOT_TOKEN_GITHUB }} - name: Formatting id: format @@ -46,14 +43,35 @@ jobs: path: . fail-on-changes: false - - name: Commit - shell: bash - run: | - git add . - git config --global user.email "github-bot@pagopa.it" - git config --global user.name "pagopa-github-bot" - git commit -m "Auto-Formatting" || exit 0 - git push origin ${{ github.ref_name}} + - uses: actions/github-script@v6.3.3 + if: steps.format.outcome != 'success' + 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 update `openapi.json` and format the code. Consider to use pre-commit to format the code.' + }) + core.setFailed('Format your code.') check_size: runs-on: ubuntu-latest @@ -99,30 +117,53 @@ jobs: console.log('ignored lines: ' + ignored + ' , consider changes: ' + changes); } - if (changes < 200){ - github.rest.issues.addLabels({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - labels: ['size/small'] + var labels = await github.rest.issues.listLabelsOnIssue({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo + }); + + 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' }) - - - var labels = await github.rest.issues.listLabelsOnIssue({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo - }); - - if (labels.data.find(label => label.name == 'size/large')){ + } + 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/large' + 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({ @@ -132,22 +173,6 @@ jobs: labels: ['size/large'] }) - 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 - }) - } - } - github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, @@ -155,20 +180,5 @@ jobs: 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._' }) - var labels = await github.rest.issues.listLabelsOnIssue({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo - }); - - 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' - }) - } - } diff --git a/.github/workflows/update_code.yml b/.github/workflows/update_code.yml new file mode 100644 index 0000000..ed41d83 --- /dev/null +++ b/.github/workflows/update_code.yml @@ -0,0 +1,86 @@ +name: Update Code + +on: + issue_comment: + types: [created, edited] + + +permissions: + contents: write + pull-requests: write + issues: write + +jobs: + update: + name: Update Openapi and Formatting + runs-on: ubuntu-latest + if: ${{ contains(github.event.comment.body, 'update_code') }} + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + token: ${{ secrets.BOT_TOKEN_GITHUB }} + + - name: Checkout Pull Request + run: hub pr checkout ${{ github.event.issue.number }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + + - name: Update OpenApi/Swagger file + run: | + cd ./openapi + chmod +x ./generate_openapi.sh + ./generate_openapi.sh + + - name: Formatting + id: format + uses: findologic/intellij-format-action@main + with: + path: . + fail-on-changes: false + + - name: Commit files + run: | + git config --local user.email "pagopa-github-bot@pagopa.it" + git config --local user.name "pagopa-github-bot" + git commit -a -m "Formatting" + git push + + + notify: + needs: [ update ] + runs-on: ubuntu-latest + name: Notify + if: ${{ always() && contains(needs.*.result, 'failure') }} + steps: + - name: Notify if Failure + uses: actions/github-script@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('Update Code is failed. Please retry.')){ + 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: 'Update Code is failed. Please retry.' + }) + core.setFailed('Update Code is failed. Please retry.')