mebn / DD2480

KTH Computer Science project, course DD2480
3 stars 0 forks source link

Implement parse_webhook in Github.rs #121

Closed marcusalstrom closed 9 months ago

marcusalstrom commented 9 months ago

Parse github wekbooks json with serde. from json to WebhookData struct

mebn commented 9 months ago

A real example of a push event can be found here, but maybe only I have access to view that.

We need to identify the json fields we need and then parse it with serde. We don't need all the fields.

This is what the push event looks like:

Headers:

Request URL: https://8f17-2001-2002-d541-d336-2da9-d690-fe40-64ea.ngrok-free.app/github_webhook
Request method: POST
Accept: */*
Content-Type: application/json
User-Agent: GitHub-Hookshot/70dba94
X-GitHub-Delivery: 106c1740-c4f4-11ee-9818-2ab8854ae5f6
X-GitHub-Event: push
X-GitHub-Hook-ID: 459236438
X-GitHub-Hook-Installation-Target-ID: 747680902
X-GitHub-Hook-Installation-Target-Type: repository

Payload:

{
  "ref": "refs/heads/issue/118",
  "before": "75025a3c5e63172489a686391d656c3a4bfef340",
  "after": "4a2541bb94c39a5b3f0bcf4885bfb84b11ba7280",
  "repository": {
    "id": 747680902,
    "node_id": "R_kgDOLJC0hg",
    "name": "DD2480",
    "full_name": "mebn/DD2480",
    "private": false,
    "owner": {
      "name": "mebn",
      "email": "nilszen.marcus@gmail.com",
      "login": "mebn",
      "id": 39590503,
      "node_id": "MDQ6VXNlcjM5NTkwNTAz",
      "avatar_url": "https://avatars.githubusercontent.com/u/39590503?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/mebn",
      "html_url": "https://github.com/mebn",
      "followers_url": "https://api.github.com/users/mebn/followers",
      "following_url": "https://api.github.com/users/mebn/following{/other_user}",
      "gists_url": "https://api.github.com/users/mebn/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/mebn/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/mebn/subscriptions",
      "organizations_url": "https://api.github.com/users/mebn/orgs",
      "repos_url": "https://api.github.com/users/mebn/repos",
      "events_url": "https://api.github.com/users/mebn/events{/privacy}",
      "received_events_url": "https://api.github.com/users/mebn/received_events",
      "type": "User",
      "site_admin": false
    },
    "html_url": "https://github.com/mebn/DD2480",
    "description": "KTH Computer Science project, course DD2480",
    "fork": false,
    "url": "https://github.com/mebn/DD2480",
    "forks_url": "https://api.github.com/repos/mebn/DD2480/forks",
    "keys_url": "https://api.github.com/repos/mebn/DD2480/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/mebn/DD2480/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/mebn/DD2480/teams",
    "hooks_url": "https://api.github.com/repos/mebn/DD2480/hooks",
    "issue_events_url": "https://api.github.com/repos/mebn/DD2480/issues/events{/number}",
    "events_url": "https://api.github.com/repos/mebn/DD2480/events",
    "assignees_url": "https://api.github.com/repos/mebn/DD2480/assignees{/user}",
    "branches_url": "https://api.github.com/repos/mebn/DD2480/branches{/branch}",
    "tags_url": "https://api.github.com/repos/mebn/DD2480/tags",
    "blobs_url": "https://api.github.com/repos/mebn/DD2480/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/mebn/DD2480/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/mebn/DD2480/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/mebn/DD2480/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/mebn/DD2480/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/mebn/DD2480/languages",
    "stargazers_url": "https://api.github.com/repos/mebn/DD2480/stargazers",
    "contributors_url": "https://api.github.com/repos/mebn/DD2480/contributors",
    "subscribers_url": "https://api.github.com/repos/mebn/DD2480/subscribers",
    "subscription_url": "https://api.github.com/repos/mebn/DD2480/subscription",
    "commits_url": "https://api.github.com/repos/mebn/DD2480/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/mebn/DD2480/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/mebn/DD2480/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/mebn/DD2480/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/mebn/DD2480/contents/{+path}",
    "compare_url": "https://api.github.com/repos/mebn/DD2480/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/mebn/DD2480/merges",
    "archive_url": "https://api.github.com/repos/mebn/DD2480/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/mebn/DD2480/downloads",
    "issues_url": "https://api.github.com/repos/mebn/DD2480/issues{/number}",
    "pulls_url": "https://api.github.com/repos/mebn/DD2480/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/mebn/DD2480/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/mebn/DD2480/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/mebn/DD2480/labels{/name}",
    "releases_url": "https://api.github.com/repos/mebn/DD2480/releases{/id}",
    "deployments_url": "https://api.github.com/repos/mebn/DD2480/deployments",
    "created_at": 1706099766,
    "updated_at": "2024-01-29T18:56:31Z",
    "pushed_at": 1707226301,
    "git_url": "git://github.com/mebn/DD2480.git",
    "ssh_url": "git@github.com:mebn/DD2480.git",
    "clone_url": "https://github.com/mebn/DD2480.git",
    "svn_url": "https://github.com/mebn/DD2480",
    "homepage": "",
    "size": 565,
    "stargazers_count": 3,
    "watchers_count": 3,
    "language": "Java",
    "has_issues": true,
    "has_projects": true,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "has_discussions": false,
    "forks_count": 0,
    "mirror_url": null,
    "archived": false,
    "disabled": false,
    "open_issues_count": 12,
    "license": null,
    "allow_forking": true,
    "is_template": false,
    "web_commit_signoff_required": false,
    "topics": [

    ],
    "visibility": "public",
    "forks": 0,
    "open_issues": 12,
    "watchers": 3,
    "default_branch": "main",
    "stargazers": 3,
    "master_branch": "main"
  },
  "pusher": {
    "name": "mebn",
    "email": "nilszen.marcus@gmail.com"
  },
  "sender": {
    "login": "mebn",
    "id": 39590503,
    "node_id": "MDQ6VXNlcjM5NTkwNTAz",
    "avatar_url": "https://avatars.githubusercontent.com/u/39590503?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/mebn",
    "html_url": "https://github.com/mebn",
    "followers_url": "https://api.github.com/users/mebn/followers",
    "following_url": "https://api.github.com/users/mebn/following{/other_user}",
    "gists_url": "https://api.github.com/users/mebn/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/mebn/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/mebn/subscriptions",
    "organizations_url": "https://api.github.com/users/mebn/orgs",
    "repos_url": "https://api.github.com/users/mebn/repos",
    "events_url": "https://api.github.com/users/mebn/events{/privacy}",
    "received_events_url": "https://api.github.com/users/mebn/received_events",
    "type": "User",
    "site_admin": false
  },
  "created": false,
  "deleted": false,
  "forced": false,
  "base_ref": null,
  "compare": "https://github.com/mebn/DD2480/compare/75025a3c5e63...4a2541bb94c3",
  "commits": [
    {
      "id": "4a2541bb94c39a5b3f0bcf4885bfb84b11ba7280",
      "tree_id": "6914856c5b9289de4b8ca80fdd5bf5f974d942d7",
      "distinct": true,
      "message": "Add: unit tests and moved from main",
      "timestamp": "2024-02-06T14:31:34+01:00",
      "url": "https://github.com/mebn/DD2480/commit/4a2541bb94c39a5b3f0bcf4885bfb84b11ba7280",
      "author": {
        "name": "Marcus Nilszén",
        "email": "nilszen.marcus@gmail.com",
        "username": "mebn"
      },
      "committer": {
        "name": "Marcus Nilszén",
        "email": "nilszen.marcus@gmail.com",
        "username": "mebn"
      },
      "added": [
        "assignment2/src/routes/github_webhook.rs",
        "assignment2/src/routes/mod.rs"
      ],
      "removed": [

      ],
      "modified": [
        "assignment2/src/main.rs"
      ]
    }
  ],
  "head_commit": {
    "id": "4a2541bb94c39a5b3f0bcf4885bfb84b11ba7280",
    "tree_id": "6914856c5b9289de4b8ca80fdd5bf5f974d942d7",
    "distinct": true,
    "message": "Add: unit tests and moved from main",
    "timestamp": "2024-02-06T14:31:34+01:00",
    "url": "https://github.com/mebn/DD2480/commit/4a2541bb94c39a5b3f0bcf4885bfb84b11ba7280",
    "author": {
      "name": "Marcus Nilszén",
      "email": "nilszen.marcus@gmail.com",
      "username": "mebn"
    },
    "committer": {
      "name": "Marcus Nilszén",
      "email": "nilszen.marcus@gmail.com",
      "username": "mebn"
    },
    "added": [
      "assignment2/src/routes/github_webhook.rs",
      "assignment2/src/routes/mod.rs"
    ],
    "removed": [

    ],
    "modified": [
      "assignment2/src/main.rs"
    ]
  }
}