Skip to content

Instantly share code, notes, and snippets.

@HelloCore
Created August 17, 2021 14:54
Show Gist options
  • Save HelloCore/c9eacb63245373e361c736435d271722 to your computer and use it in GitHub Desktop.
Save HelloCore/c9eacb63245373e361c736435d271722 to your computer and use it in GitHub Desktop.
Danger Plugin Label WIP
import 'package:danger_core/danger_core.dart';
import 'package:github/github.dart';
const LABEL_NAME = 'Work in Progress';
class DangerReportWIPGithub {
static Future<void> process() async {
if (danger.github != null) {
final token = danger.settings.github.accessToken;
final github = GitHub(auth: Authentication.withToken(token));
final repoSlug = RepositorySlug(danger.github.thisPR.owner, danger.github.thisPR.repo);
final prNo = danger.github.pr.number;
final prAPI = await github.pullRequests.get(repoSlug, prNo);
var needToRemoveLabel = true;
if (danger.github.pr.title.contains('WIP')) {
needToRemoveLabel = false;
warn('This pr considered Work in Progress');
}
final isLabelExists = prAPI.labels.where((element) => element.name == LABEL_NAME).isNotEmpty;
if (needToRemoveLabel) {
if (isLabelExists) {
final removingLabel = await github.issues.removeLabelForIssue(repoSlug, prNo, LABEL_NAME);
if (!removingLabel) {
warn('Cannot remove label [$LABEL_NAME]');
}
}
} else {
if (!isLabelExists) {
final addingLabel =
(await github.issues.addLabelsToIssue(repoSlug, prNo, [LABEL_NAME])).first;
if (addingLabel == null) {
warn('Cannot add label [$LABEL_NAME]');
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment