GeezerVison is showing github.com — fix(ci): move conditionals from yaml to code · phipag/powertools-lambda-python@782b0bf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +module.exports = Object.freeze({ | | 2 | + /** @type {string} */ | | 3 | + "PR_ACTION": process.env.PR_ACTION || "", | | 4 | + /** @type {string} */ | | 5 | + "PR_AUTHOR": process.env.PR_AUTHOR || "", | | 6 | + /** @type {string} */ | | 7 | + "PR_BODY": process.env.PR_BODY || "", | | 8 | + /** @type {string} */ | | 9 | + "PR_TITLE": process.env.PR_TITLE || "", | | 10 | + /** @type {number} */ | | 11 | + "PR_NUMBER": process.env.PR_NUMBER || 0, | | 12 | + /** @type {boolean} */ | | 13 | + "PR_IS_MERGED": process.env.PR_IS_MERGED || false, | | 14 | + /** @type {string[]} */ | | 15 | + "IGNORE_AUTHORS": ["dependabot[bot]", "markdownify[bot]"], | | 16 | + /** @type {string} */ | | 17 | + "BLOCK_LABEL": "do-not-merge", | | 18 | + /** @type {string} */ | | 19 | + "BLOCK_REASON_LABEL": "need-issue", | | 20 | +}); |
Collapse file Copy file name to clipboard +26-9Lines changed: 26 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +const { | | 2 | + PR_ACTION, | | 3 | + PR_AUTHOR, | | 4 | + PR_BODY, | | 5 | + PR_NUMBER, | | 6 | + IGNORE_AUTHORS, | | 7 | + BLOCK_LABEL, | | 8 | + BLOCK_REASON_LABEL | | 9 | +} = require("./constants") | | 10 | + | 1 | 11 | module.exports = async ({github, context, core}) => { | 2 | | - const prBody = process.env.PR_BODY; | 3 | | - const prNumber = process.env.PR_NUMBER; | 4 | | - const blockLabel = process.env.BLOCK_LABEL; | 5 | | - const blockReasonLabel = process.env.BLOCK_REASON_LABEL; | | 12 | + core.debug(`Number: ${PR_BODY}`); | | 13 | + core.debug(`Action: ${PR_ACTION}`); | | 14 | + core.debug(`Author: ${PR_AUTHOR}`); | | 15 | + core.debug(`Body: ${PR_BODY}`); | 6 | 16 |
| 7 | | - const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/; | | 17 | + if (IGNORE_AUTHORS.includes(PR_AUTHOR)) { | | 18 | + return core.notice("Author in IGNORE_AUTHORS list; skipping...") | | 19 | + } | 8 | 20 |
| 9 | | - const isMatch = RELATED_ISSUE_REGEX.exec(prBody); | | 21 | + if (PR_ACTION != "opened") { | | 22 | + return core.notice("Only newly open PRs are labelled to avoid spam; skipping") | | 23 | + } | | 24 | + | | 25 | + const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/; | | 26 | + const isMatch = RELATED_ISSUE_REGEX.exec(PR_BODY); | 10 | 27 | if (isMatch == null) { | 11 | 28 | core.info(`No related issue found, maybe the author didn't use the template but there is one.`) | 12 | 29 |
| @@ -15,14 +32,14 @@ module.exports = async ({github, context, core}) => { | 15 | 32 | owner: context.repo.owner, | 16 | 33 | repo: context.repo.repo, | 17 | 34 | body: msg, | 18 | | - issue_number: prNumber, | | 35 | + issue_number: PR_NUMBER, | 19 | 36 | }); | 20 | 37 |
| 21 | 38 | return await github.rest.issues.addLabels({ | 22 | | - issue_number: prNumber, | | 39 | + issue_number: PR_NUMBER, | 23 | 40 | owner: context.repo.owner, | 24 | 41 | repo: context.repo.repo, | 25 | | - labels: [blockLabel, blockReasonLabel] | | 42 | + labels: [BLOCK_LABEL, BLOCK_REASON_LABEL] | 26 | 43 | }) | 27 | 44 | } | 28 | 45 | } |
Collapse file Copy file name to clipboardExpand all lines: .github/workflows/on_opened_pr.yml +6-8Lines changed: 6 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | types: | 7 | 7 | - completed | 8 | 8 |
| 9 | | -env: | 10 | | - BLOCK_LABEL: "do-not-merge" | 11 | | - BLOCK_REASON_LABEL: "need-issue" | 12 | | - IGNORE_AUTHORS: '["dependabot[bot]", "markdownify[bot]"]' | 13 | 9 |
| 14 | 10 | jobs: | 15 | 11 | get_pr_details: | @@ -21,18 +17,20 @@ jobs: | 21 | 17 | token: ${{ secrets.GITHUB_TOKEN }} | 22 | 18 | check_related_issue: | 23 | 19 | needs: get_pr_details | 24 | | - # Maintenance: Refactor condition to the correct env syntax later | 25 | | - if: | | 26 | | - needs.get_pr_details.outputs.prAction == 'opened' | 27 | | - && contains(fromJson('["dependabot[bot]", "markdownify[bot]"]'), needs.get_pr_details.outputs.prAuthor) != true | 28 | 20 | runs-on: ubuntu-latest | 29 | 21 | steps: | | 22 | + - name: Debug outputs | | 23 | + run: echo "Outputs ${{ toJSON(needs.get_pr_details.outputs) }}" | 30 | 24 | - uses: actions/checkout@v3 | 31 | 25 | - name: "Ensure related issue is present" | | 26 | + if: contains('["opened", "edited"]', needs.get_pr_details.outputs.prAction) | | 27 | + if: needs.get_pr_details.outputs.prAction == 'opened' || needs.get_pr_details.outputs.prAction == 'edited' | 32 | 28 | uses: actions/github-script@v6 | 33 | 29 | env: | 34 | 30 | PR_BODY: ${{ needs.get_pr_details.outputs.prBody }} | 35 | 31 | PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }} | | 32 | + PR_ACTION: ${{ needs.get_pr_details.outputs.prAction }} | | 33 | + PR_AUTHOR: ${{ needs.get_pr_details.outputs.prAuthor }} | 36 | 34 | with: | 37 | 35 | github-token: ${{ secrets.GITHUB_TOKEN }} | 38 | 36 | script: | | |
|