Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit a41c32e

Browse files
authored
Merge pull request #3 from machine-learning-apps/deploy-functionality
Deploy functionality
2 parents c7a956a + af7d4eb commit a41c32e

11 files changed

Lines changed: 578 additions & 126 deletions

File tree

.github/workflows/test.yaml

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,94 @@ name: Tests
33
on: [push]
44

55
jobs:
6+
build-temp-container:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@master
10+
11+
- name: build-temp-container
12+
run: |
13+
echo ${PASSWORD} | docker login -u $USERNAME --password-stdin
14+
docker build -t hamelsmu/chatops:temp -f prebuild.Dockerfile .
15+
docker push hamelsmu/chatops:temp
16+
env:
17+
USERNAME: ${{ secrets.DOCKER_USERNAME }}
18+
PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
19+
620
test-static-payload:
21+
needs: [build-temp-container]
722
runs-on: ubuntu-latest
823
steps:
924

1025
- uses: actions/checkout@master
11-
26+
27+
- name: test
28+
uses: docker://hamelsmu/chatops:temp
29+
env:
30+
INPUT_APP_PEM: ${{ secrets.APP_PEM }}
31+
INPUT_APP_ID: ${{ secrets.APP_ID }}
32+
INPUT_TRIGGER_PHRASE: "/test-trigger-comment"
33+
INPUT_INDICATOR_LABEL: "test-label"
34+
INPUT_TEST_EVENT_PATH: "tests/pr_comment_payload.json"
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: emit-output
38+
uses: docker://hamelsmu/chatops:temp
39+
id: nolabel
40+
env:
41+
INPUT_APP_PEM: ${{ secrets.APP_PEM }}
42+
INPUT_APP_ID: ${{ secrets.APP_ID }}
43+
INPUT_TRIGGER_PHRASE: "/full-test-run"
44+
INPUT_TEST_EVENT_PATH: "tests/pr_comment_stuffed.json"
45+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
47+
- name: emit-output2
48+
uses: docker://hamelsmu/chatops:temp
49+
id: nolabel2
50+
env:
51+
INPUT_APP_PEM: ${{ secrets.APP_PEM }}
52+
INPUT_APP_ID: ${{ secrets.APP_ID }}
53+
INPUT_TRIGGER_PHRASE: "/something-random-no-trigger"
54+
INPUT_TEST_EVENT_PATH: "tests/pr_comment_stuffed.json"
55+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56+
57+
- name: test-output
58+
run: |
59+
import os
60+
assert os.getenv('TRAILING_LINE') == "foo bar hello"
61+
assert os.getenv('TRAILING_TOKEN') == "foo"
62+
assert os.getenv('PULL_REQUEST_NUMBER') == '3'
63+
assert os.getenv('BOOL_TRIGGERED') == 'True'
64+
assert os.getenv('COMMENTER_USERNAME') == "hamelsmu"
65+
assert os.getenv('BRANCH_NAME') == "deploy-functionality"
66+
assert os.getenv('BOOL_TRIGGERED_2') == 'False'
67+
shell: python
68+
env:
69+
TRAILING_LINE: ${{ steps.nolabel.outputs.TRAILING_LINE }}
70+
TRAILING_TOKEN: ${{ steps.nolabel.outputs.TRAILING_TOKEN }}
71+
PULL_REQUEST_NUMBER: ${{ steps.nolabel.outputs.PULL_REQUEST_NUMBER }}
72+
BOOL_TRIGGERED: ${{ steps.nolabel.outputs.BOOL_TRIGGERED }}
73+
COMMENTER_USERNAME: ${{ steps.nolabel.outputs.COMMENTER_USERNAME }}
74+
BRANCH_NAME: ${{ steps.nolabel.outputs.BRANCH_NAME }}
75+
BOOL_TRIGGERED_2: ${{ steps.nolabel2.outputs.BOOL_TRIGGERED }}
76+
1277
- name: pre-build action image
1378
run: |
1479
cd $GITHUB_WORKSPACE
1580
echo ${PASSWORD} | docker login -u $USERNAME --password-stdin
16-
docker build -t hamelsmu/chatops-workaround -f prebuild.Dockerfile .
17-
docker push hamelsmu/chatops-workaround
81+
docker build -t hamelsmu/chatops -f prebuild.Dockerfile .
82+
docker push hamelsmu/chatops
1883
env:
1984
USERNAME: ${{ secrets.DOCKER_USERNAME }}
2085
PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
2186

22-
- name: test
87+
- name: final-test
2388
uses: ./
2489
with:
2590
APP_PEM: ${{ secrets.APP_PEM }}
2691
APP_ID: ${{ secrets.APP_ID }}
2792
TRIGGER_PHRASE: "/test-trigger-comment"
2893
INDICATOR_LABEL: "test-label"
2994
TEST_EVENT_PATH: "tests/pr_comment_payload.json"
95+
env:
96+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM hamelsmu/chatops-workaround
1+
FROM hamelsmu/chatops
22

33
ENTRYPOINT ["python", "/label_app.py"]

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ jwt = "*"
1212
requests = "*"
1313
ipykernel = "*"
1414
jupyter = "*"
15+
pyjwt = "*"
16+
ipdb = "*"
1517

1618
[requires]
1719
python_version = "3.7"

Pipfile.lock

Lines changed: 26 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
![Actions Status](https://github.com/machine-learning-apps/actions-chatops-workaround/workflows/Tests/badge.svg)
1+
![Actions Status](https://github.com/machine-learning-apps/actions-chatops/workflows/Tests/badge.svg)
22

33
# Trigger Actions With ChatOps via PR Labels or Deployements
44

5-
This action helps you trigger downstream actions with a custom command made via a comment in a pull request, otherwhise known as [ChatOps](https://www.pagerduty.com/blog/what-is-chatops/). This Action is similar to [this chatops action](https://github.com/machine-learning-apps/actions-chatops), except that this Action can defer to GitHub App to (1) Make labels on a PR or (2) Create a [Deployment](https://developer.github.com/v3/repos/deployments/#create-a-deployment). The reasons you might want to do this are the following:
65

7-
- By deferring to a GitHub App to create a pull request label or a deployment event, you can trigger a downstream GitHub Action that runs in the context of a pull request. The reason for using a GitHub App is Actions cannot trigger other Actions.
8-
- It can be important to run in the context of a pull request as that is the only way you will see [Check Runs](https://developer.github.com/v3/checks/runs/) for your pull request, so you can monitor the status of your workflows.
9-
- Note that comments on PRs [triggers Actions workflows on the default branch](https://help.github.com/en/articles/events-that-trigger-workflows#issue-comment-event-issue_comment) which is not desireable for ChatOps most of the time. This Action resolves this issue by piggybacking on a GitHub app to create an event that will trigger Actions workflows on the pull request's branch instead.
10-
- This can prevent you from accidentally executing the chatops command twice as the label event will not fire if the PR is already labeled or the deployment has already been created.
6+
This action helps you trigger downstream actions with a custom command made via a comment in a pull request, otherwhise known as [ChatOps](https://www.pagerduty.com/blog/what-is-chatops/).
117

12-
Therefore, one of the required inputs is the secret key `APP_PEM` for authenticating as a GitHub App which has the following permissions:
13-
- [Deployment](https://developer.github.com/v3/apps/permissions/#permission-on-deployments): read & write
14-
- [Pull Requests](https://developer.github.com/v3/apps/permissions/#permission-on-pull-requests): read & write
8+
Optionally, you may provide credentials to authenticate as a GitHub App and label an issue once a trigger phrase is detected. Having another app other than the GitHub Action apply a label allows you to create a label event to trigger downstream Actions (since an Action cannot create events that trigger other Actions).
159

1610

1711
## Example Usage
@@ -24,17 +18,34 @@ jobs:
2418
label-pr:
2519
runs-on: ubuntu-latest
2620
steps:
27-
- uses: actions/checkout@master
28-
- name: test
29-
uses: machine-learning-apps/actions-chatops-workaround@master
21+
- name: listen for PR Comments
22+
uses: machine-learning-apps/actions-chatops@master
3023
with:
3124
APP_PEM: ${{ secrets.APP_PEM }}
3225
APP_ID: ${{ secrets.APP_ID }}
3326
TRIGGER_PHRASE: "/test-trigger-comment"
3427
INDICATOR_LABEL: "test-label"
28+
env: # you must supply GITHUB_TOKEN
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
31+
# This step clones the branch of the PR associated with the triggering phrase, but only if it is triggered.
32+
- name: clone branch of PR
33+
if: steps.prcomm.outputs.TRIGGERED == 'true'
34+
uses: actions/checkout@master
35+
with:
36+
ref: ${{ steps.prcomm.outputs.SHA }}
37+
38+
# This step is a toy example that illustrates how you can use outputs from the pr-command action
39+
- name: print variables
40+
if: steps.prcomm.outputs.TRIGGERED == 'true'
41+
run: echo "${USERNAME} made a triggering comment on PR# ${PR_NUMBER} for ${BRANCH_NAME}"
42+
env:
43+
BRANCH_NAME: ${{ steps.prcomm.outputs.BRANCH_NAME }}
44+
PR_NUMBER: ${{ steps.prcomm.outputs.PULL_REQUEST_NUMBER }}
45+
USERNAME: ${{ steps.prcomm.outputs.COMMENTER_USERNAME }}
3546
```
3647
37-
A demonstration of this in action can be found on [this PR](https://github.com/machine-learning-apps/actions-chatops-workaround/pull/2).
48+
A demonstration of this in action can be found on [this PR](https://github.com/machine-learning-apps/actions-chatops/pull/2).
3849
3950
## Mandatory Inputs
4051
@@ -52,9 +63,27 @@ A demonstration of this in action can be found on [this PR](https://github.com/m
5263

5364
- `INDICATOR_LABEL`:
5465
- description: label that wil be added to the PR if a triggering comment is detected. This is used to trigger downstream Actions with the right context of the PR.
55-
- required: true
66+
- required: false
67+
- default: ""
5668

5769
- `TEST_EVENT_PATH`:
5870
- description: An alternate place to fetch the payload for testing and debugging when making changes to this Action. This is set to they system environment variable $GITHUB_EVENT_PATH by default.
5971
- require: false
60-
default: ""
72+
- default: ""
73+
74+
75+
## Outputs
76+
77+
- `TRAILING_LINE:`: the text that immediately follows the triggering phrase that is on the same line. For example, "/trigger-phrase foo bar\n next line" will emit the value "foo bar" This is intended to be used as arguments for downstream actions.
78+
79+
- `TRAILING_TOKEN:`: this is the next token that immediately follows the triggering phrase that is on the same line. For example, "/trigger-phrase foo bar" will emit the value "foo". This is intended to be used as arguments for downstream actions.
80+
81+
- `PULL_REQUEST_NUMBER`: the number of the pull request
82+
83+
- `COMMENTER_USERNAME`: The GitHub username of the person that made the triggering comment in the PR.
84+
85+
- `BRANCH_NAME`: The name of the branch corresponding to the PR.
86+
87+
- `SHA`: The SHA of the branch on the PR at the time the triggering comment was made.
88+
89+
- `BOOL_TRIGGERED`: true or false depending on if the trigger phrase was detected and this is a pull request.

action.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name: 'ChatOps Via PR Labels'
2-
description: Trigger PR labels with chatops and a GitHub App, which is a work-around to trigger downstream Actions with the right context.
1+
name: 'ChatOps For Actions'
2+
description: Listen for special comments, or chatops commands in the comments of a PR.
33
author: Hamel Husain
44
inputs:
55
APP_PEM:
66
description: string version of your PEM file used to authenticate as a GitHub App
7-
required: true
7+
required: false
88
APP_ID:
99
description: you GITHUB App ID.
1010
required: true
@@ -13,13 +13,29 @@ inputs:
1313
required: true
1414
INDICATOR_LABEL:
1515
description: label that wil be added to the PR if a triggering comment is detected. This is used to trigger downstream Actions with the right context of the PR.
16-
required: true
16+
required: false
17+
default: ""
1718
TEST_EVENT_PATH:
1819
description: An alternate place to fetch the payload for testing and debugging when making changes to this Action. This is set to they system environment variable $GITHUB_EVENT_PATH by default.
1920
require: false
2021
default: ""
22+
outputs:
23+
TRAILING_LINE:
24+
description: the text that immediately follows the triggering phrase that is on the same line. For example, "/trigger-phrase foo bar\n next line" will emit the value "foo bar" This is intended to be used as arguments for downstream actions.
25+
TRAILING_TOKEN:
26+
description: this is the next token that immediately follows the triggering phrase that is on the same line. For example, "/trigger-phrase foo bar" will emit the value "foo". This is intended to be used as arguments for downstream actions.
27+
BOOL_TRIGGERED:
28+
description: true or false depending on if the trigger phrase was detected and this is a pull request.
29+
PULL_REQUEST_NUMBER:
30+
description: the number of the pull request
31+
COMMENTER_USERNAME:
32+
description: The GitHub username of the person that made the triggering comment in the PR.
33+
BRANCH_NAME:
34+
description: The name of the branch corresponding to the PR.
35+
SHA:
36+
description: The SHA of the branch on the PR at the time the triggering comment was made.
2137
branding:
22-
color: 'blue'
38+
color: 'gray-dark'
2339
icon: 'message-square'
2440
runs:
2541
using: 'docker'

bootstrap.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/bin/bash
2-
docker build -t hamelsmu/chatops-workaround -f prebuild.Dockerfile .
2+
docker build -t hamelsmu/chatops -f prebuild.Dockerfile .
33

4-
INPUT_TRIGGER_PHRASE="making"
4+
INPUT_TRIGGER_PHRASE="/test-trigger-comment"
55
INPUT_INDICATOR_LABEL="test-successfull"
66
TEST_EVENT_PATH="tests/pr_comment_payload.json"
77
GITHUB_REPOSITORY="machine-learning-apps/actions-ml-cicd"
88

9-
docker push hamelsmu/chatops-workaround
9+
docker push hamelsmu/chatops
1010

1111
docker run \
1212
-e INPUT_TRIGGER_PHRASE=$INPUT_TRIGGER_PHRASE \
1313
-e INPUT_INDICATOR_LABEL="$INPUT_INDICATOR_LABEL" \
1414
-e INPUT_APP_PEM="${INPUT_APP_PEM}" \
1515
-e INPUT_APP_ID="$INPUT_APP_ID" \
16-
-e INPUT_TEST_EVENT_PATH="${GITHUB_EVENT_PATH}" \
16+
-e INPUT_TEST_EVENT_PATH="${TEST_EVENT_PATH}" \
1717
-e GITHUB_REPOSITORY="$GITHUB_REPOSITORY" \
18-
hamelsmu/chatops-workaround
18+
hamelsmu/chatops

0 commit comments

Comments
 (0)