-
Notifications
You must be signed in to change notification settings - Fork 191
139 lines (117 loc) · 4.5 KB
/
Copy pathadd-hip-number.yml
File metadata and controls
139 lines (117 loc) · 4.5 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
name: Assign HIP Number and Rename File
on:
pull_request:
types: [opened, synchronize]
defaults:
run:
shell: bash
permissions:
contents: write
issues: read
pull-requests: write
checks: write
jobs:
assign-hip-number:
runs-on: hiero-improvement-proposals-linux-medium
continue-on-error: true # Silently fail - don't show red X on PRs
steps:
- name: Harden Runner
uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
with:
egress-policy: audit
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
token: ${{ secrets.GH_ACCESS_TOKEN }}
fetch-depth: 0
- name: Import GPG Key
id: gpg_importer
uses: step-security/ghaction-import-gpg@69c854a83c7f79463f8bdf46772ab09826c560cd # v6.3.1
with:
git_commit_gpgsign: true
git_tag_gpgsign: true
git_user_signingkey: true
gpg_private_key: ${{ secrets.GPG_KEY_CONTENTS }}
passphrase: ${{ secrets.GPG_KEY_PASSPHRASE }}
- name: Identify if New HIP
id: check-new
run: |
# Fetch the main branch to have all necessary references
git fetch origin main
# Get the modified files in the PR against the main branch
MODIFIED_FILES=$(git diff --name-only origin/main...$GITHUB_SHA)
echo "Modified files in PR: $MODIFIED_FILES"
# Directory path to check for HIP files
HIP_DIRECTORY='HIP'
HIP_FILES=$(echo "$MODIFIED_FILES" | grep "^$HIP_DIRECTORY/.*\.md")
echo "Filtered HIP files: $HIP_FILES"
echo "::set-output name=hip-files::$HIP_FILES"
# Check if any HIP files were modified
if [ -z "$HIP_FILES" ]; then
echo "No HIP files found in the PR."
echo "::set-output name=new-hip::false"
exit 0
fi
# Check each HIP file to see if it exists in the main branch
NEW_HIP=true
for file in $HIP_FILES; do
if git ls-tree -r origin/main --name-only | grep -q "^$file$"; then
echo "This PR modifies an existing HIP: $file"
NEW_HIP=false
break
fi
done
if [ "$NEW_HIP" = "true" ]; then
echo "This PR creates a new HIP."
echo "::set-output name=new-hip::true"
else
echo "::set-output name=new-hip::false"
fi
- name: Assign HIP Number
if: steps.check-new.outputs.new-hip == 'true'
run: |
# Extract the current PR number
PR_NUMBER=${{ github.event.pull_request.number }}
HIP_HEADER="hip: $PR_NUMBER"
HIP_FILE=$(echo "${{ steps.check-new.outputs.hip-files }}" | head -n 1)
echo "Assigning HIP number to file: $HIP_FILE"
if [ -n "$HIP_FILE" ]; then
sed -i "s/^hip:.*$/$HIP_HEADER/" "$HIP_FILE"
else
echo "No valid HIP file to assign a number. Skipping assignment."
exit 0
fi
- name: Rename HIP File
if: steps.check-new.outputs.new-hip == 'true'
run: |
# Extract PR number
PR_NUMBER=${{ github.event.pull_request.number }}
HIP_FILE=$(echo "${{ steps.check-new.outputs.hip-files }}" | head -n 1)
if [ -n "$HIP_FILE" ]; then
NEW_HIP_FILE="HIP/hip-$PR_NUMBER.md"
mv "$HIP_FILE" "$NEW_HIP_FILE"
else
echo "No HIP file found to rename. Skipping rename."
exit 0
fi
- name: Commit Changes
if: steps.check-new.outputs.new-hip == 'true'
run: |
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
PR_NUMBER=${{ github.event.pull_request.number }}
git add HIP/
# Check if there are changes to commit
if git diff --cached --quiet; then
echo "No changes to commit. Skipping."
exit 0
fi
git commit -sSm "Assigning HIP $PR_NUMBER and renaming file to hip-$PR_NUMBER.md" || {
echo "Commit failed. This may be expected for PRs from forks."
exit 0
}
git push origin HEAD:${{ github.head_ref }} || {
echo "Push failed. This is expected for PRs from forks without write access."
echo "The PR author will need to manually update their branch."
exit 0
}