@@ -16,9 +16,8 @@ name: Add Label
1616
1717on :
1818 workflow_run :
19- workflows : [Tests, CodeQL]
20- types :
21- - completed
19+ workflows : ["MaxText Package Tests"]
20+ types : [completed]
2221 pull_request_review :
2322 pull_request_review_comment :
2423 workflow_dispatch :
@@ -31,86 +30,98 @@ jobs:
3130 runs-on : ubuntu-latest
3231
3332 steps :
34- - uses : actions/github-script@v7
33+ - name : Add Pull Request Label
34+ uses : actions/github-script@v7
3535 with :
3636 script : |
37- const owner = "google"
38- const repo = "maxtext"
3937 let pull_number = -1
4038 if (context.payload.pull_request !== undefined) {
4139 pull_number = context.payload.pull_request.number
4240 } else if (context.payload.workflow_run !== undefined) {
4341 if (context.payload.workflow_run.pull_requests.length === 0) {
44- console.log ("This workflow is NOT running within a PR's context")
45- process.exit()
42+ core.setFailed ("This workflow is NOT running within a PR's context")
43+ return
4644 }
4745 console.log(context.payload.workflow_run.pull_requests)
4846 pull_number = context.payload.workflow_run.pull_requests[0].number
4947 } else {
50- console.log ("This workflow is running within an invalid context")
51- process.exit(1)
48+ core.setFailed ("This workflow is running within an invalid context")
49+ return
5250 }
53- const reviews = await github.rest.pulls.listReviews({
54- owner,
55- repo,
56- pull_number,
57- })
51+
5852 const decision_query = `
5953 query($owner: String!, $repo: String!, $pull_number: Int!) {
6054 repository(owner: $owner, name: $repo) {
6155 pullRequest(number: $pull_number) {
6256 reviewDecision # Fetches the overall review status
57+ reviews(last: 100) {
58+ nodes {
59+ state
60+ author { login }
61+ }
62+ }
6363 }
6464 }
6565 }
6666 `;
67- const decision_result = await github.graphql(decision_query, { owner, repo, pull_number });
6867
69- if (reviews.data.length === 0) {
70- console.log("Not adding pull ready because the PR is not approved yet.")
71- process.exit()
72- }
73- let is_approved = false
74- if (decision_result.repository.pullRequest.reviewDecision === "APPROVED") {
75- is_approved = true
76- }
77- if (!is_approved) {
78- console.log("Not adding pull ready because the PR is not approved yet by sufficient code owners.")
79- process.exit()
68+ const decision_result = await github.graphql(decision_query, {
69+ owner: context.repo.owner,
70+ repo: context.repo.repo,
71+ pull_number: pull_number
72+ });
73+
74+ const pullRequest = decision_result.repository.pullRequest;
75+ const uniqueApprovers = new Set(
76+ pullRequest.reviews.nodes
77+ .filter(r => r.state === 'APPROVED')
78+ .map(r => r.author.login)
79+ );
80+
81+ if (pullRequest.reviewDecision !== 'APPROVED' || uniqueApprovers.size < 2) {
82+ core.info(`PR is not ready. Decision: ${pullRequest.reviewDecision}, Approvals: ${uniqueApprovers.size}`);
83+ return;
8084 }
8185
8286 const commits = await github.rest.pulls.listCommits({
83- owner,
84- repo,
85- pull_number,
87+ owner: context.repo.owner ,
88+ repo: context.repo.repo ,
89+ pull_number: pull_number ,
8690 per_page: 100,
8791 })
8892 // Check that the number of commits in the PR is 1.
8993 if (commits.data.length !== 1) {
90- console.log ("Not adding pull ready because the PR has more than one commit. Please squash your commits.")
91- process.exit(1)
94+ core.setFailed ("Not adding pull ready because the PR has more than one commit. Please squash your commits.")
95+ return
9296 }
93- const ref = commits.data.slice(-1)[0].sha
94- const checkRuns = await github.rest.checks.listForRef({
95- owner,
96- repo,
97- ref,
98- })
99- if (checkRuns.data.check_runs.length === 0) {
100- console.log("Not adding pull ready because no check runs are associated with the last commit: " + ref)
101- process.exit()
97+
98+ const last_commit_sha = commits.data.slice(-1)[0].sha
99+
100+ const { data: checkRuns } = await github.rest.checks.listForRef({
101+ owner: context.repo.owner,
102+ repo: context.repo.repo,
103+ ref: last_commit_sha,
104+ });
105+
106+ if (checkRuns.check_runs.length === 0) {
107+ core.info("Not adding pull ready because no check runs are associated with the last commit: " + last_commit_sha)
108+ return
102109 }
103- for (const checkRun of checkRuns.data.check_runs) {
110+
111+ for (const checkRun of checkRuns.check_runs) {
112+ // Ignore the current running workflow
104113 if (checkRun.name.endsWith(context.job)) continue
105- if (checkRun.conclusion !== "success") {
106- console.log("Not adding pull ready because " + checkRun.name + " has not passed yet: " + checkRun.html_url)
107- process.exit()
114+
115+ if (checkRun.status !== 'completed' || checkRun.conclusion !== 'success') {
116+ core.info(`Waiting for check: ${checkRun.name} (Status: ${checkRun.status}, Conclusion: ${checkRun.conclusion})`);
117+ return; // Exit without failing
108118 }
109119 }
120+
110121 console.log("Adding pull ready label because the PR is approved AND all the check runs have passed")
111122 await github.rest.issues.addLabels({
112123 issue_number: pull_number,
113124 labels: ["pull ready"],
114- owner,
115- repo,
125+ owner: context.repo.owner ,
126+ repo: context.repo.repo ,
116127 })
0 commit comments