From d47ce314a2b73f70deade1858fb1bd7a5dc1c8d8 Mon Sep 17 00:00:00 2001 From: Alexande B Date: Wed, 4 Sep 2024 07:57:11 +0200 Subject: [PATCH] feat: add comment-or-create action --- .github/workflows/ci-repo-watcher.yml | 13 ++++-- comment-or-create/action.yml | 64 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 comment-or-create/action.yml diff --git a/.github/workflows/ci-repo-watcher.yml b/.github/workflows/ci-repo-watcher.yml index 6038c3e..2b69edd 100644 --- a/.github/workflows/ci-repo-watcher.yml +++ b/.github/workflows/ci-repo-watcher.yml @@ -53,9 +53,14 @@ jobs: REPOSITOY_MARKDOWN_LIST: ${{ env.NEW_REPOSITORIES }} - if: ${{ env.NEW_REPOSITORIES != 'null' }} - uses: JasonEtco/create-an-issue@v2 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN || secrets.token }} + uses: ./comment-or-create with: + token: ${{ secrets.GITHUB_TOKEN || secrets.token }} + title: New repos in ${{ env.GITHUB_REPOSITORY_OWNER }} found assignees: CAMOBAP - filename: .github/templates/new-repos.md + comment: | + Review new discovered repositories during run {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/actions/runs/{{ env.GITHUB_RUN_ID }}: + + ${{ env.NEW_REPOSITORIES }} + + And add them to `cimas-config/cimas.yml` with the right set of `files` diff --git a/comment-or-create/action.yml b/comment-or-create/action.yml new file mode 100644 index 0000000..6aa2ad5 --- /dev/null +++ b/comment-or-create/action.yml @@ -0,0 +1,64 @@ +name: comment-or-create +description: | + Action to comment or create issue if missing + +inputs: + token: + description: Token + required: true + title: + description: Issue title + required: true + comment: + description: Comment/topic string + required: true + assignees: + description: Usernames to assign + required: true + +outputs: + issue: + description: GitHub issue number + value: ${{ steps.comment-or-create.outputs.issue }} + +runs: + using: "composite" + steps: + - id: comment-or-create + uses: actions/github-script@v7 + with: + github-token: ${{ inputs.token }} + script: | + const workflowUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; + const issueTitle = '${{ inputs.title }}'; + const issueBody = '${{ inputs.comment }}'; + const assignees = '${{ inputs.assignees }}'.split(",").map(user => user.trim()) + + const { data: issues } = await github.rest.issues.listForRepo({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open' + }); + + let issueNumber = undefined; + const existingIssue = issues.find(issue => issue.title === issueTitle); + if (existingIssue) { + issueNumber = existingIssue.number; + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, + body: issueBody + }); + } else { + const newIssue = await github.rest.issues.create({ + owner: context.repo.owner, + repo: context.repo.repo, + title: issueTitle, + body: issueBody, + assignees: assignees + }); + issueNumber = newIssue.data.number; + } + + core.setOutput('issue', issueNumber); \ No newline at end of file