Create a new issue from Datadog #76
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Create a new issue from Datadog | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| issueId: | |
| description: 'Datadog issue ID' | |
| required: true | |
| type: string | |
| issueLink: | |
| description: 'Datadog issue link' | |
| required: true | |
| type: string | |
| errorType: | |
| description: 'Error type' | |
| required: true | |
| type: string | |
| errorMessage: | |
| description: 'Error message' | |
| required: true | |
| type: string | |
| errorStack: | |
| description: 'Error stack' | |
| required: true | |
| type: string | |
| permissions: | |
| issues: write | |
| contents: read | |
| jobs: | |
| create-issue: | |
| runs-on: ubuntu-latest | |
| name: Create a new issue from Datadog | |
| steps: | |
| - name: Create a new issue | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const decodeHtmlEntities = (str) => (str | |
| .replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(dec)) | |
| .replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCodePoint(parseInt(hex, 16))) | |
| .replace(/&/g, '&') | |
| .replace(/</g, '<') | |
| .replace(/>/g, '>') | |
| .replace(/"/g, '"') | |
| .replace(/'/g, "'") | |
| ); | |
| const issueId = ${{ toJSON(inputs.issueId) }}; | |
| const issueLink = ${{ toJSON(inputs.issueLink) }}; | |
| const errorType = ${{ toJSON(inputs.errorType) }}; | |
| const errorMessage = decodeHtmlEntities( ${{ toJSON(inputs.errorMessage) }} ); | |
| const errorStack = ${{ toJSON(inputs.errorStack) }}; | |
| const title = `${errorType}: ${errorMessage.substring(0,120)}${errorMessage.length > 120 ? '...' : ''}`; | |
| const body = `${errorType}\n\n${errorMessage}\n\n\`\`\`${errorStack}\n\`\`\`\n\n[see Datadog ${issueId}](${issueLink}) 🔒`; | |
| const existingIssues = await github.rest.search.issuesAndPullRequests({ | |
| q: `repo:${context.repo.owner}/${context.repo.repo} is:issue "${issueId}"`, | |
| per_page: 10 | |
| }); | |
| console.log('Found ', JSON.stringify(existingIssues.data, null, 2)); | |
| const existingCount = existingIssues.data.total_count; | |
| if (existingCount > 0) { | |
| const existingIssue = existingIssues.data.items?.[0]; | |
| console.log(`Issue already exists: ${existingIssue.html_url}`); | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: existingIssue.number, | |
| body | |
| }); | |
| await github.rest.issues.update({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: existingIssue.number, | |
| state: 'open', | |
| state_reason: 'reopened' | |
| }); | |
| return; | |
| } | |
| const newIssue = await github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title, | |
| body, | |
| labels: ['bug'] | |
| }); | |