Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow to update local member data on pull request #27

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/update-new-members.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Update new member files

on:
pull_request:
branches:
- main

permissions:
contents: write
id-token: write

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v4
- name: Fetch main branch
run: git fetch origin main:main
- name: Install npm packages
run: npm install
- name: Update new member files
run: bash ./bin/update-new-members
- name: Commit member changes, if any
run: bash ./bin/commit-member-changes
14 changes: 14 additions & 0 deletions bin/commit-member-changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -eu
# Creates a commit with the changes in `src/content/members`, if any. Must be
# run in the repo's root.

if [[ $(git status --porcelain src/content/members) ]]; then
git config user.name 'Open Source Pledge GitHub Action Runner'
git config user.email '[email protected]'
git add src/content/members
git commit -m 'Add changes to src/content/members (automated commit)'
git push
echo "Changes to src/content/members committed"
else
echo "No changes to local member data, skipping"
fi
8 changes: 8 additions & 0 deletions bin/update-all-members
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -eu
# This script fetches the JSON files specified in `members.csv` for all
# members. Must be run in the repo's root.

while IFS= read -r line; do
line_parts=(${line//,/ })
./bin/update-member ${line_parts[0]} ${line_parts[1]}
done < members.csv
68 changes: 68 additions & 0 deletions bin/update-member
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env node

import fs from 'fs';
import fetch from 'node-fetch';


const USAGE = `USAGE: ./bin/update-member foocorp https://foocorp.example.com/foocorp.json
Must be run in the repo's root.`;


async function updateMember(name, url) {
const localPath = `./src/content/members/${name}.json`;
console.log(`Fetching member: ${name}`);
console.log(`\t* Remote URL: ${url}`);
console.log(`\t* Local path: ${localPath}`);

let localMemberData = undefined;
if (fs.existsSync(localPath)) {
try {
localMemberData = JSON.parse(fs.readFileSync(localPath, 'utf8'));
} catch(e) {
console.log(`Saw invalid JSON in ${localPath}, ignoring local data`);
}
}

let remoteMemberData = undefined;
try {
remoteMemberData = await fetch(url)
.then((res) => res.text())
.then((text) => JSON.parse(text));
} catch (e) {
console.error(`ERROR: Failed to get JSON from ${url}`, e);
process.exit(1);
}

let shouldWrite = (localMemberData == undefined);

if (!shouldWrite) {
const localMtime = new Date(localMemberData.datetimeModified);
const remoteMtime = new Date(remoteMemberData.datetimeModified);
shouldWrite = remoteMtime > localMtime;
}

if (shouldWrite) {
console.log(`Remote JSON is newer, writing to ${localPath}`);
try {
fs.writeFileSync(localPath, JSON.stringify(remoteMemberData, null, 2));
} catch (e) {
console.error(`ERROR: Failed to write to ${localPath}`, e);
process.exit(1);
}
} else {
console.log("Remote JSON is older, skipping");
}
}


function main() {
const args = process.argv.slice(2);
if (args.length != 2) {
console.error(USAGE);
process.exit(1);
}
updateMember(args[0], args[1]);
}


main()
17 changes: 17 additions & 0 deletions bin/update-new-members
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -eu
# This script fetches the JSON files specified for all members that have been
# added to `members.csv` compared to the `main` branch. Must be run in the
# repo's root.

new_member_lines=$(
diff \
--changed-group-format='%>' \
--unchanged-group-format='' \
<( git show main:members.csv ) \
members.csv
) || true

while IFS= read -r line; do
line_parts=(${line//,/ })
./bin/update-member ${line_parts[0]} ${line_parts[1]}
done <<< "$new_member_lines"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"domain": "sentry.io",
"urlSource": "https://open.sentry.io/osspledge/data.json",
"datetimeModified": "2024-07-19T12:24:46Z",
"description": "Sentry has given to Open Source for soooooo many years.",
"name": "Sentry",
Expand Down
3 changes: 2 additions & 1 deletion members.csv
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sentry,https://open.sentry.io/funding/osspledge.json
sentry,https://raw.githubusercontent.com/opensourcepledge/opensourcepledge.com/vladh/schema-continued/contrib/example-schema.json
test,https://raw.githubusercontent.com/opensourcepledge/opensourcepledge.com/vladh/schema-continued/contrib/example-schema.json
123 changes: 109 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@sentry/astro": "^8.18.0",
"@tailwindcss/typography": "^0.5.13",
"astro": "^4.11.5",
"node-fetch": "^3.3.2",
"tailwindcss": "^3.4.6",
"typescript": "^5.5.3"
}
Expand Down
1 change: 0 additions & 1 deletion src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const collections = {
schema: z
.object({
domain: z.string(),
urlSource: z.string().url(),
datetimeModified: z.string().datetime(),
})
.merge(memberProvidedData),
Expand Down
Empty file added src/content/members/.gitkeep
Empty file.