Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀[Feature]: Add context aware code for GitHub Actions, so that it automatically populates certain variables about the running environment #111

Open
MariusStorhaug opened this issue Aug 7, 2024 · 1 comment
Labels
feature New feature

Comments

@MariusStorhaug
Copy link
Contributor

MariusStorhaug commented Aug 7, 2024

Idea is that when the module is loaded in either a workflow or a function app (triggering on a webhook) it should be able to collect some info about the event, type of action etc.

Payload fields:

  • Action
  • Event type (when webhook, header X-GITHUB-EVENT, when action, env:GITHUB_EVENT)

Think an addition to this would be to get info like "who am I" as in a hierarchy. What enterprise, organization, repo triggered this.

  • Enterprise
  • Organization
  • Repository

For context aware, it might also want to get the "installation" piece of the event. Not sure when these are in the payload of the event however.

Similar to the contexts that you can see made available here:
https://github.com/actions/github-script/tree/v7/

@MariusStorhaug MariusStorhaug added the feature New feature label Aug 7, 2024
@MariusStorhaug MariusStorhaug self-assigned this Aug 15, 2024
@MariusStorhaug MariusStorhaug moved this from Todo to In Progress in GitHub PowerShell Module Aug 15, 2024
@MariusStorhaug MariusStorhaug removed their assignment Nov 6, 2024
@MariusStorhaug
Copy link
Contributor Author

MariusStorhaug commented Dec 2, 2024

$context = Get-GitHubContext

# GraphQL Endpoint
$GraphQLEndpoint = 'https://api.github.com/graphql'

# Headers
$Headers = @{
    'Content-Type' = 'application/json'
}

# GraphQL Query for fetching user's organizations and repositories
$Query = @'
{
  viewer {
    enterprises(first: 100) {
      nodes {
        name
        slug
        id
      }
    }
    organizations(first: 100) {
      nodes {
        name
        login
        url
      }
    }
    repositories(first: 100, privacy: PUBLIC) {
      nodes {
        name
        url
        isPrivate
      }
    }
  }
}
'@

# Prepare the body for the HTTP POST
$Body = @{
    query = $Query
} | ConvertTo-Json -Depth 10

# Make the HTTP POST request
$response = Invoke-RestMethod -Uri $GraphQLEndpoint -Headers $Headers -Method Post -Body $Body -Token $context.Token -Authentication Bearer

# Process the response
if ($response) {
    Write-Output "User Login: $($response.data.viewer.login)"
    Write-Output 'Enterprises:'
    $response.data.viewer.enterprises.nodes | ForEach-Object {
        Write-Output "  - Name: $($_.name), Slug: $($_.slug), ID: $($_.id)"
    }
    Write-Output 'Organizations:'
    $response.data.viewer.organizations.nodes | ForEach-Object {
        Write-Output "  - Name: $($_.name), Login: $($_.login), URL: $($_.url)"
        if ($_.enterprise -ne $null) {
            Write-Output "    Enterprise: $($_.enterprise.name), URL: $($_.enterprise.url)"
        }
    }

    Write-Output 'Repositories:'
    $response.data.viewer.repositories.nodes | ForEach-Object {
        Write-Output "  - Name: $($_.name), URL: $($_.url), Private: $($_.isPrivate)"
    }
} else {
    Write-Error 'Failed to fetch data from GitHub API.'
}

@MariusStorhaug MariusStorhaug changed the title 🚀[Feature]: Add context aware code, so that it automatically populates certain variables about the running environment 🚀[Feature]: Add context aware code for GitHub Actions, so that it automatically populates certain variables about the running environment Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
Status: In Progress
Development

No branches or pull requests

1 participant