Skip to content

Commit bd857a0

Browse files
authored
Merge pull request #325 from Keyfactor/fix/update-stores-branch-existence-check
fix(ci): check branch existence instead of open PRs for checkout strategy
2 parents 5b5f7f4 + d6af52d commit bd857a0

1 file changed

Lines changed: 37 additions & 31 deletions

File tree

.github/workflows/update-stores.yml

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Create Cert Store Update Pull Request
22

33
on:
4+
schedule:
5+
- cron: '0 0 * * *'
46
repository_dispatch:
57
types: targetRepo-event
68
workflow_dispatch:
@@ -16,6 +18,12 @@ jobs:
1618
create_pull_request:
1719
runs-on: ubuntu-latest
1820
steps:
21+
- name: Set TARGET_REPO_BRANCH from schedule
22+
if: github.event_name == 'schedule'
23+
run: |
24+
echo "TARGET_REPO_BRANCH=latest" | tee -a $GITHUB_ENV
25+
echo "KFUTIL_ARG=all" | tee -a $GITHUB_ENV
26+
1927
- name: Set TARGET_REPO_BRANCH from workflow_dispatch input
2028
if: github.event_name == 'workflow_dispatch'
2129
id: set-local-env-vars
@@ -39,41 +47,39 @@ jobs:
3947
4048
- name: Check Open PRs for Existing Branch
4149
id: check-branch
42-
uses: actions/github-script@v7
50+
uses: actions/github-script@v9
4351
with:
4452
script: |
45-
// Look for open pull requests
4653
const owner = context.repo.owner;
4754
const repo = context.repo.repo;
48-
const pulls = await github.rest.pulls.list({
49-
owner,
50-
repo,
51-
state: "open"
52-
});
53-
// Filter out ones matching our branch naming convention
54-
const filteredData = pulls.data.filter(item => item.head.ref === '${{ env.BRANCH_NAME }}');
55-
const isBranch = (filteredData.length > 0)
56-
if (isBranch) {
57-
const {
58-
head: { ref: incomingBranch }, base: { ref: baseBranch }
59-
} = pulls.data[0]
60-
core.setOutput('PR_BRANCH', 'commit'); // Just commit since the branch exists
61-
console.log(`incomingBranch: ${incomingBranch}`)
62-
console.log(`baseBranch: ${baseBranch}`)
63-
} else {
64-
core.setOutput('PR_BRANCH', 'create') // No branch, create one
55+
const branchName = '${{ env.BRANCH_NAME }}';
56+
57+
// Check if the branch itself exists
58+
let branchExists = false;
59+
try {
60+
await github.rest.git.getRef({ owner, repo, ref: `heads/${branchName}` });
61+
branchExists = true;
62+
} catch (e) {
63+
branchExists = false;
6564
}
66-
console.log(`Branch exists? ${filteredData.length > 0}`)
67-
console.log(`Branch name: ${{env.BRANCH_NAME}}`)
65+
66+
// Check for an open PR targeting this branch
67+
const pulls = await github.rest.pulls.list({ owner, repo, state: "open" });
68+
const hasOpenPR = pulls.data.some(item => item.head.ref === branchName);
69+
70+
console.log(`Branch exists: ${branchExists}, Open PR: ${hasOpenPR}, Branch name: ${branchName}`);
71+
core.setOutput('PR_BRANCH', branchExists ? 'commit' : 'create');
72+
core.setOutput('HAS_OPEN_PR', String(hasOpenPR));
6873
6974
- name: set env.PR_BRANCH value for jobs
7075
run: |
7176
echo "PR_BRANCH=${{steps.check-branch.outputs.PR_BRANCH}}" | tee -a $GITHUB_ENV
77+
echo "HAS_OPEN_PR=${{steps.check-branch.outputs.HAS_OPEN_PR}}" | tee -a $GITHUB_ENV
7278
7379
# If the branch with an open PR already exists, first check out that branch from kfutil
7480
- name: Check out existing repo merge branch
7581
if: env.PR_BRANCH == 'commit'
76-
uses: actions/checkout@v4
82+
uses: actions/checkout@v6
7783
with:
7884
repository: 'keyfactor/kfutil'
7985
sparse-checkout: |
@@ -86,7 +92,7 @@ jobs:
8692
# If the branch does not exist, first check out the main branch from kfutil.
8793
- name: Check out main
8894
if: env.PR_BRANCH == 'create'
89-
uses: actions/checkout@v4
95+
uses: actions/checkout@v6
9096
with:
9197
repository: 'keyfactor/kfutil'
9298
sparse-checkout: |
@@ -103,7 +109,7 @@ jobs:
103109
104110
# Checkout and run the python tool
105111
- name: Check out python merge tool repo
106-
uses: actions/checkout@v4
112+
uses: actions/checkout@v6
107113
with:
108114
repository: 'keyfactor/integration-tools'
109115
path: './tools/'
@@ -118,7 +124,7 @@ jobs:
118124

119125
- name: Save Store Types JSON Artifact
120126
if: success()
121-
uses: actions/upload-artifact@v4
127+
uses: actions/upload-artifact@v7
122128
with:
123129
name: store-types
124130
path: |
@@ -127,14 +133,14 @@ jobs:
127133
128134
- name: Save Invalid Store Types JSON Artifact
129135
if: success()
130-
uses: actions/upload-artifact@v4
136+
uses: actions/upload-artifact@v7
131137
with:
132138
name: invalid-repos
133139
path: ./tools/store-type-merge/invalid_repos.json
134140

135141
- name: Save logs directory
136142
if: success()
137-
uses: actions/upload-artifact@v4
143+
uses: actions/upload-artifact@v7
138144
with:
139145
name: logs
140146
path: ./tools/store-type-merge/log
@@ -164,7 +170,7 @@ jobs:
164170
# Both steps will contain a check for the UPDATE_FILE variable before running
165171
- name: Add and Commit to newly created branch
166172
if: ${{ env.UPDATE_FILE == 'T' && env.PR_BRANCH == 'create' }}
167-
uses: Keyfactor/add-and-commit@v9.1.3
173+
uses: Keyfactor/add-and-commit@v9.1.4
168174
env:
169175
GITHUB_TOKEN: ${{ secrets.SDK_SYNC_PAT }}
170176
with:
@@ -179,7 +185,7 @@ jobs:
179185

180186
- name: Add and Commit to existing branch
181187
if: ${{ env.UPDATE_FILE == 'T' && env.PR_BRANCH == 'commit' }}
182-
uses: Keyfactor/add-and-commit@v9.1.3
188+
uses: Keyfactor/add-and-commit@v9.1.4
183189
env:
184190
GITHUB_TOKEN: ${{ secrets.SDK_SYNC_PAT }}
185191
with:
@@ -192,7 +198,7 @@ jobs:
192198
cwd: './merge-folder/'
193199

194200
- name: Create new PR for the newly created branch
195-
if: env.UPDATE_FILE == 'T' && env.PR_BRANCH == 'create'
201+
if: env.UPDATE_FILE == 'T' && env.HAS_OPEN_PR == 'false'
196202
uses: actions/github-script@v7
197203
with:
198204
script: |
@@ -207,7 +213,7 @@ jobs:
207213
const response = await github.rest.pulls.create({
208214
owner,
209215
repo,
210-
title: 'New Pull Request - ${{env.KFUTIL_ARG}}:${{env.TARGET_REPO_BRANCH}}',
216+
title: 'Store Types Update - ${{env.KFUTIL_ARG}}:${{env.TARGET_REPO_BRANCH}}',
211217
head: newBranch,
212218
base: baseBranch,
213219
body: 'The cert store update from ${{env.KFUTIL_ARG}}:${{env.TARGET_REPO_BRANCH}} needs to be verified and merged if correct.',

0 commit comments

Comments
 (0)