Skip to content

Commit

Permalink
ci: PLT-298: Add PR drafter
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabelonogov committed Apr 5, 2024
1 parent d668350 commit a1fc575
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/draft-pr-converter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: "Convert stale PR's to drafts"

on:
schedule:
- cron: '0 */1 * * *' # At minute 0 past every hour.
workflow_dispatch:

env:
DRAFT_PROTECT_LABEL: "draft-protect"

jobs:
convert_stale_prs:
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- uses: hmarr/[email protected]

- id: get_timestamp
name: Get timestamp
shell: bash
run: echo "ts=$(date -d '10 hours ago' +"%Y-%m-%dT%H:%M:%S")" >> $GITHUB_OUTPUT

- uses: octokit/[email protected]
name: Get PR older than 10 hours
id: get_stale_prs
env:
GITHUB_TOKEN: ${{ secrets.GIT_PAT }}
with:
query: |
{
search(query: "repo:${{ github.repository }} is:pr is:open draft:false -label:${{ env.DRAFT_PROTECT_LABEL }} updated:<=${{ steps.get_timestamp.outputs.ts }}", type: ISSUE, first: 100) {
issueCount
edges {
node {
... on PullRequest {
number
url
id
updatedAt
}
}
}
}
}
- name: Stale PRs data
run: "echo '${{ steps.get_stale_prs.outputs.data }}'"

- name: Convert to draft
id: mutation_step
shell: bash
env:
GIT_PAT: ${{ secrets.GIT_PAT }}
run: |
set -eux
echo '${{ steps.get_stale_prs.outputs.data }}' > /tmp/stale_pr.json
_pr_list=$(jq -r '.search.edges | map(.node.url) | join("\\n")' < /tmp/stale_pr.json)
if [ -n "$_pr_list" ]; then
echo "pr_list=$_pr_list" >> $GITHUB_OUTPUT
echo "exec=true" >> $GITHUB_OUTPUT
echo "$GIT_PAT" | gh auth login --with-token
for pr_id in $(jq -r '.search.edges[].node.id' < /tmp/stale_pr.json); do
gh api graphql -F id="${pr_id}" -f query='
mutation($id: ID!) {
convertPullRequestToDraft(input: { pullRequestId: $id }) {
pullRequest {
id
number
isDraft
}
}
}
'
done
fi
- name: Post to a Slack channel
id: slack
if: ${{ steps.mutation_step.outputs.exec == 'true' }}
uses: slackapi/[email protected]
with:
channel-id: 'C02LMULF4NA'
payload: '{ "type": "mrkdwn", "text": "${{ env.SLACK_MESSAGE }}" }'
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_MESSAGE: >-
*Drafted PR's*\n
${{ steps.mutation_step.outputs.pr_list }}

0 comments on commit a1fc575

Please sign in to comment.