Skip to content

Commit

Permalink
feat: define context
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiJingLong committed May 20, 2023
1 parent 498b2d9 commit e3ed8fd
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 17 deletions.
108 changes: 107 additions & 1 deletion packages/github_action_context/lib/src/context.dart
@@ -1,3 +1,109 @@
import 'dart:convert';
import 'dart:io';

import 'interface.dart';

final context = Context();

class Context {}
class Context {
late WebhookPayload payload;

late String eventName;
late String sha;
late String ref;
late String workflow;
late String action;
late String actor;
late String job;
late int runNumber;
late int runId;
late String apiUrl;
late String serverUrl;
late String graphqlUrl;

Context() {
if (Platform.environment.containsKey('GITHUB_EVENT_PATH')) {
String eventPath = Platform.environment['GITHUB_EVENT_PATH']!;
if (File(eventPath).existsSync()) {
String eventJson = File(eventPath).readAsStringSync();
payload = WebhookPayload.fromJson(jsonDecode(eventJson));
} else {
stdout.write('GITHUB_EVENT_PATH $eventPath does not exist\n');
}
}

eventName = Platform.environment['GITHUB_EVENT_NAME']!;
sha = Platform.environment['GITHUB_SHA']!;
ref = Platform.environment['GITHUB_REF']!;
workflow = Platform.environment['GITHUB_WORKFLOW']!;
action = Platform.environment['GITHUB_ACTION']!;
actor = Platform.environment['GITHUB_ACTOR']!;
job = Platform.environment['GITHUB_JOB']!;
runNumber = int.parse(Platform.environment['GITHUB_RUN_NUMBER']!);
runId = int.parse(Platform.environment['GITHUB_RUN_ID']!);
apiUrl = Platform.environment['GITHUB_API_URL'] ?? 'https://api.github.com';
serverUrl =
Platform.environment['GITHUB_SERVER_URL'] ?? 'https://github.com';
graphqlUrl = Platform.environment['GITHUB_GRAPHQL_URL'] ??
'https://api.github.com/graphql';
}

IssueInfo get issue {
final payload = this.payload;

int n;

if (payload.issue != null) {
n = payload.issue!['number'];
} else if (payload.pullRequest != null) {
n = payload.pullRequest!['number'];
} else {
n = payload['number'];
}

return IssueInfo(
owner: repo.owner,
repo: repo.repo,
number: n,
);
}

RepoInfo get repo {
if (Platform.environment.containsKey('GITHUB_REPOSITORY')) {
final repoParts = Platform.environment['GITHUB_REPOSITORY']!.split('/');
return RepoInfo(owner: repoParts[0], repo: repoParts[1]);
}

if (payload.repository != null) {
return RepoInfo(
owner: payload.repository!['owner']['login']!,
repo: payload.repository!['name']!,
);
}

throw Exception(
"context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'");
}
}

class IssueInfo {
final String owner;
final String repo;
final int? number;

IssueInfo({
required this.owner,
required this.repo,
this.number,
});
}

class RepoInfo {
final String owner;
final String repo;

RepoInfo({
required this.owner,
required this.repo,
});
}
71 changes: 56 additions & 15 deletions packages/github_action_context/lib/src/interface.dart
@@ -1,60 +1,94 @@
import 'dart:collection';
import 'dart:convert';

class PayloadRepository {
class _BaseItem with MapMixin<dynamic, dynamic> {
final Map innerMap;

_BaseItem(this.innerMap);

@override
operator [](Object? key) {
return innerMap[key];
}

@override
void operator []=(key, value) {
innerMap[key] = value;
}

@override
void clear() {
innerMap.clear();
}

@override
Iterable get keys => innerMap.keys;

@override
remove(Object? key) {
return innerMap.remove(key);
}
}

class PayloadRepository extends _BaseItem {
String? fullName;
String name;
Owner owner;
String? htmlUrl;

PayloadRepository({
PayloadRepository(
super.innerMap, {
this.fullName,
required this.name,
required this.owner,
this.htmlUrl,
});

factory PayloadRepository.fromJson(Map json) {
static PayloadRepository? fromJson(Map? json) {
if (json == null) {
return null;
}
return PayloadRepository(
json,
fullName: json['full_name'],
name: json['name'],
owner: Owner.fromJson(json['owner']),
htmlUrl: json['html_url'],
);
}

factory PayloadRepository.fromString(String text) {
final json = jsonDecode(text);
return PayloadRepository.fromJson(json);
}
}

class Owner {
class Owner extends _BaseItem {
String login;
String? name;

Owner({
Owner(
super.innerMap, {
required this.login,
this.name,
});

factory Owner.fromJson(Map<String, dynamic> json) {
factory Owner.fromJson(Map json) {
return Owner(
json,
login: json['login'],
name: json['name'],
);
}
}

class WebhookPayload {
Map<String, dynamic> repository;
class WebhookPayload extends _BaseItem {
PayloadRepository? repository;
Map<String, dynamic>? issue;
Map<String, dynamic>? pullRequest;
Map<String, dynamic>? sender;
String? action;
Map<String, dynamic>? installation;
Map<String, dynamic>? comment;

WebhookPayload({
WebhookPayload(
super.innerMap, {
required this.repository,
this.issue,
this.pullRequest,
Expand All @@ -64,9 +98,10 @@ class WebhookPayload {
this.comment,
});

factory WebhookPayload.fromJson(Map<String, dynamic> json) {
factory WebhookPayload.fromJson(Map json) {
return WebhookPayload(
repository: json['repository'],
json,
repository: PayloadRepository.fromJson(json['repository']),
issue: json['issue'],
pullRequest: json['pull_request'],
sender: json['sender'],
Expand All @@ -75,4 +110,10 @@ class WebhookPayload {
comment: json['comment'],
);
}


factory WebhookPayload.fromString(String text) {
final json = jsonDecode(text);
return WebhookPayload.fromJson(json);
}
}
2 changes: 1 addition & 1 deletion packages/github_action_context/pubspec.yaml
Expand Up @@ -5,7 +5,7 @@ repository: https://github.com/dart-action/packages
homepage: https://github.com/dart-action/packages/tree/main/packages/github_action_context

environment:
sdk: ">=2.12.0 <4.0.0"
sdk: "^3.0.0"

dependencies:
github_action_core: ^0.1.1
Expand Down

0 comments on commit e3ed8fd

Please sign in to comment.