|
| 1 | +name: Add Adopter |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + issues: write |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + add-adopter: |
| 14 | + if: contains(github.event.issue.labels.*.name, 'new-adopter') |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Parse issue and create PR |
| 21 | + uses: actions/github-script@v7 |
| 22 | + with: |
| 23 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + script: | |
| 25 | + const issue = context.payload.issue; |
| 26 | + const body = issue.body || ''; |
| 27 | +
|
| 28 | + // Parse structured fields from issue body |
| 29 | + function parseField(body, heading) { |
| 30 | + const regex = new RegExp(`### ${heading}\\s*\\n\\s*([\\s\\S]*?)(?=\\n### |$)`); |
| 31 | + const match = body.match(regex); |
| 32 | + return match ? match[1].trim() : ''; |
| 33 | + } |
| 34 | +
|
| 35 | + const orgName = parseField(body, 'Organization Name'); |
| 36 | + const orgUrl = parseField(body, 'Organization URL'); |
| 37 | + const contact = parseField(body, 'Contact'); |
| 38 | + const description = parseField(body, 'Description of Use'); |
| 39 | +
|
| 40 | + // Validate fields |
| 41 | + const errors = []; |
| 42 | + if (!orgName) errors.push('- **Organization Name** is missing'); |
| 43 | + if (!orgUrl) errors.push('- **Organization URL** is missing'); |
| 44 | + if (!orgUrl.startsWith('https://')) errors.push('- **Organization URL** must start with `https://`'); |
| 45 | + if (!contact) errors.push('- **Contact** is missing'); |
| 46 | + if (!description) errors.push('- **Description of Use** is missing'); |
| 47 | +
|
| 48 | + if (errors.length > 0) { |
| 49 | + await github.rest.issues.createComment({ |
| 50 | + owner: context.repo.owner, |
| 51 | + repo: context.repo.repo, |
| 52 | + issue_number: issue.number, |
| 53 | + body: `👋 Thanks for your interest in being listed as a Parseable adopter!\n\nUnfortunately, there were some issues with your submission:\n\n${errors.join('\n')}\n\nPlease close this issue and [open a new one](https://github.com/${context.repo.owner}/${context.repo.repo}/issues/new?template=add-adopter.yml) with the corrected information.` |
| 54 | + }); |
| 55 | + await github.rest.issues.addLabels({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + issue_number: issue.number, |
| 59 | + labels: ['invalid'] |
| 60 | + }); |
| 61 | + return; |
| 62 | + } |
| 63 | +
|
| 64 | + // Read USERS.md and check for duplicates |
| 65 | + const { data: fileData } = await github.rest.repos.getContent({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + path: 'USERS.md', |
| 69 | + ref: 'main' |
| 70 | + }); |
| 71 | + const usersContent = Buffer.from(fileData.content, 'base64').toString('utf-8'); |
| 72 | + const orgNameLower = orgName.toLowerCase().trim(); |
| 73 | +
|
| 74 | + // Parse existing org names from USERS.md for exact match |
| 75 | + const existingOrgs = usersContent.split('\n') |
| 76 | + .filter(line => line.startsWith('|')) |
| 77 | + .map(line => { |
| 78 | + const match = line.match(/\|\s*\[([^\]]+)\]/); |
| 79 | + return match ? match[1].toLowerCase().trim() : null; |
| 80 | + }) |
| 81 | + .filter(Boolean); |
| 82 | +
|
| 83 | + if (existingOrgs.includes(orgNameLower)) { |
| 84 | + await github.rest.issues.createComment({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + issue_number: issue.number, |
| 88 | + body: `👋 Thanks for your interest!\n\nIt looks like **${orgName}** is already listed in our adopters list. If you need to update the existing entry, please open a PR directly.\n\nClosing this issue as duplicate.` |
| 89 | + }); |
| 90 | + await github.rest.issues.addLabels({ |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + issue_number: issue.number, |
| 94 | + labels: ['duplicate'] |
| 95 | + }); |
| 96 | + await github.rest.issues.update({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + issue_number: issue.number, |
| 100 | + state: 'closed' |
| 101 | + }); |
| 102 | + return; |
| 103 | + } |
| 104 | +
|
| 105 | + // Sanitize user input for markdown table |
| 106 | + const sanitize = (str) => str.replace(/\|/g, '\\|').replace(/\n/g, ' ').trim(); |
| 107 | +
|
| 108 | + // Build contact link |
| 109 | + let contactCell = sanitize(contact); |
| 110 | + if (contact.startsWith('@')) { |
| 111 | + const handle = contact.substring(1).trim(); |
| 112 | + contactCell = `[@${handle}](https://github.com/${handle})`; |
| 113 | + } |
| 114 | +
|
| 115 | + // Append new row to USERS.md |
| 116 | + const safeDesc = sanitize(description); |
| 117 | + const safeOrgName = sanitize(orgName); |
| 118 | + const newRow = `| [${safeOrgName}](${orgUrl}) | ${contactCell} | ${safeDesc} |`; |
| 119 | + const updatedContent = usersContent.trimEnd() + '\n' + newRow + '\n'; |
| 120 | +
|
| 121 | + // Create branch |
| 122 | + const slug = orgName.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, ''); |
| 123 | + const branchName = `adopter/add-${slug}-${issue.number}`; |
| 124 | +
|
| 125 | + const { data: ref } = await github.rest.git.getRef({ |
| 126 | + owner: context.repo.owner, |
| 127 | + repo: context.repo.repo, |
| 128 | + ref: 'heads/main' |
| 129 | + }); |
| 130 | +
|
| 131 | + await github.rest.git.createRef({ |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + ref: `refs/heads/${branchName}`, |
| 135 | + sha: ref.object.sha |
| 136 | + }); |
| 137 | +
|
| 138 | + // Commit updated USERS.md |
| 139 | + await github.rest.repos.createOrUpdateFileContents({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + path: 'USERS.md', |
| 143 | + message: `Add ${orgName} to adopters list`, |
| 144 | + content: Buffer.from(updatedContent).toString('base64'), |
| 145 | + sha: fileData.sha, |
| 146 | + branch: branchName |
| 147 | + }); |
| 148 | +
|
| 149 | + // Create PR |
| 150 | + const { data: pr } = await github.rest.pulls.create({ |
| 151 | + owner: context.repo.owner, |
| 152 | + repo: context.repo.repo, |
| 153 | + title: `Add ${orgName} to adopters list`, |
| 154 | + head: branchName, |
| 155 | + base: 'main', |
| 156 | + body: `## New Adopter\n\n| Field | Value |\n|-------|-------|\n| **Organization** | [${orgName}](${orgUrl}) |\n| **Contact** | ${contactCell} |\n| **Description** | ${description} |\n\nCloses #${issue.number}` |
| 157 | + }); |
| 158 | +
|
| 159 | + // Comment on issue |
| 160 | + await github.rest.issues.createComment({ |
| 161 | + owner: context.repo.owner, |
| 162 | + repo: context.repo.repo, |
| 163 | + issue_number: issue.number, |
| 164 | + body: `PR created: ${pr.html_url}\n\nA maintainer will review and merge it shortly. Thanks for using Parseable!` |
| 165 | + }); |
0 commit comments