Octokit, Github Actions, and Github Webhooks - sgml/signature GitHub Wiki

<!doctype html>
<html>
    <head>
        <title>Git Repo API</title>
    </head>
    <body>
    <script type="module">
        import { Octokit } from "https://cdn.skypack.dev/@octokit/core";

        const octokit = new Octokit({
            auth: "foo"
        });
        
        async function foo(){ 
            const { data } = await octokit.request("/user");

            console.log("data:" + JSON.stringify(data))
        }

        /*
        To get the file sha, change this URL:
            https://github.com/octokit/core.js/blob/master/README.md
        to this:
            https://api.github.com/repos/octokit/core.js/contents/README.md
        the pattern is:
            https://api.github.com/repos/[owner]/[reponame]/contents/[filepath]

        And read the sha value from the JSON response
        */

        async function bar(){
            const { data } = await octokit.request('/repos/{owner}/{repo}/git/blobs/{file_sha}', {
            owner: 'sweattep',
            repo: 'ancient_quotes',
            file_sha: '8807254f4e989c1d5874d0b3e7c953703c7e85cd'
            })                
            if (data?.content) {
                console.log("Blob data:" + data?.content)
                console.log("Decoded Blob data:" + atob(data?.content))
            }
            else {
                console.log("Blob GET response has no content node")
            }
        }
        foo()
        bar()
    </script>
</body>
</html>

Github Webhooks

  • repository_dispatch is an external event you can use in a Slackbot to trigger an event in Github. For example, push text to a wiki:
@slack_events_adapter.on("gollum")
def gollum_event(event_data):
    message = event_data["event"]
    channel = message["channel"]
    user = message["user"]
    slack_web_client.chat_postMessage(channel=channel, text="A wiki page was updated!")
import requests
import json

def trigger_event(user, repo, token, event_type="custom", client_payload=None):
    url = f"https://api.github.com/repos/{user}/{repo}/dispatches"
    headers = {
        "Accept": "application/vnd.github.everest-preview+json",
        "Authorization": f"token {token}",
    }
    payload = {
        "event_type": event_type,
        "client_payload": client_payload if client_payload else {},
    }
    response = requests.post(url, headers=headers, json=payload)
    return response.status_code

# Usage
user = ""
repo = ""
token = ""
event_type = "custom"
client_payload = {"key": "value"}  # optional
status_code = trigger_event(user, repo, token, event_type, client_payload)
print(f"Response status code: {status_code}")

References

⚠️ **GitHub.com Fallback** ⚠️