Map your Issues!
Tags: actionshackathon, github, azure, showdev
Published: 2020-09-07 22:37:55

Hi, I am Giorgio Lasala and this is my submission for [Github Actions Hackaton](https://dev.to/t/actionshackathon).
This is also my _first_ post on Dev.to !

### Submission Category 

_Wacky Wildcards_


### How it works
Project is composed of three components:
- A Github workflow triggered by issue events and powered by free and open-source Github actions
- A simple Azure Function to receive event and write to Azure CosmosDb
- A PowerBI Report to show finally issues on world map! 

#### Workflow pipeline

Yaml pipeline uses two opensource Github Actions:
- `octokit/graphql-action`: to query about user location info
- `satak/webrequest-action`: to post JSON data to Azure Function

When workflow is triggered by a new issue (or comment), the _first step_ 

``` yaml
- uses: octokit/graphql-action@v2.x
  id: get_user_info
  with:
    query: |
      query info($owner:String!) { 
        user(login: $owner) {
          location
        }
      }
    owner: ${{ github.actor }}
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```


extracts location from user profile, using GraphQL Github API. 

Instead _second_ step

``` yaml
- uses: satak/webrequest-action@master
  env:
    USERLOCATION: ${{ steps.get_user_info.outputs.data }}
    USERNAME: ${{ github.actor }}
    REPOSITORY: ${{ github.repository }}
  with:
      url: 'http://0caad210bf6f.ngrok.io/api/ReceiveIssue'
      method: 'POST'
      headers: '{"x-functions-key": "${{ secrets.FUNCTION_KEY }}"}'
      payload: '{ "location": ${{ env.USERLOCATION }}, "repository": "${{ env.REPOSITORY }}", "username": "${{ env.USERNAME }}" }'
```

creates a complete JSON payload and send it to Azure Function in order to store it in database.

#### Azure Function
It is a HTTP Trigger Azure Function that receives event from Github pipeline and writes to CosmosDb using OutputBinding.
Azure function must be invoked using a function key, received by `x-functions-key` HTTP header.

``` cs

[FunctionName("ReceiveIssue")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    [CosmosDB(
        databaseName: "Github",
        collectionName: "Issues",
        ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector<Event> eventsOut,
    ILogger log)
{
    log.LogInformation("Received event");

    try
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);

        string location = data.location.user.location;
        string repos = data.repository;
        string username = data.username;

        var eventObj = new Event()
        {
            Location = location,
            Username = username,
            EventDate = DateTime.Now,
            Repository = repos
        };

        await eventsOut.AddAsync(eventObj);

        return new OkObjectResult("Event inserted");
    }
    catch(Exception ex)
    {
        log.LogError(ex, "Error insert event");
        return new BadRequestObjectResult("Event not valid");
    }
}

``` 

#### PowerBi Report
This is one of first PowerBi report, so I am not skilled about it and any suggestions are welcome!
There is an issue... on published report, data aren't updated in real time (you can find more details on [Microsoft Docs](https://docs.microsoft.com/en-us/power-bi/collaborate-share/service-publish-to-web#updates-to-reports-and-data-refresh) and a trick to schedule [hourly data refresh](https://whitepages.unlimitedviz.com/2015/09/how-to-set-an-hourly-refresh-schedule-with-power-bi/)).
However using a DirectQuery supported datasource this problem can be overcome.

At the end, report is published on a public [website](https://app.powerbi.com/view?r=eyJrIjoiNDIxZWU0MDItY2NlMy00YWYzLWE5NDUtZTVjZDMwOThjOWRmIiwidCI6IjgxNDU3Y2YxLTQ2NzAtNDAyYy05OWRiLWQ4OTg3M2MzYzJlMCIsImMiOjl9)!



### Yaml File *and* Link to Code

You can check out the code on my GitHub Repository 
{% github salem84/hackmaps %}

In next days (after I will check some stuff) I will publish also PowerBi report on GitHub repository!

### Additional Resources / Info

If you like, please add a ⭐ and especially write an [issue](https://github.com/salem84/map-your-issues/issues/new?assignees=&labels=map&template=pin-your-issue.md), so you can pin 📌 **you** on [map](https://app.powerbi.com/view?r=eyJrIjoiNDIxZWU0MDItY2NlMy00YWYzLWE5NDUtZTVjZDMwOThjOWRmIiwidCI6IjgxNDU3Y2YxLTQ2NzAtNDAyYy05OWRiLWQ4OTg3M2MzYzJlMCIsImMiOjl9)


#### Nice to have
This is obviously a simple Proof-Of-Concept project developed in some hours, but I think in next days/weeks I will try to improve such as:

⬜ Use DirectQuery Datasource to have near-real time updates (or 	alternatively develop a complete website using SignalR and Azure Maps or similar)

⬜ Improve Azure-side pipeline to improve scalability (e.g. using Azure Event Hub + data enrichments from other sources)

⬜ Create a shared-world-map of all Github repositories!

⬜ Create IaC script to speed up deployments

⬜ Extract data from issue message, using Text Analysis AI algorithms

✅ Improve Security with Function Authorization Key