Skip to content

Commit

Permalink
feat: bring github (#39)
Browse files Browse the repository at this point in the history
* wip

* Update github/lib.test.w

* Update github/lib.test.w

* updates

* updates

* updated README

* ignore test due to winglang/wing#5408

* adding description to github/package.json

* adding author to github/package.json

---------

Co-authored-by: Eyal Keren <[email protected]>
  • Loading branch information
eladcon and ekeren authored Jan 2, 2024
1 parent aa8afc4 commit 58dd1bb
Show file tree
Hide file tree
Showing 17 changed files with 4,060 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/github-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: github-pull
on:
pull_request:
paths:
- github/**
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: github
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: github
- name: Test
run: wing test
working-directory: github
- name: Pack
run: wing pack
working-directory: github
37 changes: 37 additions & 0 deletions .github/workflows/github-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: github-release
on:
push:
branches:
- main
paths:
- github/**
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: github
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: github
- name: Test
run: wing test
working-directory: github
- name: Pack
run: wing pack
working-directory: github
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: github
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 2 additions & 0 deletions github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions github/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
140 changes: 140 additions & 0 deletions github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# github

A Wing library for working with [GitHub Probot](https://github.com/probot/probot)


## Prerequisites

* [winglang](https://winglang.io).
* [Install ngrok](https://ngrok.com/docs/getting-started/)


## Installation

`sh
npm i @winglibs/github
`

## Usage

This following application is a simple GitHub application that listens to created and reopened
pull requests, look for any `*.md` files that where changed and replace their content with their
uppercase version.

* It requires a GitHub application
* Configured secrets

In order to start you need to create a GitHub application:

### Create a GitHub App

1. Goto https://github.com/settings/apps
2. Click "New GitHub App" and complete form
3. GitHub App Name
4. Homepage URL: e.g. https://winglang.io
5. Webhook:
1. Active: ✅
2. URL: http://this-will-change-automatically.com
3. Webhook secret: "this-is-a-bad-secret"
6. Permissions -> Repository permissions:
1. Contents: Read and write
2. Pull requests: Read and write
7. Subscribe to events
1. Pull request
2. Push
8. Save
9. Notice the app id and save it
10. Generate & download a private key for the app

### Create Local Secret File

`github.ProbotApp` class expects three secrets on construction, the application id, private key (AKA `pem` file) and webhook secret.

Storing the `pem` file in a json format is a bit tricky, because of the sensitivity to any white space chars and its structure.

You can use `jq`` ([Install jq](https://jqlang.github.io/jq/)) to generate your `~/.wing/secret.json` with the following command:

```sh
jq --null-input \
--arg secret "this-is-a-bad-secret" \
--arg app_id "some-app-id" \
--arg private_key "$(cat '/path/to/private-key.pem')" \
'{"github.webhook_secret": $secret, "github.app_id": $app_id, "gitub.app_key": $private_key}' > ~/.wing/secrets.json
```

### `main.w`

```js
bring util;
bring cloud;
bring github;

let uppercaseAllMarkdownFiles = inflight (ctx) => {
let repo = ctx.payload.repository;

// find all changed mdfiles by comparing the commits of the PR
let compare = ctx.octokit.repos.compareCommits(
owner: repo.owner.login,
repo: repo.name,
base: ctx.payload.pull_request.base.sha,
head: ctx.payload.pull_request.head.sha,
);

let mdFiles = MutMap<str>{};
for commit in compare.data.commits {
let commitContent = ctx.octokit.repos.getCommit(
owner: repo.owner.login,
repo: repo.name,
ref: ctx.payload.pull_request.head.ref,
);
if let files = commitContent.data.files {
for file in files {
if file.filename.endsWith(".md") &&
(file.status == "modified" || file.status == "added" || file.status == "changed") {
mdFiles.set(file.filename, file.sha);
}
}
}
}

// list over mdfiles and update them
for filename in mdFiles.keys() {
let contents = ctx.octokit.repos.getContent(
owner: repo.owner.login,
repo: repo.name,
path: filename,
ref: ctx.payload.pull_request.head.sha
);

let fileContents = util.base64Decode("{contents.data.content}");

ctx.octokit.repos.createOrUpdateFileContents(
owner: repo.owner.login,
repo: repo.name,
branch: ctx.payload.pull_request.head.ref,
sha: contents.data.sha,
path: filename,
message: "uppercase {filename}",
content: util.base64Encode(fileContents.uppercase())
);
}
};

let appId = new cloud.Secret(name:"github.app_id") as "appId";
let privateKey = new cloud.Secret(name:"gitub.app_key") as "privateKey";
let webhookSecret = new cloud.Secret(name:"github.webhook_secret") as "webhookSecret";


let markdown = new github.ProbotApp(
appId: appId,
privateKey: privateKey,
webhookSecret: webhookSecret,
onPullRequestOpened: uppercaseAllMarkdownFiles,
onPullRequestReopened: uppercaseAllMarkdownFiles
);

```

## License

This library is licensed under the [MIT License](./LICENSE).
72 changes: 72 additions & 0 deletions github/lib.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
bring util;
bring cloud;
bring "./lib.w" as probot;

// see https://github.com/winglang/wing/issues/5408
if false {

let handler = inflight (ctx) => {
let repo = ctx.payload.repository;

// find all changed mdfiles by comparing the commits of the PR
let compare = ctx.octokit.repos.compareCommits(
owner: repo.owner.login,
repo: repo.name,
base: ctx.payload.pull_request.base.sha,
head: ctx.payload.pull_request.head.sha,
);

let mdFiles = MutMap<str>{};
for commit in compare.data.commits {
let commitContent = ctx.octokit.repos.getCommit(
owner: repo.owner.login,
repo: repo.name,
ref: ctx.payload.pull_request.head.ref,
);
if let files = commitContent.data.files {
for file in files {
if file.filename.endsWith(".md") &&
(file.status == "modified" || file.status == "added" || file.status == "changed") {
mdFiles.set(file.filename, file.sha);
}
}
}
}

// list over mdfiles and update them
for filename in mdFiles.keys() {
let contents = ctx.octokit.repos.getContent(
owner: repo.owner.login,
repo: repo.name,
path: filename,
ref: ctx.payload.pull_request.head.sha
);

let fileContents = util.base64Decode("{contents.data.content}");

ctx.octokit.repos.createOrUpdateFileContents(
owner: repo.owner.login,
repo: repo.name,
branch: ctx.payload.pull_request.head.ref,
sha: contents.data.sha,
path: filename,
message: "uppercase {filename}",
content: util.base64Encode(fileContents.uppercase())
);
}
};
let appId = new cloud.Secret(name:"github.app_id") as "appId";
let privateKey = new cloud.Secret(name:"gitub.app_key") as "privateKey";
let webhookSecret = new cloud.Secret(name:"github.webhook_secret") as "webhookSecret";


let markdown = new probot.ProbotApp(
appId: appId,
privateKey: privateKey,
webhookSecret: webhookSecret,
onPullRequestOpened: handler,
onPullRequestReopened: handler
);


}
Loading

0 comments on commit 58dd1bb

Please sign in to comment.