Skip to content

Commit 4befc16

Browse files
ci: Use github-script to determine workflow context (#131)
1 parent ce3806b commit 4befc16

1 file changed

Lines changed: 77 additions & 30 deletions

File tree

.github/workflows/publish-screenshots.yml

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,83 @@ jobs:
7272
7373
- name: Determine Context
7474
id: context
75-
env:
76-
WORKFLOW_EVENT: ${{ github.event.workflow_run.event }}
77-
WORKFLOW_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
78-
run: |
79-
event_name="${WORKFLOW_EVENT}"
80-
head_branch="${WORKFLOW_HEAD_BRANCH}"
81-
pr_number="$(jq -r '.workflow_run.pull_requests[0].number // empty' "${GITHUB_EVENT_PATH}")"
82-
83-
if [ -n "${pr_number}" ] && ! [[ "${pr_number}" =~ ^[0-9]+$ ]]; then
84-
echo "Invalid pr_number value: ${pr_number}"
85-
exit 1
86-
fi
87-
88-
is_pr=false
89-
if [ "${event_name}" = "pull_request" ] && [ -n "${pr_number}" ]; then
90-
is_pr=true
91-
fi
92-
93-
is_master_push=false
94-
if [ "${event_name}" = "push" ] && [ "${head_branch}" = "master" ]; then
95-
is_master_push=true
96-
fi
97-
98-
{
99-
echo "event_name=${event_name}"
100-
echo "head_branch=${head_branch}"
101-
echo "is_pr=${is_pr}"
102-
echo "pr_number=${pr_number}"
103-
echo "is_master_push=${is_master_push}"
104-
} >> "${GITHUB_OUTPUT}"
75+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
76+
with:
77+
script: |
78+
const run = context.payload.workflow_run;
79+
const eventName = run.event || '';
80+
const headBranch = run.head_branch || '';
81+
const headRepository = run.head_repository || {};
82+
const headRepositoryName = headRepository.full_name || '';
83+
84+
function normalizePrNumber(value) {
85+
if (value === undefined || value === null || value === '') {
86+
return '';
87+
}
88+
89+
const prNumber = String(value);
90+
if (!/^\d+$/.test(prNumber)) {
91+
throw new Error(`Invalid PR number value: ${prNumber}`);
92+
}
93+
94+
return prNumber;
95+
}
96+
97+
let prNumber = '';
98+
if (eventName === 'pull_request') {
99+
prNumber = normalizePrNumber(run.pull_requests?.[0]?.number);
100+
101+
if (!prNumber) {
102+
const headOwner = headRepository.owner?.login
103+
|| headRepository.owner?.name
104+
|| headRepositoryName.split('/')[0]
105+
|| '';
106+
const head = headOwner && headBranch ? `${headOwner}:${headBranch}` : '';
107+
108+
if (head) {
109+
core.info(`workflow_run.pull_requests is empty; resolving PR from head ${head}.`);
110+
const pullRequests = await github.paginate(github.rest.pulls.list, {
111+
owner: context.repo.owner,
112+
repo: context.repo.repo,
113+
state: 'open',
114+
head,
115+
sort: 'updated',
116+
direction: 'desc',
117+
per_page: 100,
118+
});
119+
const baseRepository = `${context.repo.owner}/${context.repo.repo}`;
120+
const matchingSha = pullRequests.find((pr) => pr.head?.sha === run.head_sha);
121+
const matchingBase = pullRequests.find((pr) => pr.base?.repo?.full_name === baseRepository);
122+
const pullRequest = matchingSha || matchingBase || pullRequests[0];
123+
prNumber = normalizePrNumber(pullRequest?.number);
124+
}
125+
}
126+
127+
if (!prNumber) {
128+
throw new Error([
129+
'Unable to determine PR number for pull_request workflow_run.',
130+
`head_repository=${headRepositoryName || '<unknown>'}`,
131+
`head_branch=${headBranch || '<unknown>'}`,
132+
`head_sha=${run.head_sha || '<unknown>'}`,
133+
`payload_pull_requests=${run.pull_requests?.length || 0}`,
134+
].join(' '));
135+
}
136+
}
137+
138+
const isPr = eventName === 'pull_request' && prNumber !== '';
139+
const isMasterPush = eventName === 'push' && headBranch === 'master';
140+
141+
core.setOutput('event_name', eventName);
142+
core.setOutput('head_branch', headBranch);
143+
core.setOutput('is_pr', String(isPr));
144+
core.setOutput('pr_number', prNumber);
145+
core.setOutput('is_master_push', String(isMasterPush));
146+
147+
core.info(`event_name=${eventName}`);
148+
core.info(`head_branch=${headBranch}`);
149+
core.info(`is_pr=${isPr}`);
150+
core.info(`pr_number=${prNumber}`);
151+
core.info(`is_master_push=${isMasterPush}`);
105152
106153
- name: Checkout Screenshots Branch
107154
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

0 commit comments

Comments
 (0)