Skip to content

Commit

Permalink
Merge pull request #1 from erikschlegel/erik/insights/initial-commit
Browse files Browse the repository at this point in the history
Initial commit for ADO Insights Provider Implementation
  • Loading branch information
erikschlegel committed Nov 11, 2020
2 parents 8722cee + ba50c48 commit 8026645
Show file tree
Hide file tree
Showing 27 changed files with 2,141 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Find the Dockerfile for mcr.microsoft.com/azure-functions/python:3.0-python3.8-core-tools at this URL
# https://github.com/Azure/azure-functions-docker/blob/master/host/3.0/buster/amd64/python/python38/python38-core-tools.Dockerfile
FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8-core-tools
24 changes: 24 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Azure Functions & Python 3",
"dockerFile": "Dockerfile",
"forwardPorts": [ 7071 ],
"mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-azuretools.vscode-azurefunctions",
"ms-azuretools.vscode-docker",
"ms-python.python"
],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "npm install",

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
5 changes: 5 additions & 0 deletions .funcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git*
.vscode
local.settings.json
test
.venv
17 changes: 9 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ htmlcov/
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

Expand All @@ -59,7 +58,6 @@ coverage.xml
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
Expand Down Expand Up @@ -87,16 +85,12 @@ ipython_config.py
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# having no cross-platform support, pipenv may install dependencies that dont work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
# celery beat schedule file
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py
Expand Down Expand Up @@ -127,3 +121,10 @@ dmypy.json

# Pyre type checker
.pyre/

# Azure Functions artifacts
bin
obj
appsettings.json
local.settings.json
.python_packages
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"ms-azuretools.vscode-azurefunctions",
"ms-python.python"
]
}
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start",
"justMyCode": false
}
]
}
20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": ".venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~3",
"debug.internalConsoleOptions": "neverOpen",
"python.pythonPath": ".venv/bin/python",
"python.testing.unittestArgs": [
"-v",
"-s",
"./GitInsights/tests",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
26 changes: 26 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-python-watch",
"isBackground": true,
"dependsOn": "pipInstall"
},
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}/Scripts/python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/pip install -r requirements.txt"
},
"problemMatcher": []
}
]
}
42 changes: 42 additions & 0 deletions GitInsights/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import datetime
import json
import logging
import os

import azure.functions as func

from .mods.ado_client import AzureDevopsInsights
from .mods.kv_client import KeyvaultClient

def main(mytimer: func.TimerRequest, outputBlob: func.Out[func.InputStream]) -> None:
# Required settings
keyVaultName = os.environ["KeyvaultName"]
patSecretName = os.environ["PatSecretName"]
adoProject = os.environ["AdoProjectName"]
adoOrg = os.environ["AdoOrgName"]
repos = os.environ["AdoRepos"]
teamId = os.environ["BacklogTeamId"]

# Optional settings
profileAliasOverrides = os.environ.get("ProfileAliases", "{}") # Accounts for local git profile setup discrepencies
aliasDict = json.loads(profileAliasOverrides.replace("\'", "\""))

groupByColumns = ['contributor', 'week', 'repo']

if not keyVaultName or not patSecretName or not adoProject or not adoOrg or not repos:
raise ValueError('Required environment variables are undefined')

kvURI = f"https://{keyVaultName}.vault.azure.net"
patToken = KeyvaultClient(kvURI).getSecretValue(patSecretName)

utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()

if mytimer.past_due:
logging.info('The timer is past due!')

client = AzureDevopsInsights(adoOrg, adoProject, repos.split(','), teamId, aliasDict)
dataframe = client.aggregatePullRequestActivity(groupByColumns, patToken.value)
outputBlob.set(dataframe.to_csv(index=True))

logging.info('Python timer trigger function ran at %s', utc_timestamp)
20 changes: 20 additions & 0 deletions GitInsights/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"useMonitor": true,
"schedule": "0 */3 * * * *",
"runOnStartup": false
},
{
"type": "blob",
"direction": "out",
"name": "outputBlob",
"path": "outcontainer/{DateTime}.csv",
"connection": "gitinsights_STORAGE"
}
]
}
Empty file added GitInsights/mods/__init__.py
Empty file.
Loading

0 comments on commit 8026645

Please sign in to comment.