Skip to content

Commit

Permalink
Fix #566, Adds CCB Agenda generation workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
chillfig committed Oct 20, 2022
1 parent 35548bc commit d53b428
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .github/scripts/create_ccb_agenda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import subprocess
import json
import sys
import time
import os

# extract pull request data from GitHub
repos = ['cFS', 'cFE', 'osal', 'psp', 'sch_lab', 'ci_lab', 'to_lab', 'sample_app', 'sample_lib', 'elf2cfetbl', 'tblcrctool','cFS-GroundSystem', 'CF', 'CS', 'DS','FM', 'HK', 'HS', 'LC', 'MD', 'MM', 'SC']

for repo in repos:
subprocess.Popen('gh pr list --repo nasa/' + str(repo) + ' --search "draft:false" --json number,author,title,url,additions,deletions,labels | jq -c ' + '\'reduce range(0, length) as $index (.; (.[$index].author=.[$index].author.login | .[$index].number=(.[$index].number|"\'' + str(repo) + '\' PR #\(.)") )) | .[]\' ' + '>> temp.json', shell=True)

time.sleep(5)
subprocess.Popen('jq -s . temp.json > prs.json', shell=True)
time.sleep(5)

# load a list of pull requests as python dictionaries
with open ('prs.json') as prs_file:
prs = json.load(prs_file)

PrData = dict() # {author: [pr1, pr2, pr3, ...]}
AuthorPrChanges = dict() # {author: #TotalChanges}

for pr in prs:
ignore = False
for label in pr['labels']:
if label['name'] == 'CCB:Ignore' or label['name'] == 'CCB:Approved':
ignore = True
break
if ignore == False:
if pr['author'] not in PrData:
PrData[pr['author']] = [pr]
AuthorPrChanges[pr['author']] = pr['additions'] + pr['deletions']
else:
PrData[pr['author']].append(pr)
AuthorPrChanges[pr['author']] += pr['additions'] + pr['deletions']

# no prs to write, exit program
if len(PrData) == 0:
print("Failed to find relevant Pull Requests for the agenda. Exiting...\n")
sys.exit()

# re-order dict according to sum(additions, deletions) of each pr for each author
AuthorPrChanges = {k: v for k, v in sorted(AuthorPrChanges.items(), key=lambda item: item[1])}

# write to markdown
CCBfilename = "CCBAgenda.md"
with open(CCBfilename, 'w') as f:
f.write("## Items for Discussion\n\n")
for author in AuthorPrChanges.keys():
f.write("### @" + author + "\n\n")
for pr_auth in PrData[author]:
if (author == pr_auth['author']):
f.write("[" + pr_auth['number'] + "](" + pr_auth['url'].replace("pull", "issues") + ") " + pr_auth['title'] + "\n\n")

# close files
f.close()
prs_file.close()
time.sleep(5)
try:
os.remove("prs.json")
os.remove("temp.json")
except OSError:
pass

time.sleep(5)

if (os.stat(CCBfilename).st_size != 0):
print("CCB markdown has been successfully created")
93 changes: 93 additions & 0 deletions .github/scripts/update_wiki.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash
set -euo pipefail

function debug() {
echo "::debug file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
}
function error() {
echo "::error file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
}
if [ -z "$GITHUB_ACTOR" ]; then
error "GITHUB_ACTOR environment variable is not set"
exit 1
fi

if [ -z "$GITHUB_REPOSITORY" ]; then
error "GITHUB_REPOSITORY environment variable is not set"
exit 1
fi

if [ -z "$GH_PERSONAL_ACCESS_TOKEN" ]; then
error "GH_PERSONAL_ACCESS_TOKEN environment variable is not set"
exit 1
fi

# Wiki url
GIT_REPOSITORY_URL="https://${GH_PERSONAL_ACCESS_TOKEN}@${GITHUB_SERVER_URL#https://}/$GITHUB_REPOSITORY.wiki.git"

# Temporary directory to store wiki page data
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)

# Pull wiki files, extract variables, update wiki files
(
cd "$tmp_dir" || exit 1
git init
git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
git pull "$GIT_REPOSITORY_URL"
) || exit 1

# Dates
todaysDate=`date +%Y.%m.%d`
latestCCBdate=$(sed -n 1p $tmp_dir/_Sidebar.md | grep -o -P '(?<=CCB:-).{10}')
prevCCBdate=$(sed -n 3p $tmp_dir/_Sidebar.md | grep -o -P '(?<=CCB:-).{10}')

# Dates in seconds
todaysDateSeconds=$(date -d $(echo $todaysDate | awk -v FS=. -v OFS=- '{print $1,$2,$3}') +%s)
latestCCBdateSeconds=$(date -d $(echo $latestCCBdate | awk -v FS=. -v OFS=- '{print $1,$2,$3}') +%s)
nextCCBdateSeconds=$(date -d $(echo $nextCCBdate | awk -v FS=. -v OFS=- '{print $1,$2,$3}') +%s)

# Compute next CCB date
offset=$((((((((($todaysDateSeconds - $latestCCBdateSeconds) / 60) / 60) / 24) - 1) / 7) + 1) * 7))
nextCCBdate=`date '+%Y.%m.%d' -d "$(echo $latestCCBdate | awk -v FS=. -v OFS= '{print $1,$2,$3}')+$offset days"`

# CCB agenda filename to be pushed to wiki page
CCBAgendafilename="CCB:-"$nextCCBdate".md"

# If today is 7 days offset from last documented CCB (only on first update to wiki today)
if (( $todaysDateSeconds == $nextCCBdateSeconds )); then
# We can now update _Sidebar.md and home.md
sed -i "s/$latestCCBdate/$nextCCBdate/" $tmp_dir/_Sidebar.md
sed -i "s/$prevCCBdate/$latestCCBdate/" $tmp_dir/_Sidebar.md
sed -i "s/$latestCCBdate/$nextCCBdate/" $tmp_dir/Home.md
fi

# If today is 7 days offset from last documented CCB (first update or later update to wiki today)
if (( $todaysDateSeconds == $nextCCBdateSeconds )) || (( $todaysDateSeconds == $latestCCBdateSeconds )); then
CCBAgendafilename="CCB:-"$todaysDate".md"
fi

# change filename of CCB agenda and copy into temp dir
mv /home/runner/work/cFS/cFS/CCBAgenda.md "$CCBAgendafilename"
cp $CCBAgendafilename "$tmp_dir"

debug "Committing and pushing changes"
(
cd "$tmp_dir" || exit 1
git add "$tmp_dir/$CCBAgendafilename"
if (( $todaysDateSeconds == $latestCCBdateSeconds )); then
git commit -m "$CCBAgendafilename, Update (markdown)"
else
git commit -m "draft $CCBAgendafilename"
fi

if (( $todaysDateSeconds == $nextCCBdateSeconds )); then
git add "$tmp_dir/_Sidebar.md"
git add "$tmp_dir/Home.md"
git commit -m "$CCBAgendafilename, Update Home and Sidebar links"
fi
git push --set-upstream "$GIT_REPOSITORY_URL" master
) || exit 1

rm -rf "$tmp_dir"
exit 0
31 changes: 31 additions & 0 deletions .github/workflows/cfs-wiki.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Update wiki

on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Force bash to apply pipefail option so pipeline failures aren't masked
defaults:
run:
shell: bash

jobs:
build:
runs-on: ubuntu-latest

steps:
# Checks out a copy of your repository on the ubuntu-latest machine
- name: Checkout code
uses: actions/checkout@v2
with:
submodules: true

- name: create CCB agenda
run: python3 .github/scripts/create_ccb_agenda.py
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Access and update wiki
run: bash .github/scripts/update_wiki.sh
env:
GH_PERSONAL_ACCESS_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}

0 comments on commit d53b428

Please sign in to comment.