You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "kou (via GitHub)" <gi...@apache.org> on 2023/02/22 23:25:06 UTC

[GitHub] [arrow] kou commented on a diff in pull request #34161: GH-33977: [Dev] PR Workflow automation bot

kou commented on code in PR #34161:
URL: https://github.com/apache/arrow/pull/34161#discussion_r1115080264


##########
.github/workflows/pr_review_trigger.yml:
##########
@@ -0,0 +1,32 @@
+# 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: "Label when reviewed"
+on: pull_request_review
+
+jobs:
+  # due to GitHub actions permissions we can't change labels on the pull_request_review

Review Comment:
   ```suggestion
     # due to GitHub Actions permissions we can't change labels on the pull_request_review
   ```



##########
dev/archery/archery/bot.py:
##########
@@ -82,6 +87,121 @@ def parse_args(self, ctx, args):
 group = partial(click.group, cls=Group)
 
 
+LABEL_PREFIX = "awaiting"
+
+
+@enum.unique
+class PullRequestState(enum.Enum):
+    """State of a pull request."""
+
+    review = f"{LABEL_PREFIX} review"
+    committer_review = f"{LABEL_PREFIX} committer review"
+    changes = f"{LABEL_PREFIX} changes"
+    change_review = f"{LABEL_PREFIX} change review"
+    merge = f"{LABEL_PREFIX} merge"
+
+
+COMMITTER_ROLES = {'OWNER', 'MEMBER'}
+
+
+class PullRequestWorkflowBot:
+
+    def __init__(self, event_name, event_payload, token=None):
+        self.github = github.Github(token)
+        self.event_name = event_name
+        self.event_payload = event_payload
+
+    @cached_property
+    def pull(self):
+        """
+        Returns a github.PullRequest object associated with the event.
+        """
+        return self.repo.get_pull(self.event_payload['pull_request']['number'])
+
+    @cached_property
+    def repo(self):
+        return self.github.get_repo(self.event_payload['repository']['id'], lazy=True)
+
+    def handle(self):
+        current_state = None
+        try:
+            current_state = self.get_current_state()
+        except EventError:
+            # In case of error (more than one state) we clear state labels
+            # only possible if a label has been manually added.
+            self.clear_current_state()
+        new_state = self.get_target_state(current_state)
+        if current_state != new_state.value:
+            if current_state:
+                self.clear_current_state()
+            self.set_state(new_state)
+
+    def get_current_state(self):
+        """
+        Returns a string with the current PR state label

Review Comment:
   How about returning `PullRequestState` instead of string?
   If we return `PullRequestState` here, we can consider all `state`s are `PullRequestState` instead of `PullRequestState` or string.



##########
.github/workflows/pr_bot.yml:
##########
@@ -0,0 +1,97 @@
+# 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: "Workflow label bot"
+on:
+  pull_request_target:
+    types:
+      - opened
+      - converted_to_draft
+      - ready_for_review
+      - synchronize
+  workflow_run:
+    workflows: ["Label when reviewed"]
+    types: ['completed']
+
+permissions:
+  contents: read
+  pull-requests: write
+  issues: write
+
+jobs:
+  pr-workflow-bot-job:
+    name: "PR Workflow bot"
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout Arrow
+        uses: actions/checkout@v3
+        with:
+          path: arrow
+          repository: apache/arrow
+          ref: main
+          persist-credentials: false
+          # fetch the tags for version number generation
+          fetch-depth: 0
+      - name: Set up Python
+        uses: actions/setup-python@v4
+        with:
+          python-version: 3.8
+      - name: Install Archery and Crossbow dependencies
+        run: pip install -e arrow/dev/archery[bot]
+      - name: 'Download PR review payload'
+        id: 'download'
+        if: github.event_name == 'workflow_run'
+        uses: actions/github-script@v6
+        with:
+          script: |
+            const run_id = "${{ github.event.workflow_run.id }}";
+            let artifacts = await github.rest.actions.listWorkflowRunArtifacts({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              run_id: run_id,
+            });
+            let pr_review_artifact = artifacts.data.artifacts.filter((artifact) => {
+              return artifact.name == "pr_review_payload"
+            })[0];
+            let pr_review_download = await github.rest.actions.downloadArtifact({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              artifact_id: pr_review_artifact.id,
+              archive_format: 'zip',
+            });
+            var fs = require('fs');
+            fs.writeFileSync('${{github.workspace}}/pr_review.zip', Buffer.from(pr_review_download.data));
+      - name: Extract artifact
+        id: extract
+        if: github.event_name == 'workflow_run'
+        run: |
+          unzip pr_review.zip
+          echo "pr_review_path=$(pwd)/event.json" >> $GITHUB_OUTPUT
+      - name: Handle PR workflow event
+        env:
+          ARROW_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        run: |
+          if ${{ github.event_name == 'workflow_run' }}; then

Review Comment:
   How about using normal shell condition instead of GitHub Actions expression? `if true; then`/`if false; then` work as expected but it's not straightforward. They work because there are `/usr/bin/true` and `/usr/bin/false`.
   
   ```suggestion
             if [ "${GITHUB_EVENT_NAME}" = "workflow_run" ]; then
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org