-
Notifications
You must be signed in to change notification settings - Fork 0
110 lines (92 loc) · 4.48 KB
/
adr-accepted.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: ADR Accepted
on:
issues:
types:
- closed
jobs:
main:
name: Create ADR
runs-on: ubuntu-latest
# Only run this workflow if the closed issue has the "ADR: accepted" label
if: "${{ contains(github.event.issue.labels.*.name, 'ADR: accepted') }}"
steps:
- name: checkout main branch
uses: actions/checkout@v4
with:
ref: main
- name: get ADR number
id: next
run: |
LAST_ADR=$(ls docs/decisions/architecture/*.md | grep -Eo "architecture/adr-[0-9]+-" | sort | tail -n1 | grep -Eo "[0-9]+")
LAST_ADR=$(echo "$LAST_ADR" | sed -E 's/^0+//')
NEXT_ADR=$(($LAST_ADR + 1))
NEXT_ADR=$(printf "%04i" "$NEXT_ADR")
echo "number=$NEXT_ADR" >> "$GITHUB_OUTPUT"
- name: write the ADR
id: create-adr
uses: actions/github-script@v7
with:
script: |
const fs = require("fs/promises");
// Use the GitHub toJSON expression to get these as escaped strings.
// In Javascript, this will preserve line breaks, and using template
// strings further down will preserve all the kinds of quotes.
const title = ${{ toJSON(github.event.issue.title) }};
const body = ${{ toJSON(github.event.issue.body )}};
const slug = title
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, "") // get rid of non-ascii characters
.replace(/[\s_-]+/g, "-") // convert whitespace and underscore to dash
.replace(/^-+|-+$/g, ""); // git rid of leading/trailing dashes
const filename = `docs/decisions/architecture/adr-${{ steps.next.outputs.number }}-${slug}.md`;
// Get the current date as an ISO8601 string, split at the timestamp,
// and only keep the date portion.
const [date] = new Date().toISOString().split("T");
const adr = `# ${ title }
Date: ${ date }
### Status
Accepted
${ body }
`;
# Set outputs for the next step to use
core.setOutput('filename', filename);
core.setOutput('adr', adr);
await fs.writeFile(filename, adr, { encoding: 'utf-8' });
- name: branch, commit, and open PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="adr-auto-${{ steps.next.outputs.number }}"
git config --global user.email "[email protected]"
git config --global user.name "GSL Verification team ADR automation"
# Check if the branch already exists, exit-code 2 means it doesn't, and that creating a new branch is safe.
set +e # override set -e since we're hoping for an exit code
git ls-remote --exit-code --heads origin refs/heads/$BRANCH > /dev/null
branch_exists=$?
set -e
if [ $branch_exists -eq 2 ]; then
git checkout -b $BRANCH
git add docs/decisions/architecture/*.md
git commit -m "add ADR ${{ steps.next.outputs.number }}: ${{ github.event.issue.title }}"
git push -f origin $BRANCH
gh pr create \
--title "Add ADR ${{ steps.next.outputs.number }} to the repo" \
--label "ADR" \
--body "This pull request was opened automatically because #${{ github.event.issue.number }} was closed after being marked as an approved ADR. It contains a markdown file capturing the ADR body at the time the issue was closed. Please verify that the markdown is correct before merging!" || true
else
echo "Error - Branch $BRANCH already exists, PR will need to be created manually. ADR text is below. Exiting."
echo " --- "
echo "Filename: ${{ steps.create-adr.outputs.filename }}"
echo " --- Start File --- "
echo "${{ steps.create-adr.outputs.adr }}"
echo " --- End File --- "
# Create a step summary
echo "# Error" >> $GITHUB_STEP_SUMMARY
echo "Branch $BRANCH already exists, PR will need to be created manually. ADR contents are below. Exiting." >> $GITHUB_STEP_SUMMARY
echo "## Filename" >> $GITHUB_STEP_SUMMARY
echo "\`${{ steps.create-adr.outputs.filename }}\`" >> $GITHUB_STEP_SUMMARY
echo "## ADR Contents" >> $GITHUB_STEP_SUMMARY
echo "${{ steps.create-adr.outputs.adr }}" >> $GITHUB_STEP_SUMMARY
exit 1
fi