Skip to content

Commit

Permalink
Fixing report seated PRs (#30120)
Browse files Browse the repository at this point in the history
  • Loading branch information
victoralfaro-dotcms authored Sep 25, 2024
1 parent e66f8b9 commit 04d4666
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .github/data/slack-mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,9 @@
{
"github_user": "valentinogiardino",
"slack_id": "U0790HFL64R"
},
{
"github_user": "rsh1k",
"slack_id": "U04UP78B6BS"
}
]
45 changes: 31 additions & 14 deletions .github/workflows/cicd_scheduled_notify-seated-prs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'Notify about seated PRs'
name: 'Report seated PRs'
on:
schedule:
- cron: '0 10 * * *'
Expand All @@ -11,7 +11,7 @@ on:
default: U0125JCDSSE
env:
PR_DAY_THRESHOLD: 3
DRAFT_PR_DAY_THRESHOLD: 5
DRAFT_PR_DAY_THRESHOLD: 2
REPO: core

jobs:
Expand All @@ -33,7 +33,7 @@ jobs:
script: |
const day = new Date().getDay();
console.log(new Date());
if (day === 0 || day === 6) {
if (!'${{ inputs.current_user}}' && (day === 0 || day === 6)) {
console.log('It\'s (happy) weekend, not sending any notifications');
process.exit(0);
}
Expand Down Expand Up @@ -66,12 +66,21 @@ jobs:
return await github.paginate(opts);
};
const dateFormat = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
const nowFormat = dateFormat(now);
const isPrSeated = (pr) => {
const createdAt = new Date(Date.parse(pr.created_at));
console.log(`Now: ${now} / CreatedAt: ${createdAt}`);
console.log(`Now: ${nowFormat} / PR [${pr.number}] created at: ${dateFormat(createdAt)}`);
let weekdaysCount = 0;
for (let date = new Date(createdAt); date <= now; date.setDate(date.getDate() + 1)) {
for (let date = createdAt ; date <= now; date.setDate(date.getDate() + 1)) {
const dayOfWeek = date.getDay();
if (dayOfWeek !== 0 && dayOfWeek !== 6) {
weekdaysCount++;
Expand All @@ -87,6 +96,8 @@ jobs:
return;
}
console.log(`Detected PR [${pr.number}] ([${pr.user.login}) has been seated enough`);
let userPrs = seatedPrs.find(pr => pr.login === login);
if (!userPrs) {
userPrs = {
Expand Down Expand Up @@ -115,6 +126,7 @@ jobs:
};
const prs = await fetchOpenPrs();
prs.forEach(pr => console.log(`[${pr.number}] -> [${pr.user.login}, ${dateFormat(new Date(Date.parse(pr.created_at)))}]`));
console.log(`PRs size: [${prs.length}]`);
prs.forEach(handlePr);
Expand Down Expand Up @@ -160,18 +172,21 @@ jobs:
const prDayThreshold = ${{ env.PR_DAY_THRESHOLD }};
const draftPrDayThreshold = ${{ env.DRAFT_PR_DAY_THRESHOLD }};
const seatedPrs = ${{ needs.resolve-seated-prs.outputs.seated_prs }}
const mappings = ${{ needs.slack-channel-resolver.outputs.mappings_json }};
const members = ${{ needs.resolve-seated-prs.outputs.members_json }};
const channels = ${{ needs.slack-channel-resolver.outputs.channel_ids }};

const foundMapping = mappings.find(mapping => mapping.slack_id === member)
if (!foundMapping) {
core.warning(`Slack user Id [${member}] cannot be found, exiting`);
process.exit(0);
}

const members = ${{ needs.resolve-seated-prs.outputs.members_json }}
const channels = ${{ needs.slack-channel-resolver.outputs.channel_ids }}
console.log(JSON.stringify(members, null, 2));
console.log(JSON.stringify(channels, null, 2));
core.setOutput('guthub_user', foundMapping.github_user);
console.log(`Members: ${JSON.stringify(members, null, 2)}`);
console.log(`Channels: ${JSON.stringify(channels, null, 2)}`);
console.log(`Found mapping: ${JSON.stringify(foundMapping, null, 2)}`);

const login = foundMapping.github_user;
const userPrs = seatedPrs.find(pr => pr.login === login);
Expand All @@ -191,20 +206,22 @@ jobs:
}

message += `\n\nYou can always check your PRs at: https://github.com/${{ github.repository_owner }}/${{ env.REPO }}/pulls/${login}`

core.setOutput('message', message);
- name: Notify member
if: success() && steps.build-message.outputs.message != ''
shell: bash
run: |
channel=${{ matrix.member }}
chat_url='https://slack.com/api/chat.postMessage'
echo "Sending notification to [${{ steps.build-message.outputs.github_user }}] (${channel})"
echo "Sending notification to ${channel}"
if [[ -x '${{ inputs.current_user }}' || "${channel}" == '${{ inputs.current_user }}' ]]; then
if [[ -z '${{ inputs.current_user }}' || "${channel}" == '${{ inputs.current_user }}' ]]; then
echo "Posting notification for [${channel}] to ${chat_url}"
curl -X POST \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
-d "{ \"channel\":\"${channel}\",\"text\":\"${{ steps.build-message.outputs.message }}\" }" \
-s \
https://slack.com/api/chat.postMessage
${chat_url}
fi

0 comments on commit 04d4666

Please sign in to comment.