Skip to content

make AddLabel workflow work as expected #67046

make AddLabel workflow work as expected

make AddLabel workflow work as expected #67046

Workflow file for this run

# Copyright 2023–2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Add Label
on:
workflow_run:
workflows: ["MaxText Package Tests"]
types: [completed]
pull_request_review:
pull_request_review_comment:
workflow_dispatch:
jobs:
AddPullReady:
permissions:
checks: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Add Pull Request Label
uses: actions/github-script@v7
with:
script: |
console.log(`owner: ${context.repo.owner}`)
console.log(`repo: ${context.repo.repo}`)
const owner = "google"
const repo = "maxtext"
let pull_number = -1
if (context.payload.pull_request !== undefined) {
pull_number = context.payload.pull_request.number
} else if (context.payload.workflow_run !== undefined) {
if (context.payload.workflow_run.pull_requests.length === 0) {
core.setFailed("This workflow is NOT running within a PR's context")
}
//console.log(context.payload.workflow_run.pull_requests)
pull_number = context.payload.workflow_run.pull_requests[0].number
} else {
core.setFailed("This workflow is running within an invalid context")
}
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number,
});
// Filter to only include 'APPROVED' reviews
const approvals = reviews.filter(review => review.state === 'APPROVED').length;
console.log(`Current approval count: ${approvals}`);
if (approvals < 2) {
// core.setFailed stops the workflow and marks the step as failed
core.setFailed(`Pull Request requires at least 2 approvals, but only has ${approvals}.`);
}
const commits = await github.rest.pulls.listCommits({
owner,
repo,
pull_number,
per_page: 100,
})
// Check that the number of commits in the PR is 1.
if (commits.data.length !== 1) {
core.setFailed("Not adding pull ready because the PR has more than one commit. Please squash your commits.")
}
const ref = commits.data.slice(-1)[0].sha
const checkRuns = await github.rest.checks.listForRef({
owner,
repo,
ref,
})
if (checkRuns.data.check_runs.length === 0) {
core.setFailed("Not adding pull ready because no check runs are associated with the last commit: " + ref)
}
for (const checkRun of checkRuns.data.check_runs) {
if (checkRun.name.endsWith(context.job)) continue
if (checkRun.conclusion !== "success") {
core.setFailed("Not adding pull ready because " + checkRun.name + " has not passed yet: " + checkRun.html_url)
}
}
console.log("Adding pull ready label because the PR is approved AND all the check runs have passed")
await github.rest.issues.addLabels({
issue_number: pull_number,
labels: ["pull ready"],
owner,
repo,
})