Skip to content

Commit

Permalink
Merge pull request #27 from gimenete/filter-environment
Browse files Browse the repository at this point in the history
Filter environment
  • Loading branch information
parkr authored Feb 12, 2019
2 parents ec328c7 + 1e57713 commit 46ffca7
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 4 deletions.
20 changes: 20 additions & 0 deletions filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ action "tag-filter" {
}
```

### environment

Continue if the event payload includes a matching environment.

```workflow
action "environment-filter" {
uses = "actions/bin/filter@master"
args = "environment production"
}
```

This also supports multiple environments.

```workflow
action "environment-filter" {
uses = "actions/bin/filter@master"
args = ["environment", "staging", "production"]
}
```

## License

The Dockerfile and associated scripts and documentation in this project are released under the [MIT License](LICENSE).
Expand Down
21 changes: 21 additions & 0 deletions filter/bin/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

set -e

filter='[.deployment.environment]'

if [ -z "$GITHUB_EVENT_PATH" ]; then
echo "\$GITHUB_EVENT_PATH" not found
exit 1
fi

matches=$(jq -r "$filter" "$GITHUB_EVENT_PATH")

for environment in "$@"; do
if echo "$matches" | grep -Eq "\"$environment\""; then
exit 0
fi
done

echo "environment does not match"
exit 78
3 changes: 0 additions & 3 deletions filter/test/action.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ export GITHUB_EVENT_PATH="$BATS_TEST_DIRNAME/fixtures/event.json"
@test "action: does not match" {
run action edited
[ "$status" -eq 78 ]
echo $output
[ "$output" = "action is \"created\", not \"edited\"" ]
}

@test "action: matches a or b" {
run action "deleted|created"
echo $output
[ "$status" -eq 0 ]
[ "$output" = "" ]
}

@test "action: does not match a or b" {
run action "edited|deleted"
echo $output
[ "$status" -eq 78 ]
[ "$output" = "action is \"created\", not \"edited|deleted\"" ]
}
31 changes: 31 additions & 0 deletions filter/test/environment.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats

load bootstrap

PATH="$PATH:$BATS_TEST_DIRNAME/../bin"

export GITHUB_EVENT_PATH="$BATS_TEST_DIRNAME/fixtures/deployment.json"

@test "environment: matches" {
run environment production
[ "$status" -eq 0 ]
[ "$output" = "" ]
}

@test "environment: does not match" {
run environment staging
[ "$status" -eq 78 ]
[ "$output" = "environment does not match" ]
}

@test "environment: matches a or b" {
run environment "production|staging"
[ "$status" -eq 0 ]
[ "$output" = "" ]
}

@test "environment: does not match a or b" {
run environment "development|staging"
[ "$status" -eq 78 ]
[ "$output" = "environment does not match" ]
}
151 changes: 151 additions & 0 deletions filter/test/fixtures/deployment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"deployment": {
"url": "https://api.github.com/repos/Codertocat/Hello-World/deployments/87972451",
"id": 87972451,
"node_id": "MDEwOkRlcGxveW1lbnQ4Nzk3MjQ1MQ==",
"sha": "a10867b14bb761a232cd80139fbd4c0d33264240",
"ref": "master",
"task": "deploy",
"payload": {
},
"environment": "production",
"description": null,
"creator": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2018-05-30T20:18:45Z",
"updated_at": "2018-05-30T20:18:45Z",
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments/87972451/statuses",
"repository_url": "https://api.github.com/repos/Codertocat/Hello-World"
},
"repository": {
"id": 135493233,
"node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=",
"name": "Hello-World",
"full_name": "Codertocat/Hello-World",
"owner": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/Codertocat/Hello-World",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/Codertocat/Hello-World",
"forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks",
"keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams",
"hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks",
"issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}",
"events_url": "https://api.github.com/repos/Codertocat/Hello-World/events",
"assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}",
"branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}",
"tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags",
"blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages",
"stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers",
"contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors",
"subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription",
"commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}",
"compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges",
"archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads",
"issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}",
"pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}",
"releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}",
"deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments",
"created_at": "2018-05-30T20:18:04Z",
"updated_at": "2018-05-30T20:18:35Z",
"pushed_at": "2018-05-30T20:18:44Z",
"git_url": "git://github.com/Codertocat/Hello-World.git",
"ssh_url": "[email protected]:Codertocat/Hello-World.git",
"clone_url": "https://github.com/Codertocat/Hello-World.git",
"svn_url": "https://github.com/Codertocat/Hello-World",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"open_issues_count": 2,
"license": null,
"forks": 0,
"open_issues": 2,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
}
}
1 change: 0 additions & 1 deletion filter/test/user.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ export GITHUB_EVENT_PATH="$BATS_TEST_DIRNAME/fixtures/event.json"
@test "user: does not match" {
run user some-other-user
[ "$status" -eq 78 ]
echo $output
[ "$output" = "user does not match" ]
}

0 comments on commit 46ffca7

Please sign in to comment.