From 5347bec16267b8cfbdf52f4145d88c5fd1aa52b2 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 | 31 +++++-------- comment-or-create/action.yml | 67 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 19 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..839e1a6 100644 --- a/.github/workflows/ci-repo-watcher.yml +++ b/.github/workflows/ci-repo-watcher.yml @@ -37,25 +37,18 @@ jobs: NEW_REPOSITORIES=$(cat new-repos.yml) echo "New repositories:" echo "$NEW_REPOSITORIES" - echo "NEW_REPOSITORIES=${NEW_REPOSITORIES//$'\n'/'\n'}" >> $GITHUB_ENV + echo "NEW_REPOSITORIES<> $GITHUB_OUTPUT - - if: ${{ env.NEW_REPOSITORIES != 'null' }} - name: Download template if missing - run: | - if [ ! -f .github/templates/new-repos.md ] - then - mkdir -p .github/templates - wget https://raw.githubusercontent.com/metanorma/ci/main/.github/templates/new-repos.md -O .github/templates/new-repos.md - fi - - sed -i -e "s|REPOSITOY_MARKDOWN_LIST|${REPOSITOY_MARKDOWN_LIST}|g" .github/templates/new-repos.md - env: - 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 }} + - name: Create or update issue + if: ${{ steps.cimas-diff.outputs.NEW_REPOSITORIES != 'null' }} + uses: ./comment-or-create with: + token: ${{ secrets.GITHUB_TOKEN || secrets.token }} + title: New repos in ${{ 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 }}: + + ${{ steps.cimas-diff.outputs.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..a7a9962 --- /dev/null +++ b/comment-or-create/action.yml @@ -0,0 +1,67 @@ +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 + env: + INPUT_TITLE: ${{ inputs.title }} + INPUT_COMMENT: ${{ inputs.comment }} + INPUT_ASSIGNEES: ${{ inputs.assignees }} + uses: actions/github-script@v7 + with: + github-token: ${{ inputs.token }} + script: | + const issueTitle = process.env.INPUT_TITLE; + const issueBody = process.env.INPUT_COMMENT; + const assignees = process.env.INPUT_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; + 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);