Skip to content

Commit 4aa298d

Browse files
authored
feat: adapt for probot v11. Use @actions/core for logging (#19)
BREAKING CHANGE: package name has been renamed to "@probot/adapter-github-actions" BREAKING CHANGE: the package now has the same export as `"probot"`. Before ```js const runProbot = require('probot-actions-adapter'); const app = require('./index'); runProbot(app); ``` After ```js const { run } = require("@probot/adapter-github-actions"); const app = require("./app"); run(app); ```
1 parent fce6350 commit 4aa298d

File tree

17 files changed

+2979
-10143
lines changed

17 files changed

+2979
-10143
lines changed

.eslintrc

Lines changed: 0 additions & 25 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
on:
2-
[push, pull_request]
3-
41
name: Test
2+
on:
3+
push: {}
4+
pull_request:
5+
types: [opened, synchronize]
56

67
jobs:
7-
build:
8+
integration:
9+
runs-on: ubuntu-latest
10+
# don't run integration tests on push unless it's the main branch
11+
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main'
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: actions/setup-node@v2
15+
- run: npm ci
16+
- run: npm test
17+
createComment:
818
runs-on: ubuntu-latest
19+
# only run on push events
20+
if: github.event_name == 'push'
921
steps:
10-
- name: Debug
11-
run: echo '${{ toJson(github) }}'
12-
- uses: actions/checkout@v2
13-
- run: npm ci
14-
- run: npm run lint
15-
- run: npm test
22+
- uses: actions/checkout@v2
23+
- uses: actions/setup-node@v2
24+
- run: npm ci
25+
- run: node test/fixtures/app.js
26+
env:
27+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

LICENSE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1313
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1414
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1515
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16-

README.md

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,68 @@
1-
# :electric_plug: `probot-actions-adapter`
1+
# :electric_plug: `@probot/adapter-github-actions`
22

3-
> An adapter that takes a [Probot](https://probot.github.io/) app and makes it compatible with [GitHub Actions](https://github.com/features/actions)
3+
> Adapter to run a [Probot](https://probot.github.io/) application function in [GitHub Actions](https://github.com/features/actions)
44
5-
<a href="https://github.com/probot/actions-adapter"><img alt="GitHub Actions status" src="https://github.com/probot/actions-adapter/workflows/Build/badge.svg"></a>
6-
7-
## Contents
8-
9-
- [Installation](#installation)
10-
- [Usage](#usage)
11-
- [Authentication](#authentication)
12-
- [Caveats](#caveats)
13-
14-
## Installation
15-
16-
```shell
17-
npm i -S probot-actions-adapter
18-
```
5+
[![Build Status](https://github.com/probot/adapter-github-actions/workflows/Test/badge.svg)](https://github.com/probot/adapter-github-actions/actions)
196

207
## Usage
218

22-
1. Add an `action.js` to your Probot project, like [the one shown below](#example-actionjs)
23-
1. Add an `action.yml` to your Probot project, like [the one shown below](#example-actionyml)
24-
1. _Vendor in_ your `node_modules`, as [recommended by the official Actions documentation](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action#commit-tag-and-push-your-action-to-github)
25-
1. Optional, but recommended, update your project's README with an example workflow showing how to consume your action
9+
Create your Probot Application as always
2610

27-
### Example `action.js`
11+
```js
12+
// app.js
13+
module.exports = (app) => {
14+
app.on("issues.opened", async (context) => {
15+
const params = context.issue({ body: "Hello World!" });
16+
await context.octokit.issues.createComment(params);
17+
});
18+
};
19+
```
2820

29-
```javascript
30-
// Require the adapter
31-
const runProbot = require('probot-actions-adapter');
21+
Then in the entrypoint of your GitHub Action, require `@probot/github-action` instead of `probot`
3222

33-
// Require your Probot app's entrypoint, usually this is just index.js
34-
const app = require('./index');
23+
```js
24+
// index.js
25+
const { run } = require("@probot/github-action");
26+
const app = require("./app");
3527

36-
// Adapt the Probot app for Actions
37-
// This also acts as the main entrypoint for the Action
38-
runProbot(app);
28+
run(app).catch((error) => {
29+
console.error(error);
30+
process.exit(1);
31+
});
3932
```
4033

41-
### Example `action.yml`
34+
Then use `index.js` as your entrypoint in the `action.yml` file
4235

4336
```yaml
44-
name: 'Probot app name'
45-
description: 'Probot app description.'
37+
name: "Probot app name"
38+
description: "Probot app description."
4639
runs:
47-
using: 'node12'
48-
main: 'action.js'
40+
using: "node12"
41+
main: "action.js"
4942
```
5043
51-
See [the documentation](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions) for `action.yml` syntax details.
44+
**Important**: Your external dependencies will not be installed, you have to either vendor them in by committing the contents of the `node_modules` folder, or compile the code to a single executable script (recommended). See [GitHub's documentation](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action#commit-tag-and-push-your-action-to-github)
5245

53-
## Authentication
46+
For an example Probot App that is continuously published as GitHub Action, see https://github.com/probot/example-github-action#readme
5447

55-
Authentication is via [the `GITHUB_TOKEN` secret _automatically_ provided by GitHub](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token), which should be exposed as an environment variable, `GITHUB_TOKEN`. This can be achieved by including the appropriate [`env`](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#env), [`jobs.<job_id>.env`](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idenv), or [`jobs.<job_id>.steps.env`](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsenv) value in your workflow file.
48+
## How it works
5649

57-
### Example via `jobs.<job_id>.steps.env`
50+
[Probot](https://probot.github.io/) is a framework for building [GitHub Apps](docs.github.com/apps), which is different to creating [GitHub Actions](https://docs.github.com/actions/) in many ways, but the functionality is the same:
5851

59-
Include the following in your workflow file, when calling your Probot action:
52+
Both get notified about events on GitHub, which you can act on. While a GitHub App gets notified about a GitHub event via a webhook request sent by GitHub, a GitHub Action can receive the event payload by reading a JSON file from the file system. We can abstract away the differences, so the same hello world example app shown above works in both environments.
6053

61-
```yaml
62-
...
63-
steps:
64-
- name: My probot action
65-
...
66-
env:
67-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68-
...
69-
```
54+
Relevant differences for Probot applications:
7055

71-
## Caveats
56+
1. You cannot authenticate as the app. The `probot` instance you receive is authenticated using a GitHub token. In most cases the token will be set to `secrets.GITHUB_TOKEN`, which is [an installation access token](https://docs.github.com/en/actions/reference/authentication-in-a-workflow#about-the-github_token-secret). The provided `GITHUB_TOKEN` expires when the job is done or after 6 hours, whichever comes first. You do not have access to an `APP_ID` or `PRIVATE_KEY`, you cannot create new tokens or renew the provided one.
57+
2. `secrets.GITHUB_TOKEN` is scoped to the current repository. You cannot read data from other repositories unless they are public, you cannot update any other repositories, or access organization-level APIs.
58+
3. You could provide a personal access token instead of `secrets.GITHUB_TOKEN` to workaround the limits of a repository-scoped token, but be sure you know what you are doing.
59+
4. You don't need to configure `WEBHOOK_SECRET`, because no webhook request gets sent, the event information can directly be retrieved from environment variables and the local file system.
7260

73-
This adapter is designed to work well with Probot apps that are essentially stateless webhook handlers.
61+
For a more thorough comparison, see [@jasonetco's](https://github.com/jasonetco) posts:
7462

75-
A few other limitations exist:
63+
1. [Probot App or GitHub Action](https://jasonet.co/posts/probot-app-or-github-action/) (Jan 2019)
64+
2. [Update from April 2020](https://jasonet.co/posts/probot-app-or-github-action-v2/)
7665

77-
1. The adapter will _only_ work for Probot apps that receive webhook events that Actions also receives: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
78-
1. The adapter will _only_ work for Probot apps with a permissions set that is less than, or equivalent to the permission set attached to the `GITHUB_TOKEN`: https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token
66+
## License
7967

80-
If that's you, then great! :rocket:
68+
[ISC](LICENSE)

fixtures/push.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

index.js

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,56 @@
1-
process.env.DISABLE_WEBHOOK_EVENT_CHECK = 'true';
2-
3-
const path = require('path');
4-
const uuid = require('uuid');
5-
6-
const core = require('@actions/core');
7-
8-
const { Probot } = require('probot');
9-
10-
module.exports = (...handlers) => {
11-
// Setup Probot app
12-
const githubToken = process.env.GITHUB_TOKEN;
13-
const probot = new Probot({ githubToken });
14-
probot.setup(handlers);
15-
16-
// Process the event
17-
const event = process.env.GITHUB_EVENT_NAME;
18-
const payloadPath = process.env.GITHUB_EVENT_PATH;
19-
// eslint-disable-next-line global-require, import/no-dynamic-require
20-
const payload = require(path.resolve(payloadPath));
21-
core.debug(`Receiving event ${JSON.stringify(event)}`);
22-
return probot.receive({ name: event, payload, id: uuid.v4() }).catch(err => {
23-
// setFailed logs the message and sets a failing exit code
24-
core.setFailed(`Action failed with error: ${err.message}`);
25-
throw err;
1+
const ProbotExports = require("probot");
2+
const pino = require("pino");
3+
4+
const { transport } = require("./pino-transport-github-actions");
5+
6+
module.exports = { ...ProbotExports, run };
7+
8+
async function run(app) {
9+
const log = pino({}, transport);
10+
11+
const githubToken =
12+
process.env.GITHUB_TOKEN ||
13+
process.env.INPUT_GITHUB_TOKEN ||
14+
process.env.INPUT_TOKEN;
15+
16+
if (!githubToken) {
17+
log.error(
18+
"[probot/adapter-github-actions] a token must be passed as `env.GITHUB_TOKEN` or `with.GITHUB_TOKEN` or `with.token`, see https://github.com/probot/adapter-github-actions#usage"
19+
);
20+
return;
21+
}
22+
23+
const envVariablesMissing = [
24+
"GITHUB_RUN_ID",
25+
"GITHUB_EVENT_NAME",
26+
"GITHUB_EVENT_PATH",
27+
].filter((name) => !process.env[name]);
28+
29+
if (envVariablesMissing.length) {
30+
log.error(
31+
`[probot/adapter-github-actions] GitHub Action default environment variables missing: ${envVariablesMissing.join(
32+
", "
33+
)}. See https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables`
34+
);
35+
return;
36+
}
37+
38+
const probot = ProbotExports.createProbot({
39+
overrides: {
40+
githubToken,
41+
log,
42+
},
2643
});
27-
};
44+
45+
await probot.load(app);
46+
47+
return probot
48+
.receive({
49+
id: process.env.GITHUB_RUN_ID,
50+
name: process.env.GITHUB_EVENT_NAME,
51+
payload: require(process.env.GITHUB_EVENT_PATH),
52+
})
53+
.catch((error) => {
54+
probot.log.error(error);
55+
});
56+
}

index.test.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

jest.config.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

jest.setup-env.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)