From a00779ff5000f3a662a22deb03fbb112f64fe027 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Tue, 8 Feb 2022 11:00:06 +0100 Subject: [PATCH] chore: switch to GitHub action --- .github/workflows/contributors.yml | 22 ++---- npm-shrinkwrap.json | 2 - package.json | 2 - tools/contributors.mjs | 113 ----------------------------- 4 files changed, 7 insertions(+), 132 deletions(-) delete mode 100644 tools/contributors.mjs diff --git a/.github/workflows/contributors.yml b/.github/workflows/contributors.yml index 0a1a61185be..34a01fe1172 100644 --- a/.github/workflows/contributors.yml +++ b/.github/workflows/contributors.yml @@ -7,26 +7,18 @@ jobs: update-contributors: runs-on: ubuntu-latest steps: + - uses: navikt/github-app-token-generator@2d70c12368d1958155af4d283f9f21c9a2a8cb98 + id: get-token + with: + private-key: ${{ secrets.TOKENS_PRIVATE_KEY }} + app-id: ${{ secrets.TOKENS_APP_ID }} - name: Git checkout uses: actions/checkout@v2 with: ref: ${{ github.head_ref }} - - name: Setup Node.js - uses: actions/setup-node@v2 + - uses: netlify/update-contributors-action@main with: - node-version: '*' - cache: 'npm' - cache-dependency-path: 'npm-shrinkwrap.json' - check-latest: true - - name: Install dependencies - run: npm ci --no-audit - # required for linting to pass - - name: Install site dependencies - run: npm run site:build:install - - name: Generate Contributors - run: node ./tools/contributors.mjs ${{ secrets.GITHUB_TOKEN }} - - name: Format the package.json file - run: npm run format:fix:prettier + github-token: ${{ steps.get-token.outputs.token }} - uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: 'chore: update contributors field' diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index ac4a7de4852..4e2a39b66b3 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -126,8 +126,6 @@ "devDependencies": { "@babel/preset-react": "^7.12.13", "@netlify/eslint-config-node": "^5.1.4", - "@octokit/core": "^3.5.1", - "@octokit/plugin-paginate-rest": "^2.17.0", "ava": "^4.0.0", "c8": "^7.11.0", "eslint-plugin-sort-destructure-keys": "^1.3.5", diff --git a/package.json b/package.json index b38c74ca195..4f82223ca62 100644 --- a/package.json +++ b/package.json @@ -309,8 +309,6 @@ "devDependencies": { "@babel/preset-react": "^7.12.13", "@netlify/eslint-config-node": "^5.1.4", - "@octokit/core": "^3.5.1", - "@octokit/plugin-paginate-rest": "^2.17.0", "ava": "^4.0.0", "c8": "^7.11.0", "eslint-plugin-sort-destructure-keys": "^1.3.5", diff --git a/tools/contributors.mjs b/tools/contributors.mjs deleted file mode 100644 index b668cdc5502..00000000000 --- a/tools/contributors.mjs +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/bin/env node -import { execSync } from 'child_process' -import { readFileSync, writeFileSync } from 'fs' -import process from 'process' - -import { Octokit } from '@octokit/core' -import { paginateRest } from '@octokit/plugin-paginate-rest' - -/** - * Parses a contributor string into it's parts - * @param {string} entry - * @returns {name: string, email?: string, web?: string} - */ -const parseContributorString = (entry) => { - let name, email, web - - const matchFull = /^(.+)\s<(.+)>\s\((.+)\)$/gm.exec(entry) - const matchWeb = /^(.+)\s\((.+)\)$/gm.exec(entry) - const matchMail = /^(.+)\s<(.+)>$/gm.exec(entry) - if (matchFull) { - ;[, name, email, web] = matchFull - } else if (matchWeb) { - ;[, name, web] = matchWeb - } else if (matchMail) { - ;[, name, email] = matchMail - } else { - name = entry - } - - return { name, email, web } -} - -/** - * Generates a contributor string out of an entry - * @param {object} entry - * @param {string} entry.name - * @param {string} [entry.email] - * @param {string} [entry.web] - * @returns - */ -const createContributorString = (entry) => - [entry.name, entry.email && `<${entry.email}>`, entry.web && `(${entry.web})`].filter(Boolean).join(' ') - -// read the packageJSON -const packageJson = JSON.parse(readFileSync('package.json', 'utf-8')) -// parse the existing contributors -const existingContributors = packageJson.contributors.map((contributor) => parseContributorString(contributor)) - -// Get a list of email addresses from local git log as they are not -// part of the user information -const mailList = new Map( - execSync(`git log --format='%an⏣%ae'`) - .toString() - .split('\n') - .map((entry) => entry.split('⏣')) - .filter(([key]) => !(key.length === 0 || key.includes('[bot]'))), -) - -const [GITHUB_TOKEN] = process.argv.slice(2) - -if (!GITHUB_TOKEN) { - throw new Error('Please provide the GITHUB_TOKEN as argument to the command: node ./tools/contributors.mjs ') -} - -const PagedOctokit = Octokit.plugin(paginateRest) -const octokit = new PagedOctokit({ auth: GITHUB_TOKEN }) - -const contributorList = await octokit.paginate('GET /repos/{owner}/{repo}/contributors', { - per_page: 100, - owner: 'netlify', - repo: 'cli', -}) - -// get the user information for each contributor -const contributors = await Promise.all( - contributorList - .filter(({ type }) => type === 'User') - .map((user) => octokit.request('GET /users/{username}', { username: user.login }).then(({ data }) => data)), -) - -// generate a list of strings with name email and website -const packageJsonContributors = contributors.map((user) => { - const web = (user.twitter_username && `https://twitter.com/${user.twitter_username}`) || user.blog - let fullName = user.name || user.login - let email = mailList.get(user.login) || mailList.get(user.name) || user.email - - if (!email) { - const matchingName = [...mailList.keys()].find((name) => name.startsWith(user.name)) - if (matchingName) { - fullName = matchingName - email = mailList.get(matchingName) - } - } - - // Check if an existing user can be found if yes use the details provided in the package.json - const existing = existingContributors.find( - (cont) => - cont.name === fullName || - cont.name.startsWith(user.name) || - (cont.email && cont.email === email) || - (cont.web && cont.web === web), - ) - - if (existing) { - return createContributorString(existing) - } - - return createContributorString({ name: fullName, email, web }) -}) - -packageJson.contributors = packageJsonContributors - -writeFileSync('package.json', JSON.stringify(packageJson, null, 2), 'utf-8')