Skip to content

Commit

Permalink
feat: use v1/trigger endpoint (#6)
Browse files Browse the repository at this point in the history
* feat: use v1/trigger endpoint

* branch name

* branch name

* branch and commit
  • Loading branch information
arjunattam authored Sep 12, 2024
1 parent 3a4addc commit e0a2d72
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 81 deletions.
22 changes: 1 addition & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,17 @@
uses: empirical-run/dispatch-action@main
with:
build-url: ${{ steps.prev-step.outputs.url }}
slack-webhook-url: https://hooks.slack.com/optional/input
platform: web
platform: web # or android, ios
```
Supported inputs
- [x] build-url: **Required** input, for the URL of the application build
- For web, this points to a URL of the deployment (e.g. `https://staging.your-app.com`)
- For mobile, this points to a downloadable file, ending in `.apk`, `.aab` or `.ipa`
- [x] slack-webhook-url: **Optional** input, for a Slack incoming webhook URL; [learn more](#get-slack-alerts)
- [x] platform: **Optional** input, to specify one of the supported platforms: `web`, `android`, or `ios`. Default is `web`

> Note that this Action only supports whitelisted GitHub organizations. To get access, contact us.

### Get Slack alerts

Configure the `slack-webhook-url` input to get alerts like this.

<img width="517" alt="Slack alerts" src="https://github.com/empirical-run/dispatch-action/assets/284612/32ec902a-c8d0-48d6-afe3-447e0aaec049">

#### Configuration steps

In the following steps, we will create a new incoming webhook for your Slack workspace. For more verbose
steps, follow the [official Slack guide](https://api.slack.com/messaging/webhooks).

- Use [this link](https://api.slack.com/apps?new_app=1) to go to your Slack apps dashboard, and create a new app
- Select "From scratch" in the "Create an app" dialog
- Define an app name, say "Test alerts", and your workspace. Once submitted, your app is created
- In the app settings, click on "Incoming webhooks" in the sidebar
- Click on "Add new webhook to workspace" and choose the channel to send alerts to
- Copy the webhook URL and set it as `slack-webhook-url` in your GitHub Actions YML

## Development

```sh
Expand Down
55 changes: 25 additions & 30 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29237,22 +29237,22 @@ const isValidUrl = (s) => {
const isValidPlatform = (s) => {
return ["web", "ios", "android"].includes(s);
};
const eventType = (platform) => {
switch (platform) {
case "web":
return "run-tests";
case "android":
return "run-tests-android";
case "ios":
return "run-tests-ios";
default:
return "run-tests";
function getCommitSha() {
if (github.context.eventName === 'pull_request') {
// github.context.sha will give sha for the merged commit
return github.context.payload.pull_request.head.sha;
}
};
/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
return github.context.sha;
}
function getBranchName() {
if (github.context.eventName === 'pull_request') {
// github.context.ref will give ref for the merged commit, which is refs/pull/<pr_number>/merge
// so we pick the ref of the `head` from the pull request object
return github.context.payload.pull_request.head.ref;
}
// ref is fully-formed (e.g. refs/heads/<branch_name>)
return github.context.ref.replace("refs/heads/", "");
}
async function run() {
try {
const buildUrl = core.getInput('build-url');
Expand All @@ -29263,28 +29263,26 @@ async function run() {
core.setFailed(`Invalid config: build-url must be a valid URL.`);
}
const slackWebhookUrl = core.getInput('slack-webhook-url');
if (slackWebhookUrl && !isValidUrl(slackWebhookUrl)) {
core.setFailed(`Invalid config: slack-webhook-url must be a valid URL.`);
if (slackWebhookUrl) {
console.log(`Warning: slack-webhook-url is not a supported input, and will be ignored.`);
}
const platform = core.getInput('platform');
if (platform && !isValidPlatform(platform)) {
core.setFailed(`Invalid config: platform must be one of web, android or ios.`);
}
const clientPayload = {
build_url: buildUrl
};
if (slackWebhookUrl) {
clientPayload.slack_webhook_url = slackWebhookUrl;
}
const response = await fetch("https://dispatch.empirical.run", {
const response = await fetch("https://dispatch.empirical.run/v1/trigger", {
method: "POST",
body: JSON.stringify({
repo: {
origin: {
owner: github.context.repo.owner,
name: github.context.repo.repo
},
event_type: eventType(platform),
client_payload: clientPayload
build: {
url: buildUrl,
commit: getCommitSha(),
branch: getBranchName(),
},
platform,
})
});
const content = await response.text();
Expand Down Expand Up @@ -31200,9 +31198,6 @@ var __webpack_exports__ = {};
var exports = __webpack_exports__;

Object.defineProperty(exports, "__esModule", ({ value: true }));
/**
* The entrypoint for the action.
*/
const main_1 = __nccwpck_require__(399);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(0, main_1.run)();
Expand Down
3 changes: 0 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* The entrypoint for the action.
*/
import { run } from './main'

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down
52 changes: 25 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ const isValidPlatform = (s: string) => {
return ["web", "ios", "android"].includes(s);
}

const eventType = (platform: string) => {
switch (platform) {
case "web":
return "run-tests";
case "android":
return "run-tests-android";
case "ios":
return "run-tests-ios";
default:
return "run-tests";
function getCommitSha(): string {
if (github.context.eventName === 'pull_request') {
// github.context.sha will give sha for the merged commit
return github.context.payload.pull_request!.head.sha;
}
return github.context.sha;
}

function getBranchName(): string {
if (github.context.eventName === 'pull_request') {
// github.context.ref will give ref for the merged commit, which is refs/pull/<pr_number>/merge
// so we pick the ref of the `head` from the pull request object
return github.context.payload.pull_request!.head.ref;
}
// ref is fully-formed (e.g. refs/heads/<branch_name>)
return github.context.ref.replace("refs/heads/", "");
}

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
const buildUrl: string = core.getInput('build-url');
Expand All @@ -41,30 +42,27 @@ export async function run(): Promise<void> {
core.setFailed(`Invalid config: build-url must be a valid URL.`)
}
const slackWebhookUrl: string = core.getInput('slack-webhook-url');
if (slackWebhookUrl && !isValidUrl(slackWebhookUrl)) {
core.setFailed(`Invalid config: slack-webhook-url must be a valid URL.`)
if (slackWebhookUrl) {
console.log(`Warning: slack-webhook-url is not a supported input, and will be ignored.`)
}
const platform: string = core.getInput('platform');
if (platform && !isValidPlatform(platform)) {
core.setFailed(`Invalid config: platform must be one of web, android or ios.`)
}

const clientPayload: any = {
build_url: buildUrl
};
if (slackWebhookUrl) {
clientPayload.slack_webhook_url = slackWebhookUrl;
}

const response = await fetch("https://dispatch.empirical.run", {
const response = await fetch("https://dispatch.empirical.run/v1/trigger", {
method: "POST",
body: JSON.stringify({
repo: {
origin: {
owner: github.context.repo.owner,
name: github.context.repo.repo
},
event_type: eventType(platform),
client_payload: clientPayload
build: {
url: buildUrl,
commit: getCommitSha(),
branch: getBranchName(),
},
platform,
})
});
const content = await response.text();
Expand Down

0 comments on commit e0a2d72

Please sign in to comment.