One-way sync of GitHub pull requests to Asana tasks so engineers can track all of their work in Asana. To see a more detailed explanation of the functionality of SGTM, see the code_reviews docs.
Follow these instructions for setting up SGTM to run in your environment and your infrastructure! Note that this is currently only set up for deployment on AWS, so if you are using a cloud provider, you may need to modify some code and deploy the app yourself.
You will need to set some overrides specific to your deployment -- mostly due to the fact that AWS S3 bucket names are globally unique, but you may want to tweak some default configuration settings. So, we recommend forking this repository into your Github organization.
We recommend setting up a virtual environment to install and run your python environment. By doing so, you can eliminate
the risk that SGTM's python dependencies and settings will be mixed up with any such dependencies and settings that you
may be using in other projects. Once you install pipenv
(see Installing a Virtual Environment for Python below),
you should install all required python dependencies using pipenv install
.
You'll need to install Terraform to launch the infrastructure for SGTM.
You'll need to install Terragrunt to configure Terraform for your own account.
There are three external services you'll need to interact with, and therefore need credentials for.
Create a Personal Access Token in Asana. At Asana, we created a Guest Account to run SGTM as, so no engineer's personal access token is used, and it's clear that there's a specific "SGTM" user who is making the task updates.
Copy this Personal Access Token for the next step.
You'll need to be able to authenticate with AWS via the command line, and there are a few ways to achieve that. See here for your options, but most likely you'll already have a preferred method of interacting with AWS via the command line.
Again, you will probably want to create a new Github user in your org that is just for SGTM (since SGTM will be updating/merging PRs, it's clearer to attribute those actions to a user that is clearly name "SGTM" or something similar).
- For the Github user you want to use, generate a Personal Access Token with the following permissions:
- repo (Full control of private repositories)
- read:org (Read org and team membership, read org projects)
- Generate a secret token for your Github webhook. Github suggests generating this via
ruby -rsecurerandom -e 'puts SecureRandom.hex(20)'
, but use whatever method you are comfortable with to generate a secure secret token. Save this somewhere, as you'll need it twice in the later steps.
Copy this Personal Access Token for the next step.
- Create a file in S3 that maps github usernames to Asana user IDs. This file should be in the following format:
{
"github_username1": "asana_user_id1",
"github_username2": "asana_user_id2",
...
}
- Save the S3 bucket name and the file name in
./terraform/terraform.tfvars.json
under"github_usernames_to_asana_gids_s3_path"
. - (Optional) If the S3 bucket is not in the same AWS account as your SGTM deployment, ensure that the bucket policy on the S3 bucket allows the SGTM account to read from the bucket. Learn more about cross-account access to S3 bucket objects here: https://repost.aws/knowledge-center/cross-account-access-s3
You'll need to create an Asana project for your Github sync tasks.
- To create your "SGTM tasks" project, use the
setup_sgtm_tasks_project.py
script. The script will prompt you for the PAT you generated earlier, and guide you through setting up a brand new project or updating an existing project with the recommended Custom Fields.>>> To setup a new project python3 scripts/setup_sgtm_tasks_project.py -p "<PAT>" create -n "<PROJECT NAME>" -t "<TEAM ID>" >>> To update an existing project with the suggested custom fields python3 scripts/setup_sgtm_tasks_project.py -p "<PAT>" update -e "<EXISTING PROJECT ID>"
- If you have multiple repositories you want synced to Asana, you can create several of these projects. Make sure to take note of all of the project IDs for a later step.
- If you are on Asana Basic and do not have access to Custom Fields, the script will skip that step - SGTM will work even without the suggested fields
- Make sure that the Asana user/guest that you created earlier is a member of this projects.
NOTE: AWS S3 Bucket names are globally unique, so you will need to choose your own bucket names to be unique that no other AWS account has already created.
- In
./terraform/variables.tf
, any variable that is listed without a default value needs to be set. The preferred method of setting these values is through environment variables. For example, to se terraform variablegithub_usernames_to_asana_gids_s3_path
, you'll want to set an environment variableTF_VAR_github_usernames_to_asana_gids_s3_path
. - Save these somewhere that you and others collaborating on this deployment could share (we save ours in an Asana task internally, of course) since these will need to be the same each time you apply new changes.
You'll first need to set up the Terraform remote state to be the source of truth for the state of your deployed infrastructure.
SGTM supports both s3 and terraform cloud backend. Please select only 1 to deploy your terraform changes to.
- Run
python3 ./scripts/setup.py state
(this will create an S3 bucket and DyanmoDb lock table for Terraform) - Ensure
TF_VAR_terraform_backend_use_tfc=false
and continue the setup instructions from Step #2 below.
You'll need to have a Terraform Cloud account have the workspace you want to deploy SGTM in already setup. Make sure you have admin/write access to the workspace
- Set
TF_VAR_terraform_backend_use_tfc=true
and make sure the dependent TF_VARs are defined as well. (TF_VAR_terraform_backend_organization_name
andTF_VAR_terraform_backend_workspace_name
) - Initialize and apply the infrastructure:
> cd ./terraform
> terragrunt init
> terragrunt apply
- Save the output of
terragrunt apply
, which should print out aapi_gateway_deployment_invoke_url
. You'll need this in the next step. - Push your secrets to the ecrypted S3 bucket that Terraform just created.
cd
back to the root of your repository and run:python3 ./scripts/setup.py secrets
and follow the prompts.
For each repository that you are going to sync:
- Find that repository's Github Graphql
node_id
:- You can get this using
curl -i -u <username>:<github_personal_access_token> https://api.github.com/repos/<organization>/<repository>
- You can get this using
- Using the "SGTM tasks" project id from Create Asana Projects, update the sgtm-objects DynamoDb table with the mapping of
{"github-node": "<node_id>", "asana-id": "<project_id>"}
For each repository that you want to sync to Asana through SGTM:
- Navigate to
https://github.com/<organization>/<repository>/settings/hooks
- Click "Add webhook"
- Under "Payload URL", input the
api_gateway_deployment_invoke_url
from the previous step - Under "Content Type", select "application/json"
- Under "Secret", input your secret token that you generated earlier
- Under "Which events would you like to trigger this webhook?", select "Let me select individual events."
- Issue comments
- Pull requests
- Pull request reviews
- Pull request review comments
- Statuses
- Make sure "Active" is selected
- Click "Add webhook"
At this point, you should be all set to start getting Pull Requests synced to Asana Tasks. Open up a Pull Request, and Enjoy!
SGTM has a few optional power features that are disabled by default, but can be enabled with environment variables.
SGTM can merge your pull requests automatically when certain conditions are fulfilled. This behavior is controlled by adding labels to the PR in Github. If this feature is enabled, there are 3 different labels you can apply to your PR to cause the PR to be auto-merged under different conditions:
- 🔍
merge after tests and approval
: auto-merge this PR once tests pass and the PR is approved - 🧪
merge after tests
: auto-merge this PR once tests pass (regardless of approval status) - 🚢
merge immediately
: auto-merge this PR immediately
In all cases, a PR with merge conflicts will not be auto-merged.
How to enable:
- Set an env variable of
TF_VAR_sgtm_feature__automerge_enabled
totrue
- Create labels in your repository of
merge after tests and approval
,merge after tests
andmerge immediately
At Asana, pull requests often have corresponding Asana tasks that can be completed when the pull request merges. With this feature enabled, setting a Github label of complete tasks on merge
on a PR will automatically complete any linked Asana tasks. Asana tasks can be linked by adding their URLs to a line under the string Asana tasks:
in the PR description, as demonstrated below:
Asana tasks:
<task_to_complete_url> <another_task_to_complete_url>
How to enable:
- Set an env variable of
TF_VAR_sgtm_feature__autocomplete_enabled
totrue
- Create a label of
complete tasks on merge
in your repository
Note: If the SGTM user in your Asana domain doesn't have access to a linked task, it won't be able to merge it. You can add the SGTM user as a collaborator on a task to give it the ability to auto-complete the task.
SGTM can avoid closing tasks if the approvals come from certain Github users. This can be useful if you have specific Github users that you would like to be able to approve PRs in order to unblock merging, but that you want a second set of eyes on. For example, you may have a bot that automatically approves certain auto-generated PRs to speed up some workflow, but you still want a human to review those changes afterwards.
How to configure:
- Set an env variable of
TF_VAR_sgtm_feature__followup_review_github_users
to contain a comma-separated list of Github usernames that should have follow-up review
By default SGTM subscribes every member of a reviewing Github team to the SGTM task. You might want to turn this off if you want to use team reviewers as a marker for PR ownership or triage, but don't need every member of that team to see each PR.
How to configure:
- Set an env variable of
TF_VAR_sgtm_feature__disable_github_team_subscription
totrue
By default SGTM will assign the task corresponding to the PR to the assignee on the Github pull request. The assignee on the Asana task will change for the following events: when the PR is in draft status, when changes are requested on the PR, when the PR is approved, or when a review is requested. Turning this feature on will keep the assignee of the task corresponding to the PR to always be the author of the pull request. This is useful if you want to reduce Asana inbox noise from reassignment or if you prefer to have the author of the PR be responsible for the corresponding Asana task.
How to configure:
- Set an env variable of
TF_VAR_sgtm_feature__allow_persistent_task_assignee
totrue
- Ensure that the PR has the
persistent task assignee
Github label attached to the PR.
We recommend using pipenv
to manage your python environment for SGTM. We've checked in a Pipfile
and Pipfile.lock
to make this easier for you. If you have pipenv
installed, cd
into the SGTM directory, and run pipenv install
to install all dependencies. If you don't have pipenv
installed, you can install it via pip install pipenv
.
You can then run pipenv shell
to enter the virtual environment from a specific shell session. Run exit
to leave it.
Alternatively, you don't have to 'enter' the virtual environment - you can instead run a command inside the virtual environment by prefixing the command with pipenv run
.
SGTM's terraform configuration will also spin up a staging cluster that you can use for manual testing. To only deploy changes to staging, run terragrunt apply --target=module.sgtm-staging
. To direct webhook events to your staging cluster, use API gateway endpoint /sgtm_staging
instead of /sgtm
.
You can also choose to create new staging clusters, by copying the module "sgtm-staging"
in main.tf
. Be sure to choose a new name for your staging cluster and update the naming_suffix
parameter so that terraform doesn't override existing resources.
You can also choose to test your changes locally. Here are step-by-step instructions on how to test manually/locally:
- Create a Personal Access Token in Asana. Copy that token and export it in your shell environment (
export ASANA_API_KEY=<your_asana_personal_access_token>
) - Create a Github Personal Access Token as per the instrucitons in the Github section above. Export that token in your shell environment (
export GITHUB_API_KEY=<your_github_personal_access_token>
) - Follow the instructions in Installing a Virtual Environment for Python to install necessary requirements.
- Open up a
python
REPL in theSGTM
root directory (or useipython
, but you'll have topipenv install ipython
first) - Run the function you want to test. It's usually fine / recommended to skip the DynamoDb locking when testing locally, since you usually won't be needing to test that. Here's an example of how to test updating a pull request:
- Note what code you want to test. In this case, we want to go to src/github/webhook.py and look at
_handle_pull_request_webhook
. It looks like we need anpull_request_id
. - Get the
pull_request_id
. One easy way to do this is to run a command like thiscurl -i -u <github_username>:$GITHUB_API_KEY https://api.github.com/repos/asana/sgtm/pulls/123
and then grab thenode_id
from that response. - Open up your REPL. Import the function you want to test (in this case:
import src.github.controller as github_controller; import src.github.graphql.client as graphql_client
) - Run the code! In this case:
pull_request = graphql_client.get_pull_request(<pull_request_id>)
github_controller.upsert_pull_request(pull_request)
- Note what code you want to test. In this case, we want to go to src/github/webhook.py and look at
You may then run all tests via the command line:
ENV=test python3 -m unittest discover
Alternatively, you may run specific tests e.g. via:
ENV=test python3 -m unittest test/<python-test-file-name>.py
ENV=test python3 -m unittest test.<python-test-module-name>.<TestClassName>
ENV=test python3 -m unittest test.<python-test-module-name>.<TestClassName>.<test_function_name>
Please perform the following checks prior to pushing code
- run
black .
to autoformat your code - run
mypy
on each file that you have changed - run tests, as described in the previous section