generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add slash command action to move project cards (#269)
* feat: add slash command action * fix: get project URL from the card
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
on: | ||
issue_comment: | ||
types: [created] | ||
name: Issue Comments | ||
permissions: | ||
issues: write | ||
jobs: | ||
check_comments: | ||
name: Check comments for commands | ||
runs-on: ubuntu-latest | ||
|
||
# Run the jobs only if the new comment starts with '/' | ||
if: | | ||
!github.event.issue.pull_request && startsWith(github.event.comment.body, '/') | ||
steps: | ||
- name: Validate command | ||
id: command_check | ||
run: | | ||
if [[ "${{ github.event.comment.body }}" == /translate* ]]; then | ||
echo "COMMAND=translate" >> $GITHUB_ENV | ||
echo "TARGET_STATUS=in Translation" >> $GITHUB_ENV | ||
echo "VALID_COMMAND=true" >> $GITHUB_ENV | ||
elif [[ "${{ github.event.comment.body }}" == /review* ]]; then | ||
echo "COMMAND=review" >> $GITHUB_ENV | ||
echo "TARGET_STATUS=in Review" >> $GITHUB_ENV | ||
echo "VALID_COMMAND=true" >> $GITHUB_ENV | ||
else | ||
echo "::warning::Invalid command. The comment must start with /translate or /review." | ||
echo "VALID_COMMAND=false" >> $GITHUB_ENV | ||
fi | ||
# Reply to the user if the command was invalid | ||
- name: Comment on issue # Notify user if the command was invalid | ||
if: env.VALID_COMMAND == 'false' | ||
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. | ||
If you are not sure how to use the command, reach out to your language lead. Thank you for contributing! | ||
`; | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: issueComment | ||
}); | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# Update the status according to the command | ||
- name: Act on the command # Process valid commands | ||
if: env.VALID_COMMAND == 'true' | ||
run: | | ||
echo "The command is '${{ env.COMMAND }}'" | ||
echo "github.event.issue.node_id is ${{ github.event.issue.node_id }}" | ||
- name: Get project card | ||
if: env.VALID_COMMAND == 'true' | ||
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 | ||
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; | ||
console.log(`Card ID: ${cardId}`); | ||
console.log(`Project URL: ${projectUrl}`); | ||
core.setOutput('cardId', cardId); | ||
core.setOutput('projectUrl', projectUrl); | ||
github-token: ${{ secrets.MOVE_CARDS_TOKEN }} | ||
id: get_card | ||
- name: Update project | ||
if: env.VALID_COMMAND == 'true' | ||
uses: titoportas/[email protected] | ||
with: | ||
project-url: ${{ steps.get_card.outputs.projectUrl }} | ||
github-token: ${{ secrets.MOVE_CARDS_TOKEN }} | ||
item-id: ${{ steps.get_card.outputs.cardId }} | ||
field-keys: Status | ||
field-values: ${{ env.TARGET_STATUS }} |