|
| 1 | +name: 'Review Markdown for Discriminatory Language with ChatGPT' |
| 2 | +description: 'Reviews Markdown files in pull requests for potentially discriminatory language and provides suggestions for improvements' |
| 3 | +author: 'Ben Greenberg' |
| 4 | +inputs: |
| 5 | + openai_api_key: |
| 6 | + description: 'OpenAI API key for accessing the GPT model' |
| 7 | + required: true |
| 8 | + github_token: |
| 9 | + description: 'GitHub token for accessing the repository and pull request information' |
| 10 | + required: true |
| 11 | +outputs: |
| 12 | + suggestions: |
| 13 | + description: 'A list of suggestions for improving the Markdown files in the pull request' |
| 14 | +runs: |
| 15 | + using: 'composite' |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v2 |
| 19 | + |
| 20 | + - name: Download pre-built Rust script from remote repository |
| 21 | + run: | |
| 22 | + # Fetch the latest release information from the GitHub API |
| 23 | + release_info=$(curl -s -H "Accept: application/vnd.github+json" https://api.github.com/repos/bencgreenberg/github-action-gpt-language-check/releases/latest) |
| 24 | + |
| 25 | + # Extract the binary's download URL using 'jq' |
| 26 | + binary_url=$(echo $release_info | jq -r '.assets[] | select(.name == "github-action-gpt-language-check") | .browser_download_url') |
| 27 | +
|
| 28 | + # Download the binary using 'wget' |
| 29 | + wget "$binary_url" -O github-action-gpt-language-check |
| 30 | +
|
| 31 | + # Make the binary executable |
| 32 | + chmod +x github-action-gpt-language-check |
| 33 | + shell: bash |
| 34 | + |
| 35 | + - name: Run Rust script for each markdown file |
| 36 | + env: |
| 37 | + OPENAI_API_KEY: ${{ inputs.openai_api_key }} |
| 38 | + GITHUB_TOKEN: ${{ inputs.github_token }} |
| 39 | + run: | |
| 40 | + suggestions="" |
| 41 | + pr_files=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github+json" https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/${{ github.event.number }}/files | jq -r '.[].filename') |
| 42 | + for file in $pr_files; do |
| 43 | + if [[ $file == *.md ]]; then |
| 44 | + echo "Reviewing: $file" |
| 45 | + suggestion=$(./github-action-gpt-language-check "$file") |
| 46 | + if [[ ! -z "$suggestion" ]]; then |
| 47 | + suggestions+="- $file: $suggestion\n" |
| 48 | + echo "Suggestion found" |
| 49 | + echo "SUGGESTION_FOUND=true" >> $GITHUB_ENV |
| 50 | + fi |
| 51 | + fi |
| 52 | + done |
| 53 | + # Use printf to set SUGGESTIONS environment variable |
| 54 | + printf 'SUGGESTIONS<<EOF\n%s\nEOF' "$suggestions" >> $GITHUB_ENV |
| 55 | +
|
| 56 | + - name: Add a comment to the pull request |
| 57 | + if: ${{ env.SUGGESTION_FOUND == 'true' }} |
| 58 | + uses: actions/github-script@v5 |
| 59 | + with: |
| 60 | + github-token: ${{ inputs.github_token }} |
| 61 | + script: | |
| 62 | + const suggestionsList = process.env.SUGGESTIONS.split('\n').filter(suggestion => suggestion.trim() !== ''); |
| 63 | + let suggestionsMarkdown = '## Suggested Changes\n'; |
| 64 | + for (const suggestion of suggestionsList) { |
| 65 | + const [file, ...suggestionText] = suggestion.split(':'); |
| 66 | + suggestionsMarkdown += `### Reviewed File: **${file.trim()}**\n\n`; |
| 67 | + suggestionsMarkdown += `- ${suggestionText.join(':').trim()}\n\n`; |
| 68 | + } |
| 69 | + github.rest.issues.createComment({ |
| 70 | + issue_number: context.issue.number, |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + body: suggestionsMarkdown |
| 74 | + }); |
0 commit comments