Get GitHub Repos #233
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
name: Get GitHub Repos | |
on: | |
# cronjob trigger | |
schedule: | |
- cron: "0 0 * * *" | |
# manual trigger | |
workflow_dispatch: | |
jobs: | |
get-github-repos: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '20.x' | |
- run: npm install @octokit/core | |
- name: Get Repository Data | |
uses: actions/github-script@v7 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
script: | | |
const { Octokit, App } = require('@octokit/core') | |
const octokit = new Octokit({ | |
auth: `bearer ${process.env.GITHUB_TOKEN}` | |
}) | |
var fs = require('fs'); | |
const query = `query($org: String!) { | |
organization(login: $org) { | |
repositories(first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) { | |
nodes { | |
name | |
description | |
homepageUrl | |
repositoryTopics(first: 5) { | |
nodes { | |
topic { | |
name | |
} | |
} | |
} | |
} | |
} | |
} | |
}`; | |
const variables = { | |
org: 'ibm-client-engineering', | |
}; | |
const repoData = await github.graphql(query, variables) | |
const getProps = require('./.github/workflows/getProps.js') | |
const finalRepoData = await getProps({repoData, octokit}) | |
fs.writeFileSync('repoData.json', JSON.stringify(finalRepoData, null, 2)); | |
- name: Commit and push JSON file | |
run: | | |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
- name: Create Pull Request | |
id: cpr | |
uses: peter-evans/create-pull-request@v3 | |
continue-on-error: true | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
add-paths: | | |
repoData.json | |
commit-message: Add repository data JSON file | |
- name: Enable Pull Request Automerge | |
if: ${{steps.cpr.outcome == 'success' && steps.cpr.outputs.pull-request-operation == 'created'}} | |
uses: peter-evans/enable-pull-request-automerge@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
pull-request-number: ${{ steps.cpr.outputs.pull-request-number }} | |
merge-method: squash | |
- name: Generate a token | |
id: generate-token | |
if: ${{steps.cpr.outcome == 'success' && steps.cpr.outputs.pull-request-operation == 'created'}} | |
uses: actions/create-github-app-token@v1 | |
with: | |
app-id: ${{ vars.SOLUTIONS_HUB_APP_ID }} | |
private-key: ${{ secrets.SOLUTIONS_HUB_APP_KEY }} | |
- name: Auto approve | |
if: ${{steps.cpr.outcome == 'success' && steps.cpr.outputs.pull-request-operation == 'created'}} | |
run: gh pr review --approve "${{ steps.cpr.outputs.pull-request-number }}" | |
env: | |
GH_TOKEN: ${{ steps.generate-token.outputs.token }} |