Skip to content

Commit 0aa437f

Browse files
jyj0w0Yijia J
andauthored
Add yml file of adding pull_ready tags for copybara flow. The file is almost same as .github/workflows/AddLabel.yml (#234)
Co-authored-by: Yijia J <yijiaj@google.com>
1 parent e539007 commit 0aa437f

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

.github/workflows/add_label.yaml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: Add Label
16+
17+
on:
18+
workflow_run:
19+
workflows: [Tests, CodeQL]
20+
types:
21+
- completed
22+
pull_request_review:
23+
pull_request_review_comment:
24+
workflow_dispatch:
25+
26+
jobs:
27+
AddPullReady:
28+
permissions:
29+
checks: read
30+
pull-requests: write
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- uses: actions/github-script@v6
35+
with:
36+
script: |
37+
const owner = "google"
38+
const repo = "jetstream"
39+
let pull_number = -1
40+
if (context.payload.pull_request !== undefined) {
41+
pull_number = context.payload.pull_request.number
42+
} else if (context.payload.workflow_run !== undefined) {
43+
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()
46+
}
47+
console.log(context.payload.workflow_run.pull_requests)
48+
pull_number = context.payload.workflow_run.pull_requests[0].number
49+
} else {
50+
console.log("This workflow is running within an invalid context")
51+
process.exit(1)
52+
}
53+
54+
// This list should match with CODEOWNERS.
55+
const reviews = await github.rest.pulls.listReviews({
56+
owner,
57+
repo,
58+
pull_number,
59+
})
60+
61+
const pullRequest = await github.rest.pulls.get({
62+
owner,
63+
repo,
64+
pull_number,
65+
});
66+
const pullRequester = pullRequest.data.user.login;
67+
68+
if (reviews.data.length === 0) {
69+
console.log("Not adding pull ready because the PR is not approved yet.")
70+
process.exit()
71+
}
72+
let is_approved=false
73+
for (const review of reviews.data) {
74+
if (review.state === "APPROVED") {
75+
is_approved=true
76+
break;
77+
}
78+
}
79+
if (!is_approved) {
80+
console.log("Not adding pull ready because the PR is not approved yet by a code owner.")
81+
process.exit()
82+
}
83+
84+
const commits = await github.rest.pulls.listCommits({
85+
owner,
86+
repo,
87+
pull_number,
88+
per_page: 100,
89+
})
90+
// Check that the number of commits in the PR is 1.
91+
if (commits.data.length !== 1) {
92+
console.log("Not adding pull ready because the PR has more than one commit. Please squash your commits.")
93+
process.exit(1)
94+
}
95+
const ref = commits.data.slice(-1)[0].sha
96+
const checkRuns = await github.rest.checks.listForRef({
97+
owner,
98+
repo,
99+
ref,
100+
})
101+
if (checkRuns.data.check_runs.length === 0) {
102+
console.log("Not adding pull ready because no check runs are associated with the last commit: " + ref)
103+
process.exit()
104+
}
105+
for (const checkRun of checkRuns.data.check_runs) {
106+
if (checkRun.name.endsWith(context.job)) continue
107+
if (checkRun.conclusion !== "success") {
108+
console.log("Not adding pull ready because " + checkRun.name + " has not passed yet: " + checkRun.html_url)
109+
process.exit()
110+
}
111+
}
112+
console.log("Adding pull ready label because the PR is approved AND all the check runs have passed")
113+
await github.rest.issues.addLabels({
114+
issue_number: pull_number,
115+
labels: ["pull ready"],
116+
owner,
117+
repo,
118+
})

0 commit comments

Comments
 (0)