Skip to content

Commit

Permalink
workflow older-than-2y: extrude
Browse files Browse the repository at this point in the history
  • Loading branch information
thypon authored and brave-support-admin committed Jun 7, 2024
1 parent 309f8c6 commit 918bd26
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 51 deletions.
55 changes: 4 additions & 51 deletions .github/workflows/older-than-2y.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,9 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Older Than 2 Years Informer
id: older-than-2y
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.GH_PAT }}
script: |
const org = process.env.GITHUB_REPOSITORY_OWNER;
function formatInMessage(r) {
var pushedAt = new Date(r.pushed_at);
return `- ${r.private ? '😎 ' : ''} ${r.full_name} ${r.html_url}\t🌟 ${r.stargazers_count}🍴${r.forks} - Last pushed ${pushedAt.getFullYear()}/${pushedAt.getMonth()}/${pushedAt.getDay()+1}\n`
}
const v = await github.paginate('GET /orgs/{org}/repos', {
org: org,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
const maxOlderDate = ((d) => d.setDate(d.getDate() - 2*365))(new Date()); // 2 years
const reposOlderThanDate = v.filter(r => r.archived === false).filter(r => r.disabled === false).filter(r => new Date(r.pushed_at) < maxOlderDate)
const forks = reposOlderThanDate.filter(r => r.fork === true)
const nonForks = reposOlderThanDate.filter(r => r.fork === false)
// console.log(reposOlderThanDate[0]) // DEBUG
if (reposOlderThanDate.length == 0) return "";
var message = `${org} has ${reposOlderThanDate.length} outdated repositories.\nConsider archiving them.`
if (nonForks.length !== 0) message += "\n\nRepositories:\n"
for (var i = 0; i < nonForks.length; i++) {
var r = nonForks[i]
message += formatInMessage(r)
}
if (forks.length !== 0) message += "\n\nForks:\n"
for (var i = 0; i < forks.length; i++) {
var r = forks[i]
message += formatInMessage(r)
}
core.setSecret(message);
return message;
- uses: actions-ecosystem/action-slack-notifier@fc778468d09c43a6f4d1b8cccaca59766656996a # v1.1.0
if: ${{ fromJson(steps.older-than-2y.outputs.result) != '' }}
- name: older than 2 years informer
uses: brave/security-action/actions/older-than-2y@main
with:
github_token: ${{ secrets.GH_PAT }}
slack_token: ${{ secrets.HOTSPOTS_SLACK_TOKEN }}
message: |
[older-than-2y] ${{ fromJson(steps.older-than-2y.outputs.result) }}
channel: secops-hotspots
color: blue
verbose: false
debug: false
42 changes: 42 additions & 0 deletions actions/older-than-2y/action.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function formatInMessage (r) {
const pushedAt = new Date(r.pushed_at)
return `- ${r.private ? '😎 ' : ''} ${r.full_name} ${r.html_url}\t🌟 ${r.stargazers_count}🍴${r.forks} - Last pushed ${pushedAt.getFullYear()}/${pushedAt.getMonth()}/${pushedAt.getDay() + 1}\n`
}

module.exports = async ({ github, context, inputs, actionPath, core, debug = false }) => {
const { default: sendSlackMessage } = await import(`${actionPath}/src/sendSlackMessage.js`)

const org = context.repo.owner

const v = await github.paginate('GET /orgs/{org}/repos', {
org,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
const maxOlderDate = ((d) => d.setDate(d.getDate() - 2 * 365))(new Date()) // 2 years
const reposOlderThanDate = v.filter(r => r.archived === false).filter(r => r.disabled === false).filter(r => new Date(r.pushed_at) < maxOlderDate)
const forks = reposOlderThanDate.filter(r => r.fork === true)
const nonForks = reposOlderThanDate.filter(r => r.fork === false)
// console.log(reposOlderThanDate[0]) // DEBUG

if (reposOlderThanDate.length === 0) return ''

let message = `${org} has ${reposOlderThanDate.length} outdated repositories.\nConsider archiving them.`

if (nonForks.length !== 0) message += '\n\nRepositories:\n'
for (let i = 0; i < nonForks.length; i++) {
const r = nonForks[i]
message += formatInMessage(r)
}

if (forks.length !== 0) message += '\n\nForks:\n'
for (let i = 0; i < forks.length; i++) {
const r = forks[i]
message += formatInMessage(r)
}

core.setSecret(message)

if (message.length > 0) { await sendSlackMessage({ debug, username: 'older-than-2y', message: `[older-than-2y] ${message}`, color: 'blue', channel: '#security-hotspots', token: inputs.slack_token }) }
}
35 changes: 35 additions & 0 deletions actions/older-than-2y/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: older-than-2y
description: Older Than 2 Years Informer
inputs:
github_token:
description: 'GitHub Token'
required: true
slack_token:
description: 'Slack Token'
required: true
debug:
description: 'Debug mode'
required: false
runs:
using: 'composite'
steps:
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: '20.x'
- id: npm
run: cd ${{ github.action_path }}/../..; npm ci
shell: bash
- name: Older Than 2 Years Informer
id: older-than-2y
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
DEBUG: ${{ (inputs.debug == 'true' || runner.debug) && 'true' || 'false'}}
with:
github-token: ${{ inputs.github_token }}
script: |
const actionPath = '${{ github.action_path }}/../../'
const inputs = ${{ toJson(inputs) }}
const script = require('${{ github.action_path }}/action.cjs')
await script({github, context, inputs, actionPath, core,
debug: process.env.DEBUG === 'true'})

0 comments on commit 918bd26

Please sign in to comment.