Skip to content

Commit

Permalink
[BOT] Implement ci bot using github-script
Browse files Browse the repository at this point in the history
### Motivation

`GitHub Action` is deprecated! Implement ci bot using `actions/github-script`, instead.

https://github.com/actions/github

https://github.com/actions/github-script

### Changes

Implement ci using `actions/github-script`, based on https://github.com/zymap/bot/tree/v1.0.1

Reviewers: Jia Zhai <[email protected]>, Enrico Olivelli <[email protected]>, Yong Zhang <[email protected]>, Sijie Guo <None>

This closes apache#2352 from lamber-ken/bot-v2
  • Loading branch information
lamberken authored Jun 8, 2020
1 parent 2a24be6 commit b1c7232
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 8 deletions.
31 changes: 31 additions & 0 deletions .github/actions/bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "github-action-bot",
"description": "Bot for github actions",
"version": "1.1.1",
"author": "GitHub",
"license": "Apache LICENSE 2.0",
"main": "dist/index.js",
"private": true,
"scripts": {
"build": "ncc build src/run.js"
},
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/github": "^2.2.0",
"@actions/io": "^1.0.2"
},
"devDependencies": {
"@types/jest": "^25.1.4",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"@zeit/ncc": "^0.22.0",
"eslint": "^7.0.0",
"eslint-config-prettier": "^6.11.0",
"husky": "^4.2.5",
"jest": "^25.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"ts-jest": "^25.2.1",
"typescript": "^3.8.3"
}
}
69 changes: 69 additions & 0 deletions .github/actions/bot/src/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.
*/

async function run(core, context, github) {

try {
const owner = process.env.PROVIDER;
const repo = process.env.REPOSITORY;
const reRunCmd = process.env.RERUN_CMD;
const comment = context.payload.comment.body;

if (comment !== reRunCmd) {
console.log("this is not a bot command");
return;
}

const {
data: {
head: {
sha: prRef,
}
}
} = await github.pulls.get({
owner,
repo,
pull_number: context.issue.number,
});

const jobs = await github.checks.listForRef({
owner,
repo,
ref: prRef,
status: "completed"
});

jobs.data.check_runs.forEach(job => {
if (job.conclusion === 'failure' || job.conclusion === 'cancelled') {
console.log("rerun job " + job.name);
github.checks.rerequestSuite({
owner,
repo,
check_suite_id: job.check_suite.id
})
}
});
} catch (e) {
core.setFailed(e);
}

}

module.exports = ({core}, {context}, {github}) => {
return run(core, context, github);
}
39 changes: 31 additions & 8 deletions .github/workflows/bot.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#
# 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: Bot tests
on:
issue_comment:
Expand All @@ -8,14 +27,18 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: clone repository
uses: actions/checkout@v2

- name: Bot actions
uses: zymap/bot@v1.0.1
- name: bot actions
uses: actions/github-script@v1
env:
GITHUB_TOKEN: ${{ secrets.BKBOT_TOKEN }}
PROVIDER: 'apache'
REPOSITORY: 'bookkeeper'
RERUN_CMD: 'rerun failure checks'
with:
repo_owner: apache
repo_name: bookkeeper
rerun_cmd: rerun failure checks
comment: ${{ github.event.comment.body }}
github-token: ${{secrets.BKBOT_TOKEN}}
script: |
const path = require('path')
const scriptPath = path.resolve('.github/actions/bot/src/run.js')
require(scriptPath)({core}, {context}, {github})
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ tools/all/src/main/resources

# Exclude versionBackup file (generated by `mvn versions:set`)
**/*.versionsBackup

node_modules
package-lock.json

0 comments on commit b1c7232

Please sign in to comment.