Skip to content

Commit

Permalink
feat: add comment-or-create action
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Sep 5, 2024
1 parent c22b3eb commit 5347bec
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
31 changes: 12 additions & 19 deletions .github/workflows/ci-repo-watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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<<EOF"$'\n'"${NEW_REPOSITORIES}"$'\n'EOF >> $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`
67 changes: 67 additions & 0 deletions comment-or-create/action.yml
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 5347bec

Please sign in to comment.