danger-js icon indicating copy to clipboard operation
danger-js copied to clipboard

Support for Flutter - Dart

Open kuyazee opened this issue 3 years ago • 3 comments

Are there any plans to add support for Flutter? If not, are there any guides I can use to possibly build this ~~if I can~~

kuyazee avatar Feb 02 '21 09:02 kuyazee

No plans ( I don't use Flutter) but there's a tutorial on how to make your own Danger language implementation here: https://danger.systems/js/usage/danger-process.html

If you get something up and running ping me and we can talk about getting it into this org and being made more official 👍🏻

orta avatar Feb 02 '21 09:02 orta

We're using Danger JS with our Flutter project. It might be nice in the future to write our Dangerfile in Dart, but the JavaScript implementation works well for us today! We're just parsing the results of running flutter analyze.

Here's our dangerfile.js in full:

import { danger, fail, warn } from 'danger'
const readline = require('readline')
const fs = require('fs')
const path = require('path')

class FlutterAnalyzerMessage {
  constructor(message) {
    const found = message.match(/\[(\w+)\] (.+) \((.+):(\d+):(\d+)\)/)
    const baseDir = process.cwd().replace('/danger', '')
    this.type = found[1]
    this.body = found[2]
    this.path = path.relative(baseDir, found[3])
    this.line = found[4]
    this.char = found[5]
  }
}

const flutterAnalysisPath = 'flutter-analyze-log'

if (fs.existsSync(flutterAnalysisPath)) {
  const readInterface = readline.createInterface({
    input: fs.createReadStream(flutterAnalysisPath),
    output: process.stdout,
    console: false,
  })

  readInterface.on('line', (line) => {
    const message = new FlutterAnalyzerMessage(line)
    warn(message.body, message.path, message.line)
  })
}

And here's an example GitHub Action that uses Danger:

name: PR checks

on:
  pull_request:
  push:
    branches:
      - main
      - release/**

jobs:
  analyze:
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: '12.x'
      - uses: subosito/flutter-action@v1
        with:
          channel: stable
      - run: flutter analyze --write=danger/flutter-analyze-log
      - uses: actions/setup-node@v2
        if: ${{ failure() }}
        with:
          node-version: '>=16'
      - name: Danger
        if: ${{ failure() }}
        working-directory: danger
        env:
          GITHUB_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN }}
        run: npm install && npm run danger-ci

maxlapides avatar Jun 29 '21 21:06 maxlapides

I've already created and used Danger.dart for a while, but doesn't have time to write documentation.

For the analyzer result, you can create easy util to read it.

void readFlutterAnalyzeReport() {
  final file = File('./test_reports/flutter_analyze_report');
  if (!file.existsSync()) return;

  final lines = file
      .readAsLinesSync()
      .map((l) => l.trim())
      .where(
          (l) => l.isNotEmpty && !l.contains('lib/generated/') && !l.contains('No issues found!'))
      .toList();

  if (lines.isNotEmpty) {
    lines.forEach((line) {
      if (line.split(' ')[0] == 'info') {
        warn('🐛 $line If it\'s fine you can ignore this comment.');
      } else {
        fail('🐞 $line');
      }
    });
  }
}

HelloCore avatar Aug 04 '21 14:08 HelloCore