Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mergebot #1023

Merged
merged 16 commits into from
Jul 1, 2024
Merged
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions .knip.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"entry": ["src/index.ts", "src/types/*.ts"],
"project": "**/*.ts",
"ignoreDependencies": ["@qiwi/npm-types"]
},
"packages/mergebot": {
"entry": ["src/index.ts"],
"project": "**/*.ts"
}
},
"ignore": ["**/fixtures/**", "**/testsource/**", "**/dtslint/test/*/**", "**/*.d.ts"],
Expand Down
21 changes: 21 additions & 0 deletions packages/mergebot/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Ryan Cavanaugh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
152 changes: 152 additions & 0 deletions packages/mergebot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
This is the bot that controls the workflow of Definitely Typed PRs.

## Meta


* __State:__ Production
* __Dashboard:__ [Azure](https://ms.portal.azure.com/#@72f988bf-86f1-41af-91ab-2d7cd011db47/resource/subscriptions/57bfeeed-c34a-4ffd-a06b-ccff27ac91b8/resourceGroups/dtmergebot/providers/Microsoft.Web/sites/DTMergeBot) — [Logs](https://ms.portal.azure.com/#blade/WebsitesExtension/FunctionsIFrameBlade/id/%2Fsubscriptions%2F57bfeeed-c34a-4ffd-a06b-ccff27ac91b8%2FresourceGroups%2Fdtmergebot%2Fproviders%2FMicrosoft.Web%2Fsites%2FDTMergeBot) — [GH Actions](https://github.com/DefinitelyTyped/dt-mergebot/actions) — [GH Webhook](https://github.com/DefinitelyTyped/DefinitelyTyped/settings/hooks/193097250)

TODO: Update these links for dtmergebot2

It is both a series of command line scripts which you can use to test different states, and an Azure Function App which handles incoming webhooks from the DefinitelyTyped repo.

This repo is deployed to Azure on every push to master.
To ensure we can handle timeouts on older PRs, there is a [GitHub Action](https://github.com/DefinitelyTyped/dt-mergebot/actions) that runs the bot every 6 hours against [all open PRs](./src/run.ts), and has a bunch of useful flags for running manually too.

### Tasks for Moving to DT-tools

1. Update local tsconfig to use ../tsconfig-base
1. Update .knip settings with the actual entrypoint(s).
2. Check out the publish-on-push action Jake talked about.
3. Make sure name change from dt-mergebot to mergebot happens everywhere.
4. Log into Azure and make sure it's expecting uploads from the new repo.
5. Set up base *test* tsconfig.

# Setup

```sh
# Clone it
git clone https://github.com/DefinitelyTyped/dt-mergebot.git
cd dt-mergebot

# Deps
npm install

# Validate it works
npm test
```

# How the app works

There are three main stages once the app has a PR number:

* Query the GitHub GraphQL API for PR metadata ([`pr-info`](src/pr-info.ts))
* Create a PR actions metadata object ([`compute-pr-actions`](src/compute-pr-actions.ts))
* Do work based on the resulting actions ([`execute-pr-actions`](src/execute-pr-actions.ts))

# How the bot works

There is an Azure function in `PR-Trigger` that receives webhooks; this function's job is to find the PR number then it runs the above steps.

# Running Locally

You _probably_ don't need to do this. Use test to validate any change inside the src dir against integration tests.

However, you need to have a GitHub API access key in either: `DT_BOT_AUTH_TOKEN`, `BOT_AUTH_TOKEN` or `AUTH_TOKEN`.
Ask Ryan for the bot's auth token (TypeScript team members: Look in the team OneNote).
Don't run the bot under your own auth token as this will generate a bunch of spam from duplicate comments.

```sh
# Windows
set BOT_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

# *nix
export BOT_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

Then to run locally you'll need to install the [Azure Functions cli](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash).

# Development

```sh
# Build
npm run build

# Run the CLI to see what would happen to an existing PR
npm run single-info -- [PR_NUM]
# or
npm run single-info-debug -- [PR_NUM]
```

### If you update any queries

Run this to update the generate types:

```sh
# Code-gen the schema
npm run graphql-schema
```

### If you change project columns or labels

Run this to update the cached values:

```sh
# Regenerate src/_tests/cachedQueries.json
npm run update-test-data
```

# Tests

```sh
# Run tests, TypeScript is transpiled at runtime
npm test
```

Most of the tests run against a fixtured PR, these are high level integration tests that store the PR info and then re-run the latter two phases of the app.

To create fixtures of a current PR:

```sh
# To create a fixture for PR 43161
npm run create-fixture -- 43161
```

Then you can work against these fixtures offline with:

```sh
# Watch mode for all tests
npm test -- --watch
# Just run fixtures for one PR
npm test -- --testNamePattern 44299
```

Run a test with the debugger:

```sh
node --inspect --inspect-brk ./node_modules/.bin/jest -i --runInBand --testNamePattern 44299
```

Then use "Attach to Process ID" to connect to that test runner

If your changes require re-creating all fixtures:

```sh
npm run update-all-fixtures
```

Be careful with this, because PRs may now be in a different state e.g. it's now merged and it used to be a specific
weird state.

## Running with real webhooks

You need a tool like [ngrok](https://ngrok.com) to expose a URL from the [webhooks section](https://github.com/DefinitelyTyped/DefinitelyTyped/settings/hooks/new) on DT.

Start two terminal sessions with:

- `yarn watch` (for TypeScript changes)
- `yarn start` (for the app)

Then start a third with your localhost router like ngrok:

- `ngrok http 7071`
17 changes: 17 additions & 0 deletions packages/mergebot/apollo.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
client: {
includes: ["src/queries/**"],
service: {
name: "github",
url: "https://api.github.com/graphql",
headers: {
authorization: `Bearer ${
process.env["DT_BOT_AUTH_TOKEN"] ||
process.env["BOT_AUTH_TOKEN"] ||
process.env["AUTH_TOKEN"]
}`,
accept: "application/vnd.github.starfox-preview+json, application/vnd.github.bane-preview+json",
},
}
}
};
Loading
Loading