Enriching GitHub Repositories with Topics Using Terraform

Enriching GitHub Repositories with Topics Using Terraform

Introduction :-

Managing #GitHub repositories efficiently, especially in large organizations, can be a daunting task. One key aspect of effective repository #management is the ability to categorize and label repositories with relevant topics. Topics help in #discovering and organizing repositories, making it easier for your teams to #collaborate and find the right codebase. However, manually adding topics to numerous repositories can be time-consuming and error-prone. This is where #Terraform, the popular #infrastructure-as-code tool, comes to the rescue.

Prerequisites:-

  • #Github repository with full access.

  • #Terraform installed on your local machine.

Step-1 :- Create a folder named github-topics in your home directory.

Step-2 :- Within the github topics folder create a main.tf file and copy the below code into main.tf

#main.tf
data "github_repository" "repository" {
  full_name = var.repository
}

data "github_branch" "development" {
  repository = var.repository
  branch     = var.branch
}
resource "null_resource" "github_topics" {
  triggers = {
    repo_name = "${data.github_repository.repository.full_name}"
  }

  provisioner "local-exec" {
    command = <<-EOT
      GITHUB_TOKEN="${var.token}"
      REPO_NAME="${data.github_repository.repository.full_name}"

    curl -X PUT \
      -H "Authorization: token $GITHUB_TOKEN" \
      -H "Accept: application/vnd.github.mercy-preview+json" \
      -d '{
        "names": ["terraform", "infrastructure-as-code", "circleci", "aws", "kubernetes","argo", "cognito", "vcluster", "kubecost", "kyverno", "kubescape"]
      }' \
      "https://api.github.com/repos/$REPO_NAME/topics"
    EOT
  }
}

step-3 :- create a variable.tf file for storing the variables for main.tf file.

#variable.tf 

variable "repository" {
  type = string
  description = "name of the github repositroy"
  default  = "mahira"
}

variable "branch" {
  type = string
  description = "name of the github repository branch"
  default  = "mahira-medium"
}

variable "token" {
  type = string
  description = "github token"
  default  = "*************************"
}
#provider.tf
terraform {
  required_providers {
    github = {
     source = "integrations/github"
     version = "5.29.0"
    }
  }
}

Step-4 :- Set the appropriate values for the variables and apply the above configuration.

Step-5 :- once the #deployment is done, go to your github repository and check whether the topics are being added or not.

Conclusion :-

Incorporating #Terraform into your #GitHub repository management workflow is a powerful way to enhance #organization and productivity. With the ability to automate the addition of #topics to your repositories, you save time, reduce the risk of errors, and ensure that your codebase is optimally organized for efficient #collaboration. Remember that this is just one example of how #Terraform can be leveraged to improve your #development workflows. Feel free to explore and adapt Terraform for more #GitHub management tasks to further enhance your development process.