JavaScript Fetch API For Beginners – Explained With Code Examples #41
This file contains 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
on: | |
issue_comment: | |
types: [created] | |
name: Issue Comments | |
permissions: | |
issues: write | |
jobs: | |
validate_command: | |
name: Validate command | |
runs-on: ubuntu-latest | |
# Run the job only if the new comment starts with '/' | |
if: | | |
!github.event.issue.pull_request && startsWith(github.event.comment.body, '/') | |
outputs: | |
valid_command: ${{ steps.command_check.outputs.valid_command }} | |
command: ${{ steps.command_check.outputs.command }} | |
target_status: ${{ steps.command_check.outputs.target_status }} # Which status to move the card to | |
steps: | |
- name: Validate command | |
id: command_check | |
run: | | |
if [[ "${{ github.event.comment.body }}" == /translate* ]]; then | |
echo "::set-output name=command::translate" | |
echo "::set-output name=target_status::in Translation" | |
echo "::set-output name=valid_command::true" | |
elif [[ "${{ github.event.comment.body }}" == /review* ]]; then | |
echo "::set-output name=command::review" | |
echo "::set-output name=target_status::in Review" | |
echo "::set-output name=valid_command::true" | |
else | |
echo "::warning::Invalid command. The comment must start with /translate or /review." | |
echo "::set-output name=valid_command::false" | |
fi | |
# Reply to the user if the command was invalid | |
- name: Feedback for invalid command | |
if: steps.command_check.outputs.valid_command != 'true' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const issueComment = ` | |
@${{ github.event.comment.user.login }} The command was invalid. The comment must start with /translate or /review. | |
`; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: issueComment | |
}); | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
get_project_card_info: | |
name: Get project card information | |
needs: validate_command | |
if: needs.validate_command.outputs.valid_command == 'true' | |
runs-on: ubuntu-latest | |
outputs: | |
cardId: ${{ steps.get_card.outputs.cardId }} | |
projectUrl: ${{ steps.get_card.outputs.projectUrl }} | |
projectName: ${{ steps.get_card.outputs.projectName }} | |
steps: | |
- name: Get project card | |
id: get_card | |
uses: actions/github-script@v7 | |
env: | |
ISSUE_NODE_ID: ${{ github.event.issue.node_id }} | |
with: | |
script: | | |
const query = ` | |
query ($id: ID!) { | |
node(id: $id) { | |
... on Issue { | |
projectsV2(first: 1) { | |
edges { | |
node { | |
id | |
url | |
title | |
items(first: 100) { | |
edges { | |
node { | |
id | |
content { | |
... on Issue { | |
id | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`; | |
const variables = { | |
id: process.env.ISSUE_NODE_ID | |
}; | |
const result = await github.graphql(query, variables); | |
console.log(`Result: ${JSON.stringify(result)}`); | |
const cardId = result.node.projectsV2.edges[0].node.items.edges.find(edge => edge.node.content.id === process.env.ISSUE_NODE_ID).node.id; | |
const projectUrl = result.node.projectsV2.edges[0].node.url; | |
const projectName = result.node.projectsV2.edges[0].node.title; | |
console.log(`Card ID: ${cardId}`); | |
console.log(`Project URL: ${projectUrl}`); | |
console.log(`Project Name: ${projectName}`); | |
core.setOutput('cardId', cardId); | |
core.setOutput('projectUrl', projectUrl); | |
core.setOutput('projectName', projectName); | |
github-token: ${{ secrets.MOVE_CARDS_TOKEN }} | |
assign_issue: | |
name: Assign issue | |
needs: [validate_command, get_project_card_info] | |
if: needs.validate_command.outputs.valid_command == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout # Prepare to run the following scripts | |
uses: actions/checkout@v3 | |
- name: Assign issue to the commenter as translator | |
if: needs.validate_command.outputs.command == 'translate' | |
uses: actions/github-script@v7 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
script: | | |
const script = require('./scripts/assignTranslator.js'); | |
await script({github, context, core}); | |
- name: Assign issue to the reviewer | |
if: needs.validate_command.outputs.command == 'review' | |
uses: actions/github-script@v7 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
script: | | |
const script = require('./scripts/assignReviewer.js'); | |
const projectName = '${{ needs.get_project_card_info.outputs.projectName }}'; | |
await script({github, context, core, projectName}); | |
update_project_card: | |
name: Update project card | |
needs: [validate_command, get_project_card_info, assign_issue] | |
if: needs.validate_command.outputs.valid_command == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Update project card status | |
uses: titoportas/[email protected] | |
with: | |
project-url: ${{ needs.get_project_card_info.outputs.projectUrl }} | |
github-token: ${{ secrets.MOVE_CARDS_TOKEN }} | |
item-id: ${{ needs.get_project_card_info.outputs.cardId }} | |
field-keys: Status | |
field-values: ${{ needs.validate_command.outputs.target_status }} |