Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion .asf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,27 @@ github:
projects: true
discussions: true

# Pull requests are merged with the squash button. The commit
# message is the pull request title and description.
enabled_merge_buttons:
squash: true
squash_commit_message: PR_TITLE_AND_DESC
merge: false
rebase: false

protected_branches:
# protect main against force push
main: {}
main:
required_status_checks:
# Don't require branches to be up to date before merging.
strict: false
# The "Merge check" job in
# .github/workflows/dev_pr_merge_check.yml. The merge button
# is disabled while the pull request title isn't
# "GH-XXX: [Component] Title" or the issue isn't open and
# assigned.
contexts:
- Merge check
Comment thread
rok marked this conversation as resolved.
Resolved

collaborators:
- alinaliBQ
Expand Down
83 changes: 83 additions & 0 deletions .github/workflows/dev_pr/merge_check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.


const helpers = require("./helpers.js");

// GH-XXX: [Component] Title or MINOR: [Component] Title
const TITLE_PATTERN = /^(?:GH-(\d+)|MINOR): (?:\[[^\]]+\])+ \S/;

/**
* Checks whether the pull request can be merged with the merge
* button:
* - The title is "GH-XXX: [Component] Title" or
* "MINOR: [Component] Title". It's used as the commit title.
* - The issue in the title exists and is open. It's closed by
* merged.js on merge.
* - The issue is assigned to someone. issue_check.js assigns it to
* the pull request author if possible.
*
* @param {Object} github
* @param {Object} context
* @returns {String[]} The found problems.
*/
async function check(github, context) {
const title = context.payload.pull_request.title;
const matched = TITLE_PATTERN.exec(title);
if (!matched) {
return [
`The title must be "GH-XXX: [Component] Title" or ` +
`"MINOR: [Component] Title": ${title}`,
];
}
const issueID = matched[1];
if (!issueID) {
return [];
}

const issueInfo = await helpers.getGitHubInfo(
github, context, issueID, context.payload.number);
if (!issueInfo) {
return [`GitHub issue #${issueID} could not be retrieved.`];
}
if (issueInfo.pull_request) {
return [`#${issueID} is a pull request, not an issue.`];
}
const errors = [];
if (issueInfo.state !== "open") {
errors.push(`GitHub issue #${issueID} is already closed.`);
}
if (!issueInfo.assignees.length) {
errors.push(`GitHub issue #${issueID} is not assigned to anyone.`);
}
return errors;
}

module.exports = async ({github, context, core}) => {
// The "Dev PR" workflow assigns the issue at the same time. Wait
// for it.
let errors = await check(github, context);
for (let i = 0; i < 6 && errors.length > 0 &&
TITLE_PATTERN.test(context.payload.pull_request.title); i++) {
await new Promise((resolve) => setTimeout(resolve, 10 * 1000));
errors = await check(github, context);
}
if (errors.length > 0) {
core.setFailed(errors.join("\n"));
}
};
module.exports.check = check;
216 changes: 216 additions & 0 deletions .github/workflows/dev_pr/merged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.

const helpers = require("./helpers.js");

const VERSION_PATTERN = /^\d+\.\d+\.\d+$/;

/**
* Compares two versions like "23.0.0" numerically.
*
* @param {String} a
* @param {String} b
* @returns {Number} negative if a < b, positive if a > b, 0 otherwise.
*/
function compareVersions(a, b) {
const aParts = a.split(".").map(Number);
const bParts = b.split(".").map(Number);
for (let i = 0; i < aParts.length; i++) {
if (aParts[i] !== bParts[i]) {
return aParts[i] - bParts[i];
}
}
return 0;
}

/**
* Chooses the version a pull request merged into main is released in.
* This mirrors get_candidate_fix_version() in dev/merge_arrow_pr.py.
*
* @param {Array<String>} versions - The titles of the open milestones.
* @param {Array<String>} maintenanceBranches - The maint-X.Y.Z branches.
* @returns {String} the version or null if no version can be chosen.
*/
function detectFixVersion(versions, maintenanceBranches) {
versions = versions.filter((version) => VERSION_PATTERN.test(version));
const majorVersions = versions.filter((version) => version.endsWith(".0.0"));
if (versions.length > majorVersions.length) {
// If there is a future major release, suggest that
versions = majorVersions;
}
versions = versions
.filter((version) => !maintenanceBranches.includes(`maint-${version}`))
.sort(compareVersions);
return versions.length > 0 ? versions[0] : null;
}

/**
* Removes the PR workflow state labels from the merged pull request.
*
* @param {Object} github
* @param {Object} context
* @param {Object} pullRequest
*/
async function clearStateLabels(github, context, pullRequest) {
for (const label of pullRequest.labels) {
// All PR workflow state labels starts with "awaiting"
if (!label.name.startsWith("awaiting")) {
continue;
}
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
name: label.name
});
} catch (error) {
// The label may be already removed.
if (error.status !== 404) {
throw error;
}
}
}
}

/**
* Assigns the milestone of the release the pull request is part of to the
* GitHub Issue. It's the milestone of the pull request if it has one.
* Otherwise an already assigned milestone is kept or the next major
* release is used.
*
* @param {Object} github
* @param {Object} context
* @param {Object} pullRequest
* @param {Object} issueInfo
*/
async function assignMilestone(github, context, pullRequest, issueInfo) {
if (pullRequest.milestone) {
if (issueInfo.milestone?.number !== pullRequest.milestone.number) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueInfo.number,
milestone: pullRequest.milestone.number
});
}
return;
}
if (issueInfo.milestone) {
return;
}
const milestones = await github.paginate(github.rest.issues.listMilestones, {
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
per_page: 100
});
const branches = await github.paginate(github.rest.repos.listBranches, {
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});
const fixVersion = detectFixVersion(
milestones.map((milestone) => milestone.title),
branches
.map((branch) => branch.name)
.filter((name) => name.startsWith("maint-")));
if (!fixVersion) {
console.log(`No milestone found for GitHub issue #${issueInfo.number}`);
return;
}
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueInfo.number,
milestone: milestones.find((milestone) => milestone.title === fixVersion).number
});
}

/**
* Closes the GitHub Issue and comments which pull request resolved it
* after that. GitHub may have closed the issue already if the pull
* request description has "Closes #XXX".
*
* @param {Object} github
* @param {Object} context
* @param {Object} pullRequest
* @param {Object} issueInfo
*/
async function closeGitHubIssue(github, context, pullRequest, issueInfo) {
const issue = {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueInfo.number,
};
if (issueInfo.state !== "closed") {
await github.rest.issues.update({...issue, state: "closed"});
}
// The merge script may have added the same comment.
const body = `Issue resolved by pull request ${pullRequest.number}\n${pullRequest.html_url}`;
const comments = await github.paginate(
github.rest.issues.listComments, {...issue, per_page: 100});
if (comments.some((comment) => comment.body === body)) {
return;
}
await github.rest.issues.createComment({...issue, body});
}

/**
* Does what is needed after a pull request is merged.
*
* @param {Object} github
* @param {Object} context
* @param {Object} pullRequest
*/
async function resolve(github, context, pullRequest) {
await clearStateLabels(github, context, pullRequest);
const issue = helpers.detectIssue(pullRequest.title);
if (!issue || issue.kind !== "github") {
return;
}
const issueInfo = await helpers.getGitHubInfo(github, context, issue.id, pullRequest.number);
if (!issueInfo) {
throw new Error(`GitHub issue #${issue.id} could not be retrieved.`);
}
await assignMilestone(github, context, pullRequest, issueInfo);
await closeGitHubIssue(github, context, pullRequest, issueInfo);
}

/**
* This is for pushes to main. A pushed commit that isn't the merge
* commit of a pull request (e.g. a version bump by a release manager)
* is ignored.
*/
module.exports = async ({github, context}) => {
for (const commit of context.payload.commits) {
const {data: pullRequests} =
await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: commit.id
});
for (const pullRequest of pullRequests) {
if (pullRequest.merge_commit_sha === commit.id) {
await resolve(github, context, pullRequest);
}
}
}
};

module.exports.detectFixVersion = detectFixVersion;
module.exports.resolve = resolve;
58 changes: 58 additions & 0 deletions .github/workflows/dev_pr_merge_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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
#
# http://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: Dev PR merge check

on:
pull_request:
branches:
- main
types:
- opened
- edited
- reopened
- synchronize

concurrency:
group: ${{ github.workflow }}-${{ github.repository }}-${{ github.event.number }}
cancel-in-progress: true

permissions:
contents: read
issues: read

jobs:
merge-check:
# This is a required status check of the main branch in
# .asf.yaml: the merge button is disabled while this fails. Re-run
# this job after you fix the issue (e.g. assign it) because
# changes of the issue don't trigger this workflow.
name: Merge check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
sparse-checkout: .github/workflows/dev_pr

- name: Check title and issue
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const script = require(`${process.env.GITHUB_WORKSPACE}/.github/workflows/dev_pr/merge_check.js`);
await script({github, context, core});
Loading
Loading